select count now returns an int8. This fixes the broken 'Recent changes' pages.
[cocanwiki.git] / scripts / recent.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: recent.ml,v 1.10 2005/11/23 11:32:13 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 Cocanwiki
28 open Cocanwiki_template
29 open Cocanwiki_date
30
31 let default_offset = 0
32 let default_limit = 100
33 let max_limit = 1000
34
35 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
36   let template = get_template dbh hostid "recent.html" in
37
38   (* Count the number of changes. *)
39   let sth = dbh#prepare_cached
40     "select count(*)::int4 from pages where hostid = ?" in
41   sth#execute [`Int hostid];
42   let count = sth#fetch1int () in
43
44   (* Get the offset and limit specified, and adjust them so that we will
45    * be displaying some changes.
46    *)
47   let offset =
48     try int_of_string (q#param "offset") with Not_found -> default_offset in
49   let limit =
50     try int_of_string (q#param "limit") with Not_found -> default_limit in
51
52   let limit =
53     if limit < 1 then 1
54     else if limit > max_limit then max_limit
55     else limit in
56   let offset =
57     if offset < 0 then 0
58     else if offset >= count then max 0 (count - limit)
59     else offset in
60
61   template#set "offset" (string_of_int offset);
62   template#set "last" (string_of_int (min (offset + limit) count - 1));
63   template#set "limit" (string_of_int limit);
64   template#set "count" (string_of_int count);
65
66   template#conditional "has_next" (offset + limit < count);
67   template#set "next_offset" (string_of_int (offset + limit));
68   template#conditional "has_prev" (offset > 0);
69   template#set "prev_offset" (string_of_int (max 0 (offset - limit)));
70
71   (* Get the actual changes. *)
72   let sth =
73     dbh#prepare_cached
74       "select p.id, p.url, p.url_deleted, p.title, p.last_modified_date,
75               p.logged_ip, u.name
76          from pages p left outer join users u on p.logged_user = u.id
77         where p.hostid = ?
78         order by p.last_modified_date desc
79         offset ? limit ?" in
80   sth#execute [`Int hostid; `Int offset; `Int limit];
81
82   let table =
83     sth#map
84       (function
85          | [`Int version; `String url; _; `String title;
86             `Timestamp last_modified_date; logged_ip; logged_user] ->
87              let date = printable_date_time last_modified_date in
88              let has_logged_ip, logged_ip =
89                match logged_ip with
90                    `Null -> false, ""
91                  | `String ip -> true, ip
92                  | _ -> assert false in
93              let has_logged_user, logged_user =
94                match logged_user with
95                    `Null -> false, ""
96                  | `String name -> true, name
97                  | _ -> assert false in
98              [ "version", Template.VarString (string_of_int version);
99                "url", Template.VarString url;
100                "title", Template.VarString title;
101                "last_modified_date", Template.VarString date;
102                "has_logged_ip", Template.VarConditional has_logged_ip;
103                "logged_ip", Template.VarString logged_ip;
104                "has_logged_user", Template.VarConditional has_logged_user;
105                "logged_user", Template.VarString logged_user;
106                "is_live", Template.VarConditional true ]
107          | [`Int version; `Null; `String url; `String title;
108             `Timestamp last_modified_date; logged_ip; logged_user] ->
109              let date = printable_date_time last_modified_date in
110              let has_logged_ip, logged_ip =
111                match logged_ip with
112                    `Null -> false, ""
113                  | `String ip -> true, ip
114                  | _ -> assert false in
115              let has_logged_user, logged_user =
116                match logged_user with
117                    `Null -> false, ""
118                  | `String name -> true, name
119                  | _ -> assert false in
120              [ "version", Template.VarString (string_of_int version);
121                "url", Template.VarString url;
122                "title", Template.VarString title;
123                "last_modified_date", Template.VarString date;
124                "has_logged_ip", Template.VarConditional has_logged_ip;
125                "logged_ip", Template.VarString logged_ip;
126                "has_logged_user", Template.VarConditional has_logged_user;
127                "logged_user", Template.VarString logged_user;
128                "is_live", Template.VarConditional false ]
129          | _ -> assert false) in
130
131   template#table "recent_changes" table;
132
133   q#template template
134
135 let () =
136   register_script ~restrict:[CanEdit] run