Moved to merjis/tools/wiki.
[cocanwiki.git] / scripts / images.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: images.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 "images.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, width, height, alt, octet_length (image),
24             tn_width, tn_height
25        from images
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, width, height, alt, size, tn_width, tn_height,
37            is_deleted, has_thumbnail =
38            match row with
39              | [`Int id; `String name; `Null; `Int width; `Int height;
40                 `String alt; `Int size; `Int tn_width; `Int tn_height] ->
41                  id, name, width, height, alt, size, tn_width, tn_height,
42                  false, true
43              | [`Int id; `Null; `String name; `Int width; `Int height;
44                 `String alt; `Int size; `Int tn_width; `Int tn_height] ->
45                  id, name, width, height, alt, size, tn_width, tn_height,
46                  true, true
47              | [`Int id; `String name; `Null; `Int width; `Int height;
48                 `String alt; `Int size; `Null; `Null] ->
49                  id, name, width, height, alt, size, 0, 0,
50                  false, false
51              | [`Int id; `Null; `String name; `Int width; `Int height;
52                 `String alt; `Int size; `Null; `Null] ->
53                  id, name, width, height, alt, size, 0, 0,
54                  true, false
55              | _ -> assert false in
56          [ "id", Template.VarString (string_of_int id);
57            "name", Template.VarString name;
58            "width", Template.VarString (string_of_int width);
59            "height", Template.VarString (string_of_int height);
60            "alt", Template.VarString alt;
61            "ksize", Template.VarString (string_of_int (size / 1024));
62            "tn_width", Template.VarString (string_of_int tn_width);
63            "tn_height", Template.VarString (string_of_int tn_height);
64            "is_deleted", Template.VarConditional is_deleted;
65            "has_thumbnail", Template.VarConditional has_thumbnail ]) in
66
67   template#table "images" table;
68
69   q#template template
70
71 let () =
72   register_script run