Fetch articles in ViewableArticleContentList
authorJoshua Rüsweg <ruesweg@woltlab.com>
Fri, 27 Dec 2019 14:29:49 +0000 (15:29 +0100)
committerJoshua Rüsweg <ruesweg@woltlab.com>
Fri, 27 Dec 2019 14:29:49 +0000 (15:29 +0100)
Prevents multiple queries in notifications.

wcfsetup/install/files/lib/data/article/content/ViewableArticleContentList.class.php

index 1a3f367223d06bebdf46d1a3bc1d4e35d3de61cc..598a65aeade68725dd8dd4d2550b78a7402ddcb1 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\data\article\content;
+use wcf\data\article\ViewableArticleList;
 use wcf\data\media\ViewableMediaList;
 use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
 
@@ -29,7 +30,7 @@ class ViewableArticleContentList extends ArticleContentList {
        public function readObjects() {
                parent::readObjects();
                
-               $imageIDs = $embeddedObjectPostIDs = [];
+               $imageIDs = $embeddedObjectPostIDs = $articleIDs = [];
                foreach ($this->getObjects() as $articleContent) {
                        if ($articleContent->imageID) {
                                $imageIDs[] = $articleContent->imageID;
@@ -40,6 +41,8 @@ class ViewableArticleContentList extends ArticleContentList {
                        if ($articleContent->hasEmbeddedObjects) {
                                $embeddedObjectPostIDs[] = $articleContent->articleContentID;
                        }
+                       
+                       $articleIDs[] = $articleContent->articleID;
                }
                
                // cache images
@@ -48,23 +51,43 @@ class ViewableArticleContentList extends ArticleContentList {
                        $mediaList->setObjectIDs($imageIDs);
                        $mediaList->readObjects();
                        $images = $mediaList->getObjects();
+               }
+               
+               // load embedded objects
+               if (!empty($embeddedObjectPostIDs)) {
+                       $contentLanguageID = null;
+                       if (count($embeddedObjectPostIDs) === 1) $contentLanguageID = reset($this->objects)->languageID;
                        
-                       foreach ($this->getObjects() as $articleContent) {
+                       MessageEmbeddedObjectManager::getInstance()->loadObjects('com.woltlab.wcf.article.content', $embeddedObjectPostIDs, $contentLanguageID);
+               }
+               
+               if (!empty($articleIDs)) {
+                       $articleList = new ViewableArticleList();
+                       // to prevent an infinity loop, because the list loads otherwise the article content
+                       $articleList->enableContentLoading(false);
+                       $articleList->setObjectIDs($articleIDs);
+                       $articleList->readObjects();
+               }
+               
+               foreach ($this->getObjects() as $articleContent) {
+                       if (isset($images)) {
                                if ($articleContent->imageID && isset($images[$articleContent->imageID])) {
                                        $articleContent->setImage($images[$articleContent->imageID]);
                                }
+                               
                                if ($articleContent->teaserImageID && isset($images[$articleContent->teaserImageID])) {
                                        $articleContent->setTeaserImage($images[$articleContent->teaserImageID]);
                                }
                        }
-               }
-               
-               // load embedded objects
-               if (!empty($embeddedObjectPostIDs)) {
-                       $contentLanguageID = null;
-                       if (count($embeddedObjectPostIDs) === 1) $contentLanguageID = reset($this->objects)->languageID;
                        
-                       MessageEmbeddedObjectManager::getInstance()->loadObjects('com.woltlab.wcf.article.content', $embeddedObjectPostIDs, $contentLanguageID);
+                       if (isset($articleList)) {
+                               if ($articleList->search($articleContent->articleID) !== null) {
+                                       $articleContent->setArticle($articleList->search($articleContent->articleID));
+                               }
+                               else {
+                                       throw new \LogicException('Unable to find article with id "'. $articleContent->articleID .'".');
+                               }
+                       }
                }
        }
 }