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: duplicate_page.ml,v 1.1 2006/12/11 16:59:29 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_strings
31 open Cocanwiki_emailnotify
33 let run r (q : cgi) dbh hostid {hostname = hostname} user =
34 let page = q#param "page" in
37 if q#param_true "cancel" then
38 q#redirect ("http://" ^ hostname ^ "/" ^ page);
40 let new_title = trim (q#param "new_title") in
42 (* New title mustn't be empty string. *)
43 if new_title = "" then (
44 error ~back_button:true ~title:"Empty title not allowed"
45 r dbh hostid q "The new title cannot be empty.";
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"
55 (* Generate URL for the new title. *)
57 if page = "index" then page
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
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.");
69 if page = new_page then (
70 (* This is an error - the new page must have a new URL. *)
71 error ~back_button:true ~title:"Bad title"
72 r dbh hostid q "The new title must be different from the old title in a significant way (eg. not just a change of capitalisation).";
76 (* Create the new page from the old one. *)
77 let old_model = load_page dbh hostid ~url:page () in
78 let new_model = new_page_with_title new_title in
79 let new_model = { new_model with
80 description = old_model.description;
81 keywords = old_model.keywords;
82 noodp = old_model.noodp;
83 contents_ = old_model.contents_ } in
85 ignore (save_page r dbh hostid ~user new_model)
88 error ~title:"Page exists"
90 "Another page with the same title exists.";
97 (* Email notification. *)
98 let subject = "Page " ^ page ^ " has been duplicated" in
101 "Old title: " ^ old_title ^ "\n" ^
102 "New title: " ^ new_title in
104 email_notify ~body ~subject ~user dbh hostid;
106 let buttons = [ ok_button ("/" ^ new_page) ] in
107 ok ~title:"Page duplicated." ~buttons
108 r dbh hostid q "That page was duplicated."
111 register_script ~restrict:[CanEdit] run