Asynchronous updates.
[cocanwiki.git] / scripts / rename_page.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: rename_page.ml,v 1.2 2004/11/22 11:11:52 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_pages
30 open Cocanwiki_strings
31 open Cocanwiki_emailnotify
32
33 let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
34   let page = q#param "page" in
35
36   (* Cancelled? *)
37   if q#param_true "cancel" then (
38     q#redirect ("http://" ^ hostname ^ "/" ^ page);
39     return ()
40   );
41
42   let new_title = trim (q#param "new_title") in
43
44   (* New title mustn't be empty string. *)
45   if new_title = "" then (
46     error ~back_button:true ~title:"Empty title not allowed"
47       q "The new title cannot be empty.";
48     return ()
49   );
50
51   (* Get the old title. *)
52   let sth = dbh#prepare_cached "select title from pages
53                                  where hostid = ? and url = ?" in
54   sth#execute [`Int hostid; `String page];
55
56   let old_title = sth#fetch1string () in
57
58   (* Generate URL for the new title. *)
59   let new_page =
60     if page = "index" then page
61     else
62       match Wikilib.generate_url_of_title dbh hostid new_title with
63         | Wikilib.GenURL_OK url | Wikilib.GenURL_Duplicate url -> url
64         | Wikilib.GenURL_TooShort | Wikilib.GenURL_BadURL ->
65             error ~title:"Bad title" ~back_button:true
66               q ("The new title for the page isn't valid.  " ^
67                  "It may be too short or it may not contain " ^
68                  "enough alphabet letters.");
69             return () in
70
71   if page = new_page then (
72     (* If it's the same as the old URL, then this is a simple title change. *)
73     let model = load_page dbh hostid ~url:page () in
74     let model = { model with pt = Title new_title } in
75     let url, _ = save_page dbh hostid ~user ~r model in
76     assert (url = new_page)
77   ) else (
78     (* Not the same as the old URL, so set the old page to a redirect and
79      * create a new page.
80      *)
81     let old_model = load_page dbh hostid ~url:page () in
82     let new_model = new_page_with_title new_title in
83     let new_model = { new_model with description = old_model.description;
84                         contents = old_model.contents } in
85     let old_model = { old_model with redirect = new_page } in
86     save_page dbh hostid ~user ~r old_model;
87
88     try
89       ignore (save_page dbh hostid ~user ~r new_model)
90     with
91         SaveURLError ->
92           error ~title:"Page exists"
93             q ("Another page with the same title exists.  " ^
94                "If you tried to rename a page, then rename it back to the " ^
95                "original title, then you may see this error.  This is a bug " ^
96                "which you should raise with the site administrator.");
97           return ()
98   );
99
100   (* Finish off XXX *)
101   dbh#commit ();
102
103   (* Email notification. *)
104   let subject = "Page " ^ page ^ " has been renamed" in
105
106   let body = fun () ->
107     "Old title: " ^ old_title ^ "\n" ^
108     "New title: " ^ new_title in
109
110   email_notify ~body ~subject ~user dbh hostid;
111
112   let buttons = [ ok_button ("/" ^ new_page) ] in
113   ok ~title:"Page renamed" ~buttons
114     q "That page was renamed."
115
116 let () =
117   register_script ~restrict:[CanEdit] run