820bac60e14ee0a2170a975ad7423f3223aa7f4c
[cocanwiki.git] / scripts / restore_form.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: restore_form.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 open Cocanwiki_diff
18
19 let template = get_template "restore_form.html"
20
21 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ =
22   (* Parameters. *)
23   let page = q#param "page" in
24   let old_version = int_of_string (q#param "version") in
25
26   template#set "page" page;
27   template#set "version" (string_of_int old_version);
28
29   (* Compute the diff between the latest version of this page and the
30    * page we're wanting to restore.
31    *)
32   let sth = dbh#prepare_cached "select id from pages
33                                  where hostid = ? and url = ?" in
34   sth#execute [`Int hostid; `String page];
35
36   let version = sth#fetch1int () in
37
38   if version = old_version then (
39     error ~back_button:true ~title:"Restoring live version"
40       q "You seem to be trying to restore the live version.";
41     raise CgiExit
42   );
43
44   let diff, _ = get_diff dbh hostid page ~old_version ~version () in
45
46   (* Display the diff. *)
47   template#set "diff" diff;
48
49   q#template template
50
51 let () =
52   register_script run