Rebuilding likes now rebuilds like objects
authorAlexander Ebert <ebert@woltlab.com>
Thu, 1 Aug 2013 22:22:59 +0000 (00:22 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 1 Aug 2013 22:22:59 +0000 (00:22 +0200)
The cached users are not rebuilt yet, this will be added later.

com.woltlab.wcf/objectType.xml
wcfsetup/install/files/lib/system/worker/LikeRebuildDataWorker.class.php

index d2ea058343585b913b53a57a3ef784f07c1e6386..1bd8aa5f571d6646105b743e869fd1079ed5538f 100644 (file)
                
                <!-- rebuild data workers -->
                <type>
-                       <name>com.woltlab.wcf.user</name>
+                       <name>com.woltlab.wcf.like</name>
                        <definitionname>com.woltlab.wcf.rebuildData</definitionname>
-                       <classname><![CDATA[wcf\system\worker\UserRebuildDataWorker]]></classname>
-                       <nicevalue>70</nicevalue>
+                       <classname><![CDATA[wcf\system\worker\LikeRebuildDataWorker]]></classname>
+                       <nicevalue>-100</nicevalue>
                </type>
                <type>
-                       <name>com.woltlab.wcf.like</name>
+                       <name>com.woltlab.wcf.user</name>
                        <definitionname>com.woltlab.wcf.rebuildData</definitionname>
-                       <classname><![CDATA[wcf\system\worker\LikeRebuildDataWorker]]></classname>
+                       <classname><![CDATA[wcf\system\worker\UserRebuildDataWorker]]></classname>
+                       <nicevalue>70</nicevalue>
                </type>
                <type>
                        <name>com.woltlab.wcf.attachment</name>
index 466b541c7f505a5200178ea3a188d4f19b946d3f..801aaccf94a6b1ddb1cff7557500820077d34b70 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 namespace wcf\system\worker;
+use wcf\data\like\Like;
 use wcf\system\user\activity\point\UserActivityPointHandler;
+use wcf\system\WCF;
 
 /**
  * Worker implementation for updating likes.
@@ -29,7 +31,7 @@ class LikeRebuildDataWorker extends AbstractRebuildDataWorker {
        protected function initObjectList() {
                parent::initObjectList();
                
-               $this->objectList->sqlOrderBy = 'like_table.likeID';
+               $this->objectList->sqlOrderBy = 'like_table.objectID, like_table.likeID';
                $this->objectList->getConditionBuilder()->add('like_table.objectUserID IS NOT NULL');
        }
        
@@ -42,22 +44,68 @@ class LikeRebuildDataWorker extends AbstractRebuildDataWorker {
                if (!$this->loopCount) {
                        // reset activity points
                        UserActivityPointHandler::getInstance()->reset('com.woltlab.wcf.like.activityPointEvent.receivedLikes');
+                       
+                       // reset like object data
+                       $sql = "UPDATE  wcf".WCF_N."_like_object
+                               SET     likes = 0,
+                                       dislikes = 0,
+                                       cumulativeLikes = 0";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute();
                }
                
                $itemsToUser = array();
+               $likeObjectData = array();
                foreach ($this->objectList as $like) {
-                       if (!$like->userID) {
-                               continue;
-                       }
-                       
                        if (!isset($itemsToUser[$like->objectUserID])) {
                                $itemsToUser[$like->objectUserID] = 0;
                        }
                        
                        $itemsToUser[$like->objectUserID]++;
+                       
+                       if (!isset($likeObjectData[$like->objectTypeID])) {
+                               $likeObjectData[$like->objectTypeID] = array();
+                       }
+                       if (!isset($likeObjectData[$like->objectTypeID][$like->objectID])) {
+                               $likeObjectData[$like->objectTypeID][$like->objectID] = array(
+                                       'likes' => 0,
+                                       'dislikes' => 0,
+                                       'cumulativeLikes' => 0
+                               );
+                       }
+                       
+                       if ($like->likeValue == Like::LIKE) {
+                               $likeObjectData[$like->objectTypeID][$like->objectID]['likes']++;
+                       }
+                       else {
+                               $likeObjectData[$like->objectTypeID][$like->objectID]['dislikes']++;
+                       }
+                       $likeObjectData[$like->objectTypeID][$like->objectID]['cumulativeLikes'] += $like->likeValue;
                }
                
                // update activity points
                UserActivityPointHandler::getInstance()->fireEvents('com.woltlab.wcf.like.activityPointEvent.receivedLikes', $itemsToUser, false);
+               
+               $sql = "UPDATE  wcf".WCF_N."_like_object
+                       SET     likes = ?,
+                               dislikes = ?,
+                               cumulativeLikes = ?
+                       WHERE   objectTypeID = ?
+                               AND objectID = ?";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               
+               WCF::getDB()->beginTransaction();
+               foreach ($likeObjectData as $objectTypeID => $objects) {
+                       foreach ($objects as $objectID => $data) {
+                               $statement->execute(array(
+                                       $data['likes'],
+                                       $data['dislikes'],
+                                       $data['cumulativeLikes'],
+                                       $objectTypeID,
+                                       $objectID
+                               ));
+                       }
+               }
+               WCF::getDB()->commitTransaction();
        }
 }