Moved to merjis/tools/wiki.
[cocanwiki.git] / scripts / edit_page_css.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: edit_page_css.ml,v 1.1 2004/09/07 10:14:09 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Merjisforwiki
13
14 open Cocanwiki
15 open Cocanwiki_ok
16 open Cocanwiki_diff
17 open Cocanwiki_emailnotify
18
19 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname) _ =
20   let page = q#param "page" in
21   let css = q#param "css" in
22
23   let css = if string_is_whitespace css then `Null else `String css in
24
25   (* Get the IP address of the user, if available. *)
26   let logged_ip =
27     try `String (Connection.remote_ip (Request.connection r))
28     with Not_found -> `Null in
29
30   (* Changing the CSS creates a new version of the page.  This enables
31    * us to revert changes to the CSS easily.
32    *)
33   let sth = dbh#prepare_cached "select id, title, description, creation_date,
34                                        redirect
35                                   from pages
36                                  where hostid = ? and url = ?" in
37   sth#execute [`Int hostid; `String page];
38
39   let oldpageid, title, description, creation_date, redirect =
40     match sth#fetch1 () with
41         [ `Int id; title; description; creation_date; redirect ] ->
42           id, title, description, creation_date, redirect
43       | _ -> assert false in
44
45   let sth = dbh#prepare_cached "set constraints pages_redirect_cn deferred" in
46   sth#execute [];
47
48   let sth = dbh#prepare_cached "update pages set url_deleted = url,
49                                                  url = null
50                                  where hostid = ? and id = ?" in
51   sth#execute [`Int hostid; `Int oldpageid];
52
53   let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
54                                    description, creation_date, logged_ip,
55                                    redirect, css)
56                                 values (?, ?, ?, ?, ?, ?, ?, ?)" in
57   sth#execute [`Int hostid; `String page; title; description;
58                creation_date; logged_ip; redirect; css ];
59
60   let pageid = sth#serial "pages_id_seq" in
61
62   let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
63                                        sectionname, content, divname)
64                                 select ? as pageid, ordering, sectionname,
65                                             content, divname
66                                   from contents
67                                  where pageid = ?" in
68   sth#execute [`Int pageid; `Int oldpageid];
69
70   dbh#commit ();
71
72   (* Email notification. *)
73   let subject = "CSS for page " ^ page ^ " has been modified" in
74   let body = fun () ->
75     let diff, _ =
76       get_diff dbh hostid page ~version:pageid ~old_version:oldpageid () in
77     "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
78     diff in
79
80   email_notify ~subject ~body dbh hostid;
81
82   let buttons = [ ok_button ("/" ^ page);
83                   { StdPages.label = "Edit stylesheet again";
84                     StdPages.link = "/_bin/edit_page_css_form.cmo";
85                     StdPages.method_ = None;
86                     StdPages.params = [ "page", page ] } ] in
87   ok ~title:"Stylesheet changed" ~buttons
88     q ("The stylesheet was changed successfully.  " ^
89        "Note: You must RELOAD the page to see changes to stylesheets.")
90
91 let () =
92   register_script run