d30ff2b9a4acc082b5d8b1e7459bb3edfb77a757
[cocanwiki.git] / scripts / restore.ml
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.12 2004/10/07 11:36:46 rich Exp $
5  *
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.
10  *
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.
15  *
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.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_ok
29 open Cocanwiki_diff
30 open Cocanwiki_emailnotify
31
32 let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
33   let version = int_of_string (q#param "version") in
34   let page = q#param "page" in
35
36   if q#param_true "yes" then (
37     (* Get the IP address of the user, if available. *)
38     let logged_ip =
39       try `String (Connection.remote_ip (Request.connection r))
40       with Not_found -> `Null in
41
42     let logged_user =
43       match user with
44         | User (id, _, _) -> `Int id
45         | _ -> `Null in
46
47     (* Copy the old version of the page to be live. *)
48     let sth = dbh#prepare_cached "select title, description, creation_date,
49                                          redirect, css
50                                     from pages
51                                    where hostid = ?
52                                      and url_deleted = ? and id = ?" in
53     sth#execute [`Int hostid; `String page; `Int version];
54
55     let title, description, creation_date, redirect, css =
56       match sth#fetch1 () with
57           [ title; description; creation_date; redirect; css ] ->
58             title, description, creation_date, redirect, css
59         | _ -> assert false in
60
61     let sth =
62       dbh#prepare_cached
63         "set constraints pages_redirect_cn, sitemenu_url_cn,
64              page_emails_url_cn, links_from_cn deferred" in
65     sth#execute [];
66
67     let sth = dbh#prepare_cached "update pages set url_deleted = url,
68                                                    url = null
69                                    where hostid = ? and url = ?" in
70     sth#execute [`Int hostid; `String page];
71
72     let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
73                                      description, creation_date, logged_ip,
74                                      logged_user, redirect, css)
75                                   values (?, ?, ?, ?, ?, ?, ?, ?, ?)" in
76     sth#execute [`Int hostid; `String page; title; description;
77                  creation_date; logged_ip; logged_user; redirect; css ];
78
79     let pageid = sth#serial "pages_id_seq" in
80
81     let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
82                                          sectionname, content, divname)
83                                   select ? as pageid, ordering, sectionname,
84                                               content, divname
85                                     from contents
86                                    where pageid = ?" in
87     sth#execute [`Int pageid; `Int version];
88
89     (* Keep the links table in synch. *)
90     Cocanwiki_links.update_links_for_page dbh hostid page;
91
92     dbh#commit ();
93
94     (* Email notify. *)
95     let subject = "Page " ^ page ^ " has been restored." in
96     let body = fun () ->
97       (* Prepare the diff between this version and the previous version. *)
98       let diff, _ = get_diff dbh hostid page ~version:pageid () in
99       "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
100       diff in
101
102     email_notify ~body ~subject dbh hostid;
103
104     (* Done. *)
105     let buttons = [ ok_button ("/" ^ page) ] in
106     ok ~title:"Restored" ~buttons
107       q "The old page was restored successfully."
108   ) else
109     q#redirect ("http://" ^ hostname ^ "/" ^ page)
110
111 let () =
112   register_script ~restrict:[CanEdit] run