78c93f5ae407fc07397905b2e78ba6566e0b9ebe
[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.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 "admin/admin.html"
18
19 let run r (q : cgi) (dbh : Dbi.connection) _ _ =
20   (* Select out the alternative hostnames. *)
21   let sth = dbh#prepare_cached
22               "select hs.hostid, hs.name from hostnames hs
23                 where not exists (select 1 from hosts
24                                    where id = hs.hostid
25                                      and canonical_hostname = hs.name)" in
26   sth#execute [];
27
28   let hostnames = sth#map (function [`Int hostid; `String name] ->
29                              hostid, name
30                              | _ -> assert false) in
31
32   (* Pull out the details of all the wikis on the server. *)
33   let sth = dbh#prepare_cached
34               "select h.id, h.canonical_hostname,
35                       (select count(*) from pages
36                         where hostid = h.id and url is not null),
37                       (select max(last_modified_date) from pages
38                         where hostid = h.id and url is not null)
39                  from hosts h
40                 order by 2" in
41   sth#execute [];
42
43   let table =
44     sth#map
45       (function [`Int id; `String canonical_hostname;
46                  (`Null | `Int _) as page_count;
47                  (`Null | `Timestamp _) as last_modified_date] ->
48          let page_count = match page_count with
49              `Null -> 0
50            | `Int n -> n in
51          let last_modified_date = match last_modified_date with
52              `Null -> "-"
53            | `Timestamp date -> printable_date date in
54
55          let hostnames =
56            List.filter (fun (i, _) -> i = id) hostnames in
57          let hostnames =
58            List.map (fun (_, hostname) ->
59                        [ "hostname", Template.VarString hostname ])
60              hostnames in
61
62          [ "id", Template.VarString (string_of_int id);
63            "canonical_hostname", Template.VarString canonical_hostname;
64            "page_count", Template.VarString (string_of_int page_count);
65            "last_modified_date", Template.VarString last_modified_date;
66            "hostnames", Template.VarTable hostnames ]
67
68          | _ -> assert false) in
69
70   template#table "hosts" table;
71
72   q#template template
73
74 let () =
75   register_script run