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.36 2004/10/23 15:00:15 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 | FPRedirect of string
41 (* Referer strings which help us decide if the user came from
42 * a search engine and highlight terms in the page appropriately.
44 let search_engines = [
45 Pcre.regexp "^http://.*google\\.", [ "q"; "as_q"; "as_epq"; "as_oq" ];
46 Pcre.regexp "^http://.*yahoo\\.", [ "p" ];
47 Pcre.regexp "^http://.*msn\\.", [ "q"; "MT" ]
49 let split_words = Pcre.regexp "\\W+"
51 let split_qs_re = Pcre.regexp "\\?"
53 let xhtml_re = Pcre.regexp "<.*?>|[^<>]+"
55 let run r (q : cgi) (dbh : Dbi.connection) hostid
56 ({ edit_anon = edit_anon;
57 view_anon = view_anon } as host)
59 let page = q#param "page" in
60 let page = if page = "" then "index" else page in
62 let template_page = get_template ~page dbh hostid "page.html" in
63 let template_404 = get_template dbh hostid "page_404.html" in
65 (* Host-specific fields. *)
66 let sth = dbh#prepare_cached "select css is not null,
67 feedback_email is not null,
68 mailing_list, search_box, navigation
69 from hosts where id = ?" in
70 sth#execute [`Int hostid];
71 let has_host_css, has_feedback_email, mailing_list, search_box, navigation =
72 match sth#fetch1 () with
73 | [ `Bool has_host_css; `Bool has_feedback_email; `Bool mailing_list;
74 `Bool search_box; `Bool navigation ] ->
75 has_host_css, has_feedback_email, mailing_list, search_box,
77 | _ -> assert false in
79 (* User permissions. *)
80 let can_edit = can_edit host user in
81 let can_manage_users = can_manage_users host user in
83 (* Do we have a stats page set up? *)
84 let has_stats = server_settings_stats_page dbh <> None in
86 (* Given the referer string, return the list of search terms. If none
87 * can be found, then throws Not_found.
89 let search_terms_from_referer referer =
91 List.find (fun (rex, _) -> Pcre.pmatch ~rex referer) search_engines in
93 match Pcre.split ~rex:split_qs_re ~max:2 referer with
94 | [url] | [url;""] -> url, ""
96 | _ -> assert false in
97 let args = Cgi_args.parse qs in
99 List.find (fun argname -> List.mem_assoc argname args) argnames in
100 let search_string = List.assoc argname args in
101 Pcre.split ~rex:split_words search_string
104 (* Given a full page of XHTML, highlight search terms found in the
105 * <body> part of the page.
107 let highlight_search_terms xhtml search_terms span_class =
108 (* Split the original XHTML into strings and tags. For example if
109 * the original string is: "This is some <b>bold</b> text.<br/>", then
110 * after this step we will have the following list:
111 * [ "This is some "; "<b>"; "bold"; "</b>"; " text."; "<br/>" ]
113 let xhtml = Pcre.extract_all ~rex:xhtml_re xhtml in
114 let xhtml = Array.to_list xhtml in
115 let xhtml = List.map (fun matches -> matches.(0)) xhtml in
117 (* Find the <body> ... </body> tags. We only want to apply
118 * highlighting to tags within this area.
120 let rec list_split f acc = function
121 | [] -> List.rev acc, []
122 | ((x :: _) as xs) when f x -> List.rev acc, xs
124 let acc = x :: acc in
128 list_split (fun str -> String.starts_with str "<body") [] xhtml in
130 list_split ((=) "</body>") [] body in
131 (* NB: Hopefully, xhtml = head @ body @ tail. *)
133 (* The search terms are a list of simple words. Turn into a big
134 * regular expression, because we want to substitute for each. We
135 * end up with a regexp like '(word1|word2|word3)'.
138 Pcre.regexp ~flags:[`CASELESS]
139 ("(" ^ String.concat "|" search_terms ^ ")") in
141 (* Do the substitution, but only on text, not elements! *)
144 "<span class=\"" ^ span_class ^ "\">" ^ text ^ "</span>"
147 if String.length str > 0 && str.[0] != '<' then
148 Pcre.substitute ~rex ~subst str
152 (* Join the XHTML fragments back together again. *)
153 String.concat "" (List.concat [ head ; body ; tail ])
156 (* Check the templates table for extensions. *)
157 let get_extension url =
158 let sth = dbh#prepare_cached "select extension from templates
162 sth#execute [`String url];
165 let name = sth#fetch1string () in
166 Some (List.assoc name !extensions)
171 (* This code generates ordinary pages. *)
172 let make_page title description pageid last_modified_date has_page_css
173 version page page' extension =
174 let t = template_page in
176 t#set "last_modified_date" last_modified_date;
178 (match description with
179 None -> t#conditional "has_description" false
180 | Some description ->
181 t#conditional "has_description" true;
182 t#set "description" description);
184 if page <> page' then (* redirection *) (
186 t#set "original_page" page; (* XXX title - get it from database *)
187 t#conditional "redirected" true
190 t#conditional "redirected" false
193 t#conditional "has_host_css" has_host_css;
194 t#conditional "has_page_css" has_page_css;
196 t#conditional "has_feedback_email" has_feedback_email;
197 t#conditional "mailing_list" mailing_list;
198 t#conditional "search_box" search_box;
199 t#conditional "navigation" navigation;
201 t#conditional "can_edit" can_edit;
202 t#conditional "can_manage_users" can_manage_users;
203 t#conditional "has_stats" has_stats;
205 (* Pull out the sections in this page. *)
210 let sth = dbh#prepare_cached
211 "select ordering, sectionname, content, divname
212 from contents where pageid = ? order by ordering" in
213 sth#execute [`Int pageid];
216 (function [`Int ordering;
217 (`Null | `String _) as sectionname;
219 (`Null | `String _) as divname] ->
220 let divname, has_divname =
223 | `String divname -> divname, true in
224 let sectionname, has_sectionname =
225 match sectionname with
227 | `String sectionname -> sectionname, true in
228 let linkname = linkname_of_sectionname sectionname in
229 [ "ordering", Template.VarString (string_of_int ordering);
231 Template.VarConditional has_sectionname;
232 "sectionname", Template.VarString sectionname;
233 "linkname", Template.VarString linkname;
236 (Wikilib.xhtml_of_content dbh hostid content);
237 "has_divname", Template.VarConditional has_divname;
238 "divname", Template.VarString divname ]
239 | _ -> assert false) in
241 (* Call an extension to generate the first section in this page? *)
246 let content = extension dbh hostid page' in
248 "ordering", Template.VarString "0";
249 "has_sectionname", Template.VarConditional false;
250 "linkname", Template.VarString "";
251 "content", Template.VarString content;
252 "has_divname", Template.VarConditional true;
253 "divname", Template.VarString "form_div";
255 section :: sections in
257 t#table "sections" sections;
259 (* Are we showing an old version of the page? If so, warn. *)
262 t#conditional "is_old_version" false
264 t#conditional "is_old_version" true;
265 t#set "old_version" (string_of_int pageid));
270 t#conditional "user_logged_in" false
271 | User (_, username, _) ->
272 t#conditional "user_logged_in" true;
273 t#set "username" username);
275 (* If logged in, we want to update the recently_visited table. *)
276 if pageid <> None then (
278 | User (userid, _, _) ->
279 let sth = dbh#prepare_cached "delete from recently_visited
280 where hostid = ? and userid = ?
282 sth#execute [`Int hostid; `Int userid; `String page'];
283 let sth = dbh#prepare_cached
284 "insert into recently_visited (hostid, userid, url)
286 sth#execute [`Int hostid; `Int userid; `String page'];
291 (* Navigation links. *)
293 let max_links = 18 in (* Show no more links than this. *)
295 (* What links here. *)
296 let wlh = what_links_here dbh hostid page' in
297 let wlh = List.take max_links wlh in
298 let wlh_urls = List.map fst wlh in (* Just the URLs ... *)
302 | User (userid, _, _) ->
303 (* Recently visited URLs, but don't repeat any from the 'what
304 * links here' section, and don't link to self.
306 let not_urls = page' :: wlh_urls in
307 let limit = max_links - List.length wlh_urls in
308 let qs = Dbi.placeholders (List.length not_urls) in
311 ("select rv.url, p.title, rv.visit_time
312 from recently_visited rv, pages p
313 where rv.hostid = ? and rv.userid = ?
314 and rv.url not in " ^ qs ^ "
315 and rv.hostid = p.hostid and rv.url = p.url
318 let args = List.map (fun s -> `String s) not_urls in
320 ([`Int hostid; `Int userid] @ args @ [`Int limit]);
322 (function [`String url; `String title; _] ->
328 let f (page, title) = [ "page", Template.VarString page;
329 "title", Template.VarString title ] in
330 let table = List.map f wlh in
331 t#table "what_links_here" table;
332 t#conditional "has_what_links_here" (wlh <> []);
334 let table = List.map f rv in
335 t#table "recently_visited" table;
336 t#conditional "has_recently_visited" (rv <> []);
338 (* If both lists are empty (ie. an empty navigation box would
339 * appear), then disable navigation altogether.
341 if wlh = [] && rv = [] then t#conditional "navigation" false
344 (* If we are coming from a search engine then we want to highlight
345 * search terms throughout the whole page ...
348 let referer = Table.get (Request.headers_in r) "Referer" in
349 let search_terms = search_terms_from_referer referer in
351 (* Highlight the search terms. *)
352 let xhtml = t#to_string in
353 let xhtml = highlight_search_terms xhtml search_terms "search_term" in
355 (* Deliver the page. *)
357 ignore (print_string r xhtml)
360 (* No referer / no search terms / not a search engine referer. *)
364 (* This code generates 404 pages. *)
366 Request.set_status r 404; (* Return a 404 error code. *)
368 let t = template_404 in
374 ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
377 t#set "search_terms" search_terms;
379 t#conditional "has_host_css" has_host_css;
380 t#conditional "can_edit" can_edit;
381 t#conditional "can_manage_users" can_manage_users;
382 t#conditional "has_stats" has_stats;
387 (* Fetch a page by name. This function can give three answers:
388 * (1) Page fetched OK (fetches some details of the page).
389 * (2) Page is a redirect (fetches the name of the redirect page).
390 * (3) Page not found in database, could be template or 404 error.
392 (* XXX Should do a case-insensitive matching of URLs, and if the URL differs
393 * in case only should redirect to the lowercase version.
395 let fetch_page page version allow_redirect =
398 if allow_redirect then (
401 "select redirect, id, title, description, last_modified_date,
403 from pages where hostid = ? and url = ?" in
404 sth#execute [`Int hostid; `String page];
406 (match sth#fetch1 () with
407 | [ `Null; `Int id; `String title; `String description;
408 `Timestamp last_modified_date; `Bool has_page_css ] ->
409 FPOK (id, title, description, last_modified_date,
411 | `String redirect :: _ ->
415 Not_found -> FPNotFound)
416 ) else (* redirects not allowed ... *) (
419 "select id, title, description, last_modified_date,
421 from pages where hostid = ? and url = ?" in
422 sth#execute [`Int hostid; `String page];
424 (match sth#fetch1 () with
425 | [ `Int id; `String title; `String description;
426 `Timestamp last_modified_date; `Bool has_page_css ] ->
427 FPOK (id, title, description, last_modified_date,
431 Not_found -> FPNotFound)
436 "select id, title, description, last_modified_date,
439 where hostid = ? and id = ? and
440 (url = ? or url_deleted = ?)" in
441 sth#execute [`Int hostid; `Int version;
442 `String page; `String page];
444 (match sth#fetch1 () with
445 | [ `Int id; `String title; `String description;
446 `Timestamp last_modified_date; `Bool has_page_css ] ->
447 FPOK (id, title, description, last_modified_date,
451 Not_found -> FPNotFound)
454 (* Here we deal with the complex business of redirects and versions. *)
455 (* Only allow the no_redirect and version syntax for editors. *)
456 let allow_redirect, version =
458 not (q#param_true "no_redirect"),
459 try Some (int_of_string (q#param "version")) with Not_found -> None
463 let rec loop page' i =
464 if i > max_redirect then (
465 error ~title:"Too many redirections" ~back_button:true
466 q ("Too many redirects between pages. This may happen because " ^
467 "of a cycle of redirections.");
470 match fetch_page page' version allow_redirect with
471 | FPOK (pageid, title, description, last_modified_date, has_page_css)->
472 (* Check if the page is also a template. *)
473 let extension = get_extension page' in
474 make_page title (Some description) (Some pageid)
475 (printable_date last_modified_date) has_page_css
476 version page page' extension
477 | FPRedirect page' ->
480 (* Might be a templated page with no content in it. *)
481 let extension = get_extension page' in
482 (match extension with
483 | (Some _) as extension ->
485 make_page title None None
486 "Now" false None page page'
494 register_script ~restrict:[CanView] run