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: edit_page_title.ml,v 1.1 2004/09/25 16:05:03 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.
29 open Cocanwiki_emailnotify
30 open Cocanwiki_strings
32 let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
33 let page = q#param "page" in
34 let new_title = trim (q#param "title") in
36 (* Get the old title. *)
37 let sth = dbh#prepare_cached "select title from pages
38 where hostid = ? and url = ?" in
39 sth#execute [`Int hostid; `String page];
41 let old_title = sth#fetch1string () in
43 (* New title mustn't be empty string. *)
44 if new_title = "" then (
45 error ~back_button:true ~title:"Empty title not allowed"
46 q "The new title cannot be empty.";
50 (* If it's the index page, then they can change the title however
51 * they want. On other pages, however, you are only allowed limited
52 * edits which preserve the title -> url mapping.
54 if page <> "index" then (
56 match Wikilib.generate_url_of_title dbh hostid new_title with
57 Wikilib.GenURL_OK url -> url
58 | Wikilib.GenURL_TooShort
59 | Wikilib.GenURL_BadURL -> ""
60 | Wikilib.GenURL_Duplicate url -> url in
62 if new_page <> page then (
63 error ~back_button:true ~title:"Title altered too much"
64 q ("The new title is too different from the old one. You can only " ^
65 "make limited changes to the title of a page. The manual " ^
66 "describes why, and how to copy pages instead.");
71 (* Get the IP address of the user, if available. *)
73 try `String (Connection.remote_ip (Request.connection r))
74 with Not_found -> `Null in
78 | User (id, _, _) -> `Int id
81 (* Changing the title creates a new version of the page. This enables
82 * us to revert changes.
84 let sth = dbh#prepare_cached "select id, description, creation_date,
87 where hostid = ? and url = ?" in
88 sth#execute [`Int hostid; `String page];
90 let oldpageid, description, creation_date, redirect, css =
91 match sth#fetch1 () with
92 [ `Int id; description; creation_date; redirect; css ] ->
93 id, description, creation_date, redirect, css
94 | _ -> assert false in
96 let sth = dbh#prepare_cached
97 "set constraints pages_redirect_cn, sitemenu_url_cn,
98 page_emails_url_cn deferred" in
101 let sth = dbh#prepare_cached "update pages set url_deleted = url,
103 where hostid = ? and id = ?" in
104 sth#execute [`Int hostid; `Int oldpageid];
106 let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
107 description, creation_date, logged_ip,
108 logged_user, redirect, css)
109 values (?, ?, ?, ?, ?, ?, ?, ?, ?)" in
110 sth#execute [`Int hostid; `String page; `String new_title; description;
111 creation_date; logged_ip; logged_user; redirect; css ];
113 let pageid = sth#serial "pages_id_seq" in
115 let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
116 sectionname, content, divname)
117 select ? as pageid, ordering, sectionname,
121 sth#execute [`Int pageid; `Int oldpageid];
125 (* Email notification. *)
126 let subject = "Title of page " ^ page ^ " has been modified" in
128 "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
129 "Old title: " ^ old_title ^ "\n" ^
130 "New title: " ^ new_title ^ "\n" in
132 email_notify ~subject ~body dbh hostid;
134 let buttons = [ ok_button ("/" ^ page) ] in
135 ok ~title:"Title changed" ~buttons
136 q "The page title was changed successfully."
139 register_script ~restrict:[CanEdit] run