Fixes counter update when deleting comment with responses
authorMatthias Schmidt <gravatronics@live.com>
Wed, 22 Jan 2014 07:34:09 +0000 (08:34 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Wed, 22 Jan 2014 07:34:09 +0000 (08:34 +0100)
When deleting comments with responses, the object type processor is first called with the number of responses plus one (for the deleted comment itself). This call correctly corrects the number of comments for the relevant object.

Afterwards, when deleting the responses, the objects' comment counters were updated again for each deleted response which causes a incorrect count because the counters were now updated twice for each response. Thus, we can simply ignore the counter update altogether in CommentResponseAction when deleting responses because the comment, they belong to, is deleted.

Furthermore, there is no need for CommentResponseAction to update the response counter of those comments in this case because the comments will be deleted anyway.

wcfsetup/install/files/lib/data/comment/CommentAction.class.php
wcfsetup/install/files/lib/data/comment/response/CommentResponseAction.class.php

index fa46587930323b8544bd384ef0b3e44ca0c1f620..d7c1fbc8dbccf69d03e13f49eeed81aa7bd20260 100644 (file)
@@ -110,7 +110,9 @@ class CommentAction extends AbstractDatabaseObjectAction {
                        $commentResponseList->getConditionBuilder()->add('comment_response.commentID IN (?)', array($commentIDs));
                        $commentResponseList->readObjectIDs();
                        if (count($commentResponseList->getObjectIDs())) {
-                               $action = new CommentResponseAction($commentResponseList->getObjectIDs(), 'delete');
+                               $action = new CommentResponseAction($commentResponseList->getObjectIDs(), 'delete', array(
+                                       'ignoreCounters' => true
+                               ));
                                $action->executeAction();
                        }
                }
index fdbeeb98609b79589412a281330490d14bb1da64..98077b4191a08da5ee801e2a0583871ba059e2ad 100644 (file)
@@ -56,6 +56,8 @@ class CommentResponseAction extends AbstractDatabaseObjectAction {
                        return 0;
                }
                
+               $ignoreCounters = !empty($this->parameters['ignoreCounters']);
+               
                // read object type ids for comments
                $commentIDs = array();
                foreach ($this->objects as $response) {
@@ -77,27 +79,31 @@ class CommentResponseAction extends AbstractDatabaseObjectAction {
                                $processors[$objectTypeID] = $objectType->getProcessor();
                                $responseIDs[$objectTypeID] = array();
                        }
-                       
-                       $processors[$objectTypeID]->updateCounter($comments[$response->commentID]->objectID, -1);
                        $responseIDs[$objectTypeID][] = $response->responseID;
                        
-                       if (!isset($updateComments[$response->commentID])) {
-                               $updateComments[$response->commentID] = 0;
+                       if (!$ignoreCounters) {
+                               $processors[$objectTypeID]->updateCounter($comments[$response->commentID]->objectID, -1);
+                               
+                               if (!isset($updateComments[$response->commentID])) {
+                                       $updateComments[$response->commentID] = 0;
+                               }
+                               
+                               $updateComments[$response->commentID]++;
                        }
-                       
-                       $updateComments[$response->commentID]++;
                }
                
                // remove responses
                $count = parent::delete();
                
                // update comment responses and cached response ids
-               foreach ($comments as $comment) {
-                       $commentEditor = new CommentEditor($comment);
-                       $commentEditor->updateResponseIDs();
-                       $commentEditor->updateCounters(array(
-                               'responses' => -1 * $updateComments[$comment->commentID]
-                       ));
+               if (!$ignoreCounters) {
+                       foreach ($comments as $comment) {
+                               $commentEditor = new CommentEditor($comment);
+                               $commentEditor->updateResponseIDs();
+                               $commentEditor->updateCounters(array(
+                                       'responses' => -1 * $updateComments[$comment->commentID]
+                               ));
+                       }
                }
                
                foreach ($responseIDs as $objectTypeID => $objectIDs) {