Global replace 'raise CgiExit' -> 'return ()' to match a future version of mod_caml.
[cocanwiki.git] / scripts / page.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: page.ml,v 1.18 2004/09/23 11:56:47 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 ExtString
28
29 open Cocanwiki
30 open Cocanwiki_template
31 open Cocanwiki_ok
32 open Cocanwiki_date
33
34 (* Maximum level of redirection. *)
35 let max_redirect = 4
36
37 type fp_status = FPOK of int * string * string * Dbi.datetime * bool
38                | FPRedirect of string
39                | FPNotFound
40
41 let run r (q : cgi) (dbh : Dbi.connection) hostid {edit_anon=edit_anon} user =
42   let template_page = get_template dbh hostid "page.html" in
43   let template_404  = get_template dbh hostid "page_404.html" in
44
45   let page = q#param "page" in
46   let page = if page = "" then "index" else page in
47
48   (* Host-specific fields. *)
49   let sth = dbh#prepare_cached "select css is not null,
50                                        feedback_email is not null
51                                   from hosts where id = ?" in
52   sth#execute [`Int hostid];
53   let has_host_css, has_feedback_email =
54     match sth#fetch1 () with
55       | [ `Bool has_host_css; `Bool has_feedback_email ] ->
56           has_host_css, has_feedback_email
57       | _ -> assert false in
58
59   (* Can the user edit?  Manage users?  etc. *)
60   let can_edit = can_edit edit_anon user in
61   let can_manage_users = can_manage_users user in
62   let can_manage_contacts = can_manage_contacts user in
63   let can_manage_site = can_manage_site user in
64   let can_edit_global_css = can_edit_global_css user in
65
66   (* This code generates ordinary pages. *)
67   let make_page title description pageid last_modified_date has_page_css
68       version page page' =
69     let t = template_page in
70     t#set "title" title;
71     t#set "description" description;
72     t#set "pageid" (string_of_int pageid);
73     t#set "last_modified_date" (printable_date last_modified_date);
74
75     if page <> page' then (* redirection *) (
76       t#set "page" page';
77       t#set "original_page" page; (* XXX title - get it from database *)
78       t#conditional "redirected" true
79     ) else (
80       t#set "page" page;
81       t#conditional "redirected" false
82     );
83
84     t#conditional "has_host_css" has_host_css;
85     t#conditional "has_page_css" has_page_css;
86
87     t#conditional "has_feedback_email" has_feedback_email;
88
89     t#conditional "can_edit" can_edit;
90     t#conditional "can_manage_users" can_manage_users;
91     t#conditional "can_manage_contacts" can_manage_contacts;
92     t#conditional "can_manage_site" can_manage_site;
93     t#conditional "can_edit_global_css" can_edit_global_css;
94
95     (* Pull out the sections in this page. *)
96     let sth = dbh#prepare_cached
97                 "select ordering, sectionname, content, divname
98                    from contents
99                   where pageid = ?
100                   order by ordering" in
101     sth#execute [`Int pageid];
102
103     let sections =
104       sth#map
105         (function [`Int ordering;
106                    (`Null | `String _) as sectionname;
107                    `String content;
108                    (`Null | `String _) as divname] ->
109            let divname, has_divname =
110              match divname with
111                  `Null -> "", false
112                | `String divname -> divname, true in
113            let sectionname, has_sectionname =
114              match sectionname with
115                  `Null -> "", false
116                | `String sectionname -> sectionname, true in
117            let linkname = linkname_of_sectionname sectionname in
118            [ "ordering", Template.VarString (string_of_int ordering);
119              "has_sectionname", Template.VarConditional has_sectionname;
120              "sectionname", Template.VarString sectionname;
121              "linkname", Template.VarString linkname;
122              "content",
123                Template.VarString
124                  (Wikilib.xhtml_of_content dbh hostid content);
125              "has_divname", Template.VarConditional has_divname;
126              "divname", Template.VarString divname ]
127            | _ -> assert false) in
128
129     t#table "sections" sections;
130
131     (* Are we showing an old version of the page?  If so, warn. *)
132     (match version with
133          None ->
134            t#conditional "is_old_version" false
135        | Some pageid ->
136            t#conditional "is_old_version" true;
137            t#set "old_version" (string_of_int pageid));
138
139     (* Login status. *)
140     (match user with
141          Anonymous ->
142            t#conditional "user_logged_in" false
143        | User (_, username, _) ->
144            t#conditional "user_logged_in" true;
145            t#set "username" username);
146
147     q#template t
148   in
149
150   (* This code generates 404 pages. *)
151   let make_404 () =
152     Request.set_status r 404;           (* Return a 404 error code. *)
153
154     let t = template_404 in
155     t#set "page" page;
156
157     let search_terms =
158       String.map
159         (function
160              ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
161            | _ -> ' ') page in
162
163     t#set "search_terms" search_terms;
164
165     t#conditional "has_host_css" has_host_css;
166
167     t#conditional "can_edit" can_edit;
168     t#conditional "can_manage_users" can_manage_users;
169     t#conditional "can_manage_contacts" can_manage_contacts;
170     t#conditional "can_manage_site" can_manage_site;
171     t#conditional "can_edit_global_css" can_edit_global_css;
172
173     q#template t
174   in
175
176   (* Fetch a page by name.  This function can give three answers:
177    * (1) Page fetched OK (fetches some details of the page).
178    * (2) Page is a redirect (fetches the name of the redirect page).
179    * (3) Page not found in database, ie. 404 error.
180    *)
181   (* XXX Should do a case-insensitive matching of URLs, and if the URL differs
182    * in case only should redirect to the lowercase version.
183    *)
184   let fetch_page page version allow_redirect =
185     match version with
186       | None ->
187           if allow_redirect then (
188             let sth =
189               dbh#prepare_cached
190                 "select redirect, id, title, description, last_modified_date,
191                         css is not null
192                    from pages where hostid = ? and url = ?" in
193             sth#execute [`Int hostid; `String page];
194             (try
195                (match sth#fetch1 () with
196                   | [ `Null; `Int id; `String title; `String description;
197                       `Timestamp last_modified_date; `Bool has_page_css ] ->
198                       FPOK (id, title, description, last_modified_date,
199                             has_page_css)
200                   | `String redirect :: _ ->
201                       FPRedirect redirect
202                   | _ -> assert false)
203              with
204                  Not_found -> FPNotFound)
205           ) else (* redirects not allowed ... *) (
206             let sth =
207               dbh#prepare_cached
208                 "select id, title, description, last_modified_date,
209                         css is not null
210                    from pages where hostid = ? and url = ?" in
211             sth#execute [`Int hostid; `String page];
212             (try
213                (match sth#fetch1 () with
214                   | [ `Int id; `String title; `String description;
215                       `Timestamp last_modified_date; `Bool has_page_css ] ->
216                       FPOK (id, title, description, last_modified_date,
217                             has_page_css)
218                   | _ -> assert false)
219              with
220                  Not_found -> FPNotFound)
221           )
222       | Some version ->
223             let sth =
224               dbh#prepare_cached
225                 "select id, title, description, last_modified_date,
226                         css is not null
227                    from pages
228                   where hostid = ? and id = ? and
229                         (url = ? or url_deleted = ?)" in
230             sth#execute [`Int hostid; `Int version;
231                          `String page; `String page];
232             (try
233                (match sth#fetch1 () with
234                   | [ `Int id; `String title; `String description;
235                       `Timestamp last_modified_date; `Bool has_page_css ] ->
236                       FPOK (id, title, description, last_modified_date,
237                             has_page_css)
238                   | _ -> assert false)
239              with
240                  Not_found -> FPNotFound)
241   in
242
243   (* Here we deal with the complex business of redirects and versions. *)
244   (* Only allow the no_redirect and version syntax for editors. *)
245   let allow_redirect, version =
246     if can_edit then (
247       not (q#param_true "no_redirect"),
248       try Some (int_of_string (q#param "version")) with Not_found -> None
249     ) else
250       (true, None) in
251
252   let rec loop page' i =
253     if i > max_redirect then (
254       error ~title:"Too many redirections" ~back_button:true
255         q ("Too many redirects between pages.  This may happen because " ^
256            "of a cycle of redirections.");
257       return ()
258     ) else
259       match fetch_page page' version allow_redirect with
260         | FPOK (pageid, title, description, last_modified_date, has_page_css)->
261             make_page title description pageid last_modified_date has_page_css
262               version page page'
263         | FPRedirect page' ->
264             loop page' (i+1)
265         | FPNotFound ->
266             make_404 ()
267   in
268   loop page 0
269
270 let () =
271   register_script run