Added rebuild worker for articles
authorMarcel Werk <burntime@woltlab.com>
Tue, 7 Jun 2016 16:41:41 +0000 (18:41 +0200)
committerMarcel Werk <burntime@woltlab.com>
Tue, 7 Jun 2016 16:41:46 +0000 (18:41 +0200)
com.woltlab.wcf/objectType.xml
wcfsetup/install/files/lib/system/worker/ArticleRebuildDataWorker.class.php [new file with mode: 0644]

index 6bd115b3dbf370a9ffe3d427911d5454c75506b6..e82d5c7de57b32fc9fb9133066197b0d9534bc99 100644 (file)
                        <classname><![CDATA[wcf\system\worker\LikeUserRebuildDataWorker]]></classname>
                        <nicevalue>-90</nicevalue>
                </type>
+               <type>
+                       <name>com.woltlab.wcf.article</name>
+                       <definitionname>com.woltlab.wcf.rebuildData</definitionname>
+                       <classname><![CDATA[wcf\system\worker\ArticleRebuildDataWorker]]></classname>
+                       <nicevalue>50</nicevalue>
+               </type>
                <type>
                        <name>com.woltlab.wcf.poll</name>
                        <definitionname>com.woltlab.wcf.rebuildData</definitionname>
diff --git a/wcfsetup/install/files/lib/system/worker/ArticleRebuildDataWorker.class.php b/wcfsetup/install/files/lib/system/worker/ArticleRebuildDataWorker.class.php
new file mode 100644 (file)
index 0000000..f50fecb
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+namespace wcf\system\worker;
+use wcf\data\article\ArticleEditor;
+use wcf\data\article\ArticleList;
+use wcf\data\article\content\ArticleContentList;
+use wcf\data\object\type\ObjectTypeCache;
+use wcf\system\database\util\PreparedStatementConditionBuilder;
+use wcf\system\search\SearchIndexManager;
+use wcf\system\WCF;
+
+/**
+ * Worker implementation for updating articles.
+ * 
+ * @author     Marcel Werk
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.worker
+ * @category   Community Framework
+ * @since       2.2
+ */
+class ArticleRebuildDataWorker extends AbstractRebuildDataWorker {
+       /**
+        * @inheritDoc
+        */
+       protected $objectListClassName = ArticleList::class;
+       
+       /**
+        * @inheritDoc
+        */
+       protected $limit = 100;
+       
+       /**
+        * @inheritDoc
+        */
+       protected function initObjectList() {
+               parent::initObjectList();
+               
+               $this->objectList->sqlOrderBy = 'article.articleID';
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function execute() {
+               parent::execute();
+               
+               if (!$this->loopCount) {
+                       // reset search index
+                       SearchIndexManager::getInstance()->reset('com.woltlab.wcf.article');
+               }
+               
+               if (!count($this->objectList)) {
+                       return;
+               }
+               $articles = $this->objectList->getObjects();
+               
+               $commentObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.comment.commentableContent', 'com.woltlab.wcf.article');
+               $sql = "SELECT  COUNT(*) AS comments, SUM(responses) AS responses
+                       FROM    wcf".WCF_N."_comment
+                       WHERE   objectTypeID = ?
+                               AND objectID = ?";
+               $commentStatement = WCF::getDB()->prepareStatement($sql);
+               $comments = [];
+               
+               // update article content
+               $articleContentList = new ArticleContentList();
+               $articleContentList->getConditionBuilder()->add('article_content.articleID IN (?)', [$this->objectList->getObjectIDs()]);
+               $articleContentList->readObjects();
+               foreach ($articleContentList as $articleContent) {
+                       // count comments
+                       $commentStatement->execute([$commentObjectType->objectTypeID, $articleContent->articleContentID]);
+                       $row = $commentStatement->fetchSingleRow();
+                       if (!isset($comments[$articleContent->articleID])) $comments[$articleContent->articleID] = 0;
+                       $comments[$articleContent->articleID] += $row['comments'] + $row['responses'];
+                       
+                       // update search index
+                       SearchIndexManager::getInstance()->add('com.woltlab.wcf.article', $articleContent->articleContentID, $articleContent->content, $articleContent->title, $articles[$articleContent->articleID]->time, $articles[$articleContent->articleID]->userID, $articles[$articleContent->articleID]->username, $articleContent->languageID, $articleContent->teaser);
+               }
+               
+               // fetch cumulative likes
+               $conditions = new PreparedStatementConditionBuilder();
+               $conditions->add("objectTypeID = ?", [ObjectTypeCache::getInstance()->getObjectTypeIDByName('com.woltlab.wcf.like.likeableObject', 'com.woltlab.wcf.likeableArticle')]);
+               $conditions->add("objectID IN (?)", [$this->objectList->getObjectIDs()]);
+               
+               $sql = "SELECT  objectID, cumulativeLikes
+                       FROM    wcf".WCF_N."_like_object
+                       ".$conditions;
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute($conditions->getParameters());
+               $cumulativeLikes = [];
+               
+               /** @noinspection PhpAssignmentInConditionInspection */
+               while ($row = $statement->fetchArray()) {
+                       $cumulativeLikes[$row['objectID']] = $row['cumulativeLikes'];
+               }
+               
+               foreach ($this->objectList as $article) {
+                       $editor = new ArticleEditor($article);
+                       $data = [];
+                       
+                       // update cumulative likes
+                       $data['cumulativeLikes'] = (isset($cumulativeLikes[$article->articleID])) ? $cumulativeLikes[$article->articleID] : 0;
+                       
+                       // update comment counter
+                       $data['comments'] = (isset($comments[$article->articleID])) ? $comments[$article->articleID] : 0;
+                       
+                       // update data
+                       $editor->update($data);
+               }
+       }
+}