<?
/**
  Implementation of a GNU diff alike function from scratch.
  Copyright (C) 2003  Nils Knappmeier <nk@knappi.org>

  Permission is hereby granted, free of charge, to any person obtaining 
  a copy of this software and associated documentation files (the 
  "Software"), to deal in the Software without restriction, including 
  without limitation the rights to use, copy, modify, merge, publish, 
  distribute, sublicense, and/or sell copies of the Software, and to 
  permit persons to whom the Software is furnished to do so, subject to 
  the following conditions:

  The above copyright notice and this permission notice shall be 
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.

  Revision: $Revision: 1.11 $
  
 **/

$PatchFunction='php_Patch';


function cut_head(&$str, $key, $prefix) {
    if (strpos($str,$prefix)===0) {
	$str =  substr($str,strlen($prefix));
    } else {
	print "Something is wrong in the patch: ";
	print "'$str' should begin with '$prefix'\n";
	exit;
    }
}


/* This is the replacement for the patch function in the wiki */
function php_Patch($page, $restore) {
      
 
  $page_text = $page['text'];
  krsort($page); reset($page);
  
  foreach($page as $k=>$v) {
    if ($k < $restore) break;
    if (!preg_match('/^diff:/',$k)) continue;
    $page_text = txt_patch($page_text, $v);
  }
  return $page_text;

}

function txt_patch($text, $patch) {	


    $array=splitWithNL($text);

# print urlencode($patch)."\n";
    /* Modify patch to an array so that it 
     * is compatible to the modification */

    if ($patch=="") return $text;
    if (substr($patch, -1)=="\n") $patch = substr($patch, 0, strlen($patch)-1);

    
# print urlencode($patch)."\n";
 
    $patch_array=split("\n", $patch);




#  foreach($patch_array as $line) {
#	print urlencode($line)."\n";
#   }
    
    for ($i=0; $i<count($patch_array); $i++) {
	$patch_array[$i]=$patch_array[$i]."\n";
    }
   

    $i=0;
    $nlIndex=array_search("\\ No newline at end of file\n", $patch_array);
    while ($nlIndex!=false && $i<2) { 
	/* This shouldn't be happening more than two times in a valid patch */
	$newEntry=$patch_array[$nlIndex-1].$patch_array[$nlIndex];
	array_splice($patch_array, $nlIndex-1, 2, $newEntry);
	$nlIndex=array_search("\\ No newline at end of file\n", $patch_array);
	$i++;
    }


    /* Start computing */
    $current=0;
    do {
	if (preg_match("/^([\d,]+)([adc])([\d,]+)$/",
		    $patch_array[$current],$matches)==0) {
	    print "<pre>Error in line $current: ".$patch_array[$current].
		" not a command\n".sizeof($patch_array); 
	    print "</pre>";
	    exit;
	}
	list($full, $left, $action, $right) = $matches;

	/* Compute start and end of each side */
	list($left_start, $left_end)=split(",",$left);
	list($right_start, $right_end)=split(",",$right);
	if ($left_end=="") { $left_end = $left_start; }
	if ($right_end=="") { $right_end = $right_start; }

	/* Perform action and switch to next patch */
	if ($action=="a") {
	    $replace=array_slice($patch_array,
		    $current+1,
		    $right_end-$right_start+1);
	    
	    array_walk($replace, 'cut_head','> ');

	    array_splice($array, $right_start-1, 0, $replace);
	    $current+=$right_end-$right_start+2;
	} else if ($action=="d") {
	    /* Check whether lines in patch are like in file */
	    $should=array_slice($patch_array,
		    $current+1, 
		    $left_end-$left_start+1);
	    array_walk($should, 'cut_head','< ');
	    
	    $is=array_splice($array, 
		    $right_start, 
		    $left_end-$left_start+1);
	    if ($should!==$is) {
		print "<pre>According to the patch, in lines $left_start to ";
		print "$left_end there should be a\n";
		print urlencode(implode("",$should))."\n";
		print "but I only find a\n";
		print urlencode(implode("",$is))."\n</pre>";

	    }
	    $current+=$left_end-$left_start+2;

	} else if ($action=="c") {
	    $replace=array_slice($patch_array,
		    $current+1+$left_end-$left_start+2,
		    $right_end-$right_start+1);

	    array_walk($replace, 'cut_head','> ');
	    $is = array_splice($array, 
		    $right_start-1, 
		    $left_end-$left_start+1,
		    $replace);

	    /* Check whether lines in patch are like in text */
	    $should=array_slice($patch_array,
		    $current+1, 
		    $left_end-$left_start+1);
	    array_walk($should, 'cut_head','< ');
	    
	    if ($should!==$is) {
		print "<pre>According to the patch, in lines $left_start to";
		print "$left_end there should be a\n";
		print implode("",$should);
		print "but I only find a\n";
		print implode("",$is)."</pre>";

	    }
	    $current+=1+$left_end-$left_start+1+1+$right_end-$right_start+1;
	}

    } while ($current<count($patch_array));
    $result = implode("", $array);
    $suffix="\n\\ No newline at end of file\n";

    



    if (substr($result,-strlen($suffix))==$suffix) {
	$result=substr($result,0,strlen($result)-strlen($suffix));
    }
    return $result;
}

/* 
   File history:

   $Log: phppatch.php,v $
   Revision 1.11  2004/12/16 14:05:08  nknapp
   Changed the license statements to MIT-license in all files

   Revision 1.10  2004/03/26 01:33:07  nknapp
   Allow empty patches

   Revision 1.9  2004/03/07 11:57:09  nknapp
   Removed include(difflib)

   Revision 1.8  2004/03/04 10:07:28  nknapp
   Ready for pmwiki-0.6.9 now

   Revision 1.7  2004/03/04 09:57:25  nknapp
   For 0.6.9

   Revision 1.6  2004/02/22 18:05:09  nknapp
   Bugfix

   Revision 1.5  2004/02/14 13:47:00  nknapp
   Removed Lock(), this is done in pmwiki.php

   Revision 1.4  2004/02/13 23:13:28  nknapp
   Externalized difflib.php

   Revision 1.3  2004/02/13 22:33:01  nknapp
   Real patch method

   Revision 1.2  2004/02/13 22:21:02  nknapp
   Removed "Algorithm described"

   Revision 1.1  2004/02/13 22:18:43  nknapp
   First checkin

   Revision 1.2  2004/02/13 21:18:45  nknapp
   Minor change

   Revision 1.1  2004/02/13 21:16:02  nknapp
   First checkin

   
   vim:sw=4:ai:cin:sts=4:
*/
?>
