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