Moved to merjis/tools/wiki.
[cocanwiki.git] / scripts / sitemap.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: sitemap.ml,v 1.1 2004/09/07 10:14:09 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Merjisforwiki
13
14 open Cocanwiki
15
16 let template = get_template "sitemap.html"
17
18 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname) _ =
19   (* Pull out all the current pages, and a bit of content from each. *)
20   let sth = dbh#prepare_cached "select p.url, p.url = 'index',
21                                        p.title, p.description,
22                                        p.last_modified_date,
23                                        (select content from contents
24                                          where pageid = p.id
25                                          order by ordering limit 1) as content
26                                   from pages p
27                                  where p.hostid = ? and p.url is not null
28                                    and p.redirect is null
29                                  order by 2 desc, 3, 1" in
30   sth#execute [`Int hostid];
31
32   let table =
33     sth#map
34       (function [`String url; _; `String title; `String description;
35                  `Timestamp last_modified_date;
36                  (`Null | `String _) as content] ->
37          let url = if url = "index" then "" else url in
38          let date = printable_date last_modified_date in
39          [ "url", Template.VarString url;
40            "title", Template.VarString title;
41            "description", Template.VarString description;
42            "last_modified_date", Template.VarString date;
43            "has_content", Template.VarConditional (content <> `Null);
44            "content", Template.VarString
45              (match content with
46                   `Null -> ""
47                 | `String c ->
48                     truncate 160
49                       (Wikilib.text_of_xhtml
50                          (Wikilib.xhtml_of_content dbh hostid c))) ]
51          | _ -> assert false) in
52
53   template#set "hostname" hostname;
54   template#table "sitemap" table;
55
56   q#template template
57
58 let () =
59   register_script run