Example #1
0
 /**
  * Inject WordPress links into the XML sitemap
  *
  * @param Mage_Sitemap_Model_Sitemap $sitemap
  * @return bool
  */
 protected function _processXmlSitemap(Mage_Sitemap_Model_Sitemap $sitemap)
 {
     if (is_file($sitemap->getPreparedFilename()) && is_writeable($sitemap->getPreparedFilename())) {
         $xml = file_get_contents($sitemap->getPreparedFilename());
         if (strpos($xml, '</urlset>') !== false) {
             $xml = substr($xml, 0, strpos($xml, '</urlset>'));
             // Homepage
             $frequency = Mage::getStoreConfig('sitemap/wordpress_homepage/changefreq', $sitemap->getStoreId());
             $priority = Mage::getStoreConfig('sitemap/wordpress_homepage/priority', $sitemap->getStoreId());
             $xml .= sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', htmlspecialchars(Mage::helper('wordpress')->getUrl()), date('Y-m-d'), $frequency, $priority);
             // Posts & Pages
             foreach (array('post', 'page') as $type) {
                 $items = Mage::getResourceModel('wordpress/' . $type . '_collection')->addIsPublishedFilter()->setOrderByPostDate();
                 if (count($items) > 0) {
                     $frequency = Mage::getStoreConfig('sitemap/wordpress_' . $type . '/changefreq', $sitemap->getStoreId());
                     $priority = Mage::getStoreConfig('sitemap/wordpress_' . $type . '/priority', $sitemap->getStoreId());
                     foreach ($items as $item) {
                         $xml .= sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', htmlspecialchars($item->getPermalink()), $item->getPostModifiedDate('Y-m-d'), $frequency, $priority);
                     }
                 }
             }
             $xml .= '</urlset>';
             $f = fopen($sitemap->getPreparedFilename(), 'w');
             if ($f) {
                 fwrite($f, $xml);
                 fclose($f);
                 return true;
             }
         }
     }
     return false;
 }
