/_sitemap.rss for COCANWIKI.
[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.26 2006/08/17 09:11:31 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 hostid {hostname = hostname} user =
33   let version = Int32.of_string (q#param "version") in
34   let page = q#param "page" in
35
36   if not (q#param_true "no") then (
37     (* Get the IP address of the user, if available. *)
38     let logged_ip =
39       try Some (Connection.remote_ip (Request.connection r))
40       with Not_found -> None in
41
42     let logged_user =
43       match user with
44         | User (id, _, _, _) -> Some id
45         | _ -> None in
46
47     (* Copy the old version of the page to be live. *)
48     let rows = PGSQL(dbh)
49       "select title, description, keywords, noodp, creation_date,
50               redirect, css
51          from pages
52         where hostid = $hostid
53           and url_deleted = $page and id = $version" in
54
55     let title, description, keywords, noodp, creation_date, redirect, css =
56       match rows with
57       | [row] -> row
58       | _ -> assert false in
59
60     PGSQL(dbh)
61       "set constraints pages_redirect_cn, sitemenu_url_cn,
62            page_emails_url_cn, links_from_cn, recently_visited_url_cn
63        deferred";
64     PGSQL(dbh) "update pages set url_deleted = url, url = null
65                  where hostid = $hostid and url = $page";
66     PGSQL(dbh) "insert into pages (hostid, url, title,
67                                    description, keywords, noodp,
68                                    creation_date, logged_ip,
69                                    logged_user, redirect, css)
70                 values ($hostid, $page, $title, $description, $?keywords,
71                         $?noodp,
72                         $creation_date,
73                         $?logged_ip, $?logged_user, $?redirect, $?css)";
74
75     let pageid = PGOCaml.serial4 dbh "pages_id_seq" in
76
77     PGSQL(dbh) "insert into contents (pageid, ordering,
78                                       sectionname, content, divname, divclass,
79                                       jsgo)
80                 select $pageid, ordering, sectionname, content,
81                        divname, divclass, jsgo
82                   from contents
83                  where pageid = $version";
84
85     (* Keep the links table in synch. *)
86     Cocanwiki_links.update_links_for_page r dbh hostid page;
87
88     PGOCaml.commit dbh;
89
90     (* Email notify. *)
91     let subject = "Page " ^ page ^ " has been restored." in
92     let body = fun () ->
93       (* Prepare the diff between this version and the previous version. *)
94       let diff, _ = get_diff dbh hostid page ~version:pageid () in
95       "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
96       diff in
97
98     email_notify ~body ~subject ~user dbh hostid;
99
100     (* Done. *)
101     let buttons = [ ok_button ("/" ^ page) ] in
102     ok ~title:"Restored" ~buttons
103       dbh hostid q "The old page was restored successfully."
104   ) else
105     q#redirect ("http://" ^ hostname ^ "/" ^ page)
106
107 let () =
108   register_script ~restrict:[CanEdit] run