Scripts updated to use new PG interface.
[cocanwiki.git] / scripts / admin / host.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: host.ml,v 1.10 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/host.html"
32
33 let run r (q : cgi) dbh _ _ _ =
34   let hostid = Int32.of_string (q#param "hostid") in
35   template#set "id" (Int32.to_string hostid);
36
37   (* Pull out some overall details for this host. *)
38   let rows = PGSQL(dbh)
39     "select h.canonical_hostname, h.css is not null,
40             (select count(*) from pages
41               where hostid = h.id and url is not null),
42             (select count(*) from pages
43               where hostid = h.id),
44             (select max(last_modified_date) from pages
45               where hostid = h.id and url is not null),
46             (select min(last_modified_date) from pages
47               where hostid = h.id and url is not null)
48        from hosts h
49       where h.id = $hostid" in
50
51   let canonical_hostname, has_css, page_count, total_count,
52     last_modified_date, creation_date =
53     match rows with
54     | [ canonical_hostname, Some has_css,
55         page_count,
56         total_count,
57         last_modified_date, creation_date ] ->
58         let page_count = match page_count with
59           | None -> 0L
60           | Some n -> n in
61         let total_count = match total_count with
62           | None -> 0L
63           | Some n -> n in
64         let last_modified_date = match last_modified_date with
65           | None -> ""
66           | Some t -> printable_date t in
67         let creation_date = match creation_date with
68           | None -> ""
69           | Some t -> printable_date t in
70         canonical_hostname, has_css, page_count, total_count,
71         last_modified_date, creation_date
72     | _ -> assert false in
73
74   template#set "canonical_hostname" canonical_hostname;
75   template#conditional "has_css" has_css;
76   template#set "page_count" (Int64.to_string page_count);
77   template#set "total_count" (Int64.to_string total_count);
78   template#set "last_modified_date" last_modified_date;
79   template#set "creation_date" creation_date;
80
81   (* Pull out any aliases. *)
82   let rows = PGSQL(dbh)
83     "select name from hostnames
84       where hostid = $hostid and name <> $canonical_hostname" in
85   let table = List.map (
86     fun hostname ->
87       [ "hostname", Template.VarString hostname ]
88   ) rows in
89   template#table "hostnames" table;
90
91   q#template template
92
93 let () =
94   register_script run