Exemplo n.º 1
0
 public function render($maxUrls = NULL, $maxMegabytes = NULL, $compression = NULL)
 {
     if ($maxUrls && !count($this) > $maxUrls) {
         throw new OutOfBoundsException('Sitemap has more than ' . $maxUrls . ' URLs and needs to be spitted');
     }
     $tree = new XML_Node('urlset');
     $tree->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
     foreach ($this->urls as $url) {
         $node = new XML_Node('url');
         $node->addChild(new XML_Node('loc', $url->getLocation()));
         if ($datetime = $url->getDatetime()) {
             $node->addChild(new XML_Node('lastmod', $datetime));
         }
         $tree->addChild($node);
     }
     $builder = new XML_Builder();
     $xml = $builder->build($tree);
     if ($compression) {
         if (!in_array($compression, $this->compressions)) {
             throw new OutOfRangeException('Available compression types are ' . join(", ", $this->compressions));
         }
         switch ($compression) {
             case 'bz':
                 $xml = bzcompress($xml);
                 break;
             case 'gz':
                 $xml = gzencode($xml);
                 break;
         }
     }
     if ($maxMegabytes && strlen($xml) > $maxMegabytes * 1024 * 1024) {
         throw new OutOfBoundsException('Rendered sitemap is to large (max: ' . $maxMegabytes . ' MB)');
     }
     return $xml;
 }
Exemplo n.º 2
0
 public function render()
 {
     $tree = new XML_Node('sitemapindex');
     $tree->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
     foreach ($this->sitemaps as $sitemap) {
         $node = new XML_Node('sitemap');
         $node->addChild(new XML_Node('loc', $sitemap->getUrl()));
         if ($sitemap->getDatetime()) {
             $node->addChild(new XML_Node('lastmod', $sitemap->getDatetime()));
         }
         $tree->addChild($node);
     }
     $builder = new XML_Builder();
     return $builder->build($tree);
 }
Exemplo n.º 3
0
 /**
  *	Writes XML Tree to XML File recursive.
  *	@access		protected
  *	@param		DOMElement		$document	DOM Document
  *	@param		DOMElement		$root		DOM Element
  *	@param		XML_Node	$tree		Parent XML Node
  *	@param		string			$encoding	Encoding Character Set (utf-8 etc.)
  *	@return		void
  */
 protected function buildRecursive(DOMDocument $document, DOMElement $root, XML_Node $tree, $encoding)
 {
     foreach ($tree->getAttributes() as $key => $value) {
         $root->setAttribute($key, $value);
     }
     if ($tree->hasChildren()) {
         $children =& $tree->getChildren();
         foreach ($children as $child) {
             $element = $document->createElement($child->getNodename());
             self::buildRecursive($document, $element, $child, $encoding);
             $element = $root->appendChild($element);
         }
     } else {
         if ($tree->hasContent()) {
             $text = (string) $tree->getContent();
             $text = $document->createTextNode($text);
             $text = $root->appendChild($text);
         }
     }
 }