Example #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;
 }
Example #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);
 }