Support for users, roles, restrictions.
[cocanwiki.git] / scripts / create.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: create.ml,v 1.2 2004/09/07 13:40:10 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open ExtString
13
14 open Cocanwiki
15 open Cocanwiki_emailnotify
16 open Cocanwiki_ok
17
18 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
19   (* Get the page title. *)
20   let title = q#param "title" in
21
22   let url =
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.";
28           raise CgiExit
29       | Wikilib.GenURL_Duplicate url ->
30           q#redirect ("http://" ^ hostname ^ "/" ^ url);
31           raise CgiExit in
32
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
38          directories.";
39     raise CgiExit
40   );
41
42   (* Get the IP address of the user, if available. *)
43   let logged_ip =
44     try `String (Connection.remote_ip (Request.connection r))
45     with Not_found -> `Null in
46
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;
52                logged_ip];
53
54   let pageid = sth#serial "pages_id_seq" in
55
56   (* Create a single section. *)
57   let sectionname = "Section title - change this" in
58   let content = "Write some content here." in
59
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];
63
64   (* Commit. *)
65   dbh#commit ();
66
67   (* Email notification, if anyone is listed for this host. *)
68   let subject = "Page " ^ url ^ " has been created" in
69   let body = fun () ->
70     "Page: http://" ^ hostname ^ "/" ^ url ^ "\n" in
71
72   email_notify ~subject ~body dbh hostid;
73
74   (* Redirect to the editing page. *)
75   q#redirect ("http://" ^ hostname ^ "/" ^ url ^ "/edit")
76
77 let () =
78   register_script run