From: Marcel Werk Date: Tue, 7 Jun 2016 16:41:41 +0000 (+0200) Subject: Added rebuild worker for articles X-Git-Tag: 3.0.0_Beta_1~1496 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=ad8148409c6904ccbbfc133c4870b752c6e61951;p=GitHub%2FWoltLab%2FWCF.git Added rebuild worker for articles --- diff --git a/com.woltlab.wcf/objectType.xml b/com.woltlab.wcf/objectType.xml index 6bd115b3db..e82d5c7de5 100644 --- a/com.woltlab.wcf/objectType.xml +++ b/com.woltlab.wcf/objectType.xml @@ -283,6 +283,12 @@ -90 + + com.woltlab.wcf.article + com.woltlab.wcf.rebuildData + + 50 + com.woltlab.wcf.poll com.woltlab.wcf.rebuildData 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 index 0000000000..f50fecbc61 --- /dev/null +++ b/wcfsetup/install/files/lib/system/worker/ArticleRebuildDataWorker.class.php @@ -0,0 +1,112 @@ + + * @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); + } + } +}