Voici une classe permettant de générer un Google Sitemap en PHP5. Vous devez activer l'extension DOM pour pouvoir l'utiliser.
| Sitemap.php | <?php
class Sitemap extends DOMDocument
{
protected $root;
protected $urls;
const FREQ_ALWAYS = 'always';
const FREQ_HOURLY = 'hourly';
const FREQ_DAILY = 'daily';
const FREQ_WEEKLY = 'weekly';
const FREQ_MONTHLY = 'monthly';
const FREQ_YEARLY = 'yearly';
const FREQ_NEVER = 'never';
public function __construct()
{
parent::__construct('1.0', 'utf-8');
$this->root = $this->createElement('urlset');
$this->root->setAttribute('xmlns', 'http://www.google.com/schemas/sitemap/0.84');
$this->appendChild($this->root);
}
public function createUrl($loc, $lastMod = NULL, $changeFreq = NULL, $priority = NULL)
{
if(!is_string($loc))
{
throw new Exception("L'URL devrait être une chaîne");
}
$DOMUrl = $this->createElement('url');
$this->root->appendChild($DOMUrl);
++$this->urls;
if($this->urls >= 50000)
{
throw new Exception("Un Sitemap ne peut contenir plus de 50.000 URLs");
}
$DOMLoc = $this->createElement('loc', $loc);
$DOMUrl->appendChild($DOMLoc);
if($lastMod)
{
if(!is_int($lastMod))
{
throw new Exception("Date au format incorrect");
}
$DOMLastMod = $this->createElement('lastmod', date('Y-m-d', $lastMod));
$DOMUrl->appendChild($DOMLastMod);
}
if($changeFreq)
{
if(!in_array($changeFreq,
array
(
self::FREQ_ALWAYS, self::FREQ_DAILY, self::FREQ_HOURLY,
self::FREQ_MONTHLY, self::FREQ_NEVER, self::FREQ_WEEKLY,
self::FREQ_YEARLY
)))
{
throw new Exception("Fréquence incorrecte");
}
$DOMChangeFreq = $this->createElement('changefreq', $changeFreq);
$DOMUrl->appendChild($DOMChangeFreq);
}
if($priority)
{
if(!is_float($priority))
{
throw new Exception("Priorité devrait être un entier");
}
if($priority < 0.1)
{
throw new Exception("Priorité ne devrait pas être négatif");
}
if($priority > 1)
{
throw new Exception("Priorité ne devrait pas être supérieur à 1");
}
$DOMLastMod = $this->createElement('priority', $priority);
$DOMUrl->appendChild($DOMLastMod);
}
}
public function createElement($element, $contents = NULL)
{
return parent::createElement($element, utf8_encode($contents));
}
public function setAttribute($attribute, $contents = NULL)
{
return parent::setAttribute($attribute, utf8_encode($contents));
}
public static function getTime($date)
{
list($day, $month, $year) = split('[./-]', $date);
return mktime(0, 0, 0, $month, $day, $year);
}
}
?>
|
La méthode getTime() prend un paramètre chaîne et le convertit en timestamp. Elle accepte les formats :
- JJ-MM-AAAA
- JJ/MM/AAAA
- JJ.MM.AAAA
| Exemple d'utilisation de la classe Sitemap : | <?php
require 'Sitemap.php';
$sitemap = new Sitemap();
$sitemap->createUrl(
'http://g-rossolini.developpez.com/',
Sitemap::getTime('16/08/2007'),
Sitemap::FREQ_MONTHLY,
0.8
);
$sitemap->createUrl(
'http://g-rossolini.developpez.com/tutoriels/php/zend-framework/debuter/',
Sitemap::getTime('05/06/2007'),
Sitemap::FREQ_YEARLY
);
$sitemap->createUrl(
'http://g-rossolini.developpez.com/tutoriels/php/les-formulaires-et-php5/',
Sitemap::getTime('11/11/2006'),
Sitemap::FREQ_YEARLY
);
$sitemap->createUrl(
'http://g-rossolini.developpez.com/comparatifs/php/templates/',
Sitemap::getTime('19/03/2007'),
Sitemap::FREQ_YEARLY
);
header('Content-Type: text/xml; charset=utf-8');
echo $sitemap->saveXML();
?>
|
|