1 (* COCANWIKI - a wiki written in Objective CAML.
2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: restore.ml,v 1.7 2004/09/09 12:21:22 rich Exp $
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
30 open Cocanwiki_emailnotify
32 let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
33 let version = int_of_string (q#param "version") in
34 let page = q#param "page" in
36 if q#param_true "yes" then (
37 (* Get the IP address of the user, if available. *)
39 try `String (Connection.remote_ip (Request.connection r))
40 with Not_found -> `Null in
42 (* Copy the old version of the page to be live. *)
43 let sth = dbh#prepare_cached "select title, description, creation_date,
47 and url_deleted = ? and id = ?" in
48 sth#execute [`Int hostid; `String page; `Int version];
50 let title, description, creation_date, redirect, css =
51 match sth#fetch1 () with
52 [ title; description; creation_date; redirect; css ] ->
53 title, description, creation_date, redirect, css
54 | _ -> assert false in
58 "set constraints pages_redirect_cn, sitemenu_url_cn deferred" in
61 let sth = dbh#prepare_cached "update pages set url_deleted = url,
63 where hostid = ? and url = ?" in
64 sth#execute [`Int hostid; `String page];
66 let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
67 description, creation_date, logged_ip,
69 values (?, ?, ?, ?, ?, ?, ?, ?)" in
70 sth#execute [`Int hostid; `String page; title; description;
71 creation_date; logged_ip; redirect; css ];
73 let pageid = sth#serial "pages_id_seq" in
75 let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
76 sectionname, content, divname)
77 select ? as pageid, ordering, sectionname,
81 sth#execute [`Int pageid; `Int version];
86 let subject = "Page " ^ page ^ " has been restored." in
88 (* Prepare the diff between this version and the previous version. *)
89 let diff, _ = get_diff dbh hostid page ~version:pageid () in
90 "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
93 email_notify ~body ~subject dbh hostid;
96 let buttons = [ ok_button ("/" ^ page) ] in
97 ok ~title:"Restored" ~buttons
98 q "The old page was restored successfully."
100 q#redirect ("http://" ^ hostname ^ "/" ^ page)
103 register_script ~restrict:[CanEdit] run