#!/usr/bin/php-cgi -dcgi.force_redirect=0
<?php
# encoding: utf-8
# api: cgi
# type: output
# category: template
# title: sitemap.xml
# description: Generate indexing list for search engines
# version: 0.1
# state: beta
# depends: php:sqlite
# config: -
# access: public
#
# Crafts a sitemap.xml from wiki page list, document files,
# recent changes, etc.
#
# You might have to set up a RewriteRule or fossil-alias
# from sitemap.xml to ext/sitemap (because the ext/ prefix
# precludes listing other sections else).
#
# Then add a `robots.txt` entry like:
#   Sitemap: /sitemap.xml
#
# Or a cron job to ping:
#   wget -S http://duckduckgo.com/ping?sitemap=http://example.org/sitemap.xml
#



#-- init
include("./fossil_common.php");
header("Content-Type: text/xml; inline");
ini_set("display_errors", 1);

# head
print '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
';

# pages
loop(
    db("
        SELECT SUBSTR(tagname, 6) AS name, DATE(MAX(mtime)) AS datetime
        FROM tag LEFT JOIN tagxref ON tag.tagid=tagxref.tagid
        WHERE tagname LIKE 'wiki-%' GROUP BY name ORDER BY datetime DESC;
    "),
    "wiki/", "daily", "0.5"
);
loop(
    db("
        SELECT SUBSTR(tagname, 7) AS name, DATE(MAX(mtime)) AS datetime
        FROM tag LEFT JOIN tagxref ON tag.tagid=tagxref.tagid
        WHERE tagname LIKE 'event-%' GROUP BY name ORDER BY datetime DESC;
    "),
    "technote/"
    , "weekly", "0.2"
);
# files *.md
loop(
    db("
       SELECT name, DATE(MAX(e.mtime)) AS datetime
       FROM filename
          LEFT JOIN mlink m ON m.fnid = filename.fnid
          LEFT JOIN event e ON e.objid = m.mid
       WHERE name LIKE '%.md' OR name LIKE '%.wiki' --REGEXP '\.(md|wiki|rst)$'
       GROUP BY name
       HAVING fid <> 0
       ORDER BY datetime DESC
    "),
    "doc/trunk/"
    , "monthly", "0.3"
);

# output
function loop($r, $base, $freq="weekly", $prio="0.1") {
    foreach ($r as $page) {
        entry($base . $page["name"], $page["datetime"], $freq, $prio);
    }
}

function entry($url, $datetime, $freq, $prio) {
    $url = htmlspecialchars($url, ENT_QUOTES, "utf-8");
    print <<<EOT
    <url>
        <loc>{$_SERVER["FOSSIL_SELF"]}{$url}</loc>
        <lastmod>$datetime</lastmod>
        <changefreq>$freq</changefreq>
        <priority>$prio</priority>
    </url>\n
EOT;
}

# end
print '</urlset>';

?>