<?php if (!defined('PmWiki')) exit();
/*  Copyright 2005, 2007 Patrick R. Michaud (pmichaud@pobox.com)
    This file is sysdiff.php; 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 script replaces PmWiki's internal Diff algorithm with an
    external call to the Unix diff(1) program.  This may speed up
    processing and improve handling of large file edits.

    To use this script, simply copy it into the cookbook/ directory
    and add the line
        include_once('cookbook/sysdiff.php');
    to your local/config.php.
*/

$RecipeInfo['SysDiff']['Version'] = '2007-03-21';

# set PmWiki's default diff function
$DiffFunction = 'SysDiff';

function SysDiff($oldtext,$newtext) {
  global $WorkDir,$SysDiffCmd;
  SDV($SysDiffCmd, '/usr/bin/diff');
  if (!$SysDiffCmd) return '';
  $tempold = tempnam($WorkDir,"old");
  if ($oldfp = fopen($tempold,"w")) {
    fputs($oldfp,$oldtext);
    fclose($oldfp);
  }
  $tempnew = tempnam($WorkDir,"new");
  if ($newfp = fopen($tempnew,"w")) {
    fputs($newfp,$newtext);
    fclose($newfp);
  }
  $diff = '';
  $diff_handle = popen("$SysDiffCmd $tempold $tempnew","r");
  if ($diff_handle) {
    while (!feof($diff_handle)) { $diff .= fread($diff_handle,1024); }
    pclose($diff_handle);
  } 
  @unlink($tempold); @unlink($tempnew);
  return $diff;
}