79c7db9ebfe5879db91eabe8a38d7ee48ef0a5c8
[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.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 "images.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, width, height, alt, octet_length (image),
25             tn_width, tn_height
26        from images
27       where hostid = ? and " ^
28     (if not deleted then "name is not null"
29      else "name_deleted is not null") ^
30     " order by 2, 3" in
31   let sth = dbh#prepare_cached sql in
32   sth#execute [`Int hostid];
33
34   let table =
35     sth#map
36       (fun row ->
37          let id, name, width, height, alt, size, tn_width, tn_height,
38            is_deleted, has_thumbnail =
39            match row with
40              | [`Int id; `String name; `Null; `Int width; `Int height;
41                 `String alt; `Int size; `Int tn_width; `Int tn_height] ->
42                  id, name, width, height, alt, size, tn_width, tn_height,
43                  false, true
44              | [`Int id; `Null; `String name; `Int width; `Int height;
45                 `String alt; `Int size; `Int tn_width; `Int tn_height] ->
46                  id, name, width, height, alt, size, tn_width, tn_height,
47                  true, true
48              | [`Int id; `String name; `Null; `Int width; `Int height;
49                 `String alt; `Int size; `Null; `Null] ->
50                  id, name, width, height, alt, size, 0, 0,
51                  false, false
52              | [`Int id; `Null; `String name; `Int width; `Int height;
53                 `String alt; `Int size; `Null; `Null] ->
54                  id, name, width, height, alt, size, 0, 0,
55                  true, false
56              | _ -> assert false in
57          [ "id", Template.VarString (string_of_int id);
58            "name", Template.VarString name;
59            "width", Template.VarString (string_of_int width);
60            "height", Template.VarString (string_of_int height);
61            "alt", Template.VarString alt;
62            "ksize", Template.VarString (string_of_int (size / 1024));
63            "tn_width", Template.VarString (string_of_int tn_width);
64            "tn_height", Template.VarString (string_of_int tn_height);
65            "is_deleted", Template.VarConditional is_deleted;
66            "has_thumbnail", Template.VarConditional has_thumbnail ]) in
67
68   template#table "images" table;
69
70   q#template template
71
72 let () =
73   register_script run