[-] MO : #PSFV-605 - Fix problem with gsitemap and multishop

This commit is contained in:
mDeflotte
2012-03-07 08:52:30 +00:00
parent be2f75c9d6
commit 8fbb48ba2c
2 changed files with 105 additions and 69 deletions
+104 -69
View File
@@ -84,64 +84,65 @@ class Gsitemap extends Module
else else
$res = $this->generateSitemap(Configuration::get('PS_SHOP_DEFAULT'), GSITEMAP_FILE); $res = $this->generateSitemap(Configuration::get('PS_SHOP_DEFAULT'), GSITEMAP_FILE);
$this->_html .= '<h3 class="'. ($res ? 'conf confirm' : 'alert error') .'" style="margin-bottom: 20px">'; $this->_html .= '<h3 class="'. ($res ? 'conf confirm' : 'alert error') .'" style="margin-bottom: 20px">';
$this->_html .= $res ? $this->l('Sitemap file generated') : $this->l('Error while creating sitemap file'); $this->_html .= $res ? $this->l('Sitemap file generated') : $this->l('Error while creating sitemap file');
$this->_html .= '</h3>'; $this->_html .= '</h3>';
} }
/** /**
* Generate sitemap index to reference the sitemap of each shop * Generate sitemap index to reference the sitemap of each shop
* *
* @return bool * @return bool
*/ */
private function generateSitemapIndex() private function generateSitemapIndex()
{ {
$xmlString = <<<XML $xmlString = <<<XML
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
</sitemapindex> </sitemapindex>
XML; XML;
$xml = new SimpleXMLElement($xmlString); $xml = new SimpleXMLElement($xmlString);
$sql = 'SELECT s.id_shop, su.domain, su.domain_ssl, CONCAT(su.physical_uri, su.virtual_uri) as uri $sql = 'SELECT s.id_shop, su.domain, su.domain_ssl, CONCAT(su.physical_uri, su.virtual_uri) as uri
FROM '._DB_PREFIX_.'shop s FROM '._DB_PREFIX_.'shop s
INNER JOIN '._DB_PREFIX_.'shop_url su ON s.id_shop = su.id_shop AND su.main = 1 INNER JOIN '._DB_PREFIX_.'shop_url su ON s.id_shop = su.id_shop AND su.main = 1
WHERE s.active = 1 WHERE s.active = 1
AND s.deleted = 0 AND s.deleted = 0
AND su.active = 1'; AND su.active = 1';
if (!$result = Db::getInstance()->executeS($sql)) if (!$result = Db::getInstance()->executeS($sql))
return false; return false;
$res = true; $res = true;
foreach ($result as $row) foreach ($result as $row)
{ {
$info = pathinfo(GSITEMAP_FILE); $info = pathinfo(GSITEMAP_FILE);
$filename = $info['filename'].'-'.$row['id_shop'].'.'.$info['extension']; $filename = $info['filename'].'-'.$row['id_shop'].'.'.$info['extension'];
$replaceUrl = array('http://'.$row['domain'].$row['uri'], ((Configuration::get('PS_SSL_ENABLED')) ? 'https://' : 'http://').$row['domain_ssl'].$row['uri']);
$replaceUrl = array('http://'.$row['domain'].$row['uri'], ((Configuration::get('PS_SSL_ENABLED')) ? 'https://' : 'http://').$row['domain_ssl'].$row['uri']);
$last = $this->generateSitemap($row['id_shop'], $info['dirname'].'/'.$filename, $replaceUrl);
if ($last)
$this->_addSitemapIndexNode($xml, 'http://'.$row['domain'].(($row['uri']) ? $row['uri'] : '/').$filename, date('Y-m-d'));
$res &= $last;
}
$last = $this->generateSitemap($row['id_shop'], $info['dirname'].'/'.$filename, $replaceUrl);
if ($last)
$this->_addSitemapIndexNode($xml, 'http://'.$row['domain'].(($row['uri']) ? $row['uri'] : '/').$filename, date('Y-m-d'));
$res &= $last;
}
$fp = fopen(GSITEMAP_FILE, 'w'); $fp = fopen(GSITEMAP_FILE, 'w');
fwrite($fp, $xml->asXML()); fwrite($fp, $xml->asXML());
fclose($fp); fclose($fp);
return $res && file_exists(GSITEMAP_FILE); return $res && file_exists(GSITEMAP_FILE);
} }
/** /**
* Generate a sitemap for a shop * Generate a sitemap for a shop
* *
* @param int $shopID * @param int $shopID
* @param string $filename * @param string $filename
* @return bool * @return bool
*/ */
private function generateSitemap($shopID, $filename = '', $replaceUrl = array()) private function generateSitemap($shopID, $filename = '', $replaceUrl = array())
{ {
$langs = Language::getLanguages(); $langs = Language::getLanguages();
$shop = new Shop($shopID); $shop = new Shop($shopID);
if (!$shop->id) if (!$shop->id)
@@ -191,7 +192,7 @@ XML;
if (!isset($done[$product['id_image']])) if (!isset($done[$product['id_image']]))
{ {
if ($tmp == $product['id_product']) if ($tmp == $product['id_product'])
$res[$tmp]['images'] []= array('id_image' => $product['id_image'], 'legend_image' => $product['legend_image']); $res[$tmp]['images'] []= array('id_image' => $product['id_image'], 'legend_image' => $product['legend_image']);
else else
{ {
$tmp = $product['id_product']; $tmp = $product['id_product'];
@@ -294,18 +295,18 @@ XML;
foreach($pages as $page => $ssl) foreach($pages as $page => $ssl)
$this->_addSitemapNode($xml, $this->context->link->getPageLink($page, $ssl), '0.5', 'monthly'); $this->_addSitemapNode($xml, $this->context->link->getPageLink($page, $ssl), '0.5', 'monthly');
$xmlString = $xml->asXML(); $xmlString = $xml->asXML();
// Replace URL in XML strings by real shops URL // Replace URL in XML strings by real shops URL
if ($replaceUrl) if ($replaceUrl)
$xmlString = str_replace(array(Tools::getShopDomain(true), Tools::getShopDomainSsl(true)), $replaceUrl, $xmlString); $xmlString = str_replace(array(Tools::getShopDomain(true).'/', Tools::getShopDomainSsl(true).'/'), $replaceUrl, $xmlString);
$fp = fopen($filename, 'w'); $fp = fopen($filename, 'w');
fwrite($fp, $xmlString); fwrite($fp, $xmlString);
fclose($fp); fclose($fp);
return file_exists($filename); return file_exists($filename);
} }
private function _addSitemapIndexNode($xml, $loc, $last_mod) private function _addSitemapIndexNode($xml, $loc, $last_mod)
{ {
@@ -339,25 +340,59 @@ XML;
} }
} }
private function _displaySitemap() private function _displaySitemap()
{ {
if (file_exists(GSITEMAP_FILE) && filesize(GSITEMAP_FILE)) if (Shop::isFeatureActive())
{ {
$fp = fopen(GSITEMAP_FILE, 'r'); $sql = 'SELECT s.id_shop, su.domain, su.domain_ssl, CONCAT(su.physical_uri, su.virtual_uri) as uri
$fstat = fstat($fp); FROM '._DB_PREFIX_.'shop s
fclose($fp); INNER JOIN '._DB_PREFIX_.'shop_url su ON s.id_shop = su.id_shop AND su.main = 1
$xml = simplexml_load_file(GSITEMAP_FILE); WHERE s.active = 1
AND s.deleted = 0
AND su.active = 1';
if (!$result = Db::getInstance()->executeS($sql))
return '';
$info = pathinfo(GSITEMAP_FILE);
foreach ($result as $shop)
{
$filename = $info['dirname'].'/'.$info['filename'].'-'.$shop['id_shop'].'.'.$info['extension'];
if (file_exists($filename) && filesize($filename))
{
$fp = fopen($filename, 'r');
$fstat = fstat($fp);
fclose($fp);
$xml = simplexml_load_file($filename);
$nbPages = count($xml->url);
$this->_html .= '<h2>'.$this->l('Sitemap for: ').$shop['domain'].$shop['uri'].'</h2>';
$this->_html .= '<p>'.$this->l('Your Google sitemap file is online at the following address:').'<br />
<a href="'.Tools::getShopDomain(true, true).__PS_BASE_URI__.$info['filename'].'-'.$shop['id_shop'].'.'.$info['extension'].'" target="_blank"><b>'.Tools::getShopDomain(true, true).__PS_BASE_URI__.$info['filename'].'-'.$shop['id_shop'].'.'.$info['extension'].'</b></a></p><br />';
$this->_html .= $this->l('Update:').' <b>'.utf8_encode(strftime('%A %d %B %Y %H:%M:%S',$fstat['mtime'])).'</b><br />';
$this->_html .= $this->l('Filesize:').' <b>'.number_format(($fstat['size']*.000001), 3).'MB</b><br />';
$this->_html .= $this->l('Indexed pages:').' <b>'.$nbPages.'</b><br /><br />';
}
}
}
elseif (file_exists(GSITEMAP_FILE) && filesize(GSITEMAP_FILE))
{
$fp = fopen(GSITEMAP_FILE, 'r');
$fstat = fstat($fp);
fclose($fp);
$xml = simplexml_load_file(GSITEMAP_FILE);
$nbPages = count($xml->url); $nbPages = count($xml->url);
$this->_html .= '<p>'.$this->l('Your Google sitemap file is online at the following address:').'<br /> $this->_html .= '<p>'.$this->l('Your Google sitemap file is online at the following address:').'<br />
<a href="'.Tools::getShopDomain(true, true).__PS_BASE_URI__.'sitemap.xml" target="_blank"><b>'.Tools::getShopDomain(true, true).__PS_BASE_URI__.'sitemap.xml</b></a></p><br />'; <a href="'.Tools::getShopDomain(true, true).__PS_BASE_URI__.'sitemap.xml" target="_blank"><b>'.Tools::getShopDomain(true, true).__PS_BASE_URI__.'sitemap.xml</b></a></p><br />';
$this->_html .= $this->l('Update:').' <b>'.utf8_encode(strftime('%A %d %B %Y %H:%M:%S',$fstat['mtime'])).'</b><br />'; $this->_html .= $this->l('Update:').' <b>'.utf8_encode(strftime('%A %d %B %Y %H:%M:%S',$fstat['mtime'])).'</b><br />';
$this->_html .= $this->l('Filesize:').' <b>'.number_format(($fstat['size']*.000001), 3).'MB</b><br />'; $this->_html .= $this->l('Filesize:').' <b>'.number_format(($fstat['size']*.000001), 3).'MB</b><br />';
$this->_html .= $this->l('Indexed pages:').' <b>'.$nbPages.'</b><br /><br />'; $this->_html .= $this->l('Indexed pages:').' <b>'.$nbPages.'</b><br /><br />';
} }
} }
private function _displayForm() private function _displayForm()
{ {
+1
View File
@@ -8,6 +8,7 @@ $_MODULE['<{gsitemap}prestashop>gsitemap_5bbdad908e423585a7ecc6b61b66b313'] = 'I
$_MODULE['<{gsitemap}prestashop>gsitemap_09fa1e865bdcf4d3215d5b0005b93154'] = 'le fichier sitemap.xml.'; $_MODULE['<{gsitemap}prestashop>gsitemap_09fa1e865bdcf4d3215d5b0005b93154'] = 'le fichier sitemap.xml.';
$_MODULE['<{gsitemap}prestashop>gsitemap_5a517cec50ac8e742f6583c6c404c4e9'] = 'Fichier Sitemap généré'; $_MODULE['<{gsitemap}prestashop>gsitemap_5a517cec50ac8e742f6583c6c404c4e9'] = 'Fichier Sitemap généré';
$_MODULE['<{gsitemap}prestashop>gsitemap_0a6ee1a4f10278211d74152e633d0ed0'] = 'Erreur durant la création du fichier sitemap'; $_MODULE['<{gsitemap}prestashop>gsitemap_0a6ee1a4f10278211d74152e633d0ed0'] = 'Erreur durant la création du fichier sitemap';
$_MODULE['<{gsitemap}prestashop>gsitemap_4851323bb81fbc4aa65c1b37af8a1d1d'] = 'Sitemap pour : ';
$_MODULE['<{gsitemap}prestashop>gsitemap_eb175747116b689caadea1b84202f513'] = 'Votre fichier Google sitemap est en ligne à l\'adresse suivante :'; $_MODULE['<{gsitemap}prestashop>gsitemap_eb175747116b689caadea1b84202f513'] = 'Votre fichier Google sitemap est en ligne à l\'adresse suivante :';
$_MODULE['<{gsitemap}prestashop>gsitemap_39d616ecc73e174052f1877ac2b3c853'] = 'Mise à jour'; $_MODULE['<{gsitemap}prestashop>gsitemap_39d616ecc73e174052f1877ac2b3c853'] = 'Mise à jour';
$_MODULE['<{gsitemap}prestashop>gsitemap_b908c2f34052b5276e0bf50f0e042211'] = 'Taille du fichier'; $_MODULE['<{gsitemap}prestashop>gsitemap_b908c2f34052b5276e0bf50f0e042211'] = 'Taille du fichier';