(* 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.9 2004/09/20 10:56:47 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
* 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.
template_conflict#set "url" url;
q#template template_conflict;
- raise CgiExit
+ return ()
);
(* Defer the pages_redirect_cn constraint because that would
*)
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
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
(* 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
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."
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