Creating a Sitemap without MidCOM
This code collection shows you how you can create a sitemap using the mgd_walk_topic_tree function. Some style elements are included to allow you a flexible formatting of the resulting sitemap.
Include the main code into your page content. Add the style elements to the accociated Style and fill the required configuration variables.
The URL generator will create URLs that can be recognized by a parser that maps topic names _directly_ to an URL.
Configuration
- MidgardTopic $global_content_topic
- This is the root topic of your content tree.
- string $global_anchor_prefix
- This string will be prefixed before _all_ generated URLs.
Style Elements
The Code calls five different style elements:
- sitemap
- Called once before the sitemap output starts
- -sitemap
- Called once after the sitemap output is complete.
- sitemap-topic-group
- Called once each time the code decends one level in the topic tree.
- -sitemap-topic-group
- Called once each time the code ascends one level in the topic tree.
- sitemap-item
- Called once for every element in the Sitemap, the string $url holds the URL required to access the element in question. The element itself is passed in the MidgardTopic Variable $view.
Code
<[sitemap]>
<?php
/*
1. Loop over root topics
2. Recursivly loop over each subtree
3. for each found topic:
- Test if there are subtopics
-> if Yes, print out the link to the topic and begin looping
over subtopics
-> if No, print out the topic link and continue.
*/
function sitemap_callback($id, $level, &$oldlevel) {
global $global_anchor_prefix, $global_content_topic;
if ($level == 0) {
$oldlevel = $level;
return;
}
for ($i = 0; $i < ($level - $oldlevel); $i++) {
?><[sitemap-topic-group]><?
}
for ($i = 0; $i < ($oldlevel - $level); $i++) {
?><[-sitemap-topic-group]><?
}
if (($level == 1) && ($oldlevel > $level)) {
?><[sitemap-topic-group]><?
}
$view = mgd_get_topic($id);
$url = rawurlencode($view->name);
$temp = mgd_get_topic ($view->up);
while ($temp->id != $global_content_topic->id) {
$url = rawurlencode ($temp->name) . "/" . $url;
$temp = mgd_get_topic ($temp->up);
}
$url = $global_anchor_prefix . $url;
?><[sitemap-item]><?
$oldlevel = $level;
}
$oldlevel = -1;
mgd_walk_topic_tree ("sitemap_callback", $global_content_topic->id,
5, &$oldlevel, true, "score desc,name");
for ($i = $oldlevel; $i > 0; $i--) {
?><[-sitemap-topic-group]><?
}
?>
<[-sitemap]>