Support for users, roles, restrictions.
[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.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 "files.html"
18
19 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ =
20   let deleted = q#param_true "deleted" in
21   template#conditional "deleted" deleted;
22
23   let sql =
24     "select id, name, name_deleted, octet_length (content)
25        from files
26       where hostid = ? and " ^
27     (if not deleted then "name is not null"
28      else "name_deleted is not null") ^
29     " order by 2, 3" in
30   let sth = dbh#prepare_cached sql in
31   sth#execute [`Int hostid];
32
33   let table =
34     sth#map
35       (fun row ->
36          let id, name, size, is_deleted =
37            match row with
38              | [`Int id; `String name; `Null; `Int size] ->
39                  id, name, size, false
40              | [`Int id; `Null; `String name; `Int size] ->
41                  id, name, size, true
42              | _ -> assert false in
43          [ "id", Template.VarString (string_of_int id);
44            "name", Template.VarString name;
45            "ksize", Template.VarString (string_of_int (size / 1024));
46            "is_deleted", Template.VarConditional is_deleted ]) in
47
48   template#table "files" table;
49
50   q#template template
51
52 let () =
53   register_script run