Scripts updated to use new PG interface.
[cocanwiki.git] / scripts / recent.ml
index f77645c..2ff1e67 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.9 2004/10/21 18:35:01 rich Exp $
+ * $Id: recent.ml,v 1.12 2006/03/28 16:24:08 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
@@ -32,13 +32,16 @@ let default_offset = 0
 let default_limit = 100
 let max_limit = 1000
 
-let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
+let run r (q : cgi) dbh 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
+  let count = Option.get (
+    List.hd (
+      PGSQL(dbh) "select count(*) from pages where hostid = $hostid"
+    )
+  ) in
+  let count = Int64.to_int count in
 
   (* Get the offset and limit specified, and adjust them so that we will
    * be displaying some changes.
@@ -68,64 +71,61 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
   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 = ?
-        order by p.last_modified_date desc
-        offset ? limit ?" in
-  sth#execute [`Int hostid; `Int offset; `Int limit];
+  let rows =
+    let offset = Int32.of_int offset in
+    let limit = Int32.of_int limit in
+    PGSQL(dbh) "nullable-results"
+    "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 = $hostid
+      order by p.last_modified_date desc
+     offset $offset limit $limit" in
 
   let table =
-    sth#map
+    List.map
       (function
-        | [`Int version; `String url; _; `String title;
-           `Timestamp last_modified_date; logged_ip; logged_user] ->
-            let date = printable_date_time last_modified_date in
-            let has_logged_ip, logged_ip =
-              match logged_ip with
-                  `Null -> false, ""
-                | `String ip -> true, ip
-                | _ -> assert false in
-            let has_logged_user, logged_user =
-              match logged_user with
-                  `Null -> false, ""
-                | `String name -> true, name
-                | _ -> assert false in
-            [ "version", Template.VarString (string_of_int version);
-              "url", Template.VarString url;
-              "title", Template.VarString title;
-              "last_modified_date", Template.VarString date;
-              "has_logged_ip", Template.VarConditional has_logged_ip;
-              "logged_ip", Template.VarString logged_ip;
-              "has_logged_user", Template.VarConditional has_logged_user;
-              "logged_user", Template.VarString logged_user;
-              "is_live", Template.VarConditional true ]
-        | [`Int version; `Null; `String url; `String title;
-           `Timestamp last_modified_date; logged_ip; logged_user] ->
-            let date = printable_date_time last_modified_date in
-            let has_logged_ip, logged_ip =
-              match logged_ip with
-                  `Null -> false, ""
-                | `String ip -> true, ip
-                | _ -> assert false in
-            let has_logged_user, logged_user =
-              match logged_user with
-                  `Null -> false, ""
-                | `String name -> true, name
-                | _ -> assert false in
-            [ "version", Template.VarString (string_of_int version);
-              "url", Template.VarString url;
-              "title", Template.VarString title;
-              "last_modified_date", Template.VarString date;
-              "has_logged_ip", Template.VarConditional has_logged_ip;
-              "logged_ip", Template.VarString logged_ip;
-              "has_logged_user", Template.VarConditional has_logged_user;
-              "logged_user", Template.VarString logged_user;
-              "is_live", Template.VarConditional false ]
-        | _ -> assert false) in
+       | (Some version, Some url, _, Some title,
+         Some last_modified_date, logged_ip, logged_user) ->
+          let date = printable_date_time last_modified_date in
+          let has_logged_ip, logged_ip =
+            match logged_ip with
+              None -> false, ""
+            | Some ip -> true, ip in
+          let has_logged_user, logged_user =
+            match logged_user with
+              None -> false, ""
+            | Some name -> true, name in
+          [ "version", Template.VarString (Int32.to_string version);
+            "url", Template.VarString url;
+            "title", Template.VarString title;
+            "last_modified_date", Template.VarString date;
+            "has_logged_ip", Template.VarConditional has_logged_ip;
+            "logged_ip", Template.VarString logged_ip;
+            "has_logged_user", Template.VarConditional has_logged_user;
+            "logged_user", Template.VarString logged_user;
+            "is_live", Template.VarConditional true ]
+       | (Some version, None, Some url, Some title,
+         Some last_modified_date, logged_ip, logged_user) ->
+          let date = printable_date_time last_modified_date in
+          let has_logged_ip, logged_ip =
+            match logged_ip with
+              None -> false, ""
+            | Some ip -> true, ip in
+          let has_logged_user, logged_user =
+            match logged_user with
+              None -> false, ""
+            | Some name -> true, name in
+          [ "version", Template.VarString (Int32.to_string version);
+            "url", Template.VarString url;
+            "title", Template.VarString title;
+            "last_modified_date", Template.VarString date;
+            "has_logged_ip", Template.VarConditional has_logged_ip;
+            "logged_ip", Template.VarString logged_ip;
+            "has_logged_user", Template.VarConditional has_logged_user;
+            "logged_user", Template.VarString logged_user;
+            "is_live", Template.VarConditional false ]
+       | _ -> assert false) rows in
 
   template#table "recent_changes" table;