Hi how are you? This is a quick whitepaper on sitemap.php which comes with XOOPS 2.6.0, you can adapt this to earlier version of XOOPS if you wish and it will scale with the XOOPS Class Libraries.
First up lets examine /kernel/modules.php which contains the main 2 functions and one Class Object
XoopsSitemap for generating a sitemap.
Firstly this function appears in the class declaration of
XoopsModule which handles each individuals return of the array of
XoopsSitemap objects to the backend provider.
This function
XoopsModule::getSitemap() looks like so:
/**
* A Module
*
* @package kernel
* @author Kazumi Ono
*/
class XoopsModule extends XoopsObject
{
//...Other functions...
/**
* Sitemap contents within a module
*
* @param integer $limit
* @return mixed Sitemap result.
*/
public function getSitemap($limit = 4000)
{
if (!empty($this->getInfo('sitemap'))&&is_array($sitemap = $this->getInfo('sitemap'))) {
$xoops = xoops::getInstance();
if (file_exists($file = $xoops->path('modules/' . $this->getVar('dirname') . '/' . $sitemap['file']))) {
include_once $file;
} else {
return false;
}
if (function_exists($sitemap['func'])) {
$func = (string)$sitemap['func'];
return $func($limit);
}
}
return false;
}
}
This routine both straps the file listed in
xoops_version.php $modinfo['sitemap'] array. It has two variables like search
$modinfo['sitemap']['file'] &
$modinfo['sitemap']['func']. One element of this array supports the file that returns the modules sitemap array of
XoopsSitemap Objects the otherone lists the function to call, it only has one arguement which is the limit of URL for the module to return.
Next is the
XoopsModuleHandler which has a function in it called by /sitemap.php in the root of the XOOPS Installation which straps the modules which are active for their sitemap.
This function
XoopsModuleHandler::getSitemap() looks like so:
/**
* XOOPS module handler class.
*
* This class is responsible for providing data access mechanisms to the data source
* of XOOPS module class objects.
*
* @package kernel
* @author Kazumi Ono
* @copyright (c) 2000-2003 The Xoops Project - www.xoops.org
*/
class XoopsModuleHandler extends XoopsPersistableObjectHandler
{
//...Other functions...
/**
* returns an array of sitemap objects based on a criteria
*
* @param CriteriaElement|null $criteria
* @return array of XoopsSitemap Objects
*/
function getSitemap(CriteriaElement $criteria = null)
{
// Sets Head of Sitemap Root of site
$ret = array();
$ret[] = new XoopsSitemap();
$ret[sizeof($ret)-1]->setVar('loc', XOOPS_URL);
$ret[sizeof($ret)-1]->setVar('lastmod', time());
$ret[sizeof($ret)-1]->setVar('changefreq', 'daily');
$ret[sizeof($ret)-1]->setVar('priority', '1');
// Retrieves Sitemap from criteria of modules
$modules = $this->getObjectsArray($criteria, true);
foreach (array_keys($modules) as $i) {
foreach($modules[$i]->getSitemap() as $object) {
if (is_a($object, 'XoopsSitemap')) {
$ret[] = $object;
}
}
}
return count($ret)>0?$ret:false;
}
}
There is also another class object in /kernel/module.php which is the one called
XoopsSitemap it has no handler and is openly created in the modules with the New clause when being instanciated. It looks like so at the bottom of the file:
/**
* A Sitemap Object
*
* @package kernel
* @author Simon Roberts
*/
class XoopsSitemap extends XoopsObject
{
/**
* Constructor
*/
public function __construct()
{
$this->initVar('loc', XOBJ_DTYPE_TXTBOX, null, false, 1000);
$this->initVar('lastmod', XOBJ_DTYPE_INT, time(), false);
$this->initVar('changefreq', XOBJ_DTYPE_ENUM, 'daily', false, false, false, array('daily','weekly','monthly','yearly'));
$this->initVar('priority', XOBJ_DTYPE_ENUM, '1', false, false, false, array('1','0.9','0.8','0.7','0.6','0.5','0.4','0.3','0.2','0.1'));
}
public function toArray() {
$ret = parent::toArray();
$ret['lastmod'] = date('Y-m-d', $this->getVar('lastmod'));
return $ret;
}
}
Now you have the system template
system_sitemap.html which is additional to the other XML template for RSS but this one is for producting a sitemap file for Google, bing, yahoo etc, that real time updates as per your modules.
system_sitemap.html looks like so:
xml version="1.0" encoding="UTF-8"?>
<{foreach item=item from=$items}>
<{$item.loc}>
<{$item.lastmod}>
<{$item.changefreq}>
<{$item.priority}>
<{/foreach}>
Finally you have the file you point Google, bing, yahoo and other search engines,there is also an index in robots.txt for sitemaps which you point at the file so other search engines find it as well. this file is for XOOPS 2.6.0 you will have to reverse engineer it compairing to backend.php in previous versions to have an earlier version of XOOPS to behave with this file correctly.
sitemap.php looks like the following code:
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* XOOPS feed creator
*
* @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
* @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
* @package core
* @since 2.0.0
* @version $Id: backend.php 8236 2011-11-08 00:31:09Z trabis $
*/
include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mainfile.php';
$xoops = Xoops::getInstance();
$xoops->logger->disable();
if (function_exists('mb_http_output')) {
mb_http_output('pass');
}
header('Content-Type:text/xml; charset=utf-8');
$tpl = new XoopsTpl();
$tpl->caching = 2;
$tpl->cache_lifetime = 1800;
if (!$tpl->is_cached('module:system|system_sitemap.html')) {
$module_handler = $xoops->getHandlerModule();
$sarray = $module_handler->getSitemap(new Criteria('`isactive`', true));
if (!empty($sarray) && is_array($sarray)) {
foreach ($sarray as $node) {
$tpl->append('items', array(
'loc' => XoopsLocal::convert_encoding(htmlspecialchars($node->getVar('loc'), ENT_QUOTES)),
'lastmod' => date('Y-m-d', $node->getVar('lastmod')),
'changefreq' => $node->getVar('changefreq'),
'priority' => $node->getVar('priority')
));
}
}
}
$tpl->display('system_sitemap.html');
Now
robots.txt changes look like so:
User-agent: *
Sitemap: /sitemap.php
Disallow: /cgi-bin/
Disallow: /tmp/
Disallow: /cache/
Disallow: /class/
Disallow: /images/
Disallow: /include/
Disallow: /install/
Disallow: /kernel/
Disallow: /language/
Disallow: /templates_c/
Disallow: /themes/
Disallow: /uploads/
Now all you have to do is for the modules modify you
xoops_version.php with the sitemap function and file for the module that returns a limit of
XoopsSitemap objects in an array for the output to send to the search engines on search, this is required since around 2003/2004 for SEO Purposes as well as better ranking and quicker listing of new content.
You can find this and other components you will find in 2.6.0 in a section of tasks for 2.6.0 Merger quiet soon..
http://xoops.svn.sourceforge.net/viewvc/xoops/XoopsCore/branches/tasks/2.6.0-wishcraft/