Delete sitemap relics when rebuilding the sitemap
authorJoshua Rüsweg <josh@bastelstu.be>
Tue, 3 Oct 2017 09:11:50 +0000 (11:11 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Tue, 3 Oct 2017 09:11:50 +0000 (11:11 +0200)
Delete all previously created sitemap files on rebuild so that no more relics remain in the system.

wcfsetup/install/files/lib/system/worker/SitemapRebuildWorker.class.php

index 3c1faad535f45d543e81c246acc480236ddc711a..d455de512299fe93c6a8d2b75cae3002b0b3b629 100755 (executable)
@@ -9,6 +9,7 @@ use wcf\system\exception\ImplementationException;
 use wcf\system\exception\ParentClassException;
 use wcf\system\io\AtomicWriter;
 use wcf\system\io\File;
+use wcf\system\Regex;
 use wcf\system\request\LinkHandler;
 use wcf\system\WCF;
 use wcf\util\FileUtil;
@@ -93,6 +94,9 @@ class SitemapRebuildWorker extends AbstractWorker {
                                                // modify count, because we handle only one sitemap object per call
                                                $this->count += max(1, ceil($list->countObjects() / $this->limit)) * $this->limit;
                                        }
+                                       else {
+                                               $this->deleteSitemaps($sitemapObject->objectType);
+                                       }
                                }
                        }
                }
@@ -131,6 +135,11 @@ class SitemapRebuildWorker extends AbstractWorker {
                        $sitemapObject = $this->sitemapObjects[$this->workerData['sitemap']]->getProcessor();
                        $sitemapLoopCount = $this->workerData['sitemapLoopCount'];
                        
+                       // delete all previously created sitemap files so that no more relics remain in the system
+                       if ($sitemapLoopCount === 0) {
+                               $this->deleteSitemaps($this->sitemapObjects[$this->workerData['sitemap']]->objectType);
+                       }
+                       
                        /** @var DatabaseObjectList $objectList */
                        $objectList = $sitemapObject->getObjectList();
                        
@@ -362,6 +371,23 @@ class SitemapRebuildWorker extends AbstractWorker {
                return WCF::getPath() . 'sitemaps/';
        }
        
+       /**
+        * Unlink the sitemap files for a given object type name.
+        * 
+        * @param       string          $objectTypeName
+        */
+       private function deleteSitemaps($objectTypeName) {
+               $files = @glob(self::getSitemapPath().$objectTypeName.'*.xml');
+               if (is_array($files)) {
+                       $regex = new Regex(preg_quote($objectTypeName).'(_[0-9]*|).xml');
+                       foreach ($files as $filename) {
+                               if ($regex->match(basename($filename))) {
+                                       unlink($filename);
+                               }
+                       }
+               }
+       }
+       
        /**
         * Saves the actual user and changes the session owner to a guest. 
         */