Edit page title.
[cocanwiki.git] / scripts / edit.ml
index 47ab1fc..cede68b 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: edit.ml,v 1.8 2004/09/09 12:21:22 rich Exp $
+ * $Id: edit.ml,v 1.12 2004/09/24 15:53: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
@@ -45,9 +45,10 @@ type model_t = {
                                         * for each section. *)
 }
 
-let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
+let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
   let template = get_template dbh hostid "edit.html" in
   let template_conflict = get_template dbh hostid "edit_conflict.html" in
+  let template_email = get_template dbh hostid "edit_page_email.txt" in
 
   (* Workaround bugs in IE, specifically lack of support for <button>
    * elements.
@@ -118,12 +119,16 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
                   "(ie. not to a page which is itself a redirect).")
     );
 
-    (* All sections have sectionnames? *)
-    List.iter (function (sectionnames, _, _)
-                  when string_is_whitespace sectionnames ->
-                    add_error ("Every section must have a title.");
-                | _ -> ())
-      model.contents;
+    (* All sections after the first one have sectionnames?  The first
+     * section ONLY is allowed to have an empty title.
+     *)
+    if model.contents <> [] then
+      List.iter (function (sectionnames, _, _)
+                    when string_is_whitespace sectionnames ->
+                      add_error
+                      ("Every section except the first must have a title.");
+                  | _ -> ())
+       (List.tl model.contents);
 
     get_errors ()
   in
@@ -247,7 +252,8 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
        | _ -> assert false in
 
     (* Get the sections. *)
-    let sth = dbh#prepare_cached "select sectionname, content,
+    let sth = dbh#prepare_cached "select coalesce (sectionname, ''),
+                                         content,
                                          coalesce (divname, '')
                                     from contents
                                    where pageid = ?
@@ -377,7 +383,7 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
        template_conflict#set "url" url;
 
        q#template template_conflict;
-       raise CgiExit
+       return ()
       );
 
       (* Defer the pages_redirect_cn constraint because that would
@@ -385,7 +391,8 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
        *)
       let sth =
        dbh#prepare_cached
-         "set constraints pages_redirect_cn, sitemenu_url_cn deferred" in
+         "set constraints pages_redirect_cn, sitemenu_url_cn,
+               page_emails_url_cn deferred" in
       sth#execute [];
 
       (* Mark the old page as deleted.  NB. There is a small race
@@ -404,6 +411,11 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
        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
+
       (* Get redirect. *)
       let redirect = if model.redirect = "" then `Null
                      else `String model.redirect in
@@ -411,11 +423,11 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
       (* Create the new page. *)
       let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
                                       description, creation_date, logged_ip,
-                                      redirect, css)
-                                    values (?, ?, ?, ?, ?, ?, ?, ?)" in
+                                      logged_user, redirect, css)
+                                    values (?, ?, ?, ?, ?, ?, ?, ?, ?)" in
       sth#execute [`Int hostid; `String url; `String title;
                   `String model.description; creation_date; logged_ip;
-                  redirect; css];
+                  logged_user; redirect; css];
 
       (* New page ID <> old page ID model.id. *)
       let pageid = sth#serial "pages_id_seq" in
@@ -429,9 +441,12 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
                   let divname =
                     if string_is_whitespace divname then `Null
                     else `String divname in
+                  let sectionname =
+                    if string_is_whitespace sectionname then `Null
+                    else `String sectionname in
                   incr ordering; let ordering = !ordering in
                   sth#execute [`Int pageid; `Int ordering;
-                               `String sectionname; divname;
+                               sectionname; divname;
                                `String content])
        model.contents;
 
@@ -449,6 +464,47 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
 
       email_notify ~body ~subject dbh hostid;
 
+      (* General email notification of page edits.  Send an email to
+       * anyone in the page_emails table who has a confirmed address
+       * and who hasn't received an email already today.
+       *)
+      let sth = dbh#prepare_cached "select email, opt_out from page_emails
+                                     where hostid = ? and url = ?
+                                       and pending is null
+                                       and last_sent < current_date" in
+      sth#execute [`Int hostid; `String url];
+
+      let addrs = sth#map (function [`String email; `String opt_out] ->
+                            email, opt_out
+                            | _ -> assert false) in
+
+      if addrs <> [] then (
+       (* Construct the email. *)
+       template_email#set "hostname" hostname;
+       template_email#set "page" url;
+
+       let subject =
+         "Site notice: " ^ hostname ^ "/" ^ url ^ " has been updated" in
+
+       (* Send each email individually (they all have different opt out
+        * links).
+        *)
+       List.iter (fun (to_addr, opt_out) ->
+                    template_email#set "opt_out" opt_out;
+                    let body = template_email#to_string in
+                    Sendmail.send_mail ~subject ~to_addr:[to_addr] ~body ())
+         addrs
+      );
+
+      (* Update the database to record when these emails were sent. *)
+      let sth = dbh#prepare_cached "update page_emails
+                                       set last_sent = current_date
+                                     where hostid = ? and url = ?
+                                       and pending is null" in
+      sth#execute [`Int hostid; `String url];
+
+      dbh#commit ();
+
       let buttons = [ ok_button ("/" ^ url) ] in
       ok ~title:"Saved" ~buttons
         q "The page was saved."
@@ -479,11 +535,11 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
      let id = int_of_string (q#param "id") in
      if q#param_true "cancel" then (
        cancel id;
-       raise CgiExit
+       return ()
      );
      if q#param_true "save" then (
        let ok = try_save () in
-       if ok then raise CgiExit                (* ... else fall through *)
+       if ok then return ()            (* ... else fall through *)
      );
      continue_editing ()               (* Processes the action, if any. *)
    with