2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: page.ml,v 1.6 2004/09/08 10:42:20 rich Exp $
15 open Cocanwiki_template
19 let template_page = get_template "page.html"
20 let template_404 = get_template "page_404.html"
22 (* Maximum level of redirection. *)
25 type fp_status = FPOK of int * string * string * Dbi.datetime * bool
26 | FPRedirect of string
29 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, edit_anon) user =
30 let page = q#param "page" in
31 let page = if page = "" then "index" else page in
33 (* Host-specific CSS? *)
34 let sth = dbh#prepare_cached "select css is not null from hosts
36 sth#execute [`Int hostid];
38 match sth#fetch1 () with
39 | [ `Bool has_host_css ] -> has_host_css
40 | _ -> assert false in
42 (* Can the user edit? Manage users? *)
43 let can_edit = can_edit edit_anon user in
44 let can_manage_users = can_manage_users user in
46 (* This code generates ordinary pages. *)
47 let make_page title description pageid last_modified_date has_page_css
49 let t = template_page in
51 t#set "description" description;
52 t#set "pageid" (string_of_int pageid);
53 t#set "last_modified_date" (printable_date last_modified_date);
54 t#set "cocanwiki_package" Cocanwiki_version.package;
55 t#set "cocanwiki_version" Cocanwiki_version.version;
57 if page <> page' then (* redirection *) (
59 t#set "original_page" page; (* XXX title - get it from database *)
60 t#conditional "redirected" true
63 t#conditional "redirected" false
66 t#conditional "has_host_css" has_host_css;
67 t#conditional "has_page_css" has_page_css;
69 t#conditional "can_edit" can_edit;
70 t#conditional "can_manage_users" can_manage_users;
72 (* Pull out the sections in this page. *)
73 let sth = dbh#prepare_cached
74 "select ordering, sectionname, content, divname
78 sth#execute [`Int pageid];
82 (function [`Int ordering; `String sectionname; `String content;
83 (`Null | `String _) as divname] ->
84 let divname, has_divname =
87 | `String divname -> divname, true in
88 [ "ordering", Template.VarString (string_of_int ordering);
89 "sectionname", Template.VarString sectionname;
92 (Wikilib.xhtml_of_content dbh hostid content);
93 "has_divname", Template.VarConditional has_divname;
94 "divname", Template.VarString divname ]
95 | _ -> assert false) in
97 t#table "sections" sections;
99 (* Are we showing an old version of the page? If so, warn. *)
102 t#conditional "is_old_version" false
104 t#conditional "is_old_version" true;
105 t#set "old_version" (string_of_int pageid));
110 t#conditional "user_logged_in" false
111 | User (_, username, _) ->
112 t#conditional "user_logged_in" true;
113 t#set "username" username);
118 (* This code generates 404 pages. *)
120 Request.set_status r 404; (* Return a 404 error code. *)
122 let t = template_404 in
128 ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
131 template_404#set "search_terms" search_terms;
136 (* Fetch a page by name. This function can give three answers:
137 * (1) Page fetched OK (fetches some details of the page).
138 * (2) Page is a redirect (fetches the name of the redirect page).
139 * (3) Page not found in database, ie. 404 error.
141 (* XXX Should do a case-insensitive matching of URLs, and if the URL differs
142 * in case only should redirect to the lowercase version.
144 let fetch_page page version allow_redirect =
147 if allow_redirect then (
150 "select redirect, id, title, description, last_modified_date,
152 from pages where hostid = ? and url = ?" in
153 sth#execute [`Int hostid; `String page];
155 (match sth#fetch1 () with
156 | [ `Null; `Int id; `String title; `String description;
157 `Timestamp last_modified_date; `Bool has_page_css ] ->
158 FPOK (id, title, description, last_modified_date,
160 | `String redirect :: _ ->
164 Not_found -> FPNotFound)
165 ) else (* redirects not allowed ... *) (
168 "select id, title, description, last_modified_date,
170 from pages where hostid = ? and url = ?" in
171 sth#execute [`Int hostid; `String page];
173 (match sth#fetch1 () with
174 | [ `Int id; `String title; `String description;
175 `Timestamp last_modified_date; `Bool has_page_css ] ->
176 FPOK (id, title, description, last_modified_date,
180 Not_found -> FPNotFound)
185 "select id, title, description, last_modified_date,
188 where hostid = ? and id = ? and
189 (url = ? or url_deleted = ?)" in
190 sth#execute [`Int hostid; `Int version;
191 `String page; `String page];
193 (match sth#fetch1 () with
194 | [ `Int id; `String title; `String description;
195 `Timestamp last_modified_date; `Bool has_page_css ] ->
196 FPOK (id, title, description, last_modified_date,
200 Not_found -> FPNotFound)
203 (* Here we deal with the complex business of redirects and versions. *)
204 (* Only allow the no_redirect and version syntax for editors. *)
205 let allow_redirect, version =
207 not (q#param_true "no_redirect"),
208 try Some (int_of_string (q#param "version")) with Not_found -> None
212 let rec loop page' i =
213 if i > max_redirect then (
214 error ~title:"Too many redirections" ~back_button:true
215 q ("Too many redirects between pages. This may happen because " ^
216 "of a cycle of redirections.");
219 match fetch_page page' version allow_redirect with
220 | FPOK (pageid, title, description, last_modified_date, has_page_css)->
221 make_page title description pageid last_modified_date has_page_css
223 | FPRedirect page' ->