+csv dep for PG'OCaml.
[cocanwiki.git] / scripts / rebuild_links.ml
index 91d206e..50e8d7e 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: rebuild_links.ml,v 1.2 2004/09/28 11:28:39 rich Exp $
+ * $Id: rebuild_links.ml,v 1.7 2006/07/27 16:46:55 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,61 +28,49 @@ open Cocanwiki
 open Cocanwiki_template
 open Cocanwiki_links
 
-let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
+let run r (q : cgi) dbh hostid _ _ =
   let template_start = _get_template "rebuild_links_start.html" in
   let template = _get_template "rebuild_links.html" in
   let template_done = _get_template "rebuild_links_done.html" in
 
   (* Delete entries in the old links table. *)
-  let sth = dbh#prepare_cached "delete from links where hostid = ?" in
-  sth#execute [`Int hostid];
-
-  (* Estimate how many sections we will have to process. *)
-  let sth =
-    dbh#prepare_cached
-      "select count(c.id) from contents c, pages p
-        where c.pageid = p.id
-          and p.hostid = ?
-          and p.url is not null
-          and p.redirect is null" in
-  sth#execute [`Int hostid];
-
-  let total_sections = sth#fetch1int () in
+  PGSQL(dbh) "delete from links where hostid = $hostid";
 
   (* Pull out the list of sections to process. *)
-  let sth =
-    dbh#prepare_cached
+  let sections =
+    PGSQL(dbh)
       "select c.content, c.ordering, p.url from contents c, pages p
         where c.pageid = p.id
-          and p.hostid = ?
+          and p.hostid = $hostid
           and p.url is not null
           and p.redirect is null
         order by p.url, c.ordering" in
-  sth#execute [`Int hostid];
+
+  let total_sections = List.length sections in
 
   q#header ();
-  print_string r template_start#to_string;
+  ignore (print_string r template_start#to_string);
 
   (* Process each section ... *)
   let i = ref 0 in
 
-  sth#iter
-    (function [`String content; `Int ordering; `String url] ->
-       let pc = 100 * !i / total_sections in incr i;
-       template#set "ordering" (string_of_int ordering);
-       template#set "url" url;
-       template#set "pc" (string_of_int pc);
-       print_string r template#to_string;
-
-       let links = get_links_from_section dbh hostid content in
-       List.iter (insert_link dbh hostid url) links
+  List.iter (
+    fun (content, ordering, url) ->
+      let url = Option.get url in
+      let pc = 100 * !i / total_sections in incr i;
+      template#set "ordering" (Int32.to_string ordering);
+      template#set "url" url;
+      template#set "pc" (string_of_int pc);
+      ignore (print_string r template#to_string);
 
-       | _ -> assert false);
+      let links = get_links_from_section r dbh hostid content in
+      List.iter (insert_link dbh hostid url) links
+  ) sections;
 
   (* Finish off. *)
-  dbh#commit ();
+  PGOCaml.commit dbh;
 
-  print_string r template_done#to_string
+  ignore (print_string r template_done#to_string)
 
 let () =
   register_script ~restrict:[CanManageSite] run