68416e55ff7d790a3cf9488bdaaf34c6b6b5aa9a
[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.7 2006/03/27 18:09:47 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 sth = dbh#prepare_cached
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   sth#execute [];
41
42   let hostnames = sth#map (function [`Int hostid; `String name] ->
43                              hostid, name
44                              | _ -> assert false) in
45
46   (* Pull out the details of all the wikis on the server. *)
47   let sth = dbh#prepare_cached
48               "select h.id, h.canonical_hostname,
49                       (select count(*) from pages
50                         where hostid = h.id and url is not null),
51                       (select max(last_modified_date) from pages
52                         where hostid = h.id and url is not null)
53                  from hosts h
54                 order by 2" in
55   sth#execute [];
56
57   let table =
58     sth#map
59       (function [`Int id; `String canonical_hostname;
60                  (`Null | `Int _) as page_count;
61                  (`Null | `Timestamp _) as last_modified_date] ->
62          let page_count = match page_count with
63              `Null -> 0
64            | `Int n -> n in
65          let last_modified_date = match last_modified_date with
66              `Null -> "-"
67            | `Timestamp date -> printable_date date in
68
69          let hostnames =
70            List.filter (fun (i, _) -> i = id) hostnames in
71          let hostnames =
72            List.map (fun (_, hostname) ->
73                        [ "hostname", Template.VarString hostname ])
74              hostnames in
75
76          [ "id", Template.VarString (string_of_int id);
77            "canonical_hostname", Template.VarString canonical_hostname;
78            "page_count", Template.VarString (string_of_int page_count);
79            "last_modified_date", Template.VarString last_modified_date;
80            "hostnames", Template.VarTable hostnames ]
81
82          | _ -> assert false) in
83
84   template#table "hosts" table;
85
86   q#template template
87
88 let () =
89   register_script run