Support for users, roles, restrictions.
[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.2 2004/09/07 13:40:10 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 open Cocanwiki_template
16
17 let template = get_template "sitemap.html"
18
19 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
20   (* Pull out all the current pages, and a bit of content from each. *)
21   let sth = dbh#prepare_cached "select p.url, p.url = 'index',
22                                        p.title, p.description,
23                                        p.last_modified_date,
24                                        (select content from contents
25                                          where pageid = p.id
26                                          order by ordering limit 1) as content
27                                   from pages p
28                                  where p.hostid = ? and p.url is not null
29                                    and p.redirect is null
30                                  order by 2 desc, 3, 1" in
31   sth#execute [`Int hostid];
32
33   let table =
34     sth#map
35       (function [`String url; _; `String title; `String description;
36                  `Timestamp last_modified_date;
37                  (`Null | `String _) as content] ->
38          let url = if url = "index" then "" else url in
39          let date = printable_date last_modified_date in
40          [ "url", Template.VarString url;
41            "title", Template.VarString title;
42            "description", Template.VarString description;
43            "last_modified_date", Template.VarString date;
44            "has_content", Template.VarConditional (content <> `Null);
45            "content", Template.VarString
46              (match content with
47                   `Null -> ""
48                 | `String c ->
49                     truncate 160
50                       (Wikilib.text_of_xhtml
51                          (Wikilib.xhtml_of_content dbh hostid c))) ]
52          | _ -> assert false) in
53
54   template#set "hostname" hostname;
55   template#table "sitemap" table;
56
57   q#template template
58
59 let () =
60   register_script run