About half way through switching cocanwiki to using the new PG interface.
[cocanwiki.git] / scripts / lib / cocanwiki_diff.ml
index 849a818..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.1 2004/10/21 11:42:05 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
@@ -25,6 +25,7 @@ open Cgi
 open Printf
 
 open Cocanwiki_files
+open Cocanwiki
 
 (* Convenience code for generating diffs between versions.  See diff.ml
  * and edit.ml which both use this code.
@@ -36,57 +37,91 @@ let page_for_diff css sections =
                  content ^ "\n\n") sections)) ^
   "CSS:\n\n" ^ css
 
+let le_re = Pcre.regexp "\r?\n"
+let le_subst = Pcre.subst "\n"
+
 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).
+   *)
+  let f = Pcre.replace ~rex:le_re ~itempl:le_subst in
+  let new_page = f new_page in
+  let old_page = f old_page in
+
   let new_filename = output_tempfile new_page in
   let old_filename = output_tempfile old_page in
 
-  let cmd = sprintf "diff -u %s %s ||:" old_filename new_filename 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
+      "-u"
+    else
+      "-y --left-column" in
+  let options = options ^ " -t -b -B" in
+
+  let cmd = sprintf "diff %s %s %s ||:" options old_filename new_filename in
   let diff = pget cmd in
 
+  (* Remove the temporary files. *)
+  unlink new_filename; unlink old_filename;
+
   let diff =
-    match diff with
-       _ :: _ :: diff -> diff
-      | diff -> diff in
+    if not diff_sidebyside then
+      match diff with
+         _ :: _ :: diff -> diff
+       | diff -> diff
+    else diff in
 
-  unlink new_filename; unlink old_filename;
   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 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