+csv dep for PG'OCaml.
[cocanwiki.git] / scripts / edit_page_css.ml
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: edit_page_css.ml,v 1.25 2006/12/06 09:46:57 rich Exp $
5  *
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.
10  *
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.
15  *
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.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_ok
29 open Cocanwiki_diff
30 open Cocanwiki_emailnotify
31 open Cocanwiki_strings
32
33 let run r (q : cgi) dbh hostid {hostname = hostname} user =
34   let page = q#param "page" in
35   let css = q#param "css" in
36
37   let css = if string_is_whitespace css then None else Some css in
38
39   (* Get the IP address of the user, if available. *)
40   let logged_ip =
41     try Some (Connection.remote_ip (Request.connection r))
42     with Not_found -> None in
43
44   let logged_user =
45     match user with
46       | User (id, _, _, _) -> Some id
47       | _ -> None in
48
49   (* Changing the CSS creates a new version of the page.  This enables
50    * us to revert changes to the CSS easily.
51    *)
52   let rows = PGSQL(dbh)
53     "select id, title, description, keywords, noodp, creation_date, redirect
54        from pages
55       where hostid = $hostid and url = $page" in
56
57   let oldpageid, title, description, keywords, noodp, creation_date, redirect =
58     match rows with
59     | [row] -> row
60     | _ -> assert false in
61
62   PGSQL(dbh)
63     "set constraints pages_redirect_cn, sitemenu_url_cn,
64        page_emails_url_cn, links_from_cn, recently_visited_url_cn
65      deferred";
66
67   PGSQL(dbh) "update pages set url_deleted = url, url = null
68                where hostid = $hostid and id = $oldpageid";
69
70   PGSQL(dbh) "insert into pages (hostid, url, title,
71                                  description, keywords, noodp,
72                                  creation_date, logged_ip,
73                                  logged_user, redirect, css)
74               values ($hostid, $page, $title, $description, $?keywords,
75                       $?noodp,
76                       $creation_date, $?logged_ip, $?logged_user,
77                       $?redirect, $?css)";
78
79   let pageid = PGOCaml.serial4 dbh "pages_id_seq" in
80
81   PGSQL(dbh) "insert into contents (pageid, ordering,
82                                     sectionname, content, divname, divclass,
83                                     jsgo)
84               select $pageid as pageid, ordering, sectionname,
85                      content, divname, divclass, jsgo
86                 from contents
87                where pageid = $oldpageid";
88
89   PGOCaml.commit dbh;
90
91   (* Email notification. *)
92   let subject = "CSS for page " ^ page ^ " has been modified" in
93   let body = fun () ->
94     let diff, _ =
95       get_diff dbh hostid page ~version:pageid ~old_version:oldpageid () in
96     "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
97     diff in
98
99   email_notify ~subject ~body ~user dbh hostid;
100
101   let buttons = [ ok_button ("/" ^ page);
102                   { Template.StdPages.label = "Edit stylesheet again";
103                     Template.StdPages.link = "/_bin/edit_page_css_form.cmo";
104                     Template.StdPages.method_ = None;
105                     Template.StdPages.params = [ "page", page ] } ] in
106   ok ~title:"Stylesheet changed" ~buttons
107     r dbh hostid q
108     ("The stylesheet was changed successfully.  " ^
109      "Note: You must RELOAD the page to see changes to stylesheets.")
110
111 let () =
112   register_script ~restrict:[CanEdit] run