Scripts updated to use new PG interface.
[cocanwiki.git] / scripts / admin / admin.ml
index 78c93f5..82d4ee6 100644 (file)
@@ -1,7 +1,22 @@
-(* COCANWIKI scripts.
+(* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: admin.ml,v 1.2 2004/09/07 13:40:10 rich Exp $
+ * $Id: admin.ml,v 1.8 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
  *)
 
 open Apache
@@ -9,48 +24,40 @@ open Registry
 open Cgi
 open Printf
 
-open Merjisforwiki
-
 open Cocanwiki
 open Cocanwiki_template
+open Cocanwiki_date
 
-let template = get_template "admin/admin.html"
+let template = _get_template "admin/admin.html"
 
-let run r (q : cgi) (dbh : Dbi.connection) _ _ =
+let run r (q : cgi) dbh _ _ _ =
   (* Select out the alternative hostnames. *)
-  let sth = dbh#prepare_cached
-             "select hs.hostid, hs.name from hostnames hs
-                where not exists (select 1 from hosts
-                                   where id = hs.hostid
-                                     and canonical_hostname = hs.name)" in
-  sth#execute [];
-
-  let hostnames = sth#map (function [`Int hostid; `String name] ->
-                            hostid, name
-                            | _ -> assert false) in
+  let hostnames = PGSQL(dbh)
+    "select hs.hostid, hs.name from hostnames hs
+      where not exists (select 1 from hosts
+                         where id = hs.hostid
+                           and canonical_hostname = hs.name)" in
 
   (* Pull out the details of all the wikis on the server. *)
-  let sth = dbh#prepare_cached
-             "select h.id, h.canonical_hostname,
-                      (select count(*) from pages
-                        where hostid = h.id and url is not null),
-                      (select max(last_modified_date) from pages
-                        where hostid = h.id and url is not null)
-                 from hosts h
-                order by 2" in
-  sth#execute [];
+  let rows = PGSQL(dbh)
+    "select h.id, h.canonical_hostname,
+            (select count(*) from pages
+              where hostid = h.id and url is not null),
+            (select max(last_modified_date) from pages
+              where hostid = h.id and url is not null)
+       from hosts h
+      order by 2" in
 
   let table =
-    sth#map
-      (function [`Int id; `String canonical_hostname;
-                (`Null | `Int _) as page_count;
-                (`Null | `Timestamp _) as last_modified_date] ->
+    List.map (
+      function (id, canonical_hostname,
+               page_count, last_modified_date) ->
         let page_count = match page_count with
-            `Null -> 0
-          | `Int n -> n in
+          | None -> 0L
+          | Some n -> n in
         let last_modified_date = match last_modified_date with
-            `Null -> "-"
-          | `Timestamp date -> printable_date date in
+          | None -> "-"
+          | Some date -> printable_date date in
 
         let hostnames =
           List.filter (fun (i, _) -> i = id) hostnames in
@@ -59,13 +66,12 @@ let run r (q : cgi) (dbh : Dbi.connection) _ _ =
                       [ "hostname", Template.VarString hostname ])
             hostnames in
 
-        [ "id", Template.VarString (string_of_int id);
+        [ "id", Template.VarString (Int32.to_string id);
           "canonical_hostname", Template.VarString canonical_hostname;
-          "page_count", Template.VarString (string_of_int page_count);
+          "page_count", Template.VarString (Int64.to_string page_count);
           "last_modified_date", Template.VarString last_modified_date;
           "hostnames", Template.VarTable hostnames ]
-
-        | _ -> assert false) in
+    ) rows in
 
   template#table "hosts" table;