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.12 2004/09/17 15:24:54 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 CSS? *)
49 let sth = dbh#prepare_cached "select css is not null from hosts
51 sth#execute [`Int hostid];
53 match sth#fetch1 () with
54 | [ `Bool has_host_css ] -> has_host_css
55 | _ -> assert false in
57 (* Can the user edit? Manage users? etc. *)
58 let can_edit = can_edit edit_anon user in
59 let can_manage_users = can_manage_users user in
60 let can_manage_contacts = can_manage_contacts user in
62 (* This code generates ordinary pages. *)
63 let make_page title description pageid last_modified_date has_page_css
65 let t = template_page in
67 t#set "description" description;
68 t#set "pageid" (string_of_int pageid);
69 t#set "last_modified_date" (printable_date last_modified_date);
71 if page <> page' then (* redirection *) (
73 t#set "original_page" page; (* XXX title - get it from database *)
74 t#conditional "redirected" true
77 t#conditional "redirected" false
80 t#conditional "has_host_css" has_host_css;
81 t#conditional "has_page_css" has_page_css;
83 t#conditional "can_edit" can_edit;
84 t#conditional "can_manage_users" can_manage_users;
85 t#conditional "can_manage_contacts" can_manage_contacts;
87 (* Pull out the sections in this page. *)
88 let sth = dbh#prepare_cached
89 "select ordering, sectionname, content, divname
93 sth#execute [`Int pageid];
97 (function [`Int ordering; `String sectionname; `String content;
98 (`Null | `String _) as divname] ->
99 let divname, has_divname =
102 | `String divname -> divname, true in
103 [ "ordering", Template.VarString (string_of_int ordering);
104 "sectionname", Template.VarString sectionname;
107 (Wikilib.xhtml_of_content dbh hostid content);
108 "has_divname", Template.VarConditional has_divname;
109 "divname", Template.VarString divname ]
110 | _ -> assert false) in
112 t#table "sections" sections;
114 (* Are we showing an old version of the page? If so, warn. *)
117 t#conditional "is_old_version" false
119 t#conditional "is_old_version" true;
120 t#set "old_version" (string_of_int pageid));
125 t#conditional "user_logged_in" false
126 | User (_, username, _) ->
127 t#conditional "user_logged_in" true;
128 t#set "username" username);
133 (* This code generates 404 pages. *)
135 Request.set_status r 404; (* Return a 404 error code. *)
137 let t = template_404 in
143 ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
146 t#set "search_terms" search_terms;
148 t#conditional "has_host_css" has_host_css;
150 t#conditional "can_edit" can_edit;
151 t#conditional "can_manage_users" can_manage_users;
152 t#conditional "can_manage_contacts" can_manage_contacts;
157 (* Fetch a page by name. This function can give three answers:
158 * (1) Page fetched OK (fetches some details of the page).
159 * (2) Page is a redirect (fetches the name of the redirect page).
160 * (3) Page not found in database, ie. 404 error.
162 (* XXX Should do a case-insensitive matching of URLs, and if the URL differs
163 * in case only should redirect to the lowercase version.
165 let fetch_page page version allow_redirect =
168 if allow_redirect then (
171 "select redirect, id, title, description, last_modified_date,
173 from pages where hostid = ? and url = ?" in
174 sth#execute [`Int hostid; `String page];
176 (match sth#fetch1 () with
177 | [ `Null; `Int id; `String title; `String description;
178 `Timestamp last_modified_date; `Bool has_page_css ] ->
179 FPOK (id, title, description, last_modified_date,
181 | `String redirect :: _ ->
185 Not_found -> FPNotFound)
186 ) else (* redirects not allowed ... *) (
189 "select id, title, description, last_modified_date,
191 from pages where hostid = ? and url = ?" in
192 sth#execute [`Int hostid; `String page];
194 (match sth#fetch1 () with
195 | [ `Int id; `String title; `String description;
196 `Timestamp last_modified_date; `Bool has_page_css ] ->
197 FPOK (id, title, description, last_modified_date,
201 Not_found -> FPNotFound)
206 "select id, title, description, last_modified_date,
209 where hostid = ? and id = ? and
210 (url = ? or url_deleted = ?)" in
211 sth#execute [`Int hostid; `Int version;
212 `String page; `String page];
214 (match sth#fetch1 () with
215 | [ `Int id; `String title; `String description;
216 `Timestamp last_modified_date; `Bool has_page_css ] ->
217 FPOK (id, title, description, last_modified_date,
221 Not_found -> FPNotFound)
224 (* Here we deal with the complex business of redirects and versions. *)
225 (* Only allow the no_redirect and version syntax for editors. *)
226 let allow_redirect, version =
228 not (q#param_true "no_redirect"),
229 try Some (int_of_string (q#param "version")) with Not_found -> None
233 let rec loop page' i =
234 if i > max_redirect then (
235 error ~title:"Too many redirections" ~back_button:true
236 q ("Too many redirects between pages. This may happen because " ^
237 "of a cycle of redirections.");
240 match fetch_page page' version allow_redirect with
241 | FPOK (pageid, title, description, last_modified_date, has_page_css)->
242 make_page title description pageid last_modified_date has_page_css
244 | FPRedirect page' ->