Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
48 changes: 43 additions & 5 deletions Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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();
}
}

/**
Expand All @@ -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();
}
Expand Down