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.10 2004/09/28 10:56:39 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
31 open Cocanwiki_strings
33 let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
34 let page = q#param "page" in
35 let css = q#param "css" in
37 let css = if string_is_whitespace css then `Null else `String css in
39 (* Get the IP address of the user, if available. *)
41 try `String (Connection.remote_ip (Request.connection r))
42 with Not_found -> `Null in
46 | User (id, _, _) -> `Int id
49 (* Changing the CSS creates a new version of the page. This enables
50 * us to revert changes to the CSS easily.
52 let sth = dbh#prepare_cached "select id, title, description, creation_date,
55 where hostid = ? and url = ?" in
56 sth#execute [`Int hostid; `String page];
58 let oldpageid, title, description, creation_date, redirect =
59 match sth#fetch1 () with
60 [ `Int id; title; description; creation_date; redirect ] ->
61 id, title, description, creation_date, redirect
62 | _ -> assert false in
64 let sth = dbh#prepare_cached
65 "set constraints pages_redirect_cn, sitemenu_url_cn,
66 page_emails_url_cn, links_from_cn, links_to_cn deferred" in
69 let sth = dbh#prepare_cached "update pages set url_deleted = url,
71 where hostid = ? and id = ?" in
72 sth#execute [`Int hostid; `Int oldpageid];
74 let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
75 description, creation_date, logged_ip,
76 logged_user, redirect, css)
77 values (?, ?, ?, ?, ?, ?, ?, ?, ?)" in
78 sth#execute [`Int hostid; `String page; title; description;
79 creation_date; logged_ip; logged_user; redirect; css ];
81 let pageid = sth#serial "pages_id_seq" in
83 let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
84 sectionname, content, divname)
85 select ? as pageid, ordering, sectionname,
89 sth#execute [`Int pageid; `Int oldpageid];
93 (* Email notification. *)
94 let subject = "CSS for page " ^ page ^ " has been modified" in
97 get_diff dbh hostid page ~version:pageid ~old_version:oldpageid () in
98 "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
101 email_notify ~subject ~body dbh hostid;
103 let buttons = [ ok_button ("/" ^ page);
104 { StdPages.label = "Edit stylesheet again";
105 StdPages.link = "/_bin/edit_page_css_form.cmo";
106 StdPages.method_ = None;
107 StdPages.params = [ "page", page ] } ] in
108 ok ~title:"Stylesheet changed" ~buttons
109 q ("The stylesheet was changed successfully. " ^
110 "Note: You must RELOAD the page to see changes to stylesheets.")
113 register_script ~restrict:[CanEdit] run