<?PHP
/**
* XMap - A PHP class to use XML Sitemaps
*
* XMap is a PHP Class and a set of tools to automatize html pages creation.
* It use a XML file where you can place all <head> of your html pages like
* title, description, keywords. In addition, XMap can generated the Navigator
* Bar on your site, can tell to your page what html file to load as menu
* and so on.
*
* XMap main class is only a wrapper to another XPath class - PHPXML, downloadable
* at http://www.phpclasses.org/browse.html/package/180.html - that use
* some Xpath paths (called by XMap as Rules) and return a array with the map infos.
*
* The format of XML is
* <sitemap>
* <map id="id1">
* <element1>...</element1>
* <element2>...</element2>
* ...
* <map id="id2">
* <element1>...</element1>
* <element2>...</element2>
* ...
* </map>
* </map>
* </sitemap>
*
* Where id and element need be changed and you can expand the tree whenever you need.
*
* @author Roberto Bertó, darkelder (inside) users (dot) sourceforge (dot) net
* @version 0.2
* @copyright {@link http://www.gnu.org/copyleft/lesser.html LGPL}
* @package XMap
*/
class XMap
{
/**
* Path to <phpXML/>
*
* Tested with phpXML 1.0
*
* @access public
* @var string Absolute path to xml.php of <phpXML/>
*/
var $phpXMLFile = 'xml.php';
/**
* XMLsitemap elements
*
* Caution: do not push map inside the array
*
* @access public
* @var array Each element of array is an XMLsitemap element.
*/
var $elements = array(
"url",
"title",
"description",
"keywords",
"menu",
"name",
);
/** File to process
*
* This is the xmap.xml file to process. You can set directly by XMap($file),
* instead of setting by the variable.
*
* @access public
* @var string Filename to XML
*/
var $XMLMapFie = '';
/** Debug
*
* Set to TRUE if you want to debug
*
* @access public
* @var bool TRUE = debug
*/
var $debug = FALSE;
/**
* phpXML object
*
* @access private
* @var object
*/
var $phpXML;
/**
* XPATH rules - descendant
*
* You can add another XPATH rules. In the place of %id% we have $id.
*
* @access public
* @var array each key par is a rule
*/
var $xpathRules = array(
"descendant" => "//map[@id=\"%id%\"]/descendant::map",
"descendant-or-self" => "//map[@id=\"%id%\"]/descendant-or-self::map",
"ancestor" => "//map[@id=\"%id%\"]/ancestor::map",
"ancestor-or-self" => "//map[@id=\"%id%\"]/ancestor-or-self::map",
"here" => "//map[@id=\"%id%\"]",
"all" => "//map",
);
/**
* Constructor
*
* If you set $XMLMapFile so, the function will process it at runtime.
*
* @access public
* @param string $XMLMapFile optional fill in with path to XML file to process.
* @param string $phpXMLFile optional that override $this->phpXMLFile;
*/
function XMap ($XMLMapFile = NULL,$phpXMLFile = NULL)
{
if (!empty($phpXMLFile))
{
$this->phpXMLFile = $phpXMLFile;
}
require_once($this->phpXMLFile);
if (!empty($XMLMapFile))
{
$this->XMLMapFile = $XMLMapFile;
$this->Process($this->XMLMapFile);
}
}
/**
* Process a file and open phpXML object
*
* @param string $XMLMapFile optional if you already set $XMap->XMLMapFile
* @access public
*/
function Process ($XMLMapFile = NULL)
{
if (!empty($XMLMapFile))
{
$this->XMLMapFile = $XMLMapFile;
}
$this->phpXML = new XML($this->XMLMapFile);
}
/**
* Get elements
*
* Use print_r or $XMap->debug = TRUE to understend the way this function return the array.
*
* @access public
* @param string $rule The $XMap->xpathRules choice
* @param string $id a id attribute inside a <map> element of xmlfile
* @param bool $invert invert the array before return it
* @return array array(key => array("element1" => , "element2" =>))
*/
function Get ($rule, $id = "", $invert = FALSE)
{
if (!array_key_exists($rule,$this->xpathRules))
{
return FALSE;
}
if ($this->debug == TRUE) print "rule=$rule\n.id=$id\n";
$path = preg_replace(
array("/%id%/", ),
array(addslashes($id), ),
$this->xpathRules["$rule"]
);
if ($this->debug == TRUE) print "..path=$path\n";
$maps = $this->phpXML->evaluate($path);
// $sitemap will be returned
$sitemap = array();
foreach ($maps as $map)
{
if ($this->debug == TRUE) print "...map = $map\n";
$attributes = $this->phpXML->get_attributes($map);
$id = $attributes["id"];
if ($this->debug == TRUE) print "....id = $id\n";
foreach ($this->elements as $element)
{
$content = $this->phpXML->get_content($map."/" . $element . "[1]");
if ($this->debug == TRUE) print ".....$element=$content\n";
$sitemap["$id"]["$element"] = $content;
}
}
if ($invert == TRUE)
{
$sitemap = array_reverse($sitemap,TRUE);
}
if ($this->debug == TRUE) print_r($sitemap);
return $sitemap;
}
}
?>