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