Public Member Functions | |
generateXml () | |
Protected Member Functions | |
_construct () | |
_beforeSave () | |
getPath () | |
Protected Attributes | |
$_filePath |
Definition at line 35 of file Sitemap.php.
_beforeSave | ( | ) | [protected] |
Processing object before save data
Check path is allow
Check exists and writeable path
Check allow filename
Reimplemented from Mage_Core_Model_Abstract.
Definition at line 52 of file Sitemap.php.
00053 { 00054 $io = new Varien_Io_File(); 00055 $realPath = $io->getCleanPath(Mage::getBaseDir() . '/' . $this->getSitemapPath()); 00056 00057 /** 00058 * Check path is allow 00059 */ 00060 if (!$io->allowedPath($realPath, Mage::getBaseDir())) { 00061 Mage::throwException(Mage::helper('sitemap')->__('Please define correct path')); 00062 } 00063 /** 00064 * Check exists and writeable path 00065 */ 00066 if (!$io->fileExists($realPath, false)) { 00067 Mage::throwException(Mage::helper('sitemap')->__('Please create the specified folder "%s" before saving the sitemap.', $this->getSitemapPath())); 00068 } 00069 00070 if (!$io->isWriteable($realPath)) { 00071 Mage::throwException(Mage::helper('sitemap')->__('Please make sure that "%s" is writable by web-server.', $this->getSitemapPath())); 00072 } 00073 /** 00074 * Check allow filename 00075 */ 00076 if (!preg_match('#^[a-zA-Z0-9_\.]+$#', $this->getSitemapFilename())) { 00077 Mage::throwException(Mage::helper('sitemap')->__('Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in the filename. No spaces or other characters are allowed.')); 00078 } 00079 if (!preg_match('#\.xml$#', $this->getSitemapFilename())) { 00080 $this->setSitemapFilename($this->getSitemapFilename() . '.xml'); 00081 } 00082 00083 $this->setSitemapPath(rtrim(str_replace(str_replace('\\', '/', Mage::getBaseDir()), '', $realPath), '/') . '/'); 00084 00085 return parent::_beforeSave(); 00086 }
_construct | ( | ) | [protected] |
Init model
Reimplemented from Varien_Object.
Definition at line 47 of file Sitemap.php.
00048 { 00049 $this->_init('sitemap/sitemap'); 00050 }
generateXml | ( | ) |
Generate XML file
Generate categories sitemap
Generate products sitemap
Generate cms pages sitemap
Definition at line 107 of file Sitemap.php.
00108 { 00109 $io = new Varien_Io_File(); 00110 $io->setAllowCreateFolders(true); 00111 $io->open(array('path' => $this->getPath())); 00112 00113 if ($io->fileExists($this->getSitemapFilename()) && !$io->isWriteable($this->getSitemapFilename())) { 00114 Mage::throwException(Mage::helper('sitemap')->__('File "%s" cannot be saved. Please, make sure the directory "%s" is writeable by web server.', $this->getSitemapFilename(), $this->getPath())); 00115 } 00116 00117 $io->streamOpen($this->getSitemapFilename()); 00118 00119 $io->streamWrite('<?xml version="1.0" encoding="UTF-8"?>' . "\n"); 00120 $io->streamWrite('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'); 00121 00122 $storeId = $this->getStoreId(); 00123 $date = Mage::getSingleton('core/date')->gmtDate('Y-m-d'); 00124 $baseUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK); 00125 00126 /** 00127 * Generate categories sitemap 00128 */ 00129 $changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq'); 00130 $priority = (string)Mage::getStoreConfig('sitemap/category/priority'); 00131 $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId); 00132 foreach ($collection as $item) { 00133 $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', 00134 htmlspecialchars($baseUrl . $item->getUrl()), 00135 $date, 00136 $changefreq, 00137 $priority 00138 ); 00139 $io->streamWrite($xml); 00140 } 00141 unset($collection); 00142 00143 /** 00144 * Generate products sitemap 00145 */ 00146 $changefreq = (string)Mage::getStoreConfig('sitemap/product/changefreq'); 00147 $priority = (string)Mage::getStoreConfig('sitemap/product/priority'); 00148 $collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId); 00149 foreach ($collection as $item) { 00150 $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', 00151 htmlspecialchars($baseUrl . $item->getUrl()), 00152 $date, 00153 $changefreq, 00154 $priority 00155 ); 00156 $io->streamWrite($xml); 00157 } 00158 unset($collection); 00159 00160 /** 00161 * Generate cms pages sitemap 00162 */ 00163 $changefreq = (string)Mage::getStoreConfig('sitemap/page/changefreq'); 00164 $priority = (string)Mage::getStoreConfig('sitemap/page/priority'); 00165 $collection = Mage::getResourceModel('sitemap/cms_page')->getCollection($storeId); 00166 foreach ($collection as $item) { 00167 $xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>', 00168 htmlspecialchars($baseUrl . $item->getUrl()), 00169 $date, 00170 $changefreq, 00171 $priority 00172 ); 00173 $io->streamWrite($xml); 00174 } 00175 unset($collection); 00176 00177 $io->streamWrite('</urlset>'); 00178 $io->streamClose(); 00179 00180 $this->setSitemapTime(Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s')); 00181 $this->save(); 00182 00183 return $this; 00184 }
getPath | ( | ) | [protected] |
Return real file path
Definition at line 93 of file Sitemap.php.
00094 { 00095 if (is_null($this->_filePath)) { 00096 $this->_filePath = str_replace('//', '/', Mage::getBaseDir() . 00097 $this->getSitemapPath()); 00098 } 00099 return $this->_filePath; 00100 }
$_filePath [protected] |
Definition at line 42 of file Sitemap.php.