From: rich Date: Mon, 22 Nov 2004 11:07:32 +0000 (+0000) Subject: Allow pages to be renamed fully. If the different title would cause X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=0d913c7fee349ac0d46a9f9ce31977b2e5c545af;p=cocanwiki.git Allow pages to be renamed fully. If the different title would cause a change in URL, then we create a new page and make the old page into a redirect to the new page. However there is a problem that this doesn't work properly if you rename a page then rename it back to the original - the code would need to be smarter to work out what to do in this situation. 404 page no longer uses the user-defined stylesheet (fixes a problem on the Merjis site, for instance). --- diff --git a/conf/cocanwiki.conf b/conf/cocanwiki.conf index d029e30..04961c7 100644 --- a/conf/cocanwiki.conf +++ b/conf/cocanwiki.conf @@ -1,5 +1,5 @@ # Apache configuration for COCANWIKI. -# $Id: cocanwiki.conf,v 1.20 2004/11/04 22:18:47 rich Exp $ +# $Id: cocanwiki.conf,v 1.21 2004/11/22 11:07:32 rich Exp $ # Uncomment the following lines if necessary. You will probably need # to adjust the paths to reflect where cocanwiki is really installed. @@ -86,11 +86,11 @@ RewriteRule ^/_dist/ / [R] RewriteRule ^/([^_].*)/diff$ /_bin/diff.cmo?page=$1 [PT,L,QSA] RewriteRule ^/([^_].*)/edit$ /_bin/edit.cmo?page=$1 [PT,L,QSA] RewriteRule ^/([^_].*)/editcss$ /_bin/edit_page_css_form.cmo?page=$1 [PT,L,QSA] -RewriteRule ^/([^_].*)/edittitle$ /_bin/edit_page_title_form.cmo?page=$1 [PT,L,QSA] RewriteRule ^/([^_].*)/history$ /_bin/history.cmo?page=$1 [PT,L,QSA] RewriteRule ^/([^_].*)/history.rss$ /_bin/history_rss.cmo?page=$1 [PT,L,QSA] RewriteRule ^/([^_].*)/index.rss$ /_bin/page_rss.cmo?page=$1 [PT,L,QSA] RewriteRule ^/([^_].*)/links$ /_bin/links.cmo?page=$1 [PT,L,QSA] +RewriteRule ^/([^_].*)/rename$ /_bin/rename_page_form.cmo?page=$1 [PT,L,QSA] RewriteRule ^/([^_].*)/source$ /_bin/source.cmo?page=$1 [PT,L,QSA] RewriteRule ^/([^_].*)/stats$ /_bin/stats.cmo?page=$1 [PT,L,QSA] RewriteRule ^/([^_].*)/styles.css$ /_bin/pagestyle.cmo?page=$1 [PT,L,QSA] diff --git a/scripts/edit_page_title.ml b/scripts/edit_page_title.ml deleted file mode 100644 index 708e899..0000000 --- a/scripts/edit_page_title.ml +++ /dev/null @@ -1,140 +0,0 @@ -(* 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.6 2004/10/30 10:16:10 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, links_from_cn, recently_visited_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 ~user 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 diff --git a/scripts/lib/cocanwiki_pages.ml b/scripts/lib/cocanwiki_pages.ml index f6abd61..24e2291 100644 --- a/scripts/lib/cocanwiki_pages.ml +++ b/scripts/lib/cocanwiki_pages.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: cocanwiki_pages.ml,v 1.2 2004/10/30 10:16:10 rich Exp $ + * $Id: cocanwiki_pages.ml,v 1.3 2004/11/22 11:07:32 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 @@ -194,6 +194,12 @@ let save_page (dbh : Dbi.connection) hostid ?user ?r model = creation_date, url, title, css | _ -> assert false in + (* Title changed? *) + let title = + match model.pt with + Title new_title when title <> new_title -> new_title + | _ -> title in + (* Has someone else edited this page in the meantime? *) let sth = dbh#prepare_cached "select max(id) from pages where hostid = ? and url = ?" in diff --git a/scripts/page.ml b/scripts/page.ml index b82c7cc..325a8da 100644 --- a/scripts/page.ml +++ b/scripts/page.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: page.ml,v 1.39 2004/11/05 11:35:53 rich Exp $ + * $Id: page.ml,v 1.40 2004/11/22 11:07:32 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 @@ -401,7 +401,6 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid t#set "search_terms" search_terms; - t#conditional "has_host_css" has_host_css; t#conditional "can_edit" can_edit; t#conditional "can_manage_users" can_manage_users; t#conditional "has_stats" has_stats; diff --git a/scripts/rename_page.ml b/scripts/rename_page.ml new file mode 100644 index 0000000..8de584f --- /dev/null +++ b/scripts/rename_page.ml @@ -0,0 +1,107 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: rename_page.ml,v 1.1 2004/11/22 11:07:32 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_pages +open Cocanwiki_strings + +let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user = + let page = q#param "page" in + + (* Cancelled? *) + if q#param_true "cancel" then ( + q#redirect ("http://" ^ hostname ^ "/" ^ page); + return () + ); + + let new_title = trim (q#param "new_title") 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 () + ); + + (* 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 + + (* Generate URL for the new title. *) + let new_page = + if page = "index" then page + else + match Wikilib.generate_url_of_title dbh hostid new_title with + | Wikilib.GenURL_OK url | Wikilib.GenURL_Duplicate url -> url + | Wikilib.GenURL_TooShort | Wikilib.GenURL_BadURL -> + error ~title:"Bad title" ~back_button:true + q ("The new title for the page isn't valid. " ^ + "It may be too short or it may not contain " ^ + "enough alphabet letters."); + return () in + + if page = new_page then ( + (* If it's the same as the old URL, then this is a simple title change. *) + let model = load_page dbh hostid ~url:page () in + let model = { model with pt = Title new_title } in + let url, _ = save_page dbh hostid ~user ~r model in + assert (url = new_page) + ) else ( + (* Not the same as the old URL, so set the old page to a redirect and + * create a new page. + *) + let old_model = load_page dbh hostid ~url:page () in + let new_model = new_page_with_title new_title in + let new_model = { new_model with description = old_model.description; + contents = old_model.contents } in + let old_model = { old_model with redirect = new_page } in + save_page dbh hostid ~user ~r old_model; + + try + ignore (save_page dbh hostid ~user ~r new_model) + with + SaveURLError -> + error ~title:"Page exists" + q ("Another page with the same title exists. " ^ + "If you tried to rename a page, then rename it back to the " ^ + "original title, then you may see this error. This is a bug " ^ + "which you should raise with the site administrator."); + return () + ); + + (* Finish off XXX *) + dbh#commit (); + + let buttons = [ ok_button ("/" ^ new_page) ] in + ok ~title:"Page renamed" ~buttons + q "That page was renamed." + +let () = + register_script ~restrict:[CanEdit] run diff --git a/scripts/edit_page_title_form.ml b/scripts/rename_page_form.ml similarity index 87% rename from scripts/edit_page_title_form.ml rename to scripts/rename_page_form.ml index d968901..32d28dc 100644 --- a/scripts/edit_page_title_form.ml +++ b/scripts/rename_page_form.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: edit_page_title_form.ml,v 1.1 2004/09/25 16:05:03 rich Exp $ + * $Id: rename_page_form.ml,v 1.1 2004/11/22 11:07:32 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 @@ -28,7 +28,7 @@ open Cocanwiki open Cocanwiki_template let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ = - let template = get_template dbh hostid "edit_page_title_form.html" in + let template = get_template dbh hostid "rename_page_form.html" in let page = q#param "page" in @@ -39,7 +39,6 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ = let title = sth#fetch1string () in template#set "page" page; - template#conditional "limited_changes" (page <> "index"); template#set "title" title; q#template template diff --git a/templates/page.html b/templates/page.html index faa2980..7f1a340 100644 --- a/templates/page.html +++ b/templates/page.html @@ -55,7 +55,7 @@
  • Versions of this page
  • What links here?
  • Edit stylesheet
  • -
  • Rename page
  • +
  • Rename page
  • New page
  • Images
  • Files
  • diff --git a/templates/page_404.html b/templates/page_404.html index a1673a2..d6dda8d 100644 --- a/templates/page_404.html +++ b/templates/page_404.html @@ -6,7 +6,6 @@ -::if(has_host_css)::::end::

    Page not found

    diff --git a/templates/edit_page_title_form.html b/templates/rename_page_form.html similarity index 60% rename from templates/edit_page_title_form.html rename to templates/rename_page_form.html index 95127a2..6148556 100644 --- a/templates/edit_page_title_form.html +++ b/templates/rename_page_form.html @@ -1,15 +1,15 @@ -Edit page title +Rename page -

    Edit page title

    +

    Rename page

    -
    + @@ -18,24 +18,16 @@ - + -::if(limited_changes):: - + -::end:: - - - - -
    New title:
    Warning: -You can only make limited changes to this title, such as -changing lowercase to Uppercase. -Why is this? + +