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.59 2006/12/06 09:46:57 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
36 open Cocanwiki_extensions
37 open Cocanwiki_strings
40 | FPOK of int32 * string * string * string option * Calendar.t * bool
42 | FPInternalRedirect of string
43 | FPExternalRedirect of string
46 (* Referer strings which help us decide if the user came from
47 * a search engine and highlight terms in the page appropriately.
49 let search_engines = [
50 Pcre.regexp "^http://.*google\\.", [ "q"; "as_q"; "as_epq"; "as_oq" ];
51 Pcre.regexp "^http://.*yahoo\\.", [ "p" ];
52 Pcre.regexp "^http://.*msn\\.", [ "q"; "MT" ]
54 let split_words = Pcre.regexp "\\W+"
56 let split_qs_re = Pcre.regexp "\\?"
58 let xhtml_re = Pcre.regexp "<.*?>|[^<>]+"
60 let run r (q : cgi) dbh hostid
61 ({ edit_anon = edit_anon; view_anon = view_anon } as host)
63 let page = q#param "page" in
64 let page = if page = "" then "index" else page in
66 (* The main "page" template is split in two to improve the speed of
67 * delivery of the page. The very first part ("page_header.html")
68 * contains the page <head>, crucially including all the links to the
69 * stylesheets. We send this first and flush it out to the client so
70 * that the client can begin requesting stylesheets, background images
71 * and so on. After this we compose the main page ("page.html") and
75 let template_page_header =
76 get_template ~page r dbh hostid "page_header.html" in
77 let template_page = get_template ~page r dbh hostid "page.html" in
79 (* This is the simpler template for 404 pages. *)
80 let template_404_header = get_template r dbh hostid "page_404_header.html" in
81 let template_404 = get_template r dbh hostid "page_404.html" in
83 (* Host-specific fields. *)
86 "select feedback_email is not null, mailing_list, navigation
87 from hosts where id = $hostid" in
88 let has_feedback_email, mailing_list, navigation =
90 | [Some has_feedback_email, mailing_list, navigation] ->
91 has_feedback_email, mailing_list, navigation
92 | _ -> assert false in
94 (* User permissions. *)
95 let can_edit = can_edit host user in
96 let can_manage_users = can_manage_users host user in
98 (* Do we have a stats page set up? *)
99 let has_stats = server_settings_stats_page dbh <> None in
101 (* Given the referer string, return the list of search terms. If none
102 * can be found, then throws Not_found.
104 let search_terms_from_referer referer =
106 List.find (fun (rex, _) -> Pcre.pmatch ~rex referer) search_engines in
108 match Pcre.split ~rex:split_qs_re ~max:2 referer with
109 | [url] | [url;""] -> url, ""
110 | [url;qs] -> url, qs
111 | _ -> assert false in
112 let args = Cgi_args.parse qs in
114 List.find (fun argname -> List.mem_assoc argname args) argnames in
115 let search_string = List.assoc argname args in
116 Pcre.split ~rex:split_words search_string
119 (* Given a full page of XHTML, highlight search terms found in the
120 * <body> part of the page.
122 let highlight_search_terms xhtml search_terms span_class =
123 (* Split the original XHTML into strings and tags. For example if
124 * the original string is: "This is some <b>bold</b> text.<br/>", then
125 * after this step we will have the following list:
126 * [ "This is some "; "<b>"; "bold"; "</b>"; " text."; "<br/>" ]
128 let xhtml = Pcre.extract_all ~rex:xhtml_re xhtml in
129 let xhtml = Array.to_list xhtml in
130 let xhtml = List.map (fun matches -> matches.(0)) xhtml in
132 (* Find the <body> ... </body> tags. We only want to apply
133 * highlighting to tags within this area.
135 let rec list_split f acc = function
136 | [] -> List.rev acc, []
137 | ((x :: _) as xs) when f x -> List.rev acc, xs
139 let acc = x :: acc in
143 list_split (fun str -> String.starts_with str "<body") [] xhtml in
145 list_split ((=) "</body>") [] body in
146 (* NB: Hopefully, xhtml = head @ body @ tail. *)
148 (* The search terms are a list of simple words. Turn into a big
149 * regular expression, because we want to substitute for each. We
150 * end up with a regexp like '(word1|word2|word3)'.
153 Pcre.regexp ~flags:[`CASELESS]
154 ("(" ^ String.concat "|" search_terms ^ ")") in
156 (* Do the substitution, but only on text, not elements! *)
159 "<span class=\"" ^ span_class ^ "\">" ^ text ^ "</span>"
162 if String.length str > 0 && str.[0] != '<' then
163 Pcre.substitute ~rex ~subst str
167 (* Join the XHTML fragments back together again. *)
168 String.concat "" (List.concat [ head ; body ; tail ])
171 (* Check the templates table for extensions. *)
172 let get_extension url =
176 PGSQL(dbh) "select extension from templates
177 where $url ~ url_regexp
181 Some (List.assoc name !extensions)
183 Not_found | ExtList.List.Empty_list | Failure "hd" -> None
186 (* This code generates ordinary pages. *)
187 let make_page title description keywords
188 pageid last_modified_date has_page_css noodp
189 version page page' extension =
190 let t = template_page in
191 let th = template_page_header in
192 (*t#set "title" title; - nothing uses ::title:: on page.html - removed *)
194 (* Page title, h1 and superdirs (if any). *)
195 th#set "title" title;
198 match String.nsplit title "/" with
202 let xs = List.rev xs in
203 let h1 = List.hd xs in
204 let superdirs = List.rev (List.tl xs) in
206 (* Check the superdirs are reasonable, then convert them
207 * into paths or redlinks.
208 * If any of this fails, then there are no superdirs.
211 let pathsofar = ref "" in
215 (* Path will be something like "Dir1/Dir2". We want
216 * a URL like "dir1/dir2".
219 if i = 0 then name else !pathsofar ^ "/" ^ name in
220 (* Path so far reasonable? *)
222 match Wikilib.generate_url_of_title r dbh hostid path with
223 | Wikilib.GenURL_Duplicate url -> url, None
224 | Wikilib.GenURL_OK url ->
225 (* Is it an extension page? *)
226 (match get_extension url with
227 | Some _ -> url, None (* extension page *)
228 | None -> url, Some path (* redlink *))
229 | Wikilib.GenURL_BadURL | Wikilib.GenURL_TooShort ->
238 let superdirs = List.map (
239 fun (name, url, redlink) ->
240 let is_redlink, redlink_title =
243 | Some title -> true, title in
244 [ "url", Template.VarString url;
245 "name", Template.VarString name;
246 "is_redlink", Template.VarConditional is_redlink;
247 "redlink_title", Template.VarString redlink_title ]
250 th#conditional "has_superdirs" (superdirs <> []);
251 th#table "superdirs" superdirs;
254 t#set "last_modified_date" last_modified_date;
256 (match description with
257 None -> th#conditional "has_description" false
258 | Some description ->
259 th#conditional "has_description" true;
260 th#set "description" description);
263 None -> th#conditional "has_keywords" false
265 th#conditional "has_keywords" true;
266 th#set "keywords" keywords);
268 if page <> page' then (* redirection *) (
271 t#set "original_page" page; (* XXX title - get it from database *)
272 t#conditional "redirected" true
276 t#conditional "redirected" false
279 th#conditional "has_page_css" has_page_css;
281 (* If the per-page noodp is not null, set the noodp flag here. Otherwise
282 * we will use the default (from hosts.global_noodp) which was set
283 * in Cocanwiki_template.
287 | Some b -> th#conditional "noodp" b);
289 (* Are we showing an old version of the page? If so, warn. *)
292 t#conditional "is_old_version" false;
293 th#conditional "is_old_version" false
295 t#conditional "is_old_version" true;
296 th#conditional "is_old_version" true;
297 t#set "old_version" (Int32.to_string pageid);
298 th#set "old_version" (Int32.to_string pageid));
300 (* Just before we show the header, call any registered pre-page
301 * handlers. They might want to send cookies.
303 List.iter (fun handler ->
304 handler r q dbh hostid page') !pre_page_handlers;
306 (* At this point, we can print out the header and flush it back to
307 * the user, allowing the browser to start fetching stylesheets
308 * and background images while we compose the page.
311 ignore (print_string r th#to_string);
312 ignore (Request.rflush r);
314 t#conditional "has_feedback_email" has_feedback_email;
315 t#conditional "mailing_list" mailing_list;
316 t#conditional "navigation" navigation;
318 t#conditional "can_edit" can_edit;
319 t#conditional "can_manage_users" can_manage_users;
320 t#conditional "has_stats" has_stats;
322 (* Pull out the sections in this page. *)
327 let rows = PGSQL(dbh)
328 "select ordering, sectionname, content, divname, divclass, jsgo
329 from contents where pageid = $pageid order by ordering" in
332 (fun (ordering, sectionname, content, divname, divclass, jsgo) ->
333 let divname, has_divname =
336 | Some divname -> divname, true in
337 let divclass, has_divclass =
340 | Some divclass -> divclass, true in
344 | Some jsgo -> jsgo, true in
346 let has_divclass, divclass =
349 if divclass = "" then "jsgo_div"
350 else divclass ^ " jsgo_div")
352 has_divclass, divclass in
353 let has_div = has_divname || has_divclass in
355 let sectionname, has_sectionname =
356 match sectionname with
358 | Some sectionname -> sectionname, true in
359 let linkname = linkname_of_sectionname sectionname in
360 [ "ordering", Template.VarString (Int32.to_string ordering);
362 Template.VarConditional has_sectionname;
363 "sectionname", Template.VarString sectionname;
364 "linkname", Template.VarString linkname;
367 (Wikilib.xhtml_of_content r dbh hostid content);
368 "has_divname", Template.VarConditional has_divname;
369 "divname", Template.VarString divname;
370 "has_divclass", Template.VarConditional has_divclass;
371 "divclass", Template.VarString divclass;
372 "has_div", Template.VarConditional has_div;
373 "has_jsgo", Template.VarConditional has_jsgo;
374 "jsgo", Template.VarString jsgo ]) rows in
376 (* Call an extension to generate the first section in this page? *)
381 let content = extension r dbh hostid page' in
383 "ordering", Template.VarString "0";
384 "has_sectionname", Template.VarConditional false;
385 "linkname", Template.VarString "";
386 "content", Template.VarString content;
387 "has_divname", Template.VarConditional true;
388 "divname", Template.VarString "form_div";
389 "has_divclass", Template.VarConditional false;
390 "divclass", Template.VarString "";
391 "has_div", Template.VarConditional true;
392 "has_jsgo", Template.VarConditional false;
393 "jsgo", Template.VarString "";
395 section :: sections in
397 t#table "sections" sections;
402 t#conditional "user_logged_in" false
403 | User (_, username, _, _) ->
404 t#conditional "user_logged_in" true;
405 t#set "username" username);
407 (* Can anonymous users create accounts? If not them we don't
408 * want to offer to create accounts for them.
410 t#conditional "create_account_anon" host.create_account_anon;
412 (* If logged in, we want to update the recently_visited table. *)
413 if pageid <> None then (
415 | User (userid, _, _, _) ->
418 "delete from recently_visited
419 where hostid = $hostid and userid = $userid
422 "insert into recently_visited (hostid, userid, url)
423 values ($hostid, $userid, $page')";
427 (* Exceptions here are non-fatal. Just print them. *)
428 prerr_endline "exception updating recently_visited:";
429 prerr_endline (Printexc.to_string exn);
430 PGOCaml.rollback dbh;
432 PGOCaml.begin_work dbh;
436 (* Navigation links. *)
438 let max_links = 18 in (* Show no more links than this. *)
440 (* What links here. *)
441 let wlh = what_links_here dbh hostid page' in
442 let wlh = List.take max_links wlh in
443 let wlh_urls = List.map fst wlh in (* Just the URLs ... *)
447 | User (userid, _, _, _) ->
448 (* Recently visited URLs, but don't repeat any from the 'what
449 * links here' section, and don't link to self.
451 let not_urls = page' :: wlh_urls in
452 let limit = Int32.of_int (max_links - List.length wlh_urls) in
455 "select rv.url, p.title, rv.visit_time
456 from recently_visited rv, pages p
457 where rv.hostid = $hostid and rv.userid = $userid
458 and rv.url not in $@not_urls
459 and rv.hostid = p.hostid and rv.url = p.url
463 fun (url, title, _) -> url, title
468 let f (page, title) = [ "page", Template.VarString page;
469 "title", Template.VarString title ] in
470 let table = List.map f wlh in
471 t#table "what_links_here" table;
472 t#conditional "has_what_links_here" (wlh <> []);
474 let table = List.map f rv in
475 t#table "recently_visited" table;
476 t#conditional "has_recently_visited" (rv <> []);
478 (* If both lists are empty (ie. an empty navigation box would
479 * appear), then disable navigation altogether.
481 if wlh = [] && rv = [] then t#conditional "navigation" false
484 (* If we are coming from a search engine then we want to highlight
485 * search terms throughout the whole page ...
488 let referer = Table.get (Request.headers_in r) "Referer" in
489 let search_terms = search_terms_from_referer referer in
491 (* Highlight the search terms. *)
492 let xhtml = t#to_string in
493 let xhtml = highlight_search_terms xhtml search_terms "search_term" in
495 (* Deliver the page. *)
496 ignore (print_string r xhtml)
499 (* No referer / no search terms / not a search engine referer. *)
500 ignore (print_string r t#to_string)
503 (* This code generates 404 pages. *)
505 Request.set_status r 404; (* Return a 404 error code. *)
507 let th = template_404_header in
513 ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
516 th#set "search_terms" search_terms;
518 (* Flush out the header while we start the search. *)
520 ignore (print_string r th#to_string);
521 ignore (Request.rflush r);
523 let t = template_404 in
524 t#set "query" search_terms;
525 t#set "canonical_hostname" host.canonical_hostname;
527 (* This is a simplified version of the code in search.ml. *)
529 (* Get the keywords from the query string. *)
530 let keywords = Pcre.split ~rex:split_words search_terms in
532 List.filter (fun s -> not (string_is_whitespace s)) keywords in
533 let keywords = List.map lowercase keywords in
535 (* Turn the keywords into a tsearch2 ts_query string. *)
536 let tsquery = String.concat "&" keywords in
538 (* Search the titles first. *)
541 "select url, title, last_modified_date,
542 (lower (title) = lower ($search_terms)) as exact
544 where hostid = $hostid
547 and title_description_fti @@ to_tsquery ('default', $tsquery)
548 order by exact desc, last_modified_date desc, title" in
552 | (Some url, title, last_modified, _) ->
553 url, title, last_modified
554 | _ -> assert false) rows in
556 let have_titles = titles <> [] in
557 t#conditional "have_titles" have_titles;
559 (* Search the contents. *)
562 "select c.id, p.url, p.title, p.last_modified_date
563 from contents c, pages p
564 where c.pageid = p.id
565 and p.hostid = $hostid
567 and p.redirect is null
568 and c.content_fti @@ to_tsquery ('default', $tsquery)
569 order by p.last_modified_date desc, p.title
574 | (contentid, Some url, title, last_modified) ->
575 contentid, url, title, last_modified
576 | _ -> assert false) rows in
578 let have_contents = contents <> [] in
579 t#conditional "have_contents" have_contents;
581 (* Pull out the actual text which matched so we can generate a summary.
582 * XXX tsearch2 can actually do better than this by emboldening
583 * the text which maps.
586 if contents = [] then []
590 List.map (fun (contentid, _,_,_) -> contentid) contents in
592 "select id, sectionname, content from contents
593 where id in $@contentids" in
594 List.map (fun (id, sectionname, content) ->
595 id, (sectionname, content)) rows
598 (* Generate the final tables. *)
600 List.map (fun (url, title, last_modified) ->
601 let last_modified = printable_date last_modified in
602 [ "url", Template.VarString url;
603 "title", Template.VarString title;
604 "last_modified", Template.VarString last_modified ]
606 t#table "titles" table;
610 (fun (contentid, url, title, last_modified) ->
611 let sectionname, content = List.assoc contentid content_map in
612 let have_sectionname, sectionname =
613 match sectionname with
615 | Some sectionname -> true, sectionname in
618 (Wikilib.text_of_xhtml
619 (Wikilib.xhtml_of_content r dbh hostid content)) in
620 let linkname = linkname_of_sectionname sectionname in
621 let last_modified = printable_date last_modified in
622 [ "url", Template.VarString url;
623 "title", Template.VarString title;
624 "have_sectionname", Template.VarConditional have_sectionname;
625 "sectionname", Template.VarString sectionname;
626 "linkname", Template.VarString linkname;
627 "content", Template.VarString content;
628 "last_modified", Template.VarString last_modified ]
630 t#table "contents" table;
632 (* Do we have any results? *)
633 let have_results = have_titles || have_contents in
635 t#conditional "have_results" have_results;
637 (* Deliver the rest of the page. *)
638 ignore (print_string r t#to_string)
641 (* Fetch a page by name. This function can give three answers:
642 * (1) Page fetched OK (fetches some details of the page).
643 * (2) Page is a redirect (fetches the name of the redirect page).
644 * (3) Page not found in database, could be template or 404 error.
646 let fetch_page page version allow_redirect =
649 if allow_redirect then (
650 let rows = PGSQL(dbh)
651 "select url, redirect, id, title, description, keywords,
652 last_modified_date, css is not null, noodp
654 where hostid = $hostid and lower (url) = lower ($page)" in
656 | [Some page', _, _, _, _, _, _, _, _]
657 when page <> page' -> (* different case *)
658 FPExternalRedirect page'
659 | [ _, None, id, title, description, keywords,
660 last_modified_date, has_page_css, noodp ] ->
661 let has_page_css = Option.get has_page_css in
662 FPOK (id, title, description, keywords, last_modified_date,
664 | [_, Some redirect, _, _, _, _, _, _, _] ->
665 FPInternalRedirect redirect
668 ) else (* redirects not allowed ... *) (
669 let rows = PGSQL(dbh)
670 "select id, title, description, keywords, last_modified_date,
671 css is not null, noodp
672 from pages where hostid = $hostid and url = $page" in
674 | [ id, title, description, keywords,
675 last_modified_date, has_page_css, noodp ] ->
676 let has_page_css = Option.get has_page_css in
677 FPOK (id, title, description, keywords, last_modified_date,
683 let rows = PGSQL(dbh)
684 "select id, title, description, keywords, last_modified_date,
685 css is not null, noodp
687 where hostid = $hostid and id = $version and
688 (url = $page or url_deleted = $page)" in
690 | [ id, title, description, keywords,
691 last_modified_date, has_page_css, noodp ] ->
692 let has_page_css = Option.get has_page_css in
693 FPOK (id, title, description, keywords, last_modified_date,
699 (* Here we deal with the complex business of redirects and versions. *)
700 (* Only allow the no_redirect and version syntax for editors. *)
701 let allow_redirect, version =
703 not (q#param_true "no_redirect"),
704 try Some (Int32.of_string (q#param "version")) with Not_found -> None
708 let rec loop page' i =
709 if i > max_redirect then (
710 error ~title:"Too many redirections" ~back_button:true
712 ("Too many redirects between pages. This may happen because " ^
713 "of a cycle of redirections.");
716 match fetch_page page' version allow_redirect with
717 | FPOK (pageid, title, description, keywords,
718 last_modified_date, has_page_css, noodp)->
719 (* Check if the page is also a template. *)
720 let extension = get_extension page' in
721 make_page title (Some description) keywords (Some pageid)
722 (printable_date last_modified_date) has_page_css noodp
723 version page page' extension
724 | FPInternalRedirect page' ->
726 | FPExternalRedirect page' ->
727 (* This normally happens when a user has requested an uppercase
728 * page name. We redirect to the true (lowercase) version.
730 q#redirect ("http://" ^ host.hostname ^ "/" ^ page')
732 (* Might be a templated page with no content in it. *)
733 let extension = get_extension page' in
734 (match extension with
735 | (Some _) as extension ->
737 make_page title None None None
738 "Now" false None None page page'
746 register_script ~restrict:[CanView] run