c2587300633dfdbd2790a0479bf09c02ac0f6996
[cocanwiki.git] / scripts / undelete_image.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: undelete_image.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 open Cocanwiki_ok
17
18 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
19   let id = int_of_string (q#param "id") in
20
21   if q#param_true "yes" then (
22     (* Get the name of the image. *)
23     let sth = dbh#prepare_cached "select name_deleted from images
24                                    where hostid = ? and id = ?" in
25     sth#execute [`Int hostid; `Int id];
26
27     let name = sth#fetch1string () in
28
29     (* First delete any more recent versions of this image. *)
30     let sth = dbh#prepare_cached "update images
31                                      set name_deleted = name, name = null
32                                    where hostid = ? and name = ?" in
33     sth#execute [`Int hostid; `String name];
34
35     (* Now copy the old row, changing name_deleted back to name so the image
36      * becomes live.
37      *)
38     let sth = dbh#prepare_cached "insert into images
39                                   (hostid, name, image, width, height,
40                                    alt, title, longdesc, class,
41                                    mime_type, thumbnail, tn_width,
42                                    tn_height, tn_mime_type, upload_date)
43                                   select hostid, name_deleted, image,
44                                          width, height, alt, title, longdesc,
45                                          class, mime_type, thumbnail,
46                                          tn_width, tn_height, tn_mime_type,
47                                          upload_date
48                                     from images
49                                    where hostid = ? and id = ?" in
50     sth#execute [`Int hostid; `Int id];
51
52     dbh#commit ();
53
54     (* Done. *)
55     let buttons = [ ok_button "/_images" ] in
56     ok ~title:"Image restored" ~buttons
57       q "Image was restored successfully."
58   ) else
59     q#redirect ("http://" ^ hostname ^ "/_images")
60
61 let () =
62   register_script run