Fix the bug with unrecognised extension pages turning up as redlinks
[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.56 2006/08/14 18:25:29 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 open Cocanwiki_extensions
37 open Cocanwiki_strings
38
39 type fp_status =
40   | FPOK of int32 * string * string * string option * Calendar.t * bool
41       * bool option
42   | FPInternalRedirect of string
43   | FPExternalRedirect of string
44   | FPNotFound
45
46 (* Referer strings which help us decide if the user came from
47  * a search engine and highlight terms in the page appropriately.
48  *)
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" ]
53 ]
54 let split_words = Pcre.regexp "\\W+"
55
56 let split_qs_re = Pcre.regexp "\\?"
57
58 let xhtml_re = Pcre.regexp "<.*?>|[^<>]+"
59
60 let run r (q : cgi) dbh hostid
61     ({ edit_anon = edit_anon; view_anon = view_anon } as host)
62     user =
63   let page = q#param "page" in
64   let page = if page = "" then "index" else page in
65
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
72    * send it out second.
73    *)
74
75   let template_page_header =
76     get_template ~page dbh hostid "page_header.html" in
77   let template_page = get_template ~page dbh hostid "page.html" in
78
79   (* This is the simpler template for 404 pages. *)
80   let template_404_header  = get_template dbh hostid "page_404_header.html" in
81   let template_404  = get_template dbh hostid "page_404.html" in
82
83   (* Host-specific fields. *)
84   let rows =
85     PGSQL(dbh)
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 =
89     match rows with
90       | [Some has_feedback_email, mailing_list, navigation] ->
91           has_feedback_email, mailing_list, navigation
92       | _ -> assert false in
93
94   (* User permissions. *)
95   let can_edit = can_edit host user in
96   let can_manage_users = can_manage_users host user in
97
98   (* Do we have a stats page set up? *)
99   let has_stats = server_settings_stats_page dbh <> None in
100
101   (* Given the referer string, return the list of search terms.  If none
102    * can be found, then throws Not_found.
103    *)
104   let search_terms_from_referer referer =
105     let _, argnames =
106       List.find (fun (rex, _) -> Pcre.pmatch ~rex referer) search_engines in
107     let url, qs =
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
113     let argname =
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
117   in
118
119   (* Given a full page of XHTML, highlight search terms found in the
120    * <body> part of the page.
121    *)
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/>" ]
127      *)
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
131
132     (* Find the <body> ... </body> tags.  We only want to apply
133      * highlighting to tags within this area.
134      *)
135     let rec list_split f acc = function
136       | [] -> List.rev acc, []
137       | ((x :: _) as xs) when f x -> List.rev acc, xs
138       | x :: xs ->
139           let acc = x :: acc in
140           list_split f acc xs
141     in
142     let head, body =
143       list_split (fun str -> String.starts_with str "<body") [] xhtml in
144     let body, tail =
145       list_split ((=) "</body>") [] body in
146     (* NB: Hopefully, xhtml = head @ body @ tail. *)
147
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)'.
151      *)
152     let rex =
153       Pcre.regexp ~flags:[`CASELESS]
154         ("(" ^ String.concat "|" search_terms ^ ")") in
155
156     (* Do the substitution, but only on text, not elements! *)
157     let body =
158       let subst text =
159         "<span class=\"" ^ span_class ^ "\">" ^ text ^ "</span>"
160       in
161       List.map (fun str ->
162                   if String.length str > 0 && str.[0] != '<' then
163                     Pcre.substitute ~rex ~subst str
164                   else
165                     str) body in
166
167     (* Join the XHTML fragments back together again. *)
168     String.concat "" (List.concat [ head ; body ; tail ])
169   in
170
171   (* Check the templates table for extensions. *)
172   let get_extension url =
173     try
174       let name =
175         List.hd (
176           PGSQL(dbh) "select extension from templates
177                        where $url ~ url_regexp
178                        order by ordering
179                        limit 1"
180         ) in
181       Some (List.assoc name !extensions)
182     with
183       Not_found | ExtList.List.Empty_list | Failure "hd" -> None
184   in
185
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 *)
193
194     (* Page title, h1 and superdirs (if any). *)
195     th#set "title" title;
196
197     let superdirs, h1 =
198       match String.nsplit title "/" with
199       | [] -> [], ""
200       | [h1] -> [], h1
201       | xs ->
202           let xs = List.rev xs in
203           let h1 = List.hd xs in
204           let superdirs = List.rev (List.tl xs) in
205
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.
209            *)
210           try
211             let pathsofar = ref "" in
212             let superdirs =
213               List.mapi (
214                 fun i name ->
215                   (* Path will be something like "Dir1/Dir2".  We want
216                    * a URL like "dir1/dir2".
217                    *)
218                   let path =
219                     if i = 0 then name else !pathsofar ^ "/" ^ name in
220                   (* Path so far reasonable? *)
221                   let url, redlink =
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 ->
230                         raise Exit in
231                   pathsofar := path;
232                   name, url, redlink
233               ) superdirs in
234             superdirs, h1
235           with
236             Exit -> [], title in
237
238     let superdirs = List.map (
239       fun (name, url, redlink) ->
240         let is_redlink, redlink_title =
241           match redlink with
242           | None -> false, ""
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 ]
248     ) superdirs in
249
250     th#conditional "has_superdirs" (superdirs <> []);
251     th#table "superdirs" superdirs;
252     th#set "h1" h1;
253
254     t#set "last_modified_date" last_modified_date;
255
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);
261
262     (match keywords with
263          None -> th#conditional "has_keywords" false
264        | Some keywords ->
265            th#conditional "has_keywords" true;
266            th#set "keywords" keywords);
267
268     if page <> page' then (* redirection *) (
269       t#set "page" page';
270       th#set "page" page';
271       t#set "original_page" page; (* XXX title - get it from database *)
272       t#conditional "redirected" true
273     ) else (
274       t#set "page" page;
275       th#set "page" page;
276       t#conditional "redirected" false
277     );
278
279     th#conditional "has_page_css" has_page_css;
280
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.
284      *)
285     (match noodp with
286      | None -> ()
287      | Some b -> th#conditional "noodp" b);
288
289     (* Are we showing an old version of the page?  If so, warn. *)
290     (match version with
291          None ->
292            t#conditional "is_old_version" false;
293            th#conditional "is_old_version" false
294        | Some pageid ->
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));
299
300     (* Just before we show the header, call any registered pre-page
301      * handlers.  They might want to send cookies.
302      *)
303     List.iter (fun handler ->
304                  handler r q dbh hostid page') !pre_page_handlers;
305
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.
309      *)
310     q#header ();
311     ignore (print_string r th#to_string);
312     ignore (Request.rflush r);
313
314     t#conditional "has_feedback_email" has_feedback_email;
315     t#conditional "mailing_list" mailing_list;
316     t#conditional "navigation" navigation;
317
318     t#conditional "can_edit" can_edit;
319     t#conditional "can_manage_users" can_manage_users;
320     t#conditional "has_stats" has_stats;
321
322     (* Pull out the sections in this page. *)
323     let sections =
324       match pageid with
325           None -> []
326         | Some pageid ->
327             let rows = PGSQL(dbh)
328               "select ordering, sectionname, content, divname, jsgo
329                  from contents where pageid = $pageid order by ordering" in
330
331             List.map
332               (fun (ordering, sectionname, content, divname, jsgo) ->
333                  let divname, has_divname =
334                    match divname with
335                    | None -> "", false
336                    | Some divname -> divname, true in
337                  let jsgo, has_jsgo =
338                    match jsgo with
339                    | None -> "", false
340                    | Some jsgo -> jsgo, true in
341                  let sectionname, has_sectionname =
342                    match sectionname with
343                    | None -> "", false
344                    | Some sectionname -> sectionname, true in
345                  let linkname = linkname_of_sectionname sectionname in
346                  [ "ordering", Template.VarString (Int32.to_string ordering);
347                    "has_sectionname",
348                      Template.VarConditional has_sectionname;
349                    "sectionname", Template.VarString sectionname;
350                    "linkname", Template.VarString linkname;
351                    "content",
352                      Template.VarString
353                        (Wikilib.xhtml_of_content r dbh hostid content);
354                    "has_divname", Template.VarConditional has_divname;
355                    "divname", Template.VarString divname;
356                    "has_jsgo", Template.VarConditional has_jsgo;
357                    "jsgo", Template.VarString jsgo ]) rows in
358
359     (* Call an extension to generate the first section in this page? *)
360     let sections =
361       match extension with
362           None -> sections
363         | Some extension ->
364             let content = extension r dbh hostid page' in
365             let section = [
366               "ordering", Template.VarString "0";
367               "has_sectionname", Template.VarConditional false;
368               "linkname", Template.VarString "";
369               "content", Template.VarString content;
370               "has_divname", Template.VarConditional true;
371               "divname", Template.VarString "form_div";
372               "has_jsgo", Template.VarConditional false;
373               "jsgo", Template.VarString "";
374             ] in
375             section :: sections in
376
377     t#table "sections" sections;
378
379     (* Login status. *)
380     (match user with
381          Anonymous ->
382            t#conditional "user_logged_in" false
383        | User (_, username, _, _) ->
384            t#conditional "user_logged_in" true;
385            t#set "username" username);
386
387     (* Can anonymous users create accounts?  If not them we don't
388      * want to offer to create accounts for them.
389      *)
390     t#conditional "create_account_anon" host.create_account_anon;
391
392     (* If logged in, we want to update the recently_visited table. *)
393     if pageid <> None then (
394       match user with
395         | User (userid, _, _, _) ->
396             (try
397                PGSQL(dbh)
398                  "delete from recently_visited
399                    where hostid = $hostid and userid = $userid
400                      and url = $page'";
401                PGSQL(dbh)
402                  "insert into recently_visited (hostid, userid, url)
403                   values ($hostid, $userid, $page')";
404                PGOCaml.commit dbh;
405              with
406                exn ->
407                  (* Exceptions here are non-fatal.  Just print them. *)
408                  prerr_endline "exception updating recently_visited:";
409                  prerr_endline (Printexc.to_string exn);
410                  PGOCaml.rollback dbh;
411             );
412             PGOCaml.begin_work dbh;
413         | _ -> ()
414     );
415
416     (* Navigation links. *)
417     if navigation then (
418       let max_links = 18 in             (* Show no more links than this. *)
419
420       (* What links here. *)
421       let wlh = what_links_here dbh hostid page' in
422       let wlh = List.take max_links wlh in
423       let wlh_urls = List.map fst wlh in (* Just the URLs ... *)
424
425       let rv =
426         match user with
427           | User (userid, _, _, _) ->
428               (* Recently visited URLs, but don't repeat any from the 'what
429                * links here' section, and don't link to self.
430                *)
431               let not_urls = page' :: wlh_urls in
432               let limit = Int32.of_int (max_links - List.length wlh_urls) in
433               let rows =
434                 PGSQL(dbh)
435                   "select rv.url, p.title, rv.visit_time
436                      from recently_visited rv, pages p
437                     where rv.hostid = $hostid and rv.userid = $userid
438                       and rv.url not in $@not_urls
439                       and rv.hostid = p.hostid and rv.url = p.url
440                     order by 3 desc
441                     limit $limit" in
442               List.map (
443                 fun (url, title, _) -> url, title
444               ) rows
445           | _ -> [] in
446
447       (* Links to page. *)
448       let f (page, title) = [ "page", Template.VarString page;
449                               "title", Template.VarString title ] in
450       let table = List.map f wlh in
451       t#table "what_links_here" table;
452       t#conditional "has_what_links_here" (wlh <> []);
453
454       let table = List.map f rv in
455       t#table "recently_visited" table;
456       t#conditional "has_recently_visited" (rv <> []);
457
458       (* If both lists are empty (ie. an empty navigation box would
459        * appear), then disable navigation altogether.
460        *)
461       if wlh = [] && rv = [] then t#conditional "navigation" false
462     );
463
464     (* If we are coming from a search engine then we want to highlight
465      * search terms throughout the whole page ...
466      *)
467     try
468       let referer = Table.get (Request.headers_in r) "Referer" in
469       let search_terms = search_terms_from_referer referer in
470
471       (* Highlight the search terms. *)
472       let xhtml = t#to_string in
473       let xhtml = highlight_search_terms xhtml search_terms "search_term" in
474
475       (* Deliver the page. *)
476       ignore (print_string r xhtml)
477     with
478         Not_found ->
479           (* No referer / no search terms / not a search engine referer. *)
480           ignore (print_string r t#to_string)
481   in
482
483   (* This code generates 404 pages. *)
484   let make_404 () =
485     Request.set_status r 404;           (* Return a 404 error code. *)
486
487     let th = template_404_header in
488     th#set "page" page;
489
490     let search_terms =
491       String.map
492         (function
493              ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
494            | _ -> ' ') page in
495
496     th#set "search_terms" search_terms;
497
498     (* Flush out the header while we start the search. *)
499     q#header ();
500     ignore (print_string r th#to_string);
501     ignore (Request.rflush r);
502
503     let t = template_404 in
504     t#set "query" search_terms;
505     t#set "canonical_hostname" host.canonical_hostname;
506
507     (* This is a simplified version of the code in search.ml. *)
508     let have_results =
509       (* Get the keywords from the query string. *)
510       let keywords = Pcre.split ~rex:split_words search_terms in
511       let keywords =
512         List.filter (fun s -> not (string_is_whitespace s)) keywords in
513       let keywords = List.map String.lowercase keywords in
514
515       (* Turn the keywords into a tsearch2 ts_query string. *)
516       let tsquery = String.concat "&" keywords in
517
518       (* Search the titles first. *)
519       let rows =
520         PGSQL(dbh)
521             "select url, title, last_modified_date,
522                     (lower (title) = lower ($search_terms)) as exact
523                from pages
524               where hostid = $hostid
525                 and url is not null
526                 and redirect is null
527                 and title_description_fti @@ to_tsquery ('default', $tsquery)
528               order by exact desc, last_modified_date desc, title" in
529
530       let titles =
531         List.map (function
532                   | (Some url, title, last_modified, _) ->
533                       url, title, last_modified
534                   | _ -> assert false) rows in
535
536       let have_titles = titles <> [] in
537       t#conditional "have_titles" have_titles;
538
539       (* Search the contents. *)
540       let rows =
541         PGSQL(dbh)
542           "select c.id, p.url, p.title, p.last_modified_date
543              from contents c, pages p
544             where c.pageid = p.id
545               and p.hostid = $hostid
546               and url is not null
547               and p.redirect is null
548               and c.content_fti @@ to_tsquery ('default', $tsquery)
549             order by p.last_modified_date desc, p.title
550             limit 50" in
551
552       let contents =
553         List.map (function
554                   | (contentid, Some url, title, last_modified) ->
555                       contentid, url, title, last_modified
556                   | _ -> assert false) rows in
557
558       let have_contents = contents <> [] in
559       t#conditional "have_contents" have_contents;
560
561       (* Pull out the actual text which matched so we can generate a summary.
562        * XXX tsearch2 can actually do better than this by emboldening
563        * the text which maps.
564        *)
565       let content_map =
566         if contents = [] then []
567         else (
568           let rows =
569             let contentids =
570               List.map (fun (contentid, _,_,_) -> contentid) contents in
571             PGSQL(dbh)
572               "select id, sectionname, content from contents
573                 where id in $@contentids" in
574           List.map (fun (id, sectionname, content) ->
575                       id, (sectionname, content)) rows
576         ) in
577
578       (* Generate the final tables. *)
579       let table =
580         List.map (fun (url, title, last_modified) ->
581                     let last_modified = printable_date last_modified in
582                     [ "url", Template.VarString url;
583                       "title", Template.VarString title;
584                       "last_modified", Template.VarString last_modified ]
585                  ) titles in
586       t#table "titles" table;
587
588       let table =
589         List.map
590           (fun (contentid, url, title, last_modified) ->
591              let sectionname, content = List.assoc contentid content_map in
592              let have_sectionname, sectionname =
593                match sectionname with
594                  None -> false, ""
595                | Some sectionname -> true, sectionname in
596              let content =
597                truncate 160
598                  (Wikilib.text_of_xhtml
599                     (Wikilib.xhtml_of_content r dbh hostid content)) in
600              let linkname = linkname_of_sectionname sectionname in
601              let last_modified = printable_date last_modified in
602              [ "url", Template.VarString url;
603                "title", Template.VarString title;
604                "have_sectionname", Template.VarConditional have_sectionname;
605                "sectionname", Template.VarString sectionname;
606                "linkname", Template.VarString linkname;
607                "content", Template.VarString content;
608                "last_modified", Template.VarString last_modified ]
609           ) contents in
610       t#table "contents" table;
611
612       (* Do we have any results? *)
613       let have_results = have_titles || have_contents in
614       have_results in
615     t#conditional "have_results" have_results;
616
617     (* Deliver the rest of the page. *)
618     ignore (print_string r t#to_string)
619   in
620
621   (* Fetch a page by name.  This function can give three answers:
622    * (1) Page fetched OK (fetches some details of the page).
623    * (2) Page is a redirect (fetches the name of the redirect page).
624    * (3) Page not found in database, could be template or 404 error.
625    *)
626   let fetch_page page version allow_redirect =
627     match version with
628       | None ->
629           if allow_redirect then (
630             let rows = PGSQL(dbh)
631               "select url, redirect, id, title, description, keywords,
632                       last_modified_date, css is not null, noodp
633                  from pages
634                 where hostid = $hostid and lower (url) = lower ($page)" in
635             match rows with
636             | [Some page', _, _, _, _, _, _, _, _]
637                 when page <> page' -> (* different case *)
638                 FPExternalRedirect page'
639             | [ _, None, id, title, description, keywords,
640                 last_modified_date, has_page_css, noodp ] ->
641                 let has_page_css = Option.get has_page_css in
642                 FPOK (id, title, description, keywords, last_modified_date,
643                       has_page_css, noodp)
644             | [_, Some redirect, _, _, _, _, _, _, _] ->
645                 FPInternalRedirect redirect
646             | [] -> FPNotFound
647             | _ -> assert false
648           ) else (* redirects not allowed ... *) (
649             let rows = PGSQL(dbh)
650               "select id, title, description, keywords, last_modified_date,
651                       css is not null, noodp
652                  from pages where hostid = $hostid and url = $page" in
653             match rows with
654             | [ id, title, description, keywords,
655                 last_modified_date, has_page_css, noodp ] ->
656                 let has_page_css = Option.get has_page_css in
657                 FPOK (id, title, description, keywords, last_modified_date,
658                       has_page_css, noodp)
659             | [] -> FPNotFound
660             | _ -> assert false
661           )
662       | Some version ->
663           let rows = PGSQL(dbh)
664             "select id, title, description, keywords, last_modified_date,
665                     css is not null, noodp
666                from pages
667               where hostid = $hostid and id = $version and
668                     (url = $page or url_deleted = $page)" in
669           match rows with
670           | [ id, title, description, keywords,
671               last_modified_date, has_page_css, noodp ] ->
672               let has_page_css = Option.get has_page_css in
673               FPOK (id, title, description, keywords, last_modified_date,
674                     has_page_css, noodp)
675           | [] -> FPNotFound
676           | _ -> assert false
677   in
678
679   (* Here we deal with the complex business of redirects and versions. *)
680   (* Only allow the no_redirect and version syntax for editors. *)
681   let allow_redirect, version =
682     if can_edit then (
683       not (q#param_true "no_redirect"),
684       try Some (Int32.of_string (q#param "version")) with Not_found -> None
685     ) else
686       (true, None) in
687
688   let rec loop page' i =
689     if i > max_redirect then (
690       error ~title:"Too many redirections" ~back_button:true
691         dbh hostid q
692         ("Too many redirects between pages.  This may happen because " ^
693          "of a cycle of redirections.");
694       return ()
695     ) else
696       match fetch_page page' version allow_redirect with
697         | FPOK (pageid, title, description, keywords,
698                 last_modified_date, has_page_css, noodp)->
699             (* Check if the page is also a template. *)
700             let extension = get_extension page' in
701             make_page title (Some description) keywords (Some pageid)
702               (printable_date last_modified_date) has_page_css noodp
703               version page page' extension
704         | FPInternalRedirect page' ->
705             loop page' (i+1)
706         | FPExternalRedirect page' ->
707             (* This normally happens when a user has requested an uppercase
708              * page name.  We redirect to the true (lowercase) version.
709              *)
710             q#redirect ("http://" ^ host.hostname ^ "/" ^ page')
711         | FPNotFound ->
712             (* Might be a templated page with no content in it. *)
713             let extension = get_extension page' in
714             (match extension with
715                | (Some _) as extension ->
716                    let title = page' in
717                    make_page title None None None
718                      "Now" false None None page page'
719                      extension
720                | None ->
721                    make_404 ())
722   in
723   loop page 0
724
725 let () =
726   register_script ~restrict:[CanView] run