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.25 2004/10/04 15:19:56 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 (* Referer strings which help us decide if the user came from
43 * a search engine and highlight terms in the page appropriately.
45 let search_engines = [
46 Pcre.regexp "^http://.*google\\.", [ "q"; "as_q"; "as_epq"; "as_oq" ];
47 Pcre.regexp "^http://.*yahoo\\.", [ "p" ];
48 Pcre.regexp "^http://.*msn\\.", [ "q"; "MT" ]
50 let split_words = Pcre.regexp "\\W+"
52 let split_qs_re = Pcre.regexp "\\?"
54 let xhtml_re = Pcre.regexp "<.*?>|[^<>]+"
56 let run r (q : cgi) (dbh : Dbi.connection) hostid
57 ({ edit_anon = edit_anon;
58 view_anon = view_anon } as host)
60 let template_page = get_template dbh hostid "page.html" in
61 let template_404 = get_template dbh hostid "page_404.html" in
63 let page = q#param "page" in
64 let page = if page = "" then "index" else page in
66 (* Host-specific fields. *)
67 let sth = dbh#prepare_cached "select css is not null,
68 feedback_email is not null,
69 mailing_list, search_box
70 from hosts where id = ?" in
71 sth#execute [`Int hostid];
72 let has_host_css, has_feedback_email, mailing_list, search_box =
73 match sth#fetch1 () with
74 | [ `Bool has_host_css; `Bool has_feedback_email; `Bool mailing_list;
76 has_host_css, has_feedback_email, mailing_list, search_box
77 | _ -> assert false in
79 (* Can the user edit? Manage users? etc. *)
80 let can_edit = can_edit host user in
81 let can_manage_users = can_manage_users host user in
82 let can_manage_contacts = can_manage_contacts host user in
83 let can_manage_site = can_manage_site host user in
84 let can_edit_global_css = can_edit_global_css host user in
86 (* Do we have a stats page set up? *)
87 let has_stats = server_settings_stats_page dbh <> None in
89 (* Given the referer string, return the list of search terms. If none
90 * can be found, then throws Not_found.
92 let search_terms_from_referer referer =
94 List.find (fun (rex, _) -> Pcre.pmatch ~rex referer) search_engines in
96 match Pcre.split ~rex:split_qs_re ~max:2 referer with
97 | [url] | [url;""] -> url, ""
99 | _ -> assert false in
100 let args = Cgi_args.parse qs in
102 List.find (fun argname -> List.mem_assoc argname args) argnames in
103 let search_string = List.assoc argname args in
104 Pcre.split ~rex:split_words search_string
107 (* Given a full page of XHTML, highlight search terms found in the
108 * <body> part of the page.
110 let highlight_search_terms xhtml search_terms span_class =
111 (* Split the original XHTML into strings and tags. For example if
112 * the original string is: "This is some <b>bold</b> text.<br/>", then
113 * after this step we will have the following list:
114 * [ "This is some "; "<b>"; "bold"; "</b>"; " text."; "<br/>" ]
116 let xhtml = Pcre.extract_all ~rex:xhtml_re xhtml in
117 let xhtml = Array.to_list xhtml in
118 let xhtml = List.map (fun matches -> matches.(0)) xhtml in
120 (* Find the <body> ... </body> tags. We only want to apply
121 * highlighting to tags within this area.
123 let rec list_split f acc = function
124 | [] -> List.rev acc, []
125 | ((x :: _) as xs) when f x -> List.rev acc, xs
127 let acc = x :: acc in
131 list_split (fun str -> String.starts_with str "<body") [] xhtml in
133 list_split ((=) "</body>") [] body in
134 (* NB: Hopefully, xhtml = head @ body @ tail. *)
136 (* The search terms are a list of simple words. Turn into a big
137 * regular expression, because we want to substitute for each. We
138 * end up with a regexp like '(word1|word2|word3)'.
141 Pcre.regexp ~flags:[`CASELESS]
142 ("(" ^ String.concat "|" search_terms ^ ")") in
144 (* Do the substitution, but only on text, not elements! *)
147 "<span class=\"" ^ span_class ^ "\">" ^ text ^ "</span>"
150 if String.length str > 0 && str.[0] != '<' then
151 Pcre.substitute ~rex ~subst str
155 (* Join the XHTML fragments back together again. *)
156 String.concat "" (List.concat [ head ; body ; tail ])
159 (* This code generates ordinary pages. *)
160 let make_page title description pageid last_modified_date has_page_css
162 let t = template_page in
164 t#set "description" description;
165 t#set "pageid" (string_of_int pageid);
166 t#set "last_modified_date" (printable_date last_modified_date);
168 if page <> page' then (* redirection *) (
170 t#set "original_page" page; (* XXX title - get it from database *)
171 t#conditional "redirected" true
174 t#conditional "redirected" false
177 t#conditional "has_host_css" has_host_css;
178 t#conditional "has_page_css" has_page_css;
180 t#conditional "has_feedback_email" has_feedback_email;
181 t#conditional "mailing_list" mailing_list;
182 t#conditional "search_box" search_box;
184 t#conditional "can_edit" can_edit;
185 t#conditional "can_manage_users" can_manage_users;
186 t#conditional "can_manage_contacts" can_manage_contacts;
187 t#conditional "can_manage_site" can_manage_site;
188 t#conditional "can_edit_global_css" can_edit_global_css;
190 t#conditional "has_stats" has_stats;
192 (* Pull out the sections in this page. *)
193 let sth = dbh#prepare_cached
194 "select ordering, sectionname, content, divname
197 order by ordering" in
198 sth#execute [`Int pageid];
202 (function [`Int ordering;
203 (`Null | `String _) as sectionname;
205 (`Null | `String _) as divname] ->
206 let divname, has_divname =
209 | `String divname -> divname, true in
210 let sectionname, has_sectionname =
211 match sectionname with
213 | `String sectionname -> sectionname, true in
214 let linkname = linkname_of_sectionname sectionname in
215 [ "ordering", Template.VarString (string_of_int ordering);
216 "has_sectionname", Template.VarConditional has_sectionname;
217 "sectionname", Template.VarString sectionname;
218 "linkname", Template.VarString linkname;
221 (Wikilib.xhtml_of_content dbh hostid content);
222 "has_divname", Template.VarConditional has_divname;
223 "divname", Template.VarString divname ]
224 | _ -> assert false) in
226 t#table "sections" sections;
228 (* Are we showing an old version of the page? If so, warn. *)
231 t#conditional "is_old_version" false
233 t#conditional "is_old_version" true;
234 t#set "old_version" (string_of_int pageid));
239 t#conditional "user_logged_in" false
240 | User (_, username, _) ->
241 t#conditional "user_logged_in" true;
242 t#set "username" username);
244 (* If we are coming from a search engine then we want to highlight
245 * search terms throughout the whole page ...
248 let referer = Table.get (Request.headers_in r) "Referer" in
249 let search_terms = search_terms_from_referer referer in
251 (* Highlight the search terms. *)
252 let xhtml = t#to_string in
253 let xhtml = highlight_search_terms xhtml search_terms "search_term" in
255 (* Deliver the page. *)
260 (* No referer / no search terms / not a search engine referer. *)
264 (* This code generates 404 pages. *)
266 Request.set_status r 404; (* Return a 404 error code. *)
268 let t = template_404 in
274 ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
277 t#set "search_terms" search_terms;
279 t#conditional "has_host_css" has_host_css;
281 t#conditional "can_edit" can_edit;
282 t#conditional "can_manage_users" can_manage_users;
283 t#conditional "can_manage_contacts" can_manage_contacts;
284 t#conditional "can_manage_site" can_manage_site;
285 t#conditional "can_edit_global_css" can_edit_global_css;
287 t#conditional "has_stats" has_stats;
292 (* Fetch a page by name. This function can give three answers:
293 * (1) Page fetched OK (fetches some details of the page).
294 * (2) Page is a redirect (fetches the name of the redirect page).
295 * (3) Page not found in database, ie. 404 error.
297 (* XXX Should do a case-insensitive matching of URLs, and if the URL differs
298 * in case only should redirect to the lowercase version.
300 let fetch_page page version allow_redirect =
303 if allow_redirect then (
306 "select redirect, id, title, description, last_modified_date,
308 from pages where hostid = ? and url = ?" in
309 sth#execute [`Int hostid; `String page];
311 (match sth#fetch1 () with
312 | [ `Null; `Int id; `String title; `String description;
313 `Timestamp last_modified_date; `Bool has_page_css ] ->
314 FPOK (id, title, description, last_modified_date,
316 | `String redirect :: _ ->
320 Not_found -> FPNotFound)
321 ) else (* redirects not allowed ... *) (
324 "select id, title, description, last_modified_date,
326 from pages where hostid = ? and url = ?" in
327 sth#execute [`Int hostid; `String page];
329 (match sth#fetch1 () with
330 | [ `Int id; `String title; `String description;
331 `Timestamp last_modified_date; `Bool has_page_css ] ->
332 FPOK (id, title, description, last_modified_date,
336 Not_found -> FPNotFound)
341 "select id, title, description, last_modified_date,
344 where hostid = ? and id = ? and
345 (url = ? or url_deleted = ?)" in
346 sth#execute [`Int hostid; `Int version;
347 `String page; `String page];
349 (match sth#fetch1 () with
350 | [ `Int id; `String title; `String description;
351 `Timestamp last_modified_date; `Bool has_page_css ] ->
352 FPOK (id, title, description, last_modified_date,
356 Not_found -> FPNotFound)
359 (* Here we deal with the complex business of redirects and versions. *)
360 (* Only allow the no_redirect and version syntax for editors. *)
361 let allow_redirect, version =
363 not (q#param_true "no_redirect"),
364 try Some (int_of_string (q#param "version")) with Not_found -> None
368 let rec loop page' i =
369 if i > max_redirect then (
370 error ~title:"Too many redirections" ~back_button:true
371 q ("Too many redirects between pages. This may happen because " ^
372 "of a cycle of redirections.");
375 match fetch_page page' version allow_redirect with
376 | FPOK (pageid, title, description, last_modified_date, has_page_css)->
377 make_page title description pageid last_modified_date has_page_css
379 | FPRedirect page' ->
387 register_script ~restrict:[CanView] run