Magento 1.9 – How to Get All Magento URLs by Store View

magento-1.9sitemapsurl

I'm looking to retrieve all URLs of a Magento website (CMS pages, categories, products…) subdivided by store view.
Something like the default Magento Sitemap, but with a different format: specifically I'm trying to get a "one URL per row" plain text file, one for each store view.

Best Answer

Take a look at how Magento generates it's sitemap.xml file.

Specifically the Mage_Sitemap_Model_Sitemap class method generateXml. For example, following snippet generates them for the categories, I've rewritten it a little to export to a flat text file.

$ioWrite = new Varien_Io_File();
$path = ''; 
$ioWrite->open(array('path' => $path));
$ioWrite->streamOpen($path.'urls.txt');

$storeId =  Mage::app()->getStore()->getId();
$date    = Mage::getSingleton('core/date')->gmtDate('Y-m-d');
$baseUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);

/**
 * Generate categories sitemap
 */
$changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
$priority   = (string)Mage::getStoreConfig('sitemap/category/priority', $storeId);
$collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
$categories = new Varien_Object();
$categories->setItems($collection);
Mage::dispatchEvent('sitemap_categories_generating_before', array(
    'collection' => $categories
));
foreach ($categories->getItems() as $item) {
    $ioWrite->streamWrite(htmlspecialchars($baseUrl . $item->getUrl()));
}
unset($collection);
$ioWrite->close();

Add the code from the class to your own script to generate a text file