_file = WWW_PATH . '/' . $this->_fileName; } public function start() { $file = str_replace('.xml', '.' . $this->_fileIndex . '.xml', $this->_file); $this->_checkFile($file); $this->openUri($file); $this->setIndent(true); $this->setIndentString(' '); $this->startDocument('1.0', 'UTF-8'); $this->startElement('urlset'); $this->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); $this->_urlCount = 0; $this->_size = 0; return $this; } public function startIndex() { $this->_checkFile($this->_file); $this->openUri($this->_file); $this->setIndent(true); $this->setIndentString(' '); $this->startDocument('1.0', 'UTF-8'); $this->startElement('sitemapindex'); $this->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); return $this; } public function endIndex() { $this->endElement(); // sitemapindex $this->endDocument(); } public function writeUrl($url, $lastmod = null) { $this->_flushed = false; $this->startElement('url'); $this->startElement('loc'); $this->text($url); $this->endElement(); // loc if (!empty($lastmod)){ $this->startElement('lastmod'); $this->text(date('c', strtotime($lastmod))); $this->endElement(); //lastmod } $this->endElement(); // url $this->_urlCount++; $this->_size += $this->flush(); if ($this->_urlCount == $this->_maxUrlCount || ($this->_size >= $this->_maxSize - 256)) { $this->end(); $this->flush(); $this->_fileIndex++; $this->start(); } return $this; } protected function _checkFile($file) { if (!is_writable(dirname($file))) { throw new App_Sitemap_Xml_Exception('Destination directory "' . dirname($file) . '" is not writable'); } if (file_exists($file) && !is_writable($file)) { throw new App_Sitemap_Xml_Exception('Destination file "' . $file . '" is not writable'); } return $this; } public function end() { $this->endElement(); // urlset $this->endDocument(); return $this; } public function finish() { $this->flush(); if (1 == $this->_fileIndex) { $this->_checkFile($this->_file); $file = str_replace('.xml', '.' . $this->_fileIndex . '.xml', $this->_file); rename($file, $this->_file); return $this; } $this->startIndex(); for ($i = 0; $i < $this->_fileIndex; $i++) { $this->startElement('sitemap'); $this->startElement('loc'); $file = str_replace('.xml', '.' . ($i + 1) . '.xml', $this->_fileName); $this->text(BASE_URL . '/' . $file); $this->endElement(); // loc $this->endElement(); // sitemap } $this->endIndex(); $this->flush(); return $this; } public function output() { if (!$handle = fopen($this->_file, 'r')) { throw new App_Sitemap_Xml_Exception('Can not open file "' . $this->_file. '"'); } header('Content-type: text/xml'); fpassthru($handle); fclose($handle); if (constant('DEBUG')) { // Don't store sitemap.xml in file system for DEBUG mode self::removeFiles(); } exit; } public static function removeFiles() { foreach (glob(constant('WWW_PATH'). '/sitemap*.xml') as $filename) { unlink($filename); } } }