(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: cocanwiki_diff.ml,v 1.4 2004/09/20 10:56:47 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 open Registry open Cgi open Printf open Cocanwiki_files (* Convenience code for generating diffs between versions. See diff.ml * and edit.ml which both use this code. *) let page_for_diff css sections = (String.concat "" (List.map (fun (sectionname, content) -> "HEADING: " ^ sectionname ^ "\n\n" ^ content ^ "\n\n") sections)) ^ "CSS:\n\n" ^ css let diff_cmd old_page new_page = 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 let diff = pget cmd in let diff = match diff with _ :: _ :: diff -> diff | diff -> 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 sth = dbh#prepare_cached "select coalesce (sectionname, ''), content from contents where pageid = ? order by ordering" in sth#execute [`Int version]; let sections = sth#map (function [`String sectionname; `String content] -> sectionname, content | _ -> assert false) in let page = page_for_diff css sections in page ) let get_diff (dbh : Dbi.connection) 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 (* 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 old_page new_page in diff, old_version