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