









Le sitemap est un systeme mis en place par google pour aider les werbmaster a mieu 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 page. grace à sa, le moteur arrive mieu a lister les page du site et donc a référencer.
Cette classe va parcourir un répertoire et généré le fichier XML de sitemap en fonction des pages trouvée
Pour générer un sitemap a partir d'élément de votre base de donnée, ou ligne à ligne, regardez ici : sitemap_manuel
<?php if (version_compare(PHP_VERSION,'5','>=')) require_once('domxml_php4_to_php5.php'); //Charge le convertisseur si PHP5 class Sitemap { /** * Contruction fucntion * * @param string $dirName * * @return void */ function Sitemap($dirName,$rootName,$FilterExt,$replacer,$removExt) { $this->_dirArray = array() ; $this->_fileArray = array() ; $this->_dirName = '/'; $this->_nameArray = array(); $this->_flag = true; $this->_nodeFlag = true; $this->_dom = ''; $this->_root = ''; $this->_fileNode = ''; $this->_dirNode = ''; $this->_childDirNode = ''; if (trim($dirName) != '') { $this->_dirName = trim($dirName); $this->_rootName = trim($rootName); $this->_filterExt=$FilterExt; $this->_ReplaceSlash=$replacer; $this->_RemovExt=$removExt; } } /** * Function to convert time in secs into mins * * @param $timeInSec int * * $return string */ function convertSecToMins($timeInSec) { if( $timeInSec < 60 ) { if( $timeInSec > 1) { $secString = "secs"; } else { $secString = "sec"; } $timeTaken = "$timeInSec $secString"; } else { $seconds = ($timeInSec % 60); $minutes = ($timeInSec/60); $minutes = sprintf("%01.0f", $minutes); if( $minutes > 1) { $minString = "mins"; } else { $minString = "min"; } if( $seconds > 1) { $secString = "secs"; } else { $secString = "sec"; } $timeTaken = "$minutes $minString $seconds $secString"; } return trim($timeTaken); } /** * * Function to read a directory recursively and finds a string in all the files. * it also returns the number of times a sting was found in a file * * @param $path string * * @return mixed */ function readDirectory($path = '') { if (trim($path) == '') { $path = $this->_dirName; } if (substr($path,-1)!='/') $path.='/'; $returnArray = array(); $handle = @opendir($path); while ($file = @readdir($handle) ) { if (is_dir($path.$file) && $file != "." && $file != "..") { $sub_dir = $path . $file . "/" ; array_push($this->_dirArray, $sub_dir); $this->readDirectory($sub_dir); } elseif (is_file($path.$file) && $file != "." && $file != "..") { foreach ($this->_filterExt as $ext) { $no = true; if (strtolower(substr($file,- strlen($ext)))==strtolower($ext)) { $no=false; break; } } if ($no == false) { $sub_dir = $path . $file ; array_push($this->_fileArray, $sub_dir); } } } $returnArray = array($this->_dirArray, $this->_fileArray ); return $returnArray; } /** * This function generates the sitemap * * @return mixed */ function generateXmlSiteMap($writeToFile = false) { $mainArray = $this->readDirectory(''); $fileArray = $mainArray[1]; $this->startDomXml('urlset'); for ($i=0; $i < count($fileArray); $i++) { $fileName = trim(substr(strrchr($fileArray[$i], '/'), 1)); $dirName = trim(str_replace($fileName, '', $fileArray[$i])); $this->populateArray($dirName); } $foldersInRootDir = $this->getFoldersInRootDir(); foreach ($this->_nameArray as $name=>$key) { $this->generateNodes($name, $key , $foldersInRootDir); } if ($writeToFile) { $fp = fopen('xmlOutput.xml', 'w+') or die("<B>ERROR: </B> Failed to open file for writing."); //php5 fwrite($fp, $this->_dom->saveXML()) or die("<B>ERROR: </B> Failed to write contents to the file."); fwrite($fp, $this->_dom->dump_mem(true)) or die("<B>ERROR: </B> Failed to write contents to the file."); fclose($fp); echo "Content written to file: <a href='xmlOutput.xml'> Click to open.</a>"; } else { //php 5 return $this->_dom->saveXML(); return $this->_dom->dump_mem(true); } } /** * This function generates the nodes * * @param string $name * @param array $foldersInRootDir * @param array $key * * @return void */ function generateNodes($name, $key, $foldersInRootDir) { $childDirArray = $this->getChildDir($name); /* echo "<pre>"; echo $name."<br>"; print_r($foldersInRootDir); print_r($this->_dirArray); print_r($childDirArray); echo "<HR>"; */ if (in_array($name, $foldersInRootDir)) { for ($i=0; $i < count($key); $i++) { $this->createDirNode('url', ''); $f=str_replace('/',$this->_ReplaceSlash,str_replace($this->_dirName,'',$name)).$key[$i]; if ($this->_RemovExt==true) { foreach ($this->_filterExt as $ext) { if (strtolower(substr($f,- strlen($ext)))==strtolower($ext)) { $f=substr($f,0,strlen($f)-strlen($ext)); break; } } } $this->createFileNode('loc', $this->_rootName.$f); if (@filemtime($name.$key[$i]) != false) { //php5 $f=date ("c", filemtime($name.$key[$i])); $f=date ("Y-m-d¤H:i:sO", filemtime($name.$key[$i])); $f=str_replace('¤','T',$f); $f=substr($f,0,strlen($f)-2).':'.substr($f,-2); $this->createFileNode('lastmod', $f); } } if (count($childDirArray)) { for ($i=0; $i < count($childDirArray); $i++) { //$this->createDirNode('dirname', $childDirArray[$i], true); //$this->createDirNode('url', $childDirArray[$i], true); $this->generateNodes($childDirArray[$i], $key, $this->_dirArray); } } } } /** * 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); } } } /** * This function returns the names of the folder inside the parent dir * * @param string $parentDir * @access private * * @return array */ function getChildDir($parentDir) { $returnArray = array(); foreach ($this->_dirArray as $tmp) { if (substr_count($tmp, $parentDir)) { $dirToBeAdded = substr($tmp, (strpos($tmp, $parentDir)+strlen($parentDir)) ) ; if ( trim($dirToBeAdded) != '' && substr_count($dirToBeAdded, '/') == 1) { $returnArray[] = $parentDir.$dirToBeAdded; } } } return $returnArray; } /** * This function returns the name(s) of all folders in the root/parent dir * * @param string $rootdir * * @return array */ function getFoldersInRootDir() { $returnArray = array(); foreach ($this->_dirArray as $dir) { $tmpArray = explode('/', $dir); if (isset($tmpArray[1])) { if (!in_array($this->_dirName.$tmpArray[count($tmpArray)-2]."/", $returnArray)) { array_push($returnArray, $this->_dirName.$tmpArray[count($tmpArray)-2]."/"); } } } return $returnArray; } /** * This function populated the array for dir/file * * @param array $dirName * @return void */ function populateArray($dirName) { $fileArray = $this->_fileArray; for ($i=0; $i < count($fileArray); $i++) { $dirPath = substr($fileArray[$i], 0, strrpos($fileArray[$i], '/')).'/'; if (trim($dirName) == trim($dirPath)) { $tempArray[] = trim(str_replace($dirPath, '', $fileArray[$i])); } } $this->_nameArray[$dirName] = $tempArray; } }
Tous d'abord, il faut créer une instance de la classe
require_once('sitemap.inc.php'); $sitemap = new Sitemap($DIR_PATH,$root,$filter,':',true);
Vous remarquer que lors de la création de l'instance, on passe certain parametre.
Voici la liste :
Ensuite il faut appeler la fonction generateXmlSiteMap qui va construire le XML.
Cette fonction accepte 1 parametre. si il est a true, la fonction va creer phisiquement “outpout.xml” si il est a false, elle renvera en retour le XML.
$xmlContent = $sitemap->generateXmlSiteMap(false);
Cet exemple permet de généré le sitemap du wiki :
require_once('sitemap.inc.php'); //chemin a parcourir //Chemin web //Extention ke lon accepte //Caractere de remplacement des slash (si aucun, mettre '/') //Si true enleve les extention de fichier, sinon les garde $root='http://www.woow-fr.com/wikistuce/doku.php?id='; $repertoire=dirname(__file__).'/'; $sitemap = new Sitemap($repertoire,$root, array('.htm'), '/', false); $xmlContent = $sitemap->generateXmlSiteMap(false); header('Content-Type:text/xml'); echo $xmlContent;
require_once('sitemap.inc.php'); define ('DIR_PATH', dirname(__file__).'/data/pages/'); $root="http://www.woow-fr.com/wikistuce/doku.php?id="; $filter[0]='.txt'; //chemin a parcourir //Chemin web //Extention ke lon accepte //Caractere de remplacement des slash (si aucun, mettre '/') //Si true enleve les extention de fichier, sinon les garde $sitemap = new Sitemap(DIR_PATH,$root,$filter,':',true); $xmlContent = $sitemap->generateXmlSiteMap(false); header('Content-Type:text/xml'); echo $xmlContent;
Discussion