Moved to merjis/tools/wiki.
[cocanwiki.git] / scripts / undelete_file.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: undelete_file.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 file. *)
22     let sth = dbh#prepare_cached "select name_deleted from files
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 file. *)
29     let sth = dbh#prepare_cached "update files
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 file
35      * becomes live.
36      *)
37     let sth = dbh#prepare_cached "insert into files
38                                   (hostid, name, content, title, mime_type,
39                                    upload_date)
40                                   select hostid, name_deleted, content,
41                                          title, mime_type, upload_date
42                                     from files
43                                    where hostid = ? and id = ?" in
44     sth#execute [`Int hostid; `Int id];
45
46     dbh#commit ();
47
48     (* Done. *)
49     let buttons = [ ok_button "/_files" ] in
50     ok ~title:"File restored" ~buttons
51       q "File was restored successfully."
52   ) else
53     q#redirect ("http://" ^ hostname ^ "/_files")
54
55 let () =
56   register_script run