About half way through switching cocanwiki to using the new PG interface.
[cocanwiki.git] / scripts / lib / cocanwiki_diff.ml
index ff143a2..004960d 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_diff.ml,v 1.2 2004/10/30 10:16:10 rich Exp $
+ * $Id: cocanwiki_diff.ml,v 1.4 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
@@ -40,7 +40,7 @@ let page_for_diff css sections =
 let le_re = Pcre.regexp "\r?\n"
 let le_subst = Pcre.subst "\n"
 
-let diff_cmd ~user old_page new_page =
+let diff_cmd old_page new_page =
   (* Convert line-endings in the input files from \r\n to \n.  Diff
    * can get confused by the \r characters, particularly in side-by-side
    * mode when asked to expand tabs (-y -t).
@@ -52,10 +52,10 @@ let diff_cmd ~user old_page new_page =
   let new_filename = output_tempfile new_page in
   let old_filename = output_tempfile old_page in
 
-  let diff_sidebyside =
-    match user with
-       Anonymous -> false
-      | User (_, _, _, prefs) -> prefs.diff_sidebyside in
+  (* Side-by-side mode was good, but stupidly implemented.  It's
+   * disabled right now.
+   *)
+  let diff_sidebyside = false in
 
   let options =
     if not diff_sidebyside then
@@ -79,47 +79,54 @@ let diff_cmd ~user old_page new_page =
 
   String.concat "\n" diff
 
-let get_version_for_diff (dbh : Dbi.connection) version =
-  if version = 0 then "" else (
-    let sth = dbh#prepare_cached "select coalesce (css, '') as css
-                                    from pages where id = ?" in
-    sth#execute [`Int version];
-
-    let css = sth#fetch1string () in
+let get_version_for_diff dbh version =
+  if version = 0l then ""
+  else (
+    let css = List.hd (
+      PGSQL(dbh)
+       "select css from pages where id = $version"
+    ) in
+    let css = match css with None -> "" | Some css -> css in
 
-    let sth = dbh#prepare_cached "select coalesce (sectionname, ''), content
-                                    from contents where pageid = ?
-                                   order by ordering" in
-    sth#execute [`Int version];
+    let rows = PGSQL(dbh)
+      "select sectionname, content
+         from contents where pageid = $version
+        order by ordering" in
 
     let sections =
-      sth#map (function
-                  [`String sectionname; `String content] ->
-                    sectionname, content
-                | _ -> assert false) in
+      List.map (
+       function
+       | (Some sectionname, content) ->
+           sectionname, content
+       | (None, content) ->
+           "", content
+      ) rows in
     let page = page_for_diff css sections in
 
     page
   )
 
-let get_diff (dbh : Dbi.connection) hostid ~user page ?old_version ~version ()=
+let get_diff dbh hostid page ?old_version ~version ()=
   let old_version =
     match old_version with
       | Some version -> version
       | None ->
-         let sth = dbh#prepare_cached "select id from pages
-                                         where hostid = ?
-                                           and url_deleted = ? and id < ?
-                                         order by 1 desc limit 1" in
-         sth#execute [`Int hostid; `String page; `Int version];
-
-         try sth#fetch1int ()
-         with Not_found -> 0 in
+         try
+           List.hd (
+             PGSQL(dbh)
+               "select id from pages
+                  where hostid = $hostid
+                    and url_deleted = $page
+                    and id < $version
+                  order by 1 desc limit 1"
+           )
+         with
+           Not_found | ExtList.List.Empty_list -> 0l in
 
   (* Get the two versions. *)
   let new_page = get_version_for_diff dbh version in
   let old_page = get_version_for_diff dbh old_version in
 
   (* Compute the diff of the two versions. *)
-  let diff = diff_cmd ~user old_page new_page in
+  let diff = diff_cmd old_page new_page in
   diff, old_version