2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: page.ml,v 1.2 2004/09/07 13:40:10 rich Exp $
17 open Cocanwiki_template
20 let template_page = get_template "page.html"
21 let template_404 = get_template "page_404.html"
23 (* Maximum level of redirection. *)
26 type fp_status = FPOK of int * string * string * Dbi.datetime * bool
27 | FPRedirect of string
30 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ =
31 let page = q#param "page" in
32 let page = if page = "" then "index" else page in
34 (* Host-specific CSS? *)
35 let sth = dbh#prepare_cached "select css is not null from hosts
37 sth#execute [`Int hostid];
39 match sth#fetch1 () with
40 | [ `Bool has_host_css ] -> has_host_css
41 | _ -> assert false in
43 (* This code generates ordinary pages. *)
44 let make_page title description pageid last_modified_date has_page_css
46 let t = template_page in
48 t#set "description" description;
49 t#set "pageid" (string_of_int pageid);
50 t#set "last_modified_date" (printable_date last_modified_date);
51 t#set "cocanwiki_package" Cocanwiki_version.package;
52 t#set "cocanwiki_version" Cocanwiki_version.version;
54 if page <> page' then (* redirection *) (
56 t#set "original_page" page; (* XXX title - get it from database *)
57 t#conditional "redirected" true
60 t#conditional "redirected" false
63 t#conditional "has_host_css" has_host_css;
64 t#conditional "has_page_css" has_page_css;
66 (* Pull out the sections in this page. *)
67 let sth = dbh#prepare_cached
68 "select ordering, sectionname, content, divname
72 sth#execute [`Int pageid];
76 (function [`Int ordering; `String sectionname; `String content;
77 (`Null | `String _) as divname] ->
78 let divname, has_divname =
81 | `String divname -> divname, true in
82 [ "ordering", Template.VarString (string_of_int ordering);
83 "sectionname", Template.VarString sectionname;
86 (Wikilib.xhtml_of_content dbh hostid content);
87 "has_divname", Template.VarConditional has_divname;
88 "divname", Template.VarString divname ]
89 | _ -> assert false) in
91 t#table "sections" sections;
93 (* Are we showing an old version of the page? If so, warn. *)
96 t#conditional "is_old_version" false
98 t#conditional "is_old_version" true;
99 t#set "old_version" (string_of_int pageid));
104 (* This code generates 404 pages. *)
106 Request.set_status r 404; (* Return a 404 error code. *)
108 let t = template_404 in
114 ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
117 template_404#set "search_terms" search_terms;
122 (* Fetch a page by name. This function can give three answers:
123 * (1) Page fetched OK (fetches some details of the page).
124 * (2) Page is a redirect (fetches the name of the redirect page).
125 * (3) Page not found in database, ie. 404 error.
127 (* XXX Should do a case-insensitive matching of URLs, and if the URL differs
128 * in case only should redirect to the lowercase version.
130 let fetch_page page version allow_redirect =
133 if allow_redirect then (
136 "select redirect, id, title, description, last_modified_date,
138 from pages where hostid = ? and url = ?" in
139 sth#execute [`Int hostid; `String page];
141 (match sth#fetch1 () with
142 | [ `Null; `Int id; `String title; `String description;
143 `Timestamp last_modified_date; `Bool has_page_css ] ->
144 FPOK (id, title, description, last_modified_date,
146 | `String redirect :: _ ->
150 Not_found -> FPNotFound)
151 ) else (* redirects not allowed ... *) (
154 "select id, title, description, last_modified_date,
156 from pages where hostid = ? and url = ?" in
157 sth#execute [`Int hostid; `String page];
159 (match sth#fetch1 () with
160 | [ `Int id; `String title; `String description;
161 `Timestamp last_modified_date; `Bool has_page_css ] ->
162 FPOK (id, title, description, last_modified_date,
166 Not_found -> FPNotFound)
171 "select id, title, description, last_modified_date,
174 where hostid = ? and id = ? and
175 (url = ? or url_deleted = ?)" in
176 sth#execute [`Int hostid; `Int version;
177 `String page; `String page];
179 (match sth#fetch1 () with
180 | [ `Int id; `String title; `String description;
181 `Timestamp last_modified_date; `Bool has_page_css ] ->
182 FPOK (id, title, description, last_modified_date,
186 Not_found -> FPNotFound)
189 (* Here we deal with the complex business of redirects and versions. *)
190 let allow_redirect = not (q#param_true "no_redirect") in
192 try Some (int_of_string (q#param "version")) with Not_found -> None in
194 let rec loop page' i =
195 if i > max_redirect then (
196 error ~title:"Too many redirections" ~back_button:true
197 q ("Too many redirects between pages. This may happen because " ^
198 "of a cycle of redirections.");
201 match fetch_page page' version allow_redirect with
202 | FPOK (pageid, title, description, last_modified_date, has_page_css)->
203 make_page title description pageid last_modified_date has_page_css
205 | FPRedirect page' ->