Example #2
0
 public function generateXml()
 {
     $enabled = (bool) Mage::getStoreConfig('atwix_sitemap/general/enabled');
     if (!$enabled) {
         return parent::generateXml();
     }
     $helper = Mage::helper('atwix_sitemap');
     $limit = (int) Mage::getStoreConfig('atwix_sitemap/general/limit');
     if ($limit == 0) {
         $limit = self::ITEM_LIMIT;
     }
     $this->fileCreate();
     $storeId = $this->getStoreId();
     $date = date('c', strtotime($this->getSitemapTime()));
     $baseUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
     /**
      * Generate cms pages sitemap
      */
     $changefreq = (string) Mage::getStoreConfig('sitemap/page/changefreq');
     $prioritypages = '0.6';
     $collection = Mage::getResourceModel('sitemap/cms_page')->getCollection($storeId);
     /**
      * Delete old cms pages files
      */
     try {
         foreach (glob($this->getPath() . substr($this->getSitemapFilename(), 0, strpos($this->getSitemapFilename(), '.xml')) . '_pages_*.xml') as $f) {
             unlink($f);
         }
     } catch (Exception $e) {
         Mage::getSingleton('adminhtml/session')->addError($helper->__('Unable to delete old products sitemaps') . $e->getMessage());
     }
     /**
      * Brake to pages
      */
     $datepageindex = date('c');
     $pages = ceil(count($collection) / $limit);
     $i = 0;
     while ($i < $pages) {
         $name = '_pages_' . $i . '.xml';
         $this->subFileCreate($name);
         $subCollection = array_slice($collection, $i * $limit, $limit);
         foreach ($subCollection as $item) {
             $datepages = date('c', strtotime(Mage::getModel('cms/page')->load($item->getId())->getUpdateTime()));
             $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%s</priority></url>', $item->getUrl() == 'home-v2' ? $baseUrl : htmlspecialchars($baseUrl . $item->getUrl()), $datepages, $item->getUrl() == 'home-v2' ? 'always' : $changefreq, $item->getUrl() == 'home-v2' ? '1' : $prioritypages);
             $this->sitemapSubFileAddLine($xml, $name);
         }
         $this->subFileClose($name);
         /**
          * Adding link of the subfile to the main file
          */
         $xml = sprintf('<sitemap><loc>%s</loc><lastmod>%s</lastmod></sitemap>', htmlspecialchars($this->getSubFileUrl($name)), $date);
         $this->sitemapFileAddLine($xml);
         $i++;
     }
     unset($collection);
     /**
      * Generate categories sitemap
      */
     $changefreq = (string) Mage::getStoreConfig('sitemap/category/changefreq');
     $prioritycat = '0.8';
     $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
     /**
      * Delete old category files
      */
     try {
         foreach (glob($this->getPath() . substr($this->getSitemapFilename(), 0, strpos($this->getSitemapFilename(), '.xml')) . '_cat_*.xml') as $f) {
             unlink($f);
         }
     } catch (Exception $e) {
         Mage::getSingleton('adminhtml/session')->addError($helper->__('Unable to delete old categories sitemaps') . $e->getMessage());
     }
     /**
      * Brake to pages
      */
     $datecatindex = date('c');
     $pages = ceil(count($collection) / $limit);
     $i = 0;
     while ($i < $pages) {
         $name = '_cat_' . $i . '.xml';
         $this->subFileCreate($name);
         $subCollection = array_slice($collection, $i * $limit, $limit);
         foreach ($subCollection as $item) {
             $datecat = date('c', strtotime(Mage::getModel('catalog/category')->load($item->getId())->getUpdatedAt()));
             $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%s</priority></url>', htmlspecialchars($baseUrl . $item->getUrl()), $datecat, $changefreq, $prioritycat);
             $this->sitemapSubFileAddLine($xml, $name);
         }
         $this->subFileClose($name);
         /**
          * Adding link of the subfile to the main file
          */
         $xml = sprintf('<sitemap><loc>%s</loc><lastmod>%s</lastmod></sitemap>', htmlspecialchars($this->getSubFileUrl($name)), $date);
         $this->sitemapFileAddLine($xml);
         $i++;
     }
     unset($collection);
     /**
      * Generate products sitemap
      */
     $changefreq = (string) Mage::getStoreConfig('sitemap/product/changefreq');
     $priorityprod = '0.7';
     $collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId);
     /**
      * Delete old category files
      */
     try {
         foreach (glob($this->getPath() . substr($this->getSitemapFilename(), 0, strpos($this->getSitemapFilename(), '.xml')) . '_prod_*.xml') as $f) {
             unlink($f);
         }
     } catch (Exception $e) {
         Mage::getSingleton('adminhtml/session')->addError($helper->__('Unable to delete old products sitemaps') . $e->getMessage());
     }
     /**
      * Brake to pages
      */
     $dateprod = date('c');
     $dateprodindex = date('c');
     $pages = ceil(count($collection) / $limit);
     $i = 0;
     while ($i < $pages) {
         $name = '_prod_' . $i . '.xml';
         $this->subFileCreate($name);
         $subCollection = array_slice($collection, $i * $limit, $limit);
         foreach ($subCollection as $item) {
             $categories = $item->getCategoryIds();
             //$prod_upt =date('c', strtotime($item->getUpdatedAt()));
             if (in_array(282, $categories)) {
                 $prod_url = Mage::getBaseUrl() . 'building-material/tmt-steel.html';
             } else {
                 $store = Mage::app()->getStore();
                 $path = Mage::getResourceModel('core/url_rewrite')->getRequestPathByIdPath("product/{$_proId}/{$_catId}", $store);
                 $url = $store->getBaseUrl($store::URL_TYPE_WEB) . $path;
                 $product = Mage::getModel('catalog/product')->load($item->getId());
                 $prod_upt = date('c', strtotime($product->getUpdatedAt()));
                 $category_id = $product->getCategoryIds();
                 $category = Mage::getModel('catalog/category')->load($category_id[0]);
                 $prod_url = Mage::getBaseUrl() . $product->getUrlPath($category);
                 $prodcurl = str_replace('/index.php/', '', $prod_url);
             }
             $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%s</priority></url>', htmlspecialchars($prodcurl), $prod_upt, $changefreq, $priorityprod);
             $this->sitemapSubFileAddLine($xml, $name);
         }
         $this->subFileClose($name);
         /**
          * Adding link of the subfile to the main file
          */
         $xml = sprintf('<sitemap><loc>%s</loc><lastmod>%s</lastmod></sitemap>', htmlspecialchars($this->getSubFileUrl($name)), $date);
         $this->sitemapFileAddLine($xml);
         $i++;
     }
     unset($collection);
     $this->fileClose();
     $this->setSitemapTime(Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s'));
     $this->save();
     return $this;
 }
 /**
  * Returns an xml file with contains the urls for
  * - categories url
  * - products in categories (with the ids of products and category)
  * - products url
  * - other cms pages
  * This can e.g. be used to create a horizontal navigation in the product view
  * @return Mage_Sitemap_Model_Sitemap
  */
 public function generateXml()
 {
     $io = new Varien_Io_File();
     $io->setAllowCreateFolders(true);
     $io->open(array('path' => $this->getPath()));
     if ($io->fileExists($this->getOptimizedSitemapFilename()) && !$io->isWriteable($this->getOptimizedSitemapFilename())) {
         Mage::throwException(Mage::helper('sitemap')->__('File "%s" cannot be saved. Please, make sure the directory "%s" is writeable by web server.', $this->getOptimizedSitemapFilename(), $this->getPath()));
     }
     $io->streamOpen($this->getOptimizedSitemapFilename());
     $io->streamWrite('<?xml version="1.0" encoding="UTF-8"?>' . "\n");
     $io->streamWrite('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:narfstudios="http://www.narf-studios.de">');
     $storeId = $this->getStoreId();
     $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);
     foreach ($collection as $item) {
         $url = $baseUrl . $item->getUrl();
         $url = str_replace('/index.php', '', $url);
         $xml = sprintf('<url><narfstudios:type>category</narfstudios:type><narfstudios:catid>' . $item->getId() . '</narfstudios:catid><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', htmlspecialchars($url), $date, $changefreq, $priority);
         $io->streamWrite($xml);
     }
     unset($collection);
     /**
      * Generate products in categories sitemap
      */
     $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
     foreach ($collection as $item) {
         $category = Mage::getModel('catalog/category')->load($item->getId());
         $pCollection = $category->getProductCollection();
         $pCollection->addAttributeToFilter('status', 1);
         $pCollection->addAttributeToFilter('visibility', 4);
         //catalog, search
         $pCollection->addAttributeToSort('name', 'asc');
         foreach ($pCollection as $product) {
             $product = Mage::getModel('catalog/product')->load($product->getId());
             $burl = Mage::getBaseUrl() . Mage::getModel('core/url_rewrite')->loadByIdPath('product/' . $product->getId() . '/' . $category->getId())->getRequestPath();
             $url = str_replace('/index.php', '', $burl);
             $xml = sprintf('<url><narfstudios:type>category_product</narfstudios:type><narfstudios:catid>' . $category->getId() . '</narfstudios:catid><narfstudios:pid>' . $product->getId() . '</narfstudios:pid><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', htmlspecialchars($url), $date, $changefreq, $priority);
             $io->streamWrite($xml);
         }
     }
     /**
      * Generate products sitemap
      */
     $changefreq = (string) Mage::getStoreConfig('sitemap/product/changefreq', $storeId);
     $priority = (string) Mage::getStoreConfig('sitemap/product/priority', $storeId);
     $collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId);
     foreach ($collection as $item) {
         $xml = sprintf('<url><narfstudios:type>product</narfstudios:type><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', htmlspecialchars($baseUrl . $item->getUrl()), $date, $changefreq, $priority);
         $io->streamWrite($xml);
     }
     unset($collection);
     /**
      * Generate cms pages sitemap
      */
     $changefreq = (string) Mage::getStoreConfig('sitemap/page/changefreq', $storeId);
     $priority = (string) Mage::getStoreConfig('sitemap/page/priority', $storeId);
     $collection = Mage::getResourceModel('sitemap/cms_page')->getCollection($storeId);
     foreach ($collection as $item) {
         $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', htmlspecialchars($baseUrl . $item->getUrl()), $date, $changefreq, $priority);
         $io->streamWrite($xml);
     }
     unset($collection);
     $io->streamWrite('</urlset>');
     $io->streamClose();
     $this->setSitemapTime(Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s'));
     $this->save();
     parent::generateXml();
     return $this;
 }