(* COCANWIKI scripts. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: create.ml,v 1.2 2004/09/07 13:40:10 rich Exp $ *) open Apache open Registry open Cgi open Printf open ExtString open Cocanwiki open Cocanwiki_emailnotify open Cocanwiki_ok let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ = (* Get the page title. *) let title = q#param "title" in let url = match Wikilib.generate_url_of_title dbh hostid title with Wikilib.GenURL_OK url -> url | Wikilib.GenURL_TooShort | Wikilib.GenURL_BadURL -> error ~back_button:true ~title:"Bad page name" q "The page name supplied is too short or invalid."; raise CgiExit | Wikilib.GenURL_Duplicate url -> q#redirect ("http://" ^ hostname ^ "/" ^ url); raise CgiExit in (* Description field must contain something. *) let description = q#param "description" in if description = "" then ( error ~back_button:true ~title:"Description field missing" q "You must write a brief description for search engines and directories."; raise CgiExit ); (* Get the IP address of the user, if available. *) let logged_ip = try `String (Connection.remote_ip (Request.connection r)) with Not_found -> `Null in (* Create the page. *) let sth = dbh#prepare_cached "insert into pages (hostid, url, title, description, logged_ip) values (?, ?, ?, ?, ?)" in sth#execute [`Int hostid; `String url; `String title; `String description; logged_ip]; let pageid = sth#serial "pages_id_seq" in (* Create a single section. *) let sectionname = "Section title - change this" in let content = "Write some content here." in let sth = dbh#prepare_cached "insert into contents (pageid, ordering, sectionname, content) values (?, 1, ?, ?)" in sth#execute [`Int pageid; `String sectionname; `String content]; (* Commit. *) dbh#commit (); (* Email notification, if anyone is listed for this host. *) let subject = "Page " ^ url ^ " has been created" in let body = fun () -> "Page: http://" ^ hostname ^ "/" ^ url ^ "\n" in email_notify ~subject ~body dbh hostid; (* Redirect to the editing page. *) q#redirect ("http://" ^ hostname ^ "/" ^ url ^ "/edit") let () = register_script run