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.44 2006/03/28 13:20:00 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.
31 open Cocanwiki_template
34 open Cocanwiki_server_settings
37 type fp_status = FPOK of int * string * string * Dbi.datetime * bool
38 | FPInternalRedirect of string
39 | FPExternalRedirect 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 hostid
57 ({ edit_anon = edit_anon; view_anon = view_anon } as host)
59 let page = q#param "page" in
60 let page = if page = "" then "index" else page in
62 (* The main "page" template is split in two to improve the speed of
63 * delivery of the page. The very first part ("page_header.html")
64 * contains the page <head>, crucially including all the links to the
65 * stylesheets. We send this first and flush it out to the client so
66 * that the client can begin requesting stylesheets, background images
67 * and so on. After this we compose the main page ("page.html") and
71 let template_page_header =
72 get_template ~page dbh hostid "page_header.html" in
73 let template_page = get_template ~page dbh hostid "page.html" in
75 (* This is the simpler template for 404 pages. *)
76 let template_404 = get_template dbh hostid "page_404.html" in
78 (* Host-specific fields. *)
80 "select css is not null, feedback_email is not null, mailing_list, navigation
81 from hosts where id = $hostid" in
82 let has_host_css, has_feedback_email, mailing_list, navigation =
84 | [Some has_host_css, Some has_feedback_email,
85 mailing_list, navigation] ->
86 has_host_css, has_feedback_email, mailing_list, navigation
87 | _ -> assert false in
89 (* User permissions. *)
90 let can_edit = can_edit host user in
91 let can_manage_users = can_manage_users host user in
93 (* Do we have a stats page set up? *)
94 let has_stats = server_settings_stats_page dbh <> None in
96 (* Given the referer string, return the list of search terms. If none
97 * can be found, then throws Not_found.
99 let search_terms_from_referer referer =
101 List.find (fun (rex, _) -> Pcre.pmatch ~rex referer) search_engines in
103 match Pcre.split ~rex:split_qs_re ~max:2 referer with
104 | [url] | [url;""] -> url, ""
105 | [url;qs] -> url, qs
106 | _ -> assert false in
107 let args = Cgi_args.parse qs in
109 List.find (fun argname -> List.mem_assoc argname args) argnames in
110 let search_string = List.assoc argname args in
111 Pcre.split ~rex:split_words search_string
114 (* Given a full page of XHTML, highlight search terms found in the
115 * <body> part of the page.
117 let highlight_search_terms xhtml search_terms span_class =
118 (* Split the original XHTML into strings and tags. For example if
119 * the original string is: "This is some <b>bold</b> text.<br/>", then
120 * after this step we will have the following list:
121 * [ "This is some "; "<b>"; "bold"; "</b>"; " text."; "<br/>" ]
123 let xhtml = Pcre.extract_all ~rex:xhtml_re xhtml in
124 let xhtml = Array.to_list xhtml in
125 let xhtml = List.map (fun matches -> matches.(0)) xhtml in
127 (* Find the <body> ... </body> tags. We only want to apply
128 * highlighting to tags within this area.
130 let rec list_split f acc = function
131 | [] -> List.rev acc, []
132 | ((x :: _) as xs) when f x -> List.rev acc, xs
134 let acc = x :: acc in
138 list_split (fun str -> String.starts_with str "<body") [] xhtml in
140 list_split ((=) "</body>") [] body in
141 (* NB: Hopefully, xhtml = head @ body @ tail. *)
143 (* The search terms are a list of simple words. Turn into a big
144 * regular expression, because we want to substitute for each. We
145 * end up with a regexp like '(word1|word2|word3)'.
148 Pcre.regexp ~flags:[`CASELESS]
149 ("(" ^ String.concat "|" search_terms ^ ")") in
151 (* Do the substitution, but only on text, not elements! *)
154 "<span class=\"" ^ span_class ^ "\">" ^ text ^ "</span>"
157 if String.length str > 0 && str.[0] != '<' then
158 Pcre.substitute ~rex ~subst str
162 (* Join the XHTML fragments back together again. *)
163 String.concat "" (List.concat [ head ; body ; tail ])
166 (* Check the templates table for extensions. *)
167 let get_extension url =
171 PGSQL(dbh) "select extension from templates
172 where $url ~ url_regexp
176 Some (List.assoc name !extensions)
178 Not_found | ExtList.List.Empty_list -> None
181 (* This code generates ordinary pages. *)
182 let make_page title description pageid last_modified_date has_page_css
183 version page page' extension =
184 let t = template_page in
185 let th = template_page_header in
187 th#set "title" title;
188 t#set "last_modified_date" last_modified_date;
190 (match description with
191 None -> th#conditional "has_description" false
192 | Some description ->
193 th#conditional "has_description" true;
194 th#set "description" description);
196 if page <> page' then (* redirection *) (
199 t#set "original_page" page; (* XXX title - get it from database *)
200 t#conditional "redirected" true
204 t#conditional "redirected" false
207 th#conditional "has_host_css" has_host_css;
208 th#conditional "has_page_css" has_page_css;
210 (* Are we showing an old version of the page? If so, warn. *)
213 t#conditional "is_old_version" false;
214 th#conditional "is_old_version" false
216 t#conditional "is_old_version" true;
217 th#conditional "is_old_version" true;
218 t#set "old_version" (Int32.to_string pageid);
219 th#set "old_version" (Int32.to_string pageid));
221 (* At this point, we can print out the header and flush it back to
222 * the user, allowing the browser to start fetching stylesheets
223 * and background images while we compose the page.
226 print_string r th#to_string;
229 t#conditional "has_feedback_email" has_feedback_email;
230 t#conditional "mailing_list" mailing_list;
231 t#conditional "navigation" navigation;
233 t#conditional "can_edit" can_edit;
234 t#conditional "can_manage_users" can_manage_users;
235 t#conditional "has_stats" has_stats;
237 (* Pull out the sections in this page. *)
242 let rows = PGSQL(dbh)
243 "select ordering, sectionname, content, divname
244 from contents where pageid = $pageid order by ordering" in
247 (fun (ordering, sectionname, content, divname) ->
248 let divname, has_divname =
251 | Some divname -> divname, true in
252 let sectionname, has_sectionname =
253 match sectionname with
255 | Some sectionname -> sectionname, true in
256 let linkname = linkname_of_sectionname sectionname in
257 [ "ordering", Template.VarString (Int32.to_string ordering);
259 Template.VarConditional has_sectionname;
260 "sectionname", Template.VarString sectionname;
261 "linkname", Template.VarString linkname;
264 (Wikilib.xhtml_of_content dbh hostid content);
265 "has_divname", Template.VarConditional has_divname;
266 "divname", Template.VarString divname ]) rows in
268 (* Call an extension to generate the first section in this page? *)
273 let content = extension dbh hostid page' in
275 "ordering", Template.VarString "0";
276 "has_sectionname", Template.VarConditional false;
277 "linkname", Template.VarString "";
278 "content", Template.VarString content;
279 "has_divname", Template.VarConditional true;
280 "divname", Template.VarString "form_div";
282 section :: sections in
284 t#table "sections" sections;
289 t#conditional "user_logged_in" false
290 | User (_, username, _, _) ->
291 t#conditional "user_logged_in" true;
292 t#set "username" username);
294 (* If logged in, we want to update the recently_visited table. *)
295 if pageid <> None then (
297 | User (userid, _, _, _) ->
299 "delete from recently_visited
300 where hostid = $hostid and userid = $userid and url = $page'";
302 "insert into recently_visited (hostid, userid, url)
303 values ($hostid, $userid, $page')";
305 PGOCaml.begin_work dbh;
309 (* Navigation links. *)
311 let max_links = 18 in (* Show no more links than this. *)
313 (* What links here. *)
314 let wlh = what_links_here dbh hostid page' in
315 let wlh = List.take max_links wlh in
316 let wlh_urls = List.map fst wlh in (* Just the URLs ... *)
320 | User (userid, _, _, _) ->
321 (* Recently visited URLs, but don't repeat any from the 'what
322 * links here' section, and don't link to self.
324 let not_urls = page' :: wlh_urls in
325 let limit = Int32.of_int (max_links - List.length wlh_urls) in
328 "select rv.url, p.title, rv.visit_time
329 from recently_visited rv, pages p
330 where rv.hostid = $hostid and rv.userid = $userid
331 and rv.url not in $@not_urls
332 and rv.hostid = p.hostid and rv.url = p.url
336 fun (url, title, _) -> url, title
341 let f (page, title) = [ "page", Template.VarString page;
342 "title", Template.VarString title ] in
343 let table = List.map f wlh in
344 t#table "what_links_here" table;
345 t#conditional "has_what_links_here" (wlh <> []);
347 let table = List.map f rv in
348 t#table "recently_visited" table;
349 t#conditional "has_recently_visited" (rv <> []);
351 (* If both lists are empty (ie. an empty navigation box would
352 * appear), then disable navigation altogether.
354 if wlh = [] && rv = [] then t#conditional "navigation" false
357 (* If we are coming from a search engine then we want to highlight
358 * search terms throughout the whole page ...
361 let referer = Table.get (Request.headers_in r) "Referer" in
362 let search_terms = search_terms_from_referer referer in
364 (* Highlight the search terms. *)
365 let xhtml = t#to_string in
366 let xhtml = highlight_search_terms xhtml search_terms "search_term" in
368 (* Deliver the page. *)
369 ignore (print_string r xhtml)
372 (* No referer / no search terms / not a search engine referer. *)
373 ignore (print_string r t#to_string)
376 (* This code generates 404 pages. *)
378 Request.set_status r 404; (* Return a 404 error code. *)
380 let t = template_404 in
386 ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
389 t#set "search_terms" search_terms;
391 t#conditional "can_edit" can_edit;
392 t#conditional "can_manage_users" can_manage_users;
393 t#conditional "has_stats" has_stats;
398 (* Fetch a page by name. This function can give three answers:
399 * (1) Page fetched OK (fetches some details of the page).
400 * (2) Page is a redirect (fetches the name of the redirect page).
401 * (3) Page not found in database, could be template or 404 error.
403 let fetch_page page version allow_redirect =
406 if allow_redirect then (
407 let rows = PGSQL(dbh)
408 "select url, redirect, id, title, description,
409 last_modified_date, css is not null
411 where hostid = $hostid and lower (url) = lower ($page)" in
413 | [page', _, _, _, _, _, _]
414 when page <> page' -> (* different case *)
415 FPExternalRedirect page'
416 | [ _, None, id, title, description,
417 last_modified_date, has_page_css ] ->
418 let has_page_css = Option.get has_page_css in
419 FPOK (id, title, description, last_modified_date,
421 | [_, Some redirect, _, _, _, _, _] ->
422 FPInternalRedirect redirect
425 ) else (* redirects not allowed ... *) (
426 let rows = PGSQL(dbh)
427 "select id, title, description, last_modified_date,
429 from pages where hostid = $hostid and url = $page" in
431 | [ id, title, description,
432 last_modified_date, has_page_css ] ->
433 let has_page_css = Option.get has_page_css in
434 FPOK (id, title, description, last_modified_date,
440 let rows = PGSQL(dbh)
441 "select id, title, description, last_modified_date,
444 where hostid = $hostid and id = $version and
445 (url = $page or url_deleted = $page)" in
447 | [ id, title, description,
448 last_modified_date, has_page_css ] ->
449 let has_page_css = Option.get has_page_css in
450 FPOK (id, title, description, last_modified_date,
456 (* Here we deal with the complex business of redirects and versions. *)
457 (* Only allow the no_redirect and version syntax for editors. *)
458 let allow_redirect, version =
460 not (q#param_true "no_redirect"),
461 try Some (Int32.of_string (q#param "version")) with Not_found -> None
465 let rec loop page' i =
466 if i > max_redirect then (
467 error ~title:"Too many redirections" ~back_button:true
469 ("Too many redirects between pages. This may happen because " ^
470 "of a cycle of redirections.");
473 match fetch_page page' version allow_redirect with
474 | FPOK (pageid, title, description, last_modified_date, has_page_css)->
475 (* Check if the page is also a template. *)
476 let extension = get_extension page' in
477 make_page title (Some description) (Some pageid)
478 (printable_date last_modified_date) has_page_css
479 version page page' extension
480 | FPInternalRedirect page' ->
482 | FPExternalRedirect page' ->
483 (* This normally happens when a user has request an uppercase
484 * page name. We redirect to the true (lowercase) version.
486 q#redirect ("http://" ^ host.hostname ^ "/" ^ page');
489 (* Might be a templated page with no content in it. *)
490 let extension = get_extension page' in
491 (match extension with
492 | (Some _) as extension ->
494 make_page title None None
495 "Now" false None page page'
503 register_script ~restrict:[CanView] run