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.13 2006/12/06 09:46:57 rich Exp $
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.
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.
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.
28 open Cocanwiki_template
31 let default_offset = 0
32 let default_limit = 100
35 let run r (q : cgi) dbh hostid _ _ =
36 let template = get_template r dbh hostid "recent.html" in
38 (* Count the number of changes. *)
39 let count = Option.get (
41 PGSQL(dbh) "select count(*) from pages where hostid = $hostid"
44 let count = Int64.to_int count in
46 (* Get the offset and limit specified, and adjust them so that we will
47 * be displaying some changes.
50 try int_of_string (q#param "offset") with Not_found -> default_offset in
52 try int_of_string (q#param "limit") with Not_found -> default_limit in
56 else if limit > max_limit then max_limit
60 else if offset >= count then max 0 (count - limit)
63 template#set "offset" (string_of_int offset);
64 template#set "last" (string_of_int (min (offset + limit) count - 1));
65 template#set "limit" (string_of_int limit);
66 template#set "count" (string_of_int count);
68 template#conditional "has_next" (offset + limit < count);
69 template#set "next_offset" (string_of_int (offset + limit));
70 template#conditional "has_prev" (offset > 0);
71 template#set "prev_offset" (string_of_int (max 0 (offset - limit)));
73 (* Get the actual changes. *)
75 let offset = Int32.of_int offset in
76 let limit = Int32.of_int limit in
77 PGSQL(dbh) "nullable-results"
78 "select p.id, p.url, p.url_deleted, p.title, p.last_modified_date,
80 from pages p left outer join users u on p.logged_user = u.id
81 where p.hostid = $hostid
82 order by p.last_modified_date desc
83 offset $offset limit $limit" in
88 | (Some version, Some url, _, Some title,
89 Some last_modified_date, logged_ip, logged_user) ->
90 let date = printable_date_time last_modified_date in
91 let has_logged_ip, logged_ip =
94 | Some ip -> true, ip in
95 let has_logged_user, logged_user =
96 match logged_user with
98 | Some name -> true, name in
99 [ "version", Template.VarString (Int32.to_string version);
100 "url", Template.VarString url;
101 "title", Template.VarString title;
102 "last_modified_date", Template.VarString date;
103 "has_logged_ip", Template.VarConditional has_logged_ip;
104 "logged_ip", Template.VarString logged_ip;
105 "has_logged_user", Template.VarConditional has_logged_user;
106 "logged_user", Template.VarString logged_user;
107 "is_live", Template.VarConditional true ]
108 | (Some version, None, Some url, Some title,
109 Some last_modified_date, logged_ip, logged_user) ->
110 let date = printable_date_time last_modified_date in
111 let has_logged_ip, logged_ip =
114 | Some ip -> true, ip in
115 let has_logged_user, logged_user =
116 match logged_user with
118 | Some name -> true, name in
119 [ "version", Template.VarString (Int32.to_string version);
120 "url", Template.VarString url;
121 "title", Template.VarString title;
122 "last_modified_date", Template.VarString date;
123 "has_logged_ip", Template.VarConditional has_logged_ip;
124 "logged_ip", Template.VarString logged_ip;
125 "has_logged_user", Template.VarConditional has_logged_user;
126 "logged_user", Template.VarString logged_user;
127 "is_live", Template.VarConditional false ]
128 | _ -> assert false) rows in
130 template#table "recent_changes" table;
135 register_script ~restrict:[CanEdit] run