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