Commented a bug.
[cocanwiki.git] / scripts / recent.ml
index 997462a..f77645c 100644 (file)
@@ -1,7 +1,7 @@
 (* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: recent.ml,v 1.8 2004/09/21 13:01:16 rich Exp $
+ * $Id: recent.ml,v 1.9 2004/10/21 18:35:01 rich Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,20 +28,55 @@ open Cocanwiki
 open Cocanwiki_template
 open Cocanwiki_date
 
-let max_age = "3 months"
+let default_offset = 0
+let default_limit = 100
+let max_limit = 1000
 
 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
   let template = get_template dbh hostid "recent.html" in
 
+  (* Count the number of changes. *)
+  let sth = dbh#prepare_cached "select count(*) from pages where hostid = ?" in
+  sth#execute [`Int hostid];
+  let count = sth#fetch1int () in
+
+  (* Get the offset and limit specified, and adjust them so that we will
+   * be displaying some changes.
+   *)
+  let offset =
+    try int_of_string (q#param "offset") with Not_found -> default_offset in
+  let limit =
+    try int_of_string (q#param "limit") with Not_found -> default_limit in
+
+  let limit =
+    if limit < 1 then 1
+    else if limit > max_limit then max_limit
+    else limit in
+  let offset =
+    if offset < 0 then 0
+    else if offset >= count then max 0 (count - limit)
+    else offset in
+
+  template#set "offset" (string_of_int offset);
+  template#set "last" (string_of_int (min (offset + limit) count - 1));
+  template#set "limit" (string_of_int limit);
+  template#set "count" (string_of_int count);
+
+  template#conditional "has_next" (offset + limit < count);
+  template#set "next_offset" (string_of_int (offset + limit));
+  template#conditional "has_prev" (offset > 0);
+  template#set "prev_offset" (string_of_int (max 0 (offset - limit)));
+
+  (* Get the actual changes. *)
   let sth =
     dbh#prepare_cached
       "select p.id, p.url, p.url_deleted, p.title, p.last_modified_date,
               p.logged_ip, u.name
          from pages p left outer join users u on p.logged_user = u.id
         where p.hostid = ?
-          and p.last_modified_date >= current_timestamp - interval ?
-        order by p.last_modified_date desc" in
-  sth#execute [`Int hostid; `String max_age];
+        order by p.last_modified_date desc
+        offset ? limit ?" in
+  sth#execute [`Int hostid; `Int offset; `Int limit];
 
   let table =
     sth#map