From 7880c5f8c308586aaa4be2b58c6a95ac3cacc1bd Mon Sep 17 00:00:00 2001 From: Michael Ryvkin Date: Tue, 22 Apr 2014 17:02:28 -0400 Subject: [PATCH] Added support for gzip compression of sitemap files. --- README.md | 4 ++++ Sitemap.php | 48 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 399ca1a..02cc83b 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,10 @@ from a sql result, or whatever. If you need to change domain for sitemap instance, you can override it via `setDomain` method. $sitemap->setDomain('http://blog.example.com'); + +If you would like to generate compressed (.xml.gz) sitemap files, enable gzip compression by using `setGzip` method. + + $sitemap->setGzip(); Finally we create index for sitemap files. This method also closes tags of latest generated xml file. diff --git a/Sitemap.php b/Sitemap.php index 09c5455..6164c2b 100644 --- a/Sitemap.php +++ b/Sitemap.php @@ -25,6 +25,7 @@ class Sitemap { private $filename = 'sitemap'; private $current_item = 0; private $current_sitemap = 0; + private $gzip = false; const EXT = '.xml'; const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9'; @@ -118,6 +119,26 @@ public function setFilename($filename) { return $this; } + /** + * Enables/disables sitemap file gzip compression + * + * @param bool $state + * @return Sitemap + */ + public function setGzip($state = true) { + $this->gzip = $state; + return $this; + } + + /** + * Determines whether sitemap file gzip compression is enabled + * + * @return bool + */ + public function getGzip() { + return $this->gzip; + } + /** * Returns current item count * @@ -158,11 +179,17 @@ private function incCurrentSitemap() { */ private function startSitemap() { $this->setWriter(new XMLWriter()); - if ($this->getCurrentSitemap()) { - $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT); + if($this->getGzip()){ + $this->getWriter()->openMemory(); } else { - $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::EXT); + if ($this->getCurrentSitemap()) { + $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT); + } else { + $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::EXT); + } + $this->incCurrentSitemap(); } + $this->getWriter()->startDocument('1.0', 'UTF-8'); $this->getWriter()->setIndent(true); $this->getWriter()->startElement('urlset'); @@ -184,7 +211,6 @@ public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = $this->endSitemap(); } $this->startSitemap(); - $this->incCurrentSitemap(); } $this->incCurrentItem(); $this->getWriter()->startElement('url'); @@ -223,6 +249,18 @@ private function endSitemap() { } $this->getWriter()->endElement(); $this->getWriter()->endDocument(); + + if($this->getGzip()){ + $filename = ($this->getCurrentSitemap()) + ? $this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT + : $this->getPath() . $this->getFilename() . self::EXT; + + $file = gzopen($filename.'.gz', 'w'); + gzwrite($file, $this->getWriter()->outputMemory()); + gzclose($file); + + $this->incCurrentSitemap(); + } } /** @@ -241,7 +279,7 @@ public function createSitemapIndex($loc, $lastmod = 'Today') { $indexwriter->writeAttribute('xmlns', self::SCHEMA); for ($index = 0; $index < $this->getCurrentSitemap(); $index++) { $indexwriter->startElement('sitemap'); - $indexwriter->writeElement('loc', $loc . $this->getFilename() . ($index ? self::SEPERATOR . $index : '') . self::EXT); + $indexwriter->writeElement('loc', $loc . $this->getFilename() . ($index ? self::SEPERATOR . $index : '') . self::EXT . ($this->getGzip() ? '.gz' : '')); $indexwriter->writeElement('lastmod', $this->getLastModifiedDate($lastmod)); $indexwriter->endElement(); }