Sonata\Exporter\Writer\SitemapWriter::generateSitemapIndex PHP Метод

generateSitemapIndex() публичный статический Метод

Generates the sitemap index from the sitemap part avaible in the folder.
public static generateSitemapIndex ( string $folder, string $baseUrl, string $pattern = 'sitemap*.xml', string $filename = 'sitemap.xml' )
$folder string A folder to write sitemap index
$baseUrl string A base URL
$pattern string A sitemap pattern, optional
$filename string A sitemap file name, optional
    public static function generateSitemapIndex($folder, $baseUrl, $pattern = 'sitemap*.xml', $filename = 'sitemap.xml')
    {
        $content = "<?xml version='1.0' encoding='UTF-8'?" . ">\n<sitemapindex xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/1.0 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd' xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n";
        foreach (glob(sprintf('%s/%s', $folder, $pattern)) as $file) {
            $stat = stat($file);
            $content .= sprintf("\t" . '<sitemap><loc>%s/%s</loc><lastmod>%s</lastmod></sitemap>' . "\n", $baseUrl, basename($file), date('Y-m-d', $stat['mtime']));
        }
        $content .= '</sitemapindex>';
        file_put_contents(sprintf('%s/%s', $folder, $filename), $content);
    }

Usage Example

Пример #1
0
    public function testSimpleWriteAdvanced()
    {
        $writer = new SitemapWriter($this->folder, 'test', array('image'), false);
        $writer->open();
        $writer->write(array('url' => 'https://sonata-project.org/bundle/', 'lastmod' => '2012-12-26', 'change' => 'daily', 'type' => 'default'));
        $writer->write(array('url' => 'https://sonata-project.org/bundle/', 'lastmod' => '2012-12-26', 'change' => 'weekly', 'type' => 'image', 'images' => array(array('url' => 'https://sonata-project.org/uploads/media/default/0001/01/thumb_1_default_small.jpg', 'caption' => 'sonata img'))));
        $writer->close();
        $generatedFiles = $this->getFiles();
        $this->assertEquals(1, count($generatedFiles));
        $this->assertEquals($this->folder . '/sitemap_test_00001.xml', $generatedFiles[0]);
        SitemapWriter::generateSitemapIndex($this->folder, 'https://sonata-project.org');
        $generatedFiles = $this->getFiles();
        $this->assertEquals(2, count($generatedFiles));
        // this will throw an exception if the xml is invalid
        new SimpleXMLElement(file_get_contents($generatedFiles[1]));
        $expected = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    <url><loc>https://sonata-project.org/bundle/</loc><lastmod>2012-12-26</lastmod><changefreq>weekly</changefreq><priority>0.5</priority></url>
    <url><loc>https://sonata-project.org/bundle/</loc><image:image><image:loc>https://sonata-project.org/uploads/media/default/0001/01/thumb_1_default_small.jpg</image:loc><image:caption>sonata img</image:caption></image:image></url>
</urlset>
XML;
        $this->assertEquals(trim($expected), file_get_contents($generatedFiles[1]));
    }