Moved to merjis/tools/wiki.
[cocanwiki.git] / scripts / files.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: files.ml,v 1.1 2004/09/07 10:14:09 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13
14 open Merjisforwiki
15
16 let template = get_template "files.html"
17
18 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _) _ =
19   let deleted = q#param_true "deleted" in
20   template#conditional "deleted" deleted;
21
22   let sql =
23     "select id, name, name_deleted, octet_length (content)
24        from files
25       where hostid = ? and " ^
26     (if not deleted then "name is not null"
27      else "name_deleted is not null") ^
28     " order by 2, 3" in
29   let sth = dbh#prepare_cached sql in
30   sth#execute [`Int hostid];
31
32   let table =
33     sth#map
34       (fun row ->
35          let id, name, size, is_deleted =
36            match row with
37              | [`Int id; `String name; `Null; `Int size] ->
38                  id, name, size, false
39              | [`Int id; `Null; `String name; `Int size] ->
40                  id, name, size, true
41              | _ -> assert false in
42          [ "id", Template.VarString (string_of_int id);
43            "name", Template.VarString name;
44            "ksize", Template.VarString (string_of_int (size / 1024));
45            "is_deleted", Template.VarConditional is_deleted ]) in
46
47   template#table "files" table;
48
49   q#template template
50
51 let () =
52   register_script run