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