Multiple xml sitemap in Magento

When the amount of the products/categories/pages in Magento is becoming bigger then 50000 in sum, it's required to split the xml sitemap to a few chunks. It's possible to do with the small fix in the code/core/Mage/Sitemap/Model/Sitemap.php.

We need to add the new function here

    public function check_counter(&$io) {
        static $counter;
        $counter++;

        if ($counter == 50000) {
            $io->streamWrite('</urlset>');
            $io->streamClose();
            $filename = preg_replace('/\.xml/', '-'.round($counter/50000).'.xml', $this->getSitemapFilename());
            $io->streamOpen($filename);
            $io->streamWrite('<?xml version="1.0" encoding="UTF-8"?>' . "\n");
            $io->streamWrite('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
        }
    }

This function will check the amount of the written urls and will create the new file if required. We need to call this function after each url writing, So in default Magento it should be called 3 times in each collection cycle:

        foreach ($collection as $item) {
            ...
            $io->streamWrite($xml);
            $this->check_counter($io);
        }