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: create.ml,v 1.5 2004/09/09 12:21:22 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_emailnotify
33 let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
34 (* Get the page title. *)
35 let title = q#param "title" in
38 match Wikilib.generate_url_of_title dbh hostid title with
39 Wikilib.GenURL_OK url -> url
40 | Wikilib.GenURL_TooShort | Wikilib.GenURL_BadURL ->
41 error ~back_button:true ~title:"Bad page name"
42 q "The page name supplied is too short or invalid.";
44 | Wikilib.GenURL_Duplicate url ->
45 q#redirect ("http://" ^ hostname ^ "/" ^ url);
48 (* Description field must contain something. *)
49 let description = q#param "description" in
50 if description = "" then (
51 error ~back_button:true ~title:"Description field missing"
52 q "You must write a brief description for search engines and
57 (* Get the IP address of the user, if available. *)
59 try `String (Connection.remote_ip (Request.connection r))
60 with Not_found -> `Null in
62 (* Create the page. *)
63 let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
64 description, logged_ip)
65 values (?, ?, ?, ?, ?)" in
66 sth#execute [`Int hostid; `String url; `String title; `String description;
69 let pageid = sth#serial "pages_id_seq" in
71 (* Create a single section. *)
72 let sectionname = "Section title - change this" in
73 let content = "Write some content here." in
75 let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
76 sectionname, content) values (?, 1, ?, ?)" in
77 sth#execute [`Int pageid; `String sectionname; `String content];
82 (* Email notification, if anyone is listed for this host. *)
83 let subject = "Page " ^ url ^ " has been created" in
85 "Page: http://" ^ hostname ^ "/" ^ url ^ "\n" in
87 email_notify ~subject ~body dbh hostid;
89 (* Redirect to the editing page. *)
90 q#redirect ("http://" ^ hostname ^ "/" ^ url ^ "/edit")
93 register_script ~restrict:[CanEdit] run