About half way through switching cocanwiki to using the new PG interface.
[cocanwiki.git] / scripts / lib / cocanwiki_mail.ml
index 6ac57f2..494e60c 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: cocanwiki_mail.ml,v 1.1 2004/10/21 11:42:05 rich Exp $
+ * $Id: cocanwiki_mail.ml,v 1.2 2006/03/27 16:43:44 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
@@ -148,13 +148,13 @@ struct
 end
 
 type message =
-    { id : int;
+    { id : int32;
       inet_message_id : string;
       references : string list;
       subject : string;
       base_subject : string;
       is_reply : bool;
-      message_date : Dbi.datetime }
+      message_date : PGOCaml.timestamptz }
 
 type tree = Tree of message option * tree list
 
@@ -162,35 +162,37 @@ type tree = Tree of message option * tree list
  * The algorithm was originally by JWZ, http://www.jwz.org/doc/threading.html,
  * simplified and implemented by Radu Grigore <radugrigore@yahoo.com>.
  *)
-let thread_mail (dbh : Dbi.connection) hostid ?user ?r year month =
+let thread_mail dbh hostid ?user ?r year month =
   (* Pull out all the emails relevant to this month. *)
-  let sth =
-    dbh#prepare_cached "select id, subject, inet_message_id, message_date
-                          from messages
-                         where hostid = ?
-                           and extract (year from message_date) = ?
-                           and extract (month from message_date) = ?" in
-  sth#execute [`Int hostid; `Int year; `Int month];
+  let rows =
+    let year = Int32.of_int year in
+    let month = Int32.of_int month in
+    PGSQL(dbh)
+      "select id, subject, inet_message_id, message_date
+         from messages
+        where hostid = $hostid
+          and extract (year from message_date) :: int = $year
+          and extract (month from message_date) :: int = $month" in
 
   let msgs =
-    sth#map
-      (function [`Int id; `String subject; `String inet_message_id;
-                `Timestamp message_date] ->
-        id, (inet_message_id, subject, message_date)
-        | _ -> assert false) in
+    List.map (
+      fun (id, subject, inet_message_id, message_date) ->
+       id, (inet_message_id, subject, message_date)
+    ) rows in
 
   let references =
     if msgs <> [] then (
-      let sth =
-       let qs = Dbi.placeholders (List.length msgs) in
-       dbh#prepare_cached ("select message_id, inet_message_id, ordering
-                               from msg_references
-                              where message_id in " ^ qs ^ "
-                              order by message_id, ordering") in
-      sth#execute (List.map (fun (id, _) -> `Int id) msgs);
-      sth#map (function [`Int id; `String inet_message_id; _] ->
-                id, inet_message_id
-                | _ -> assert false)
+      let ids = List.map fst msgs in
+      let rows =
+       PGSQL(dbh)
+         "select message_id, inet_message_id, ordering
+             from msg_references
+            where message_id in $@ids
+            order by message_id, ordering" in
+      List.map (
+       fun (id, inet_message_id, _) ->
+         id, inet_message_id
+      ) rows
     ) else [] in
 
   (* Aggregate the msgs and references structures together.
@@ -354,11 +356,13 @@ let thread_mail (dbh : Dbi.connection) hostid ?user ?r year month =
 
   let first_section =
     let sectionname =
-      sprintf "Thread index for %s %04d" (long_month month) year in
+      sprintf "Thread index for %s %04d"
+       (!Printer.month_name (Date.month_of_int month)) year in
     let content =
       template#set "year" (string_of_int year);
       template#set "month" (sprintf "%02d" month);
-      template#set "long_month" (long_month month);
+      template#set "long_month"
+       (!Printer.month_name (Date.month_of_int month));
       let prev_year, prev_month =
        if month = 1 then year - 1, 12
        else year, month - 1 in
@@ -379,7 +383,7 @@ let thread_mail (dbh : Dbi.connection) hostid ?user ?r year month =
              let {id = id; subject = subject} = message in
 
              let url =
-               let title = sprintf "Mail/%s (%d)" subject id in
+               let title = sprintf "Mail/%s (%ld)" subject id in
                match Wikilib.generate_url_of_title dbh hostid title with
                    Wikilib.GenURL_OK url | Wikilib.GenURL_Duplicate url -> url
                  | Wikilib.GenURL_TooShort | Wikilib.GenURL_BadURL ->
@@ -398,15 +402,15 @@ let thread_mail (dbh : Dbi.connection) hostid ?user ?r year month =
 
       template#to_string
     in
-    (sectionname, "", content)
+    (Some sectionname, None, content)
   in
 
   let contents =
-    match model.contents with
+    match model.contents_ with
       | [] | [_] -> [ first_section ]
       | x :: xs -> first_section :: xs in
 
-  let model = { model with contents = contents } in
+  let model = { model with contents_ = contents } in
 
   (* Save the page. *)
   try