Mobile sitemaps with MobileJoomla & XMap

I've been working a lot on improving my website lately and I noticed that even with the help of my favorite Joomla extensions XMap and MobileJoomla, I was not able to submit a decent mobile sitemap to Google Webmaster tools.

Here is a small change you can easily implement and as usual, absolutely no hack !

A mobile sitemap is not really different from a regular sitemap, the xml namespace has an extra attribute to tell is's mobile, and so are the links.

XMap is a great component that allows template overrides and we will leverage this functionality to render a sitemap for mobiles.

And voilà! Adding &mobile=1 to your xml sitemap url will render a mobile compliant sitemap. You can also force a specific device template by adding &markup=iphone

For the geeks, the changes are explained below:

Changes in /templates/YOUR_TEMPLATE/html/com_xmap/xml/default_class.php

<?php
[...]
require_once(JPATH_COMPONENT . DS . 'displayer.php');class XmapXmlDisplayer extends XmapDisplayer
{
[...]
var $isNews = 0;
/*****************************/
/* Change for mobile sitemap */
/*****************************/
var $isMobile = 0;
var $mobileMarkup = '';
var $mobileDomain = '';
/*****************************/
/* Change for mobile sitemap */
/*****************************/
function __construct($config, $sitemap)
{
parent::__construct($config, $sitemap);
$this->uids = array();
/*****************************/
/* Change for mobile sitemap */
/*****************************/
//Adding &mobile=1 to the xml sitemap url will trigger the mobile mode
//You can also pass the mobile markup you want to promote (default is iPhone)
$this->isMobile = JRequest::getVar('mobile',0);
if ($this->isMobile) {
//Check if Mobile Joomla is installed and gather the mobile domain is set
if( file_exists(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_mobilejoomla'.DS.'classes'.DS.'mobilejoomla.php')) {
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_mobilejoomla'.DS.'classes'.DS.'mobilejoomla.php');
if(class_exists('MobileJoomla')) {
$this->mobileMarkup = JRequest::getVar('markup','iphone');
$MobileJoomla_Settings =& MobileJoomla::getConfig();
$this->mobileDomain = $MobileJoomla_Settings[$this->mobileMarkup.'.domain'];
}
}
}
/*****************************/
/* Change for mobile sitemap */
/*****************************/
[...]
}
/**
* Prints an XML node for the sitemap
*
* @param stdclass $node
*/
function printNode($node)
{
if ($this->isExcluded($node->id,$node->uid)) {
return FALSE;
}
if ($this->isNews && (!isset($node->newsItem) || !$node->newsItem)) {
return true;
}
// Get the item's URL
$link = JRoute::_($node->link, true, (@$node->secure? 1: -1));
/*****************************/
/* Change for mobile sitemap */
/*****************************/
//Transform link for mobile device is necessary
if ($this->isMobile) {
$uri = new JURI($link);
if ($this->mobileDomain) {
//If a specific domain is set for mobile, use it
$uri->setHost($this->mobileDomain);
} else {
//Otherwize, just append the mobile markup to the url
$uri->setVar('device',$this->mobileMarkup);
}
$link = $uri->toString();
//Ensure XHTML-compliant urls
$link = htmlspecialchars($link);
}
/*****************************/
/* Change for mobile sitemap */
/*****************************/
if (!isset($node->browserNav))
$node->browserNav = 0;
if ($node->browserNav != 3   // ignore "no link"
&& empty($this->_links[$link])) { // ignore links that have been added already
$this->count++;
$this->_links[$link] = 1;
if (!isset($node->priority))
$node->priority = "0.5";
if (!isset($node->changefreq))
$node->changefreq = 'daily';
// Get the chancefrequency and priority for this item
$changefreq = $this->getProperty('changefreq', $node->changefreq, $node->id, 'xml', $node->uid);
$priority = $this->getProperty('priority', $node->priority, $node->id, 'xml', $node->uid);
echo '<url>' . "\n";
echo '<loc>', $link, '</loc>' . "\n";
/*****************************/
/* Change for mobile sitemap */
/*****************************/
if ($this->isMobile) {
echo '<mobile:mobile/>' . "\n";
}
/*****************************/
/* Change for mobile sitemap */
/*****************************/
[...]
} else {
return empty($this->_links[$link]);
}
return true;
}
[...]
}

Changes in /templates/YOUR_TEMPLATE/html/com_xmap/xml/default.php

$params = $this->item->params;
$live_site = substr_replace(JURI::root(), "", -1, 1);
header('Content-type: text/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>',"\n";
if (($this->item->params->get('beautify_xml', 1) == 1) && !$this->displayer->isNews 
/*****************************/
/* Change for mobile sitemap */
/*****************************/
&& !$this->displayer->isMobile
/*****************************/
/* Change for mobile sitemap */
/*****************************/
) {
$params = '&amp;filter_showtitle='.JRequest::getBool('filter_showtitle',0);
$params .= (JRequest::getCmd('lang')?'&amp;lang='.JRequest::getCmd('lang'):'');
echo '<?xml-stylesheet type="text/xsl" href="'. $live_site.'/index.php?option=com_xmap&amp;view=xml&amp;layout=xsl&amp;tmpl=component&amp;id='.$this->item->id.$params.'"?>'."\n";
}
if ($this->displayer->isMobile) {
/*****************************/
/* Change for mobile sitemap */
/*****************************/
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">    
<?php
/*****************************/
/* Change for mobile sitemap */
/*****************************/
} else {
?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 <a href="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd</a>" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"<?php echo ($this->displayer->isNews? ' xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"':''); ?><?php echo ($this->displayer->isMobile? ' xmlns:news="http://www.google.com/schemas/sitemap-mobile/1.0"':''); ?>>
<?php
}
?>
<?php echo $this->loadTemplate('items'); ?>
</urlset>

Top
joomlart