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