Don't send an empty file if the uploaded file is not given.
[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.7 2004/09/09 12:21:22 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 : Dbi.connection) hostid { hostname = hostname } _ =
34   let page = q#param "page" in
35   let css = q#param "css" in
36
37   let css = if string_is_whitespace css then `Null else `String css in
38
39   (* Get the IP address of the user, if available. *)
40   let logged_ip =
41     try `String (Connection.remote_ip (Request.connection r))
42     with Not_found -> `Null in
43
44   (* Changing the CSS creates a new version of the page.  This enables
45    * us to revert changes to the CSS easily.
46    *)
47   let sth = dbh#prepare_cached "select id, title, description, creation_date,
48                                        redirect
49                                   from pages
50                                  where hostid = ? and url = ?" in
51   sth#execute [`Int hostid; `String page];
52
53   let oldpageid, title, description, creation_date, redirect =
54     match sth#fetch1 () with
55         [ `Int id; title; description; creation_date; redirect ] ->
56           id, title, description, creation_date, redirect
57       | _ -> assert false in
58
59   let sth = dbh#prepare_cached
60               "set constraints pages_redirect_cn, sitemenu_url_cn deferred" in
61   sth#execute [];
62
63   let sth = dbh#prepare_cached "update pages set url_deleted = url,
64                                                  url = null
65                                  where hostid = ? and id = ?" in
66   sth#execute [`Int hostid; `Int oldpageid];
67
68   let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
69                                    description, creation_date, logged_ip,
70                                    redirect, css)
71                                 values (?, ?, ?, ?, ?, ?, ?, ?)" in
72   sth#execute [`Int hostid; `String page; title; description;
73                creation_date; logged_ip; redirect; css ];
74
75   let pageid = sth#serial "pages_id_seq" in
76
77   let sth = dbh#prepare_cached "insert into contents (pageid, ordering,
78                                        sectionname, content, divname)
79                                 select ? as pageid, ordering, sectionname,
80                                             content, divname
81                                   from contents
82                                  where pageid = ?" in
83   sth#execute [`Int pageid; `Int oldpageid];
84
85   dbh#commit ();
86
87   (* Email notification. *)
88   let subject = "CSS for page " ^ page ^ " has been modified" in
89   let body = fun () ->
90     let diff, _ =
91       get_diff dbh hostid page ~version:pageid ~old_version:oldpageid () in
92     "Page: http://" ^ hostname ^ "/" ^ page ^ "\n\n" ^
93     diff in
94
95   email_notify ~subject ~body dbh hostid;
96
97   let buttons = [ ok_button ("/" ^ page);
98                   { StdPages.label = "Edit stylesheet again";
99                     StdPages.link = "/_bin/edit_page_css_form.cmo";
100                     StdPages.method_ = None;
101                     StdPages.params = [ "page", page ] } ] in
102   ok ~title:"Stylesheet changed" ~buttons
103     q ("The stylesheet was changed successfully.  " ^
104        "Note: You must RELOAD the page to see changes to stylesheets.")
105
106 let () =
107   register_script ~restrict:[CanEdit] run