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.19 2004/09/23 15:16:21 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
33 open Cocanwiki_server_settings
35 (* Maximum level of redirection. *)
38 type fp_status = FPOK of int * string * string * Dbi.datetime * bool
39 | FPRedirect of string
42 let run r (q : cgi) (dbh : Dbi.connection) hostid {edit_anon=edit_anon} user =
43 let template_page = get_template dbh hostid "page.html" in
44 let template_404 = get_template dbh hostid "page_404.html" in
46 let page = q#param "page" in
47 let page = if page = "" then "index" else page in
49 (* Host-specific fields. *)
50 let sth = dbh#prepare_cached "select css is not null,
51 feedback_email is not null
52 from hosts where id = ?" in
53 sth#execute [`Int hostid];
54 let has_host_css, has_feedback_email =
55 match sth#fetch1 () with
56 | [ `Bool has_host_css; `Bool has_feedback_email ] ->
57 has_host_css, has_feedback_email
58 | _ -> assert false in
60 (* Can the user edit? Manage users? etc. *)
61 let can_edit = can_edit edit_anon user in
62 let can_manage_users = can_manage_users user in
63 let can_manage_contacts = can_manage_contacts user in
64 let can_manage_site = can_manage_site user in
65 let can_edit_global_css = can_edit_global_css user in
67 (* Do we have a stats page set up? *)
68 let has_stats = server_settings_stats_page dbh <> None in
70 (* This code generates ordinary pages. *)
71 let make_page title description pageid last_modified_date has_page_css
73 let t = template_page in
75 t#set "description" description;
76 t#set "pageid" (string_of_int pageid);
77 t#set "last_modified_date" (printable_date last_modified_date);
79 if page <> page' then (* redirection *) (
81 t#set "original_page" page; (* XXX title - get it from database *)
82 t#conditional "redirected" true
85 t#conditional "redirected" false
88 t#conditional "has_host_css" has_host_css;
89 t#conditional "has_page_css" has_page_css;
91 t#conditional "has_feedback_email" has_feedback_email;
93 t#conditional "can_edit" can_edit;
94 t#conditional "can_manage_users" can_manage_users;
95 t#conditional "can_manage_contacts" can_manage_contacts;
96 t#conditional "can_manage_site" can_manage_site;
97 t#conditional "can_edit_global_css" can_edit_global_css;
99 t#conditional "has_stats" has_stats;
101 (* Pull out the sections in this page. *)
102 let sth = dbh#prepare_cached
103 "select ordering, sectionname, content, divname
106 order by ordering" in
107 sth#execute [`Int pageid];
111 (function [`Int ordering;
112 (`Null | `String _) as sectionname;
114 (`Null | `String _) as divname] ->
115 let divname, has_divname =
118 | `String divname -> divname, true in
119 let sectionname, has_sectionname =
120 match sectionname with
122 | `String sectionname -> sectionname, true in
123 let linkname = linkname_of_sectionname sectionname in
124 [ "ordering", Template.VarString (string_of_int ordering);
125 "has_sectionname", Template.VarConditional has_sectionname;
126 "sectionname", Template.VarString sectionname;
127 "linkname", Template.VarString linkname;
130 (Wikilib.xhtml_of_content dbh hostid content);
131 "has_divname", Template.VarConditional has_divname;
132 "divname", Template.VarString divname ]
133 | _ -> assert false) in
135 t#table "sections" sections;
137 (* Are we showing an old version of the page? If so, warn. *)
140 t#conditional "is_old_version" false
142 t#conditional "is_old_version" true;
143 t#set "old_version" (string_of_int pageid));
148 t#conditional "user_logged_in" false
149 | User (_, username, _) ->
150 t#conditional "user_logged_in" true;
151 t#set "username" username);
156 (* This code generates 404 pages. *)
158 Request.set_status r 404; (* Return a 404 error code. *)
160 let t = template_404 in
166 ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
169 t#set "search_terms" search_terms;
171 t#conditional "has_host_css" has_host_css;
173 t#conditional "can_edit" can_edit;
174 t#conditional "can_manage_users" can_manage_users;
175 t#conditional "can_manage_contacts" can_manage_contacts;
176 t#conditional "can_manage_site" can_manage_site;
177 t#conditional "can_edit_global_css" can_edit_global_css;
179 t#conditional "has_stats" has_stats;
184 (* Fetch a page by name. This function can give three answers:
185 * (1) Page fetched OK (fetches some details of the page).
186 * (2) Page is a redirect (fetches the name of the redirect page).
187 * (3) Page not found in database, ie. 404 error.
189 (* XXX Should do a case-insensitive matching of URLs, and if the URL differs
190 * in case only should redirect to the lowercase version.
192 let fetch_page page version allow_redirect =
195 if allow_redirect then (
198 "select redirect, id, title, description, last_modified_date,
200 from pages where hostid = ? and url = ?" in
201 sth#execute [`Int hostid; `String page];
203 (match sth#fetch1 () with
204 | [ `Null; `Int id; `String title; `String description;
205 `Timestamp last_modified_date; `Bool has_page_css ] ->
206 FPOK (id, title, description, last_modified_date,
208 | `String redirect :: _ ->
212 Not_found -> FPNotFound)
213 ) else (* redirects not allowed ... *) (
216 "select id, title, description, last_modified_date,
218 from pages where hostid = ? and url = ?" in
219 sth#execute [`Int hostid; `String page];
221 (match sth#fetch1 () with
222 | [ `Int id; `String title; `String description;
223 `Timestamp last_modified_date; `Bool has_page_css ] ->
224 FPOK (id, title, description, last_modified_date,
228 Not_found -> FPNotFound)
233 "select id, title, description, last_modified_date,
236 where hostid = ? and id = ? and
237 (url = ? or url_deleted = ?)" in
238 sth#execute [`Int hostid; `Int version;
239 `String page; `String page];
241 (match sth#fetch1 () with
242 | [ `Int id; `String title; `String description;
243 `Timestamp last_modified_date; `Bool has_page_css ] ->
244 FPOK (id, title, description, last_modified_date,
248 Not_found -> FPNotFound)
251 (* Here we deal with the complex business of redirects and versions. *)
252 (* Only allow the no_redirect and version syntax for editors. *)
253 let allow_redirect, version =
255 not (q#param_true "no_redirect"),
256 try Some (int_of_string (q#param "version")) with Not_found -> None
260 let rec loop page' i =
261 if i > max_redirect then (
262 error ~title:"Too many redirections" ~back_button:true
263 q ("Too many redirects between pages. This may happen because " ^
264 "of a cycle of redirections.");
267 match fetch_page page' version allow_redirect with
268 | FPOK (pageid, title, description, last_modified_date, has_page_css)->
269 make_page title description pageid last_modified_date has_page_css
271 | FPRedirect page' ->