(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: edit_page_title.ml,v 1.1 2004/09/25 16:05:03 rich Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Apache open Registry open Cgi open Printf open Cocanwiki open Cocanwiki_ok open Cocanwiki_emailnotify open Cocanwiki_strings let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user = let page = q#param "page" in let new_title = trim (q#param "title") in (* Get the old title. *) let sth = dbh#prepare_cached "select title from pages where hostid = ? and url = ?" in sth#execute [`Int hostid; `String page]; let old_title = sth#fetch1string () in (* New title mustn't be empty string. *) if new_title = "" then ( error ~back_button:true ~title:"Empty title not allowed" q "The new title cannot be empty."; return () ); (* If it's the index page, then they can change the title however * they want. On other pages, however, you are only allowed limited * edits which preserve the title -> url mapping. *) if page <> "index" then ( let new_page = match Wikilib.generate_url_of_title dbh hostid new_title with Wikilib.GenURL_OK url -> url | Wikilib.GenURL_TooShort | Wikilib.GenURL_BadURL -> "" | Wikilib.GenURL_Duplicate url -> url in if new_page <> page then ( error ~back_button:true ~title:"Title altered too much" q ("The new title is too different from the old one. You can only " ^ "make limited changes to the title of a page. The manual " ^ "describes why, and how to copy pages instead."); return () ) ); (* Get the IP address of the user, if available. *) let logged_ip = try `String (Connection.remote_ip (Request.connection r)) with Not_found -> `Null in let logged_user = match user with | User (id, _, _) -> `Int id | _ -> `Null in (* Changing the title creates a new version of the page. This enables * us to revert changes. *) let sth = dbh#prepare_cached "select id, description, creation_date, redirect, css from pages where hostid = ? and url = ?" in sth#execute [`Int hostid; `String page]; let oldpageid, description, creation_date, redirect, css = match sth#fetch1 () with [ `Int id; description; creation_date; redirect; css ] -> id, description, creation_date, redirect, css | _ -> assert false in let sth = dbh#prepare_cached "set constraints pages_redirect_cn, sitemenu_url_cn, page_emails_url_cn deferred" in sth#execute []; let sth = dbh#prepare_cached "update pages set url_deleted = url, url = null where hostid = ? and id = ?" in sth#execute [`Int hostid; `Int oldpageid]; let sth = dbh#prepare_cached "insert into pages (hostid, url, title, description, creation_date, logged_ip, logged_user, redirect, css) values (?, ?, ?, ?, ?, ?, ?, ?, ?)" in sth#execute [`Int hostid; `String page; `String new_title; description; creation_date; logged_ip; logged_user; redirect; css ]; let pageid = sth#serial "pages_id_seq" in let sth = dbh#prepare_cached "insert into contents (pageid, ordering, sectionname, content, divname) select ? as pageid, ordering, sectionname, content, divname from contents where pageid = ?" in sth#execute [`Int pageid; `Int oldpageid]; dbh#commit (); (* Email notification. *) let subject = "Title of page " ^ page ^ " has been modified" in let body = fun () -> "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^ "Old title: " ^ old_title ^ "\n" ^ "New title: " ^ new_title ^ "\n" in email_notify ~subject ~body dbh hostid; let buttons = [ ok_button ("/" ^ page) ] in ok ~title:"Title changed" ~buttons q "The page title was changed successfully." let () = register_script ~restrict:[CanEdit] run