Le sitemap est un systeme mis en place par google pour aider les webmaster a mieux référencer leur site.
Il s'agit d'un fichier XML qui contient chaque page du site ansi que la date de modification des pages. grâce à ça, le moteur arrive mieus à lister les pages du site et donc a référencer.
Cette classe va générer un sitemap en fonction de paramètre que vous lui passerez pour chaque balise.
Pour générer un sitemap a partir de la liste des fichier d'un répertoire regardez ici : sitemap_listing_repertoire
<?php class Sitemap { /** * Contruction function * * @param string $dirName * * @return void */ function Sitemap() { $this->startDomXml('urlset'); } /** * This fucntion creates the instance of DOM Xml object * * @return void */ function startDomXml($rootNodeName) { //php5 $this->_dom = new DOMDocument('1.0'); $this->_dom = domxml_new_doc('1.0'); $this->_dom->formatOutput = true; $this->_root = $this->_dom->create_Element($rootNodeName); $t = $this->_dom->create_Attribute("xmlns","http://www.google.com/schemas/sitemap/0.84"); $this->_root->append_Child($t); $t = $this->_dom->create_Attribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance"); $this->_root->append_Child($t); $t = $this->_dom->create_Attribute("xsi:schemaLocation","http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd"); $this->_root->append_Child($t); //php5 $t = $this->_dom->createAttribute($param); //php5 $t->value=$value; //php5 $this->_root->append_Child($t); $this->_root = $this->_dom->append_Child($this->_root); } /** * This function creates a dir node in the xml file * * @param string $dirNode * @return void */ function createDirNode($dirNode, $value, $isChildNode = false, $appendToChildNode = false) { if (!is_object($this->_dom)) { die ("<B>ERROR:</B> Cannot call fucntion: ".__FUNCTION__." without creating dom xml object"); } else { if ($appendToChildNode) { $node = $this->_childDirNode; } else { $node = $this->_dirNode; } if ($isChildNode) { //php5 $this->_childDirNode = $this->_dom->create_element($dirNode, $value); $this->_childDirNode = $this->_dom->create_element($dirNode); $this->_childDirNode = $node->append_Child($this->_childDirNode); } else { //php5 $this->_dirNode = $this->_dom->create_element($dirNode, $value); $this->_dirNode = $this->_dom->create_element($dirNode); $this->_dirNode = $this->_root->append_Child($this->_dirNode); } } } /** * This function creates a file node in the xml file * * @param string $fileNode * @return void */ function createFileNode($fileNode, $value, $appendToChildNode = false) { if (!is_object($this->_dom)) { die ("<B>ERROR:</B> Cannot call fucntion: ".__FUNCTION__." without creating dom xml object"); } else { if ($appendToChildNode) { //php5 $this->_fileNode = $this->_dom->create_element($fileNode, $value); $this->_fileNode = $this->_dom->create_element($fileNode); $t = $this->_dom->create_text_node($value); $this->_fileNode->append_Child($t); $this->_fileNode = $this->_childDirNode->append_Child($this->_fileNode); } else { //php5 $this->_fileNode = $this->_dom->create_element($fileNode, $value); $this->_fileNode = $this->_dom->create_element($fileNode); $t = $this->_dom->create_text_node($value); $this->_fileNode->append_Child($t); $this->_fileNode = $this->_dirNode->append_Child($this->_fileNode); } } } function addNode($name, $dte='') { $this->createDirNode('url', ''); $this->createFileNode('loc', $name); if ($dte=='') { $dte=date("Y-m-d¤H:i:sO"); $dte=str_replace('¤','T',$dte); $dte=substr($dte,0,strlen($dte)-2).':'.substr($dte,-2); } $this->createFileNode('lastmod', $dte); } function GetMap() { return $this->_dom->dump_mem(true); } } ?>
Voici comment l'utiliser :
require_once('sitemap.inc.php'); $sitemap = new Sitemap(); for ($i=0;$i!=10;$i++) { $dte=date("Y-m-d¤H:i:sO"); $dte=str_replace('¤','T',$dte); $dte=substr($dte,0,strlen($dte)-2).':'.substr($dte,-2); $sitemap->addNode('http://monsite/'.$i.'.htm',$dte); } $xmlContent = $sitemap->GetMap(); header('Content-Type:text/xml'); echo $xmlContent;
— iDo 02/12/2005 16:02
Discussion