Changes done on the Mac.
[cocanwiki.git] / scripts / page.ml
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 $
5  *
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.
10  *
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.
15  *
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.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open ExtString
28 open ExtList
29
30 open Cocanwiki
31 open Cocanwiki_template
32 open Cocanwiki_ok
33 open Cocanwiki_date
34 open Cocanwiki_server_settings
35 open Cocanwiki_links
36
37 type fp_status = FPOK of int * string * string * Dbi.datetime * bool
38                | FPInternalRedirect of string
39                | FPExternalRedirect of string
40                | FPNotFound
41
42 (* Referer strings which help us decide if the user came from
43  * a search engine and highlight terms in the page appropriately.
44  *)
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" ]
49 ]
50 let split_words = Pcre.regexp "\\W+"
51
52 let split_qs_re = Pcre.regexp "\\?"
53
54 let xhtml_re = Pcre.regexp "<.*?>|[^<>]+"
55
56 let run r (q : cgi) dbh hostid
57     ({ edit_anon = edit_anon; view_anon = view_anon } as host)
58     user =
59   let page = q#param "page" in
60   let page = if page = "" then "index" else page in
61
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
68    * send it out second.
69    *)
70
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
74
75   (* This is the simpler template for 404 pages. *)
76   let template_404  = get_template dbh hostid "page_404.html" in
77
78   (* Host-specific fields. *)
79   let rows = PGSQL(dbh)
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 =
83     match rows with
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
88
89   (* User permissions. *)
90   let can_edit = can_edit host user in
91   let can_manage_users = can_manage_users host user in
92
93   (* Do we have a stats page set up? *)
94   let has_stats = server_settings_stats_page dbh <> None in
95
96   (* Given the referer string, return the list of search terms.  If none
97    * can be found, then throws Not_found.
98    *)
99   let search_terms_from_referer referer =
100     let _, argnames =
101       List.find (fun (rex, _) -> Pcre.pmatch ~rex referer) search_engines in
102     let url, qs =
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
108     let argname =
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
112   in
113
114   (* Given a full page of XHTML, highlight search terms found in the
115    * <body> part of the page.
116    *)
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/>" ]
122      *)
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
126
127     (* Find the <body> ... </body> tags.  We only want to apply
128      * highlighting to tags within this area.
129      *)
130     let rec list_split f acc = function
131       | [] -> List.rev acc, []
132       | ((x :: _) as xs) when f x -> List.rev acc, xs
133       | x :: xs ->
134           let acc = x :: acc in
135           list_split f acc xs
136     in
137     let head, body =
138       list_split (fun str -> String.starts_with str "<body") [] xhtml in
139     let body, tail =
140       list_split ((=) "</body>") [] body in
141     (* NB: Hopefully, xhtml = head @ body @ tail. *)
142
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)'.
146      *)
147     let rex =
148       Pcre.regexp ~flags:[`CASELESS]
149         ("(" ^ String.concat "|" search_terms ^ ")") in
150
151     (* Do the substitution, but only on text, not elements! *)
152     let body =
153       let subst text =
154         "<span class=\"" ^ span_class ^ "\">" ^ text ^ "</span>"
155       in
156       List.map (fun str ->
157                   if String.length str > 0 && str.[0] != '<' then
158                     Pcre.substitute ~rex ~subst str
159                   else
160                     str) body in
161
162     (* Join the XHTML fragments back together again. *)
163     String.concat "" (List.concat [ head ; body ; tail ])
164   in
165
166   (* Check the templates table for extensions. *)
167   let get_extension url =
168     try
169       let name =
170         List.hd (
171           PGSQL(dbh) "select extension from templates
172                        where $url ~ url_regexp
173                        order by ordering
174                        limit 1"
175         ) in
176       Some (List.assoc name !extensions)
177     with
178       Not_found | ExtList.List.Empty_list -> None
179   in
180
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
186     t#set "title" title;
187     th#set "title" title;
188     t#set "last_modified_date" last_modified_date;
189
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);
195
196     if page <> page' then (* redirection *) (
197       t#set "page" page';
198       th#set "page" page';
199       t#set "original_page" page; (* XXX title - get it from database *)
200       t#conditional "redirected" true
201     ) else (
202       t#set "page" page;
203       th#set "page" page;
204       t#conditional "redirected" false
205     );
206
207     th#conditional "has_host_css" has_host_css;
208     th#conditional "has_page_css" has_page_css;
209
210     (* Are we showing an old version of the page?  If so, warn. *)
211     (match version with
212          None ->
213            t#conditional "is_old_version" false;
214            th#conditional "is_old_version" false
215        | Some pageid ->
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));
220
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.
224      *)
225     q#header ();
226     print_string r th#to_string;
227     Request.rflush r;
228
229     t#conditional "has_feedback_email" has_feedback_email;
230     t#conditional "mailing_list" mailing_list;
231     t#conditional "navigation" navigation;
232
233     t#conditional "can_edit" can_edit;
234     t#conditional "can_manage_users" can_manage_users;
235     t#conditional "has_stats" has_stats;
236
237     (* Pull out the sections in this page. *)
238     let sections =
239       match pageid with
240           None -> []
241         | Some pageid ->
242             let rows = PGSQL(dbh)
243               "select ordering, sectionname, content, divname
244                  from contents where pageid = $pageid order by ordering" in
245
246             List.map
247               (fun (ordering, sectionname, content, divname) ->
248                  let divname, has_divname =
249                    match divname with
250                    | None -> "", false
251                    | Some divname -> divname, true in
252                  let sectionname, has_sectionname =
253                    match sectionname with
254                    | None -> "", false
255                    | Some sectionname -> sectionname, true in
256                  let linkname = linkname_of_sectionname sectionname in
257                  [ "ordering", Template.VarString (Int32.to_string ordering);
258                    "has_sectionname",
259                      Template.VarConditional has_sectionname;
260                    "sectionname", Template.VarString sectionname;
261                    "linkname", Template.VarString linkname;
262                    "content",
263                      Template.VarString
264                        (Wikilib.xhtml_of_content dbh hostid content);
265                    "has_divname", Template.VarConditional has_divname;
266                    "divname", Template.VarString divname ]) rows in
267
268     (* Call an extension to generate the first section in this page? *)
269     let sections =
270       match extension with
271           None -> sections
272         | Some extension ->
273             let content = extension dbh hostid page' in
274             let section = [
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";
281             ] in
282             section :: sections in
283
284     t#table "sections" sections;
285
286     (* Login status. *)
287     (match user with
288          Anonymous ->
289            t#conditional "user_logged_in" false
290        | User (_, username, _, _) ->
291            t#conditional "user_logged_in" true;
292            t#set "username" username);
293
294     (* If logged in, we want to update the recently_visited table. *)
295     if pageid <> None then (
296       match user with
297         | User (userid, _, _, _) ->
298             PGSQL(dbh)
299               "delete from recently_visited
300                 where hostid = $hostid and userid = $userid and url = $page'";
301             PGSQL(dbh)
302               "insert into recently_visited (hostid, userid, url)
303                values ($hostid, $userid, $page')";
304             PGOCaml.commit dbh;
305             PGOCaml.begin_work dbh;
306         | _ -> ()
307     );
308
309     (* Navigation links. *)
310     if navigation then (
311       let max_links = 18 in             (* Show no more links than this. *)
312
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 ... *)
317
318       let rv =
319         match user with
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.
323                *)
324               let not_urls = page' :: wlh_urls in
325               let limit = Int32.of_int (max_links - List.length wlh_urls) in
326               let rows =
327                 PGSQL(dbh)
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
333                     order by 3 desc
334                     limit $limit" in
335               List.map (
336                 fun (url, title, _) -> url, title
337               ) rows
338           | _ -> [] in
339
340       (* Links to page. *)
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 <> []);
346
347       let table = List.map f rv in
348       t#table "recently_visited" table;
349       t#conditional "has_recently_visited" (rv <> []);
350
351       (* If both lists are empty (ie. an empty navigation box would
352        * appear), then disable navigation altogether.
353        *)
354       if wlh = [] && rv = [] then t#conditional "navigation" false
355     );
356
357     (* If we are coming from a search engine then we want to highlight
358      * search terms throughout the whole page ...
359      *)
360     try
361       let referer = Table.get (Request.headers_in r) "Referer" in
362       let search_terms = search_terms_from_referer referer in
363
364       (* Highlight the search terms. *)
365       let xhtml = t#to_string in
366       let xhtml = highlight_search_terms xhtml search_terms "search_term" in
367
368       (* Deliver the page. *)
369       ignore (print_string r xhtml)
370     with
371         Not_found ->
372           (* No referer / no search terms / not a search engine referer. *)
373           ignore (print_string r t#to_string)
374   in
375
376   (* This code generates 404 pages. *)
377   let make_404 () =
378     Request.set_status r 404;           (* Return a 404 error code. *)
379
380     let t = template_404 in
381     t#set "page" page;
382
383     let search_terms =
384       String.map
385         (function
386              ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
387            | _ -> ' ') page in
388
389     t#set "search_terms" search_terms;
390
391     t#conditional "can_edit" can_edit;
392     t#conditional "can_manage_users" can_manage_users;
393     t#conditional "has_stats" has_stats;
394
395     q#template t
396   in
397
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.
402    *)
403   let fetch_page page version allow_redirect =
404     match version with
405       | None ->
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
410                  from pages
411                 where hostid = $hostid and lower (url) = lower ($page)" in
412             match rows with
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,
420                       has_page_css)
421             | [_, Some redirect, _, _, _, _, _] ->
422                 FPInternalRedirect redirect
423             | [] -> FPNotFound
424             | _ -> assert false
425           ) else (* redirects not allowed ... *) (
426             let rows = PGSQL(dbh)
427               "select id, title, description, last_modified_date,
428                       css is not null
429                  from pages where hostid = $hostid and url = $page" in
430             match rows with
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,
435                       has_page_css)
436             | [] -> FPNotFound
437             | _ -> assert false
438           )
439       | Some version ->
440           let rows = PGSQL(dbh)
441             "select id, title, description, last_modified_date,
442                     css is not null
443                from pages
444               where hostid = $hostid and id = $version and
445                     (url = $page or url_deleted = $page)" in
446           match rows with
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,
451                     has_page_css)
452           | [] -> FPNotFound
453           | _ -> assert false
454   in
455
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 =
459     if can_edit then (
460       not (q#param_true "no_redirect"),
461       try Some (Int32.of_string (q#param "version")) with Not_found -> None
462     ) else
463       (true, None) in
464
465   let rec loop page' i =
466     if i > max_redirect then (
467       error ~title:"Too many redirections" ~back_button:true
468         dbh hostid q
469         ("Too many redirects between pages.  This may happen because " ^
470          "of a cycle of redirections.");
471       return ()
472     ) else
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' ->
481             loop page' (i+1)
482         | FPExternalRedirect page' ->
483             (* This normally happens when a user has request an uppercase
484              * page name.  We redirect to the true (lowercase) version.
485              *)
486             q#redirect ("http://" ^ host.hostname ^ "/" ^ page');
487             return ()
488         | FPNotFound ->
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 ->
493                    let title = page' in
494                    make_page title None None
495                      "Now" false None page page'
496                      extension
497                | None ->
498                    make_404 ())
499   in
500   loop page 0
501
502 let () =
503   register_script ~restrict:[CanView] run