1 (* COCANWIKI - a wiki written in Objective CAML.
2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: edit.ml,v 1.34 2006/08/04 12:20:06 rich Exp $
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
30 open Cocanwiki_template
32 open Cocanwiki_emailnotify
34 open Cocanwiki_strings
37 let run r (q : cgi) dbh hostid {hostname = hostname} user =
38 let template = get_template dbh hostid "edit.html" in
39 let template_conflict = get_template dbh hostid "edit_conflict.html" in
40 let template_email = get_template dbh hostid "edit_page_email.txt" in
42 (* Workaround bugs in IE, specifically lack of support for <button>
47 let ua = Table.get (Request.headers_in r) "User-Agent" in
48 ignore (String.find ua "MSIE"); (* Throws Invalid_string if not found. *)
51 Not_found | Invalid_string -> false in
52 template#conditional "msie" msie;
54 (* Build the internal model from the parameters passed to the script. *)
55 let build_internal_model () =
56 let id = Int32.of_string (q#param "id") in
57 let description = q#param "description" in
58 let redirect = q#param "redirect" in
60 if string_is_whitespace redirect then
61 None else Some redirect in
62 let pt = match q#param "pt_type" with
63 | "page" -> Page (q#param "pt_value")
64 | "title" -> Title (q#param "pt_value")
65 | _ -> failwith "unknown value for pt_type parameter" in
67 let contents = ref [] in
69 while q#param_exists ("content_" ^ string_of_int !i) do
70 let sectionname = q#param ("sectionname_" ^ string_of_int !i) in
72 if string_is_whitespace sectionname then None else Some sectionname in
73 let content = q#param ("content_" ^ string_of_int !i) in
74 let divname = q#param ("divname_" ^ string_of_int !i) in
76 if string_is_whitespace divname then None else Some divname in
77 let jsgo = q#param ("jsgo_" ^ string_of_int !i) in
78 let jsgo = if string_is_whitespace jsgo then None else Some jsgo in
79 contents := (sectionname, divname, jsgo, content) :: !contents;
82 let contents = List.rev !contents in
86 description = description;
88 contents_ = contents; }
91 (* Check for errors in the model. *)
92 let check_for_errors model =
93 let errors = ref [] in
94 let add_error msg = errors := msg :: !errors in
95 let get_errors () = List.rev !errors in
97 (match model.redirect with
100 if model.contents_ = [] then
101 add_error ("This page is empty. Use 'Insert new section here' " ^
102 "to write something!");
104 (* Description field? *)
105 if model.description = "" then
106 add_error ("The description field is very important! " ^
108 "used by search engines and directories to describe " ^
109 "what's on this page.");
112 (* Redirect points to a real page? *)
114 let model_id = model.id in
117 where hostid = $hostid and url is not null
118 and url = $redirect and id <> $model_id
119 and redirect is null" in
121 let ok = rows = [Some 1l] in
123 add_error ("Redirect must point to an ordinary page " ^
124 "(ie. not to a page which is itself a redirect).")
127 (* All sections after the first one have sectionnames? The first
128 * section ONLY is allowed to have an empty title.
130 if model.contents_ <> [] then
134 "Every section except the first must have a title.";
136 (List.tl model.contents_);
138 (* There are two constraints on any non-null jsgo's:
139 * (1) Must only be present if divname is non-null.
140 * (2) Must point to a valid URL on the current host.
144 | (_, None, Some _, _) ->
146 "Javascript onclick can only be used with a CSS id."
147 | (_, _, Some jsgo, _) ->
149 PGSQL(dbh) "select 1 from pages
150 where hostid = $hostid
153 and redirect is null" in
154 let ok = rows = [Some 1l] in
156 add_error ("Javascript onclick must point to an ordinary page " ^
157 "on the current site (ie. not to a redirect). " ^
158 "Do not put '/' at the beginning of the URL.")
165 (* Various "actions" that can be performed on the model. *)
166 let action_insert model posn item =
167 (* posn = 0 means insert before the first element of the current list. *)
172 | n, x :: xs -> x :: (loop (n-1, xs))
174 let contents = loop (posn, model.contents_) in
175 { model with contents_ = contents }
177 let action_moveup model posn =
178 (* posn = 1 means move up the first element, ie. do nothing
179 * posn = 2 means move up the second element to the first position
187 | 2, x :: y :: xs -> y :: x :: xs
188 | n, x :: xs -> x :: (loop (n-1, xs))
190 let contents = loop (posn, model.contents_) in
191 { model with contents_ = contents }
193 let action_movedn model posn =
194 (* posn = 1 means move down the first element to the second position
201 | 1, x :: y :: xs -> y :: x :: xs
202 | n, x :: xs -> x :: (loop (n-1, xs))
204 let contents = loop (posn, model.contents_) in
205 { model with contents_ = contents }
207 let action_delete model posn =
208 (* posn = 1 means delete the first element *)
214 | n, x :: xs -> x :: (loop (n-1, xs))
216 let contents = loop (posn, model.contents_) in
217 { model with contents_ = contents }
220 (* Convert model to template. *)
221 let model_to_template model template =
222 template#set "id" (Int32.to_string model.id);
223 template#set "description" model.description;
227 template#set "pt_type" "page";
228 template#set "pt_value" page
230 template#set "pt_type" "title";
231 template#set "pt_value" title);
233 (* Redirects table. *)
235 let model_id = model.id in
237 "select url, title from pages
238 where url is not null
240 and hostid = $hostid and id <> $model_id
242 let table = List.map (
244 let url = Option.get url in
245 let selected = model.redirect = Some url in
246 [ "url", Template.VarString url;
247 "title", Template.VarString title;
248 "selected", Template.VarConditional selected ]
250 template#table "redirects" table;
252 if model.id <> 0l then (
253 (* Need to go to the database to get the title of the page ... *)
255 let model_id = model.id in
257 "select title from pages
258 where hostid = $hostid and id = $model_id" in
259 let title = List.hd rows in
260 template#set "title" title;
263 | Page page -> template#set "title" page
264 | Title title -> template#set "title" title
267 let ordering = ref 0 in
270 (fun (sectionname, divname, jsgo, content) ->
271 incr ordering; let ordering = Int32.of_int !ordering in
272 let sectionname = match sectionname with None -> "" | Some s -> s in
273 let divname = match divname with None -> "" | Some s -> s in
274 let jsgo = match jsgo with None -> "" | Some s -> s in
275 [ "ordering", Template.VarString (Int32.to_string ordering);
276 "sectionname", Template.VarString sectionname;
277 "divname", Template.VarString divname;
278 "jsgo", Template.VarString jsgo;
279 "content", Template.VarString content ]) model.contents_ in
280 template#table "contents" table;
282 (* Check for errors and put those into the template. *)
283 let errors = check_for_errors model in
284 let errors = List.map (fun msg ->
285 [ "error", Template.VarString msg ]) errors in
286 template#table "errors" errors;
287 template#conditional "has_errors" (errors <> [])
290 (* Check if a URL exists in the database. *)
291 let page_exists page =
292 let rows = PGSQL(dbh)
293 "select 1 from pages where hostid = $hostid and url = $page" in
297 (* Begin editing a page, pulling the page out of the database and building
300 let begin_editing page =
301 let model = load_page dbh hostid ~url:page () in
302 model_to_template model template
305 (* Begin editing with a blank page, typically a template. *)
306 let begin_editing_new pt =
307 (* Just check the title. *)
311 match Wikilib.generate_url_of_title r dbh hostid title with
312 | Wikilib.GenURL_OK url -> ()
313 | Wikilib.GenURL_Duplicate url ->
314 q#redirect ("http://" ^ hostname ^ "/" ^ url)
315 | Wikilib.GenURL_TooShort | Wikilib.GenURL_BadURL ->
316 error ~back_button:true ~title:"Bad page name"
318 "The page name supplied is too short or invalid.";
322 let model = match pt with
323 | Page url -> new_page pt
324 | Title title -> new_page_with_title title in
326 model_to_template model template
329 let continue_editing () =
330 let model = ref (build_internal_model ()) in
332 (* An "action" parameter? *)
333 let is_action, get_action =
334 let actions = q#params in
335 (* Don't actually care about the value fields ... *)
336 let actions = List.map (fun (str, _) -> str) actions in
337 (* Some of our actions are imagemaps, so parameters like name.x, name.y
338 * need to be changed to name and have resulting duplicates removed.
341 List.filter (fun str ->
342 String.length str > 7 &&
343 String.sub str 0 7 = "action_" &&
344 not (String.ends_with str ".y")) actions in
347 if String.ends_with str ".x" then (
348 let str = String.sub str 0 (String.length str - 2) in
354 let action_type = String.sub str 7 6 in
356 String.sub str 14 (String.length str - 14) in
357 let action_value = int_of_string action_value in
358 action_type, action_value) actions in
360 let is_action typ = List.mem_assoc typ actions in
361 let get_value typ = List.assoc typ actions in
366 if is_action "insert" then (
367 let posn = get_action "insert" in
369 Some "The title of this section",
371 "Write something here." in
372 model := action_insert !model posn item
373 ) else if is_action "moveup" then (
374 let posn = get_action "moveup" in
375 model := action_moveup !model posn
376 ) else if is_action "movedn" then (
377 let posn = get_action "movedn" in
378 model := action_movedn !model posn
379 ) else if is_action "delete" then (
380 let posn = get_action "delete" in
381 model := action_delete !model posn
384 model_to_template !model template
387 (* Try to save the page. Only returns if there were errors in
391 let model = build_internal_model () in
392 let no_errors = [] = check_for_errors model in
394 (* No errors, so we can save the page ... *)
397 save_page r dbh hostid ~user model
400 error ~back_button:true ~title:"Page exists"
401 dbh hostid q ("While you were editing that page, it looks " ^
402 "like another user created the same page.");
405 | SaveConflict (new_version, old_version, url, css) ->
406 (* Edited by someone else ... Get the other's changes. *)
408 get_diff dbh hostid url ~old_version ~version:new_version () in
410 (* Synthesize our own changes. *)
411 let old_page = get_version_for_diff dbh old_version in
413 page_for_diff model css in
414 let our_diff = diff_cmd old_page new_page in
416 (* Fill out the conflict template. *)
417 template_conflict#set "other_diff" other_diff;
418 template_conflict#set "our_diff" our_diff;
419 template_conflict#set "old_version"
420 (Int32.to_string old_version);
421 template_conflict#set "new_version"
422 (Int32.to_string new_version);
423 template_conflict#set "url" url;
425 q#template template_conflict;
428 (* General email notification of page edits. Send an email to
429 * anyone in the page_emails table who has a confirmed address
430 * and who hasn't received an email already today.
432 let rows = PGSQL(dbh)
433 "select email, opt_out from page_emails
434 where hostid = $hostid and url = $url
436 and last_sent < current_date" in
437 let addrs = List.map (
438 fun (email, opt_out) ->
442 if addrs <> [] then (
443 (* Construct the email. *)
444 template_email#set "hostname" hostname;
445 template_email#set "page" url;
448 "Site notice: " ^ hostname ^ "/" ^ url ^ " has been updated" in
451 "text/plain", ["charset", Mimestring.mk_param "UTF-8"] in
453 (* Send each email individually (they all have different opt out
456 List.iter (fun (to_addr, opt_out) ->
457 template_email#set "opt_out" opt_out;
458 let body = template_email#to_string in
460 let msg = Netsendmail.compose ~to_addrs:["", to_addr]
461 ~subject ~content_type body in
462 Netsendmail.sendmail msg)
466 (* Update the database to record when these emails were sent. *)
469 set last_sent = current_date
470 where hostid = $hostid and url = $url
471 and pending is null";
473 (* Commit changes to the database. *)
476 (* Email notification, if anyone is listed for this host. *)
477 let subject = "Page " ^ url ^ " has been edited" in
480 (* Prepare the diff between this version and the previous version. *)
481 let diff, _ = get_diff dbh hostid url ~version:pageid () in
482 "Page: http://" ^ hostname ^ "/" ^ url ^ "\n\n" ^
485 email_notify ~body ~subject ~user dbh hostid;
487 (* Redirect back to the URL. *)
488 q#redirect ("http://" ^ hostname ^ "/" ^ url)
495 let rows = PGSQL(dbh)
496 "select coalesce (url, url_deleted)
498 where hostid = $hostid and id = $id" in
499 Option.get (List.hd rows)
500 ) else if q#param "pt_type" = "page" then
503 (* Create a new page, but the user hits the cancel button. Because
504 * we didn't save where they came from, we now have nowhere to
505 * go. So we redirect to the home page. XXX
509 q#redirect ("http://" ^ hostname ^ "/" ^ url)
512 (* This code decides where we are in the current editing cycle.
515 * id - if set, then we are in the midst of editing a page.
516 * save - if set, then we want to save the page.
517 * cancel - if set, abandon changes and go back to viewing the page.
518 * action_* - one of the action buttons was set, eg. move up/down.
519 * page - the page URL opened newly for editing, or a template which
521 * title - page doesn't yet exist; create it.
524 try Some (Int32.of_string (q#param "id")) with Not_found -> None in
526 | None -> (* Begin editing the page. *)
527 if q#param_exists "page" then (
528 let page = q#param "page" in
529 let page = if page = "" then "index" else page in
530 if page_exists page then
533 begin_editing_new (Page page)
535 let title = q#param "title" in
536 begin_editing_new (Title title)
540 if q#param_true "cancel" then
542 if q#param_true "save" then
543 try_save (); (* might fail and fall through ... *)
550 register_script ~restrict:[CanEdit] run