Added {{shell}} for running external shell commands.
[cocanwiki.git] / scripts / rename_page.ml
index 8de584f..89accff 100644 (file)
@@ -1,7 +1,7 @@
 (* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: rename_page.ml,v 1.1 2004/11/22 11:07:32 rich Exp $
+ * $Id: rename_page.ml,v 1.10 2006/12/06 09:46:57 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,50 +28,49 @@ open Cocanwiki
 open Cocanwiki_ok
 open Cocanwiki_pages
 open Cocanwiki_strings
+open Cocanwiki_emailnotify
 
-let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
+let run r (q : cgi) dbh hostid {hostname = hostname} user =
   let page = q#param "page" in
 
   (* Cancelled? *)
-  if q#param_true "cancel" then (
+  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.";
+      r dbh hostid 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
+  let old_title = List.hd (
+    PGSQL (dbh) "select title from pages
+                  where hostid = $hostid and url = $page"
+  ) 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
+      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.");
+             r dbh hostid 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
+    let url, _ = save_page r dbh hostid ~user model in
     assert (url = new_page)
   ) else (
     (* Not the same as the old URL, so set the old page to a redirect and
@@ -79,29 +78,42 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
      *)
     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;
+    let new_model = { new_model with
+                       description = old_model.description;
+                       keywords = old_model.keywords;
+                       noodp = old_model.noodp;
+                       contents_ = old_model.contents_ } in
+    let old_model = { old_model with redirect = Some new_page } in
+    ignore (save_page r dbh hostid ~user old_model);
 
     try
-      ignore (save_page dbh hostid ~user ~r new_model)
+      ignore (save_page r dbh hostid ~user 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.");
+           r dbh hostid 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 ();
+  PGOCaml.commit dbh;
+
+  (* Email notification. *)
+  let subject = "Page " ^ page ^ " has been renamed" in
+
+  let body = fun () ->
+    "Old title: " ^ old_title ^ "\n" ^
+    "New title: " ^ new_title in
+
+  email_notify ~body ~subject ~user dbh hostid;
 
   let buttons = [ ok_button ("/" ^ new_page) ] in
   ok ~title:"Page renamed" ~buttons
-    q "That page was renamed."
+    r dbh hostid q "That page was renamed."
 
 let () =
   register_script ~restrict:[CanEdit] run