Fix recursively loading embedded objects in articles
authorMatthias Schmidt <gravatronics@live.com>
Thu, 8 Jul 2021 07:36:37 +0000 (09:36 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Thu, 8 Jul 2021 07:36:37 +0000 (09:36 +0200)
Close #4382
See 3af7b77d40ed93c95cebb22004ab745bf69683e2

wcfsetup/install/files/lib/data/article/ViewableArticleList.class.php
wcfsetup/install/files/lib/data/article/content/ViewableArticleContentList.class.php
wcfsetup/install/files/lib/system/message/embedded/object/ArticleMessageEmbeddedObjectHandler.class.php

index 293de4d2b29b3aaa0c26f15cf5c4dea1ef203ca1..096f9fa86d2a08e9317e3ecbe2eae3ae3a8085a7 100644 (file)
@@ -37,6 +37,13 @@ class ViewableArticleList extends ArticleList
      */
     protected $contentLoading = true;
 
+    /**
+     * enables/disables the loading of embedded objects in the article contents
+     * @var bool
+     * @since   5.4
+     */
+    protected $embeddedObjectLoading = true;
+
     /**
      * @inheritDoc
      */
@@ -92,6 +99,7 @@ class ViewableArticleList extends ArticleList
         // get article content
         if ($this->contentLoading && !empty($this->objectIDs)) {
             $contentList = new ViewableArticleContentList();
+            $contentList->enableEmbeddedObjectLoading($this->embeddedObjectLoading);
             $contentList->getConditionBuilder()->add('article_content.articleID IN (?)', [$this->objectIDs]);
             $contentList->getConditionBuilder()->add(
                 '(article_content.languageID IS NULL OR article_content.languageID = ?)',
@@ -128,4 +136,15 @@ class ViewableArticleList extends ArticleList
     {
         $this->contentLoading = $enable;
     }
+
+    /**
+     * Enables/disables the loading of embedded objects in the article contents.
+     *
+     * @param bool $enable
+     * @since   5.4
+     */
+    public function enableEmbeddedObjectLoading(bool $enable = true): void
+    {
+        $this->embeddedObjectLoading = $enable;
+    }
 }
index 56fa8fa1740024772ff64ee999107885c3f098c8..670aa27469ad0ce2888311a2555c887e5457cb09 100644 (file)
@@ -28,6 +28,13 @@ class ViewableArticleContentList extends ArticleContentList
      */
     public $decoratorClassName = ViewableArticleContent::class;
 
+    /**
+     * enables/disables the loading of embedded objects in the article contents
+     * @var bool
+     * @since   5.4
+     */
+    protected $embeddedObjectLoading = true;
+
     /**
      * @inheritDoc
      */
@@ -64,7 +71,7 @@ class ViewableArticleContentList extends ArticleContentList
         }
 
         // load embedded objects
-        if (!empty($embeddedObjectContentIDs)) {
+        if ($this->embeddedObjectLoading && !empty($embeddedObjectContentIDs)) {
             MessageEmbeddedObjectManager::getInstance()->loadObjects(
                 'com.woltlab.wcf.article.content',
                 $embeddedObjectContentIDs,
@@ -100,4 +107,15 @@ class ViewableArticleContentList extends ArticleContentList
             }
         }
     }
+
+    /**
+     * Enables/disables the loading of embedded objects in the article contents.
+     *
+     * @param bool $enable
+     * @since   5.4
+     */
+    public function enableEmbeddedObjectLoading(bool $enable = true): void
+    {
+        $this->embeddedObjectLoading = $enable;
+    }
 }
index 4d53b9fbdcffb05aa7201fac177eab18ac735339..13381e4a4a9a9d2944e46e1a61036ab914311180 100644 (file)
@@ -38,11 +38,17 @@ class ArticleMessageEmbeddedObjectHandler extends AbstractSimpleMessageEmbeddedO
      */
     public function loadObjects(array $objectIDs)
     {
-        $viewableArticles = ViewableArticleRuntimeCache::getInstance()->getObjects($objectIDs);
+        // Do not use `ViewableArticleRuntimeCache` to avoid recursively loading embedded objects.
+        $articleList = new AccessibleArticleList();
+        $articleList->enableEmbeddedObjectLoading(false);
+        $articleList->getConditionBuilder()->add('article.articleID IN (?)', [$objectIDs]);
+        $articleList->readObjects();
+        $articles = $articleList->getObjects();
+
         $contentLanguageID = MessageEmbeddedObjectManager::getInstance()->getContentLanguageID();
         if ($contentLanguageID !== null) {
             $articleIDs = [];
-            foreach ($viewableArticles as $article) {
+            foreach ($articles as $article) {
                 if (
                     $article !== null
                     && $article->getArticleContent()->languageID
@@ -59,12 +65,12 @@ class ArticleMessageEmbeddedObjectHandler extends AbstractSimpleMessageEmbeddedO
                 $list->readObjects();
 
                 foreach ($list->getObjects() as $articleContent) {
-                    $viewableArticles[$articleContent->articleID]->setArticleContent($articleContent);
+                    $articles[$articleContent->articleID]->setArticleContent($articleContent);
                 }
             }
         }
 
-        return $viewableArticles;
+        return $articles;
     }
 
     /**