Support for users, roles, restrictions.
[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.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 file. *)
23     let sth = dbh#prepare_cached "select name_deleted from files
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 file. *)
30     let sth = dbh#prepare_cached "update files
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 file
36      * becomes live.
37      *)
38     let sth = dbh#prepare_cached "insert into files
39                                   (hostid, name, content, title, mime_type,
40                                    upload_date)
41                                   select hostid, name_deleted, content,
42                                          title, mime_type, upload_date
43                                     from files
44                                    where hostid = ? and id = ?" in
45     sth#execute [`Int hostid; `Int id];
46
47     dbh#commit ();
48
49     (* Done. *)
50     let buttons = [ ok_button "/_files" ] in
51     ok ~title:"File restored" ~buttons
52       q "File was restored successfully."
53   ) else
54     q#redirect ("http://" ^ hostname ^ "/_files")
55
56 let () =
57   register_script run