From 52b710cca3c4dfafbe046a83415d5d964a1ec5bd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Tue, 3 Oct 2017 11:11:50 +0200 Subject: [PATCH] Delete sitemap relics when rebuilding the sitemap Delete all previously created sitemap files on rebuild so that no more relics remain in the system. --- .../worker/SitemapRebuildWorker.class.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/wcfsetup/install/files/lib/system/worker/SitemapRebuildWorker.class.php b/wcfsetup/install/files/lib/system/worker/SitemapRebuildWorker.class.php index 3c1faad535..d455de5122 100755 --- a/wcfsetup/install/files/lib/system/worker/SitemapRebuildWorker.class.php +++ b/wcfsetup/install/files/lib/system/worker/SitemapRebuildWorker.class.php @@ -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. */ -- 2.20.1