Add isAvailableType() method for sitemap object types
authorJoshua Rüsweg <josh@bastelstu.be>
Thu, 14 Sep 2017 10:11:42 +0000 (12:11 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Thu, 14 Sep 2017 10:11:42 +0000 (12:11 +0200)
This method checks the requirements (e.g. module options) for the object type.
See #2286

wcfsetup/install/files/lib/system/sitemap/object/AbstractSitemapObjectObjectType.class.php
wcfsetup/install/files/lib/system/sitemap/object/ArticleCategorySitemapObject.class.php
wcfsetup/install/files/lib/system/sitemap/object/ArticleSitemapObject.class.php
wcfsetup/install/files/lib/system/sitemap/object/ISitemapObjectObjectType.class.php
wcfsetup/install/files/lib/system/sitemap/object/UserSitemapObject.class.php
wcfsetup/install/files/lib/system/worker/SitemapRebuildWorker.class.php

index a01cd729c2646e0590e974a5fef58ce92694b1d7..7b1dabcaa622988240f33fc31b58d4650a66f437 100755 (executable)
@@ -40,4 +40,11 @@ abstract class AbstractSitemapObjectObjectType implements ISitemapObjectObjectTy
        public function canView(DatabaseObject $object) {
                return true;
        }
+       
+       /**
+        * @inheritDoc
+        */
+       public function isAvailableType() {
+               return true; 
+       }
 }
index 2d4993102f36e75e970fc9c21cb32ba3a196a0b3..848567033d68ede4c67e72768426b4b887ed8620 100644 (file)
@@ -40,4 +40,11 @@ class ArticleCategorySitemapObject extends AbstractSitemapObjectObjectType {
                /** @var $object ArticleCategory */
                return $object->isAccessible();
        }
+       
+       /**
+        * @inheritDoc
+        */
+       public function isAvailableType() {
+               return MODULE_ARTICLE;
+       }
 }
index 39402db1600a9c5613bdb23920743fda05f3b69f..3d00d6fa00311ecb7449528d0ec165840053d739 100644 (file)
@@ -27,4 +27,11 @@ class ArticleSitemapObject extends AbstractSitemapObjectObjectType {
                /** @var $object ArticleContent */
                return $object->getArticle()->canRead();
        }
+       
+       /**
+        * @inheritDoc
+        */
+       public function isAvailableType() {
+               return MODULE_ARTICLE;
+       }
 }
index 375a30d7e75135db052a3b21557bccdf1e594b87..ec844d6185f0ac2541c2c5b7f09e00e35d39a77a 100755 (executable)
@@ -49,4 +49,11 @@ interface ISitemapObjectObjectType {
         * @return      boolean
         */
        public function canView(DatabaseObject $object);
+       
+       /**
+        * Checks the requirements (e.g. module options) for this object type.
+        *
+        * @return      boolean
+        */
+       public function isAvailableType(); 
 }
index 9fabf9816c199eeb25f18c455c4a2a476fcd2815..345360c4c1bb16fd46202c4c44bcce2b00ba7848 100755 (executable)
@@ -1,7 +1,6 @@
 <?php
 namespace wcf\system\sitemap\object;
 use wcf\data\user\User;
-use wcf\data\DatabaseObject;
 use wcf\system\WCF;
 
 /**
@@ -31,7 +30,7 @@ class UserSitemapObject extends AbstractSitemapObjectObjectType {
        /**
         * @inheritDoc
         */
-       public function canView(DatabaseObject $object) {
+       public function isAvailableType() {
                return WCF::getSession()->getPermission('user.profile.canViewUserProfile');
        }
 }
index 790ed3a0474d6c6cbfe4714e0baf0c8924f2ddac..3c1faad535f45d543e81c246acc480236ddc711a 100755 (executable)
@@ -62,35 +62,44 @@ class SitemapRebuildWorker extends AbstractWorker {
         * @inheritDoc
         */
        protected function countObjects() {
-               if ($this->count === null) {
-                       // reset count
-                       $this->count = 0;
-                       
-                       // read sitemaps
-                       $sitemapObjects = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.sitemap.object');
-                       foreach ($sitemapObjects as $sitemapObject) {
-                               if ($sitemapObject->isDisabled === null || !$sitemapObject->isDisabled) {
-                                       $this->sitemapObjects[] = $sitemapObject;
-                                       
+               // changes session owner to 'System' during the building of sitemaps
+               $this->changeUserToGuest();
+               
+               try {
+                       if ($this->count === null) {
+                               // reset count
+                               $this->count = 0;
+                               
+                               // read sitemaps
+                               $sitemapObjects = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.sitemap.object');
+                               foreach ($sitemapObjects as $sitemapObject) {
                                        $processor = $sitemapObject->getProcessor();
                                        
-                                       $list = $processor->getObjectList();
-                                       
-                                       if (!($list instanceof DatabaseObjectList)) {
-                                               throw new ParentClassException(get_class($list), DatabaseObjectList::class);
+                                       if ($processor->isAvailableType() && ($sitemapObject->isDisabled === null || !$sitemapObject->isDisabled)) {
+                                               $this->sitemapObjects[] = $sitemapObject;
+                                               
+                                               $list = $processor->getObjectList();
+                                               
+                                               if (!($list instanceof DatabaseObjectList)) {
+                                                       throw new ParentClassException(get_class($list), DatabaseObjectList::class);
+                                               }
+                                               
+                                               if (SITEMAP_INDEX_TIME_FRAME > 0 && $processor->getLastModifiedColumn() !== null) {
+                                                       $list->getConditionBuilder()->add($processor->getLastModifiedColumn() . " > ?", [
+                                                               TIME_NOW - SITEMAP_INDEX_TIME_FRAME * 86400 // one day (60 * 60 * 24)
+                                                       ]);
+                                               }
+                                               
+                                               // modify count, because we handle only one sitemap object per call
+                                               $this->count += max(1, ceil($list->countObjects() / $this->limit)) * $this->limit;
                                        }
-                                       
-                                       if (SITEMAP_INDEX_TIME_FRAME > 0 && $processor->getLastModifiedColumn() !== null) {
-                                               $list->getConditionBuilder()->add($processor->getLastModifiedColumn() . " > ?", [
-                                                       TIME_NOW - SITEMAP_INDEX_TIME_FRAME * 86400 // one day (60 * 60 * 24)
-                                               ]);
-                                       }
-                                       
-                                       // modify count, because we handle only one sitemap object per call
-                                       $this->count += max(1, ceil($list->countObjects() / $this->limit)) * $this->limit;
                                }
                        }
                }
+               finally {
+                       // change session owner back to the actual user
+                       $this->changeToActualUser();
+               }
        }
        
        /**