2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: create.ml,v 1.3 2004/09/08 09:54:28 rich Exp $
15 open Cocanwiki_emailnotify
18 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
19 (* Get the page title. *)
20 let title = q#param "title" in
23 match Wikilib.generate_url_of_title dbh hostid title with
24 Wikilib.GenURL_OK url -> url
25 | Wikilib.GenURL_TooShort | Wikilib.GenURL_BadURL ->
26 error ~back_button:true ~title:"Bad page name"
27 q "The page name supplied is too short or invalid.";
29 | Wikilib.GenURL_Duplicate url ->
30 q#redirect ("http://" ^ hostname ^ "/" ^ url);
33 (* Description field must contain something. *)
34 let description = q#param "description" in
35 if description = "" then (
36 error ~back_button:true ~title:"Description field missing"
37 q "You must write a brief description for search engines and
42 (* Get the IP address of the user, if available. *)
44 try `String (Connection.remote_ip (Request.connection r))
45 with Not_found -> `Null in
47 (* Create the page. *)
48 let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
49 description, logged_ip)
50 values (?, ?, ?, ?, ?)" in
51 sth#execute [`Int hostid; `String url; `String title; `String description;
54 let pageid = sth#serial "pages_id_seq" in
56 (* Create a single section. *)
57 let sectionname = "Section title - change this" in
58 let content = "Write some content here." in
60 let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
61 sectionname, content) values (?, 1, ?, ?)" in
62 sth#execute [`Int pageid; `String sectionname; `String content];
67 (* Email notification, if anyone is listed for this host. *)
68 let subject = "Page " ^ url ^ " has been created" in
70 "Page: http://" ^ hostname ^ "/" ^ url ^ "\n" in
72 email_notify ~subject ~body dbh hostid;
74 (* Redirect to the editing page. *)
75 q#redirect ("http://" ^ hostname ^ "/" ^ url ^ "/edit")
78 register_script ~restrict:[CanEdit] run