Scripts updated to use new PG interface.
[cocanwiki.git] / scripts / admin / admin.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: admin.ml,v 1.8 2006/03/28 16:24:08 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_template
29 open Cocanwiki_date
30
31 let template = _get_template "admin/admin.html"
32
33 let run r (q : cgi) dbh _ _ _ =
34   (* Select out the alternative hostnames. *)
35   let hostnames = PGSQL(dbh)
36     "select hs.hostid, hs.name from hostnames hs
37       where not exists (select 1 from hosts
38                          where id = hs.hostid
39                            and canonical_hostname = hs.name)" in
40
41   (* Pull out the details of all the wikis on the server. *)
42   let rows = PGSQL(dbh)
43     "select h.id, h.canonical_hostname,
44             (select count(*) from pages
45               where hostid = h.id and url is not null),
46             (select max(last_modified_date) from pages
47               where hostid = h.id and url is not null)
48        from hosts h
49       order by 2" in
50
51   let table =
52     List.map (
53       function (id, canonical_hostname,
54                 page_count, last_modified_date) ->
55          let page_count = match page_count with
56            | None -> 0L
57            | Some n -> n in
58          let last_modified_date = match last_modified_date with
59            | None -> "-"
60            | Some date -> printable_date date in
61
62          let hostnames =
63            List.filter (fun (i, _) -> i = id) hostnames in
64          let hostnames =
65            List.map (fun (_, hostname) ->
66                        [ "hostname", Template.VarString hostname ])
67              hostnames in
68
69          [ "id", Template.VarString (Int32.to_string id);
70            "canonical_hostname", Template.VarString canonical_hostname;
71            "page_count", Template.VarString (Int64.to_string page_count);
72            "last_modified_date", Template.VarString last_modified_date;
73            "hostnames", Template.VarTable hostnames ]
74     ) rows in
75
76   template#table "hosts" table;
77
78   q#template template
79
80 let () =
81   register_script run