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