All new templates. Site menu displays basically everywhere.
[cocanwiki.git] / scripts / admin / admin.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: admin.ml,v 1.5 2004/09/09 09:35:34 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13 open Cocanwiki_template
14 open Cocanwiki_date
15
16 let template = _get_template "admin/admin.html"
17
18 let run r (q : cgi) (dbh : Dbi.connection) _ _ _ =
19   (* Select out the alternative hostnames. *)
20   let sth = dbh#prepare_cached
21               "select hs.hostid, hs.name from hostnames hs
22                 where not exists (select 1 from hosts
23                                    where id = hs.hostid
24                                      and canonical_hostname = hs.name)" in
25   sth#execute [];
26
27   let hostnames = sth#map (function [`Int hostid; `String name] ->
28                              hostid, name
29                              | _ -> assert false) in
30
31   (* Pull out the details of all the wikis on the server. *)
32   let sth = dbh#prepare_cached
33               "select h.id, h.canonical_hostname,
34                       (select count(*) from pages
35                         where hostid = h.id and url is not null),
36                       (select max(last_modified_date) from pages
37                         where hostid = h.id and url is not null)
38                  from hosts h
39                 order by 2" in
40   sth#execute [];
41
42   let table =
43     sth#map
44       (function [`Int id; `String canonical_hostname;
45                  (`Null | `Int _) as page_count;
46                  (`Null | `Timestamp _) as last_modified_date] ->
47          let page_count = match page_count with
48              `Null -> 0
49            | `Int n -> n in
50          let last_modified_date = match last_modified_date with
51              `Null -> "-"
52            | `Timestamp date -> printable_date date in
53
54          let hostnames =
55            List.filter (fun (i, _) -> i = id) hostnames in
56          let hostnames =
57            List.map (fun (_, hostname) ->
58                        [ "hostname", Template.VarString hostname ])
59              hostnames in
60
61          [ "id", Template.VarString (string_of_int id);
62            "canonical_hostname", Template.VarString canonical_hostname;
63            "page_count", Template.VarString (string_of_int page_count);
64            "last_modified_date", Template.VarString last_modified_date;
65            "hostnames", Template.VarTable hostnames ]
66
67          | _ -> assert false) in
68
69   template#table "hosts" table;
70
71   q#template template
72
73 let () =
74   register_script run