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