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.17 2004/09/22 10:19:26 rich Exp $
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.
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.
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.
30 open Cocanwiki_template
34 (* Maximum level of redirection. *)
37 type fp_status = FPOK of int * string * string * Dbi.datetime * bool
38 | FPRedirect of string
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
45 let page = q#param "page" in
46 let page = if page = "" then "index" else page in
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
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
66 (* This code generates ordinary pages. *)
67 let make_page title description pageid last_modified_date has_page_css
69 let t = template_page in
71 t#set "description" description;
72 t#set "pageid" (string_of_int pageid);
73 t#set "last_modified_date" (printable_date last_modified_date);
75 if page <> page' then (* redirection *) (
77 t#set "original_page" page; (* XXX title - get it from database *)
78 t#conditional "redirected" true
81 t#conditional "redirected" false
84 t#conditional "has_host_css" has_host_css;
85 t#conditional "has_page_css" has_page_css;
87 t#conditional "has_feedback_email" has_feedback_email;
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;
95 (* Pull out the sections in this page. *)
96 let sth = dbh#prepare_cached
97 "select ordering, sectionname, content, divname
100 order by ordering" in
101 sth#execute [`Int pageid];
105 (function [`Int ordering;
106 (`Null | `String _) as sectionname;
108 (`Null | `String _) as divname] ->
109 let divname, has_divname =
112 | `String divname -> divname, true in
113 let sectionname, has_sectionname =
114 match sectionname with
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;
124 (Wikilib.xhtml_of_content dbh hostid content);
125 "has_divname", Template.VarConditional has_divname;
126 "divname", Template.VarString divname ]
127 | _ -> assert false) in
129 t#table "sections" sections;
131 (* Are we showing an old version of the page? If so, warn. *)
134 t#conditional "is_old_version" false
136 t#conditional "is_old_version" true;
137 t#set "old_version" (string_of_int pageid));
142 t#conditional "user_logged_in" false
143 | User (_, username, _) ->
144 t#conditional "user_logged_in" true;
145 t#set "username" username);
150 (* This code generates 404 pages. *)
152 Request.set_status r 404; (* Return a 404 error code. *)
154 let t = template_404 in
160 ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
163 t#set "search_terms" search_terms;
165 t#conditional "has_host_css" has_host_css;
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;
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.
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.
184 let fetch_page page version allow_redirect =
187 if allow_redirect then (
190 "select redirect, id, title, description, last_modified_date,
192 from pages where hostid = ? and url = ?" in
193 sth#execute [`Int hostid; `String page];
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,
200 | `String redirect :: _ ->
204 Not_found -> FPNotFound)
205 ) else (* redirects not allowed ... *) (
208 "select id, title, description, last_modified_date,
210 from pages where hostid = ? and url = ?" in
211 sth#execute [`Int hostid; `String page];
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,
220 Not_found -> FPNotFound)
225 "select id, title, description, last_modified_date,
228 where hostid = ? and id = ? and
229 (url = ? or url_deleted = ?)" in
230 sth#execute [`Int hostid; `Int version;
231 `String page; `String page];
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,
240 Not_found -> FPNotFound)
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 =
247 not (q#param_true "no_redirect"),
248 try Some (int_of_string (q#param "version")) with Not_found -> None
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.");
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
263 | FPRedirect page' ->