// Fixed #PSCFI-5331

This commit is contained in:
bMancone
2012-04-10 14:14:41 +00:00
parent b88bacedcc
commit 972d7fbe0d
+69 -59
View File
@@ -112,13 +112,13 @@ XML;
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); $last = $this->generateSitemap($row['id_shop'], $info['dirname'].'/'.$filename, $replaceUrl);
@@ -126,7 +126,7 @@ XML;
$this->_addSitemapIndexNode($xml, 'http://'.$row['domain'].(($row['uri']) ? $row['uri'] : '/').$filename, date('Y-m-d')); $this->_addSitemapIndexNode($xml, 'http://'.$row['domain'].(($row['uri']) ? $row['uri'] : '/').$filename, date('Y-m-d'));
$res &= $last; $res &= $last;
} }
$fp = fopen(GSITEMAP_FILE, 'w'); $fp = fopen(GSITEMAP_FILE, 'w');
fwrite($fp, $xml->asXML()); fwrite($fp, $xml->asXML());
fclose($fp); fclose($fp);
@@ -182,36 +182,42 @@ XML;
AND ps.id_shop = '.$shopID.' AND ps.id_shop = '.$shopID.'
'.(Configuration::get('GSITEMAP_ALL_PRODUCTS') ? '' : 'HAVING level_depth IS NOT NULL').' '.(Configuration::get('GSITEMAP_ALL_PRODUCTS') ? '' : 'HAVING level_depth IS NOT NULL').'
ORDER BY pl.id_product, pl.id_lang ASC'; ORDER BY pl.id_product, pl.id_lang ASC';
$products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
$resource = Db::getInstance(_PS_USE_SQL_SLAVE_)->query($sql);
$tmp = null; // array used to know which product/image was already added (blacklist)
$res = null;
$done = null; $done = null;
foreach ($products as $product) $sitemap = null;
// iterates on the products, to gather the image ids
while ($product = Db::getInstance()->nextRow($resource))
{ {
if (!isset($done[$product['id_image']])) // if the product has not been added
$id_product = $product['id_product'];
if (!isset($done[$id_product]['added']))
{ {
if ($tmp == $product['id_product']) // priority
$res[$tmp]['images'] []= array('id_image' => $product['id_image'], 'legend_image' => $product['legend_image']); if (($priority = 0.7 - ($product['level_depth'] / 10)) < 0.1)
else $priority = 0.1;
{
$tmp = $product['id_product']; // adds the product
$res[$tmp] = $product; $tmpLink = $this->context->link->getProductLink((int)($product['id_product']), $product['link_rewrite'], $product['category'], $product['ean13'], (int)($product['id_lang']));
unset($res[$tmp]['id_image'], $res[$tmp]['legend_image']); $sitemap = $this->_addSitemapNode($xml, $tmpLink, $priority, 'weekly', substr($product['date_upd'], 0, 10));
$res[$tmp]['images'] []= array('id_image' => $product['id_image'], 'legend_image' => $product['legend_image']);
} // considers the product has added
$done[$product['id_image']] = true; $done[$id_product]['added'] = true;
} }
}
foreach ($res as $product) // if the image has not been added
{ $id_image = $product['id_image'];
if (($priority = 0.7 - ($product['level_depth'] / 10)) < 0.1) if (!isset($done[$id_product][$id_image]) && $id_image)
$priority = 0.1; {
// adds the image
$this->_addSitemapNodeImage($sitemap, $product);
$tmpLink = $this->context->link->getProductLink((int)($product['id_product']), $product['link_rewrite'], $product['category'], $product['ean13'], (int)($product['id_lang'])); // considers the image as added
$sitemap = $this->_addSitemapNode($xml, $tmpLink, $priority, 'weekly', substr($product['date_upd'], 0, 10)); $done[$id_product][$id_image] = true;
$sitemap = $this->_addSitemapNodeImage($sitemap, $product); }
} }
/* Categories Generator */ /* Categories Generator */
@@ -329,15 +335,12 @@ XML;
private function _addSitemapNodeImage($xml, $product) private function _addSitemapNodeImage($xml, $product)
{ {
foreach ($product['images'] as $img) $image = $xml->addChild('image', null, 'http://www.google.com/schemas/sitemap-image/1.1');
{ $image->addChild('loc', htmlspecialchars($this->context->link->getImageLink($product['link_rewrite'], (int)$product['id_product'].'-'.(int)$product['id_image'])), 'http://www.google.com/schemas/sitemap-image/1.1');
$image = $xml->addChild('image', null, 'http://www.google.com/schemas/sitemap-image/1.1');
$image->addChild('loc', htmlspecialchars($this->context->link->getImageLink($product['link_rewrite'], (int)$product['id_product'].'-'.(int)$img['id_image'])), 'http://www.google.com/schemas/sitemap-image/1.1');
$legend_image = preg_replace('/(&+)/i', '&amp;', $img['legend_image']); $legend_image = preg_replace('/(&+)/i', '&amp;', $product['legend_image']);
$image->addChild('caption', $legend_image, 'http://www.google.com/schemas/sitemap-image/1.1'); $image->addChild('caption', $legend_image, 'http://www.google.com/schemas/sitemap-image/1.1');
$image->addChild('title', $legend_image, 'http://www.google.com/schemas/sitemap-image/1.1'); $image->addChild('title', $legend_image, 'http://www.google.com/schemas/sitemap-image/1.1');
}
} }
private function _displaySitemap() private function _displaySitemap()
@@ -352,11 +355,11 @@ XML;
AND su.active = 1'; AND su.active = 1';
if (!$result = Db::getInstance()->executeS($sql)) if (!$result = Db::getInstance()->executeS($sql))
return ''; return '';
$this->_html .= '<h2>'.$this->l('Sitemap index').'</h2>'; $this->_html .= '<h2>'.$this->l('Sitemap index').'</h2>';
$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 />';
$info = pathinfo(GSITEMAP_FILE); $info = pathinfo(GSITEMAP_FILE);
foreach ($result as $shop) foreach ($result as $shop)
{ {
@@ -367,14 +370,14 @@ XML;
$fstat = fstat($fp); $fstat = fstat($fp);
fclose($fp); fclose($fp);
$xml = simplexml_load_file($filename); $xml = simplexml_load_file($filename);
$nbPages = count($xml->url); $nbPages = count($xml->url);
$sitemap_uri = 'http://'.$shop['domain'].$shop['uri'].$info['filename'].'-'.$shop['id_shop'].'.'.$info['extension']; $sitemap_uri = 'http://'.$shop['domain'].$shop['uri'].$info['filename'].'-'.$shop['id_shop'].'.'.$info['extension'];
$this->_html .= '<h2>'.$this->l('Sitemap for: ').$shop['domain'].$shop['uri'].'</h2>'; $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 /> $this->_html .= '<p>'.$this->l('Your Google sitemap file is online at the following address:').'<br />
<a href="'.$sitemap_uri.'" target="_blank"><b>'.$sitemap_uri.'</b></a></p><br />'; <a href="'.$sitemap_uri.'" target="_blank"><b>'.$sitemap_uri.'</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 />';
@@ -400,34 +403,31 @@ XML;
} }
private function _displayForm() private function _displayForm()
{ {
if (Tools::usingSecureMode()) if (Tools::usingSecureMode())
$domain = Tools::getShopDomainSsl(true); $domain = Tools::getShopDomainSsl(true);
else else
$domain = Tools::getShopDomain(true); $domain = Tools::getShopDomain(true);
$this->_html .= $this->_html .= '
'<form action="'.Tools::htmlentitiesUTF8($_SERVER['REQUEST_URI']).'" method="post"> <form action="'.Tools::htmlentitiesUTF8($_SERVER['REQUEST_URI']).'" method="post">
<div style="margin:0 0 20px 0;"> <div style="margin:0 0 20px 0;">
<input type="checkbox" name="GSITEMAP_ALL_PRODUCTS" id="GSITEMAP_ALL_PRODUCTS" style="vertical-align: middle;" value="1" '.(Configuration::get('GSITEMAP_ALL_PRODUCTS') ? 'checked="checked"' : '').' /> <label class="t" for="GSITEMAP_ALL_PRODUCTS">'.$this->l('Sitemap also includes products from inactive categories').'</label> <input type="checkbox" name="GSITEMAP_ALL_PRODUCTS" id="GSITEMAP_ALL_PRODUCTS" style="vertical-align: middle;" value="1" '.(Configuration::get('GSITEMAP_ALL_PRODUCTS') ? 'checked="checked"' : '').' /> <label class="t" for="GSITEMAP_ALL_PRODUCTS">'.$this->l('Sitemap also includes products from inactive categories').'</label>
</div> </div>
<div style="margin:0 0 20px 0;"> <div style="margin:0 0 20px 0;">
<input type="checkbox" name="GSITEMAP_ALL_CMS" id="GSITEMAP_ALL_CMS" style="vertical-align: middle;" value="1" '.(Configuration::get('GSITEMAP_ALL_CMS') ? 'checked="checked"' : '').' /> <label class="t" for="GSITEMAP_ALL_CMS">'.$this->l('Sitemap also includes CMS pages which are not in a CMS block').'</label> <input type="checkbox" name="GSITEMAP_ALL_CMS" id="GSITEMAP_ALL_CMS" style="vertical-align: middle;" value="1" '.(Configuration::get('GSITEMAP_ALL_CMS') ? 'checked="checked"' : '').' /> <label class="t" for="GSITEMAP_ALL_CMS">'.$this->l('Sitemap also includes CMS pages which are not in a CMS block').'</label>
</div> </div>
<input name="btnSubmit" class="button" type="submit" <input name="btnSubmit" class="button" type="submit"
value="'.((!file_exists(GSITEMAP_FILE)) ? $this->l('Generate sitemap file') : $this->l('Update sitemap file')).'" /> value="'.((!file_exists(GSITEMAP_FILE)) ? $this->l('Generate sitemap file') : $this->l('Update sitemap file')).'" />
</form><br /> </form><br />
<h2>'.$this->l('Use cron job to re-build the sitemap:').'</h2> <h2>'.$this->l('Use cron job to re-build the sitemap:').'</h2>
<p> <p>
<b>'.$domain.__PS_BASE_URI__.'modules/gsitemap/gsitemap-cron?&token='.substr(Tools::encrypt('gsitemap/cron'),0,10).'&GSITEMAP_ALL_CMS='.((int)Configuration::get('GSITEMAP_ALL_CMS')).'&GSITEMAP_ALL_PRODUCTS='.((int)Configuration::get('GSITEMAP_ALL_PRODUCTS')).'</b> <b>'.$domain.__PS_BASE_URI__.'modules/gsitemap/gsitemap-cron.php?&token='.substr(Tools::encrypt('gsitemap/cron'),0,10).'&GSITEMAP_ALL_CMS='.((int)Configuration::get('GSITEMAP_ALL_CMS')).'&GSITEMAP_ALL_PRODUCTS='.((int)Configuration::get('GSITEMAP_ALL_PRODUCTS')).'</b>
</p>'; </p>';
} }
public function getContent() public function getContent()
{ {
$this->_html .= '<h2>'.$this->l('Search Engine Optimization').'</h2>
'.$this->l('See').' <a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=156184&from=40318&rd=1" style="font-weight:bold;text-decoration:underline;" target="_blank">
'.$this->l('this page').'</a> '.$this->l('for more information').'<br /><br />';
if (Tools::isSubmit('btnSubmit')) if (Tools::isSubmit('btnSubmit'))
{ {
$this->_postValidation(); $this->_postValidation();
@@ -438,10 +438,20 @@ XML;
$this->_html .= '<div class="alert error">'.$err.'</div>'; $this->_html .= '<div class="alert error">'.$err.'</div>';
} }
$this->_html .= '
<fieldset>
<legend>'.$this->l('Search Engine Optimization').'</legend>
<br />
'.$this->l('See').' <a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=156184&from=40318&rd=1" style="font-weight:bold;text-decoration:underline;" target="_blank">
'.$this->l('this page').'</a> '.$this->l('for more information').'
<br />';
$this->_displaySitemap(); $this->_displaySitemap();
$this->_displayForm(); $this->_displayForm();
$this->_html .= '</fieldset>';
return $this->_html; return $this->_html;
} }
} }