50587229220ee2ff46ae05e95ec88bfbeb83d4ea
[cocanwiki.git] / scripts / admin / host.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: host.ml,v 1.3 2004/09/07 14:58: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/host.html"
17
18 let run r (q : cgi) (dbh : Dbi.connection) _ _ =
19   let hostid = int_of_string (q#param "hostid") in
20
21   template#set "id" (string_of_int hostid);
22
23   (* Pull out some overall details for this host. *)
24   let sth = dbh#prepare_cached
25               "select h.canonical_hostname, h.css is not null,
26                       (select count(*) from pages
27                         where hostid = h.id and url is not null),
28                       (select count(*) from pages
29                         where hostid = h.id),
30                       (select max(last_modified_date) from pages
31                         where hostid = h.id and url is not null),
32                       (select min(last_modified_date) from pages
33                         where hostid = h.id and url is not null)
34                  from hosts h
35                 where h.id = ?" in
36   sth#execute [`Int hostid];
37
38   let canonical_hostname, has_css, page_count, total_count,
39     last_modified_date, creation_date =
40     match sth#fetch1 () with
41         [ `String canonical_hostname;
42           `Bool has_css;
43           (`Null | `Int _) as page_count; (`Null | `Int _) as total_count;
44           (`Null | `Timestamp _) as last_modified_date;
45           (`Null | `Timestamp _) as creation_date ] ->
46           let page_count = match page_count with
47               `Null -> 0
48             | `Int n -> n in
49           let total_count = match total_count with
50               `Null -> 0
51             | `Int n -> n in
52           let last_modified_date = match last_modified_date with
53               `Null -> ""
54             | `Timestamp t -> printable_date t in
55           let creation_date = match creation_date with
56               `Null -> ""
57             | `Timestamp t -> printable_date t in
58           canonical_hostname, has_css, page_count, total_count,
59           last_modified_date, creation_date
60       | _ -> assert false in
61
62   template#set "canonical_hostname" canonical_hostname;
63   template#conditional "has_css" has_css;
64   template#set "page_count" (string_of_int page_count);
65   template#set "total_count" (string_of_int total_count);
66   template#set "last_modified_date" last_modified_date;
67   template#set "creation_date" creation_date;
68
69   (* Pull out any aliases. *)
70   let sth = dbh#prepare_cached "select name from hostnames
71                                  where hostid = ?
72                                    and name <> ?" in
73   sth#execute [`Int hostid; `String canonical_hostname];
74
75   let table = sth#map (function [`String hostname] ->
76                          [ "hostname", Template.VarString hostname ]
77                          | _ -> assert false) in
78   template#table "hostnames" table;
79
80   (* Pull out any email notifications. *)
81   let sth = dbh#prepare_cached "select email, name from email_notify
82                                  where hostid = ?" in
83   sth#execute [`Int hostid];
84
85   let table = sth#map (function
86                            [`String email; `Null] ->
87                              [ "email", Template.VarString email;
88                                "name", Template.VarString "" ]
89                          | [ `String email; `String name] ->
90                              [ "email", Template.VarString email;
91                                "name", Template.VarString name ]
92                          | _ -> assert false) in
93   template#table "emails" table;
94
95   q#template template
96
97 let () =
98   register_script run