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