Removed dependency on imported merjislib.
[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.3 2004/09/07 14:58:34 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13 open Cocanwiki_template
14 open Cocanwiki_ok
15
16 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
17   let id = int_of_string (q#param "id") in
18
19   if q#param_true "yes" then (
20     (* Get the name of the file. *)
21     let sth = dbh#prepare_cached "select name_deleted from files
22                                    where hostid = ? and id = ?" in
23     sth#execute [`Int hostid; `Int id];
24
25     let name = sth#fetch1string () in
26
27     (* First delete any more recent versions of this file. *)
28     let sth = dbh#prepare_cached "update files
29                                      set name_deleted = name, name = null
30                                    where hostid = ? and name = ?" in
31     sth#execute [`Int hostid; `String name];
32
33     (* Now copy the old row, changing name_deleted back to name so the file
34      * becomes live.
35      *)
36     let sth = dbh#prepare_cached "insert into files
37                                   (hostid, name, content, title, mime_type,
38                                    upload_date)
39                                   select hostid, name_deleted, content,
40                                          title, mime_type, upload_date
41                                     from files
42                                    where hostid = ? and id = ?" in
43     sth#execute [`Int hostid; `Int id];
44
45     dbh#commit ();
46
47     (* Done. *)
48     let buttons = [ ok_button "/_files" ] in
49     ok ~title:"File restored" ~buttons
50       q "File was restored successfully."
51   ) else
52     q#redirect ("http://" ^ hostname ^ "/_files")
53
54 let () =
55   register_script run