Script for rebuilding the links table from scratch.
[cocanwiki.git] / scripts / edit.ml
index 00d48ab..e8cc7c5 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.10 2004/09/21 13:01:15 rich Exp $
+ * $Id: edit.ml,v 1.13 2004/09/28 10:56:39 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
@@ -48,6 +48,7 @@ type model_t = {
 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.
@@ -382,7 +383,7 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
        template_conflict#set "url" url;
 
        q#template template_conflict;
-       raise CgiExit
+       return ()
       );
 
       (* Defer the pages_redirect_cn constraint because that would
@@ -390,7 +391,8 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
        *)
       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, links_from_cn, links_to_cn deferred" in
       sth#execute [];
 
       (* Mark the old page as deleted.  NB. There is a small race
@@ -462,6 +464,47 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
 
       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."
@@ -492,11 +535,11 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
      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