900b8b2bbef8381ea34fb667039f98e7badc04d1
[cocanwiki.git] / scripts / page.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: page.ml,v 1.2 2004/09/07 13:40:10 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open ExtString
13
14 open Merjisforwiki
15
16 open Cocanwiki
17 open Cocanwiki_template
18 open Cocanwiki_ok
19
20 let template_page = get_template "page.html"
21 let template_404  = get_template "page_404.html"
22
23 (* Maximum level of redirection. *)
24 let max_redirect = 4
25
26 type fp_status = FPOK of int * string * string * Dbi.datetime * bool
27                | FPRedirect of string
28                | FPNotFound
29
30 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ =
31   let page = q#param "page" in
32   let page = if page = "" then "index" else page in
33
34   (* Host-specific CSS? *)
35   let sth = dbh#prepare_cached "select css is not null from hosts
36                                  where id = ?" in
37   sth#execute [`Int hostid];
38   let has_host_css =
39     match sth#fetch1 () with
40       | [ `Bool has_host_css ] -> has_host_css
41       | _ -> assert false in
42
43   (* This code generates ordinary pages. *)
44   let make_page title description pageid last_modified_date has_page_css
45       version page page' =
46     let t = template_page in
47     t#set "title" title;
48     t#set "description" description;
49     t#set "pageid" (string_of_int pageid);
50     t#set "last_modified_date" (printable_date last_modified_date);
51     t#set "cocanwiki_package" Cocanwiki_version.package;
52     t#set "cocanwiki_version" Cocanwiki_version.version;
53
54     if page <> page' then (* redirection *) (
55       t#set "page" page';
56       t#set "original_page" page; (* XXX title - get it from database *)
57       t#conditional "redirected" true
58     ) else (
59       t#set "page" page;
60       t#conditional "redirected" false
61     );
62
63     t#conditional "has_host_css" has_host_css;
64     t#conditional "has_page_css" has_page_css;
65
66     (* Pull out the sections in this page. *)
67     let sth = dbh#prepare_cached
68                 "select ordering, sectionname, content, divname
69                    from contents
70                   where pageid = ?
71                   order by ordering" in
72     sth#execute [`Int pageid];
73
74     let sections =
75       sth#map
76         (function [`Int ordering; `String sectionname; `String content;
77                    (`Null | `String _) as divname] ->
78            let divname, has_divname =
79              match divname with
80                  `Null -> "", false
81                | `String divname -> divname, true in
82            [ "ordering", Template.VarString (string_of_int ordering);
83              "sectionname", Template.VarString sectionname;
84              "content",
85                Template.VarString
86                  (Wikilib.xhtml_of_content dbh hostid content);
87              "has_divname", Template.VarConditional has_divname;
88              "divname", Template.VarString divname ]
89            | _ -> assert false) in
90
91     t#table "sections" sections;
92
93     (* Are we showing an old version of the page?  If so, warn. *)
94     (match version with
95          None ->
96            t#conditional "is_old_version" false
97        | Some pageid ->
98            t#conditional "is_old_version" true;
99            t#set "old_version" (string_of_int pageid));
100
101     q#template t
102   in
103
104   (* This code generates 404 pages. *)
105   let make_404 () =
106     Request.set_status r 404;           (* Return a 404 error code. *)
107
108     let t = template_404 in
109     t#set "page" page;
110
111     let search_terms =
112       String.map
113         (function
114              ('a'..'z' | 'A'..'Z' | '0'..'9') as c -> c
115            | _ -> ' ') page in
116
117     template_404#set "search_terms" search_terms;
118
119     q#template t
120   in
121
122   (* Fetch a page by name.  This function can give three answers:
123    * (1) Page fetched OK (fetches some details of the page).
124    * (2) Page is a redirect (fetches the name of the redirect page).
125    * (3) Page not found in database, ie. 404 error.
126    *)
127   (* XXX Should do a case-insensitive matching of URLs, and if the URL differs
128    * in case only should redirect to the lowercase version.
129    *)
130   let fetch_page page version allow_redirect =
131     match version with
132       | None ->
133           if allow_redirect then (
134             let sth =
135               dbh#prepare_cached
136                 "select redirect, id, title, description, last_modified_date,
137                         css is not null
138                    from pages where hostid = ? and url = ?" in
139             sth#execute [`Int hostid; `String page];
140             (try
141                (match sth#fetch1 () with
142                   | [ `Null; `Int id; `String title; `String description;
143                       `Timestamp last_modified_date; `Bool has_page_css ] ->
144                       FPOK (id, title, description, last_modified_date,
145                             has_page_css)
146                   | `String redirect :: _ ->
147                       FPRedirect redirect
148                   | _ -> assert false)
149              with
150                  Not_found -> FPNotFound)
151           ) else (* redirects not allowed ... *) (
152             let sth =
153               dbh#prepare_cached
154                 "select id, title, description, last_modified_date,
155                         css is not null
156                    from pages where hostid = ? and url = ?" in
157             sth#execute [`Int hostid; `String page];
158             (try
159                (match sth#fetch1 () with
160                   | [ `Int id; `String title; `String description;
161                       `Timestamp last_modified_date; `Bool has_page_css ] ->
162                       FPOK (id, title, description, last_modified_date,
163                             has_page_css)
164                   | _ -> assert false)
165              with
166                  Not_found -> FPNotFound)
167           )
168       | Some version ->
169             let sth =
170               dbh#prepare_cached
171                 "select id, title, description, last_modified_date,
172                         css is not null
173                    from pages
174                   where hostid = ? and id = ? and
175                         (url = ? or url_deleted = ?)" in
176             sth#execute [`Int hostid; `Int version;
177                          `String page; `String page];
178             (try
179                (match sth#fetch1 () with
180                   | [ `Int id; `String title; `String description;
181                       `Timestamp last_modified_date; `Bool has_page_css ] ->
182                       FPOK (id, title, description, last_modified_date,
183                             has_page_css)
184                   | _ -> assert false)
185              with
186                  Not_found -> FPNotFound)
187   in
188
189   (* Here we deal with the complex business of redirects and versions. *)
190   let allow_redirect = not (q#param_true "no_redirect") in
191   let version =
192     try Some (int_of_string (q#param "version")) with Not_found -> None in
193
194   let rec loop page' i =
195     if i > max_redirect then (
196       error ~title:"Too many redirections" ~back_button:true
197         q ("Too many redirects between pages.  This may happen because " ^
198            "of a cycle of redirections.");
199       raise CgiExit
200     ) else
201       match fetch_page page' version allow_redirect with
202         | FPOK (pageid, title, description, last_modified_date, has_page_css)->
203             make_page title description pageid last_modified_date has_page_css
204               version page page'
205         | FPRedirect page' ->
206             loop page' (i+1)
207         | FPNotFound ->
208             make_404 ()
209   in
210   loop page 0
211
212 let () =
213   register_script run