From e6d0ff83efdc7b236241e0486c2825b1d413d5f3 Mon Sep 17 00:00:00 2001 From: Zachary Doll Date: Mon, 5 May 2014 08:56:57 -0500 Subject: [PATCH] Add dba/counts handler Closes #1896. When deleting spammers, I found my tag counts would be messed up if they used any tags. This fixes that. --- plugins/Tagging/class.tagging.plugin.php | 12 ++++++++++++ plugins/Tagging/class.tagmodel.php | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/plugins/Tagging/class.tagging.plugin.php b/plugins/Tagging/class.tagging.plugin.php index 1c78dac76ae..7da2e1c8c38 100644 --- a/plugins/Tagging/class.tagging.plugin.php +++ b/plugins/Tagging/class.tagging.plugin.php @@ -609,6 +609,18 @@ public function Controller_Delete($Sender) { $Sender->Render('delete', '', 'plugins/Tagging'); } + /** + * Add update routines to the DBA controller + * + * @param DbaController $Sender + */ + public function DbaController_CountJobs_Handler($Sender) { + $Name = 'Recalculate Tag.CountDiscussions'; + $Url = "/dba/counts.json?" . http_build_query(array('table' => 'Tag', 'column' => 'CountDiscussions')); + + $Sender->Data['Jobs'][$Name] = $Url; + } + /** * Setup is called when the plugin is enabled. */ diff --git a/plugins/Tagging/class.tagmodel.php b/plugins/Tagging/class.tagmodel.php index 3a0718796f1..18c37e1a669 100644 --- a/plugins/Tagging/class.tagmodel.php +++ b/plugins/Tagging/class.tagmodel.php @@ -70,6 +70,27 @@ public function Save($FormPostValues, $Settings = FALSE) { } } } + + public function Counts($Column, $UserID = NULL) { + $Px = $this->Database->DatabasePrefix; + // Delete all the orphaned tagdiscussion records + $Sql = 'delete td.* ' . + "from {$Px}TagDiscussion as td " . + "left join {$Px}Discussion as d ON td.DiscussionID = d.DiscussionID " . + 'where d.DiscussionID is null'; + + $this->Database->Query($Sql); + + $Result = array('Complete' => TRUE); + switch($Column) { + case 'CountDiscussions': + Gdn::Database()->Query(DBAModel::GetCountSQL('count', 'Tag', 'TagDiscussion', 'CountDiscussions', 'DiscussionID', 'TagID', 'TagID')); + break; + default: + throw new Gdn_UserException("Unknown column $Column"); + } + return $Result; + } public static function ValidateTag($Tag) { // Tags can't contain commas.