Joomla sitemap fun!

Attention! Nerd alert! This post is almost strictly intended as a write-up of some recent web code stuff I did, so if you're here for my normal ridiculous stream of cat / chocolate / kid pichurs or rantings, you may be left wanting:

I haven't posted code here in ages. That's a shame, actually, because I love sharing my code so people can school me on how much better it could be.


Thus, I submit for your approval / derision / amusement / amazement my latest bit of brilliance. I needed something to generate a sitemap for a my latest client Joomla site. I looked through all the available plugins and they either didn't work with Joomla 1.5, or didn't support our most important extension (the amazing EventList). So, I adapted some code I wrote a while back for my old blog software.

All up, I'm very happy with the results. It uses mainly Joomla built-ins -- something I always try to do when possible -- and thus should work pretty much as a drop-in on any Joomla 1.5 site. Regrettably, I had to resort to actually pulling stuff directly out of the database -- for the EventlList stuff -- but I wasn't left with much choice, unfortunately.

A huge shout-out to Christoph Lukes, specifically and the whole team over at http://schlu.net for their hard work on EventList, and for the code for the xmap extension, from which I was able to get enough information to get my EventList sitemap hack working.

Without further Ado, the code:


<?php
// Set flag that this is a parent file
define'_JEXEC');

define('JPATH_BASE'dirname(__FILE__) );

define'DS'DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( 
JPATH_BASE .DS.'includes'.DS.'framework.php' );

JDEBUG $_PROFILER->mark'afterLoad' ) : null;

/**
 * CREATE THE APPLICATION
 *
 * NOTE :
 */
$mainframe =& JFactory::getApplication('site');

/**
 * INITIALISE THE APPLICATION
 *
 * NOTE :
 */
// set the language
$mainframe->initialise();

JPluginHelper::importPlugin('system');

$menu        = & JSite::getMenu();
$items        $menu->getItems("menutype""topmenu");
$gendate date('M jS, Y h:i:s A');

// print the header and the url item for the home page
$xmlout = <<<EOXML
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="http://{$_SERVER["HTTP_HOST"]}/sitemap.php"?>
<!-- generator="Joomla" -->
<!-- generated-on="
{$gendate}" -->
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/09/sitemap.xsd"    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://
{$_SERVER["HTTP_HOST"]}</loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
EOXML;
// add all the menu items
foreach($items as $mitem) {
    if(
$mitem->name != 'Home') {
        
// parse the link for each item out and produce a SEF link for it
        
$purl parse_url($mitem->link,PHP_URL_QUERY);
        
$purl split("&"$purl);
        foreach(
$purl as $tp) {
            
$tps split("=",$tp);
            
$tps[1] = ereg_replace("com_"""$tps[1]); // strip the "com_" bit out of the component name
                                     // to get just the component name
            
$mitem->linkvars[$tps[0]] = $tps[1];
        }
        
// print_r($mitem->linkvars);

        $mitem->link "component/".$mitem->linkvars['option']."/".$mitem->linkvars['view']."/".$mitem->linkvars['id'];
        
$xmlout .= <<<EOXML

    <url>
        <loc>http://
{$_SERVER['HTTP_HOST']}/{$mitem->link}</loc>
        <changefreq>daily</changefreq>
        <priority>0.2</priority>
    </url>
EOXML;
    }
}
/**
** begin eventlist stuff. this next section
** can be completely commented out if you're
** NOT using evenlist
**/

$query = <<<SQL
SELECT id
FROM #__eventlist_events
WHERE published = 1
ORDER BY id
SQL;

$database->setQuery($query);
$events $database->loadObjectList();
foreach(
$events as $tevent) {
    
$xmlout .= <<<EOXML

    <url>
        <loc>http://
{$_SERVER['HTTP_HOST']}/component/eventlist/details/{$tevent->id}</loc>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
EOXML;
}
/**
** end eventlist section
**/

$xmlout .= <<<EOXML

</urlset>
EOXML;
print 
$xmlout;
?>


Leave a Reply