phpBB

Code Changes

File: includes/diff/diff.php

  Unmodified   Added   Modified   Removed
Line 17Line 17
}

/**

}

/**

* Code from pear.php.net, Text_Diff-0.2.1 (beta) package

* Code from pear.php.net, Text_Diff-1.1.0 package

* http://pear.php.net/package/Text_Diff/
*
* Modified by phpBB Group to meet our coding standards

* http://pear.php.net/package/Text_Diff/
*
* Modified by phpBB Group to meet our coding standards

Line 25Line 25
*
* General API for generating and formatting diffs - the differences between
* two sequences of strings.

*
* General API for generating and formatting diffs - the differences between
* two sequences of strings.

 
*
* Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)

*
* @package diff
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>

*
* @package diff
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>

Line 45Line 48
	*/
function diff(&$from_content, &$to_content, $preserve_cr = true)
{

	*/
function diff(&$from_content, &$to_content, $preserve_cr = true)
{

		$diff_engine = &new diff_engine();

		$diff_engine = new diff_engine();

		$this->_edits = $diff_engine->diff($from_content, $to_content, $preserve_cr);
}


		$this->_edits = $diff_engine->diff($from_content, $to_content, $preserve_cr);
}


Line 55Line 58
	function get_diff()
{
return $this->_edits;

	function get_diff()
{
return $this->_edits;

 
	}

/**
* returns the number of new (added) lines in a given diff.
*
* @since Text_Diff 1.1.0
*
* @return integer The number of new lines
*/
function count_added_lines()
{
$count = 0;

for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
{
$edit = $this->_edits[$i];

if (is_a($edit, 'diff_op_add') || is_a($edit, 'diff_op_change'))
{
$count += $edit->nfinal();
}
}
return $count;
}

/**
* Returns the number of deleted (removed) lines in a given diff.
*
* @since Text_Diff 1.1.0
*
* @return integer The number of deleted lines
*/
function count_deleted_lines()
{
$count = 0;

for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
{
$edit = $this->_edits[$i];

if (is_a($edit, 'diff_op_delete') || is_a($edit, 'diff_op_change'))
{
$count += $edit->norig();
}
}
return $count;

	}

/**

	}

/**

Line 62Line 111
	*
* Example:
* <code>

	*
* Example:
* <code>

	* $diff = &new diff($lines1, $lines2);

	* $diff = new diff($lines1, $lines2);

	* $rev = $diff->reverse();
* </code>
*

	* $rev = $diff->reverse();
* </code>
*

Line 83Line 132

$rev->_edits = array();



$rev->_edits = array();


		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

 
			$edit = $this->_edits[$i];

			$rev->_edits[] = $edit->reverse();
}


			$rev->_edits[] = $edit->reverse();
}


Line 98Line 148
	*/
function is_empty()
{

	*/
function is_empty()
{

		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

			if (!is_a($edit, 'diff_op_copy'))




			$edit = $this->_edits[$i];

// skip diff_op_copy
if (is_a($edit, 'diff_op_copy'))

			{

			{

 
				continue;
}

if (is_a($edit, 'diff_op_delete') || is_a($edit, 'diff_op_add'))
{
$orig = $edit->orig;
$final = $edit->final;

// We can simplify one case where the array is usually supposed to be empty...
if (sizeof($orig) == 1 && trim($orig[0]) === '') $orig = array();
if (sizeof($final) == 1 && trim($final[0]) === '') $final = array();

if (!$orig && !$final)
{
continue;
}


				return false;
}

				return false;
}

 

return false;

		}

		}

 


		return true;
}


		return true;
}


Line 119Line 192
	{
$lcs = 0;


	{
$lcs = 0;


		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

 
			$edit = $this->_edits[$i];


			if (is_a($edit, 'diff_op_copy'))
{
$lcs += sizeof($edit->orig);

			if (is_a($edit, 'diff_op_copy'))
{
$lcs += sizeof($edit->orig);

Line 140Line 215
	{
$lines = array();


	{
$lines = array();


		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

 
			$edit = $this->_edits[$i];


			if ($edit->orig)
{
array_splice($lines, sizeof($lines), 0, $edit->orig);

			if ($edit->orig)
{
array_splice($lines, sizeof($lines), 0, $edit->orig);

Line 161Line 238
	{
$lines = array();


	{
$lines = array();


		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

 
			$edit = $this->_edits[$i];


			if ($edit->final)
{
array_splice($lines, sizeof($lines), 0, $edit->final);

			if ($edit->final)
{
array_splice($lines, sizeof($lines), 0, $edit->final);

Line 213Line 292

$prevtype = null;



$prevtype = null;


		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

 
			$edit = $this->_edits[$i];


			if ($prevtype == get_class($edit))
{
trigger_error("[diff] Edit sequence is non-optimal", E_USER_ERROR);

			if ($prevtype == get_class($edit))
{
trigger_error("[diff] Edit sequence is non-optimal", E_USER_ERROR);

Line 285Line 366
	var $orig;
var $final;


	var $orig;
var $final;


	function reverse()

	function &reverse()

	{
trigger_error('[diff] Abstract method', E_USER_ERROR);
}

	{
trigger_error('[diff] Abstract method', E_USER_ERROR);
}

Line 321Line 402

function &reverse()
{


function &reverse()
{

		$reverse = &new diff_op_copy($this->final, $this->orig);

		$reverse = new diff_op_copy($this->final, $this->orig);

		return $reverse;
}
}

		return $reverse;
}
}

Line 342Line 423

function &reverse()
{


function &reverse()
{

		$reverse = &new diff_op_add($this->orig);

		$reverse = new diff_op_add($this->orig);

		return $reverse;
}
}

		return $reverse;
}
}

Line 363Line 444

function &reverse()
{


function &reverse()
{

		$reverse = &new diff_op_delete($this->final);

		$reverse = new diff_op_delete($this->final);

		return $reverse;
}
}

		return $reverse;
}
}

Line 384Line 465

function &reverse()
{


function &reverse()
{

		$reverse = &new diff_op_change($this->final, $this->orig);

		$reverse = new diff_op_change($this->final, $this->orig);

		return $reverse;
}
}

		return $reverse;
}
}

Line 411Line 492
	* @param array $final1  The first version to compare to.
* @param array $final2 The second version to compare to.
*/

	* @param array $final1  The first version to compare to.
* @param array $final2 The second version to compare to.
*/

	function diff3(&$orig, &$final1, &$final2)

	function diff3(&$orig, &$final1, &$final2, $preserve_cr = true)

	{

	{

		$diff_engine = &new diff_engine();

		$diff_engine = new diff_engine();





		$diff_1 = $diff_engine->diff($orig, $final1);
$diff_2 = $diff_engine->diff($orig, $final2);

		$diff_1 = $diff_engine->diff($orig, $final1, $preserve_cr);
$diff_2 = $diff_engine->diff($orig, $final2, $preserve_cr);





		unset($engine);

		unset($diff_engine);


$this->_edits = $this->_diff3($diff_1, $diff_2);
}

/**


$this->_edits = $this->_diff3($diff_1, $diff_2);
}

/**

	* Return merged output
*
* @param string $label1 the cvs file version/label from the original set of lines
* @param string $label2 the cvs file version/label from the new set of lines
* @param string $label_sep the explanation between label1 and label2 - more of a helper for the user
* @param bool $get_conflicts if set to true only the number of conflicts is returned
* @param bool $merge_new if set to true the merged output will have the new file contents on a conflicting merge
*
* @return mixed the merged output

	* Return number of conflicts









	*/

	*/

	function merged_output($label1 = 'CURRENT_FILE', $label2 = 'NEW_FILE', $label_sep = 'DIFF_SEP_EXPLAIN', $get_conflicts = false, $merge_new = false)

	function get_num_conflicts()

	{

	{

		global $user;

		$conflicts = 0;





		if ($get_conflicts)
{
foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)



			{

			{

 
			$edit = $this->_edits[$i];


				if ($edit->is_conflict())
{

				if ($edit->is_conflict())
{

					$this->_conflicting_blocks++;

				$conflicts++;

				}
}


				}
}


			return $this->_conflicting_blocks;

		return $conflicts;

		}

		}

 

/**
* Get conflicts content for download. This is generally a merged file, but preserving conflicts and adding explanations to it.
* A user could then go through this file, search for the conflicts and changes the code accordingly.
*
* @param string $label1 the cvs file version/label from the original set of lines
* @param string $label2 the cvs file version/label from the new set of lines
* @param string $label_sep the explanation between label1 and label2 - more of a helper for the user
*
* @return mixed the merged output
*/
function get_conflicts_content($label1 = 'CURRENT_FILE', $label2 = 'NEW_FILE', $label_sep = 'DIFF_SEP_EXPLAIN')
{
global $user;


$label1 = (!empty($user->lang[$label1])) ? $user->lang[$label1] : $label1;
$label2 = (!empty($user->lang[$label2])) ? $user->lang[$label2] : $label2;


$label1 = (!empty($user->lang[$label1])) ? $user->lang[$label1] : $label1;
$label2 = (!empty($user->lang[$label2])) ? $user->lang[$label2] : $label2;

Line 457Line 544

$lines = array();



$lines = array();


		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

 
			$edit = $this->_edits[$i];


			if ($edit->is_conflict())
{

			if ($edit->is_conflict())
{

				if (!$merge_new)
{
$lines = array_merge($lines, array('<<<<<<<' . ($label1 ? ' ' . $label1 : '')), $edit->final1, array('=======' . ($label_sep ? ' ' . $label_sep : '')), $edit->final2, array('>>>>>>>' . ($label2 ? ' ' . $label2 : '')));
}
else
{
$lines = array_merge($lines, $edit->final1);
}

				// Start conflict label
$label_start = array('<<<<<<< ' . $label1);
$label_mid = array('======= ' . $label_sep);
$label_end = array('>>>>>>> ' . $label2);

$lines = array_merge($lines, $label_start, $edit->final1, $label_mid, $edit->final2, $label_end);



				$this->_conflicting_blocks++;
}
else

				$this->_conflicting_blocks++;
}
else

Line 478Line 565
		}

return $lines;

		}

return $lines;

 
	}

/**
* Return merged output (used by the renderer)
*
* @return mixed the merged output
*/
function merged_output()
{
return $this->get_conflicts_content();

	}

/**

	}

/**

Line 487Line 584
	{
$lines = array();


	{
$lines = array();


		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

 
			$edit = $this->_edits[$i];


			if ($edit->is_conflict())
{
$lines = array_merge($lines, $edit->final2);

			if ($edit->is_conflict())
{
$lines = array_merge($lines, $edit->final2);

Line 509Line 608
	{
$lines = array();


	{
$lines = array();


		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

 
			$edit = $this->_edits[$i];


			if ($edit->is_conflict())
{
$lines = array_merge($lines, $edit->final1);

			if ($edit->is_conflict())
{
$lines = array_merge($lines, $edit->final1);

Line 531Line 632
	{
$conflicts = array();


	{
$conflicts = array();


		foreach ($this->_edits as $edit)

		for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)

		{

		{

 
			$edit = $this->_edits[$i];


			if ($edit->is_conflict())
{
$conflicts[] = array($edit->final1, $edit->final2);

			if ($edit->is_conflict())
{
$conflicts[] = array($edit->final1, $edit->final2);

Line 548Line 651
	function _diff3(&$edits1, &$edits2)
{
$edits = array();

	function _diff3(&$edits1, &$edits2)
{
$edits = array();

		$bb = &new diff3_block_builder();

		$bb = new diff3_block_builder();


$e1 = current($edits1);
$e2 = current($edits2);


$e1 = current($edits1);
$e2 = current($edits2);

Line 565Line 668
				}

$ncopy = min($e1->norig(), $e2->norig());

				}

$ncopy = min($e1->norig(), $e2->norig());

				$edits[] = &new diff3_op_copy(array_slice($e1->orig, 0, $ncopy));

				$edits[] = new diff3_op_copy(array_slice($e1->orig, 0, $ncopy));


if ($e1->norig() > $ncopy)
{


if ($e1->norig() > $ncopy)
{

Line 656Line 759
	{
if (!isset($this->_merged))
{

	{
if (!isset($this->_merged))
{

 
			// Prepare the arrays before we compare them. ;)
$this->solve_prepare();


			if ($this->final1 === $this->final2)
{
$this->_merged = &$this->final1;

			if ($this->final1 === $this->final2)
{
$this->_merged = &$this->final1;

Line 670Line 776
			}
else
{

			}
else
{

 
				// The following tries to aggressively solve conflicts...

				$this->_merged = false;

				$this->_merged = false;

 
				$this->solve_conflict();

			}
}


			}
}


Line 680Line 788
	function is_conflict()
{
return ($this->merged() === false) ? true : false;

	function is_conflict()
{
return ($this->merged() === false) ? true : false;

 
	}

/**
* Function to prepare the arrays for comparing - we want to skip over newline changes
* @author acydburn
*/
function solve_prepare()
{
// We can simplify one case where the array is usually supposed to be empty...
if (sizeof($this->orig) == 1 && trim($this->orig[0]) === '') $this->orig = array();
if (sizeof($this->final1) == 1 && trim($this->final1[0]) === '') $this->final1 = array();
if (sizeof($this->final2) == 1 && trim($this->final2[0]) === '') $this->final2 = array();

// Now we only can have the case where the only difference between arrays are newlines, so compare all cases

// First, some strings we can compare...
$orig = $final1 = $final2 = '';

foreach ($this->orig as $null => $line) $orig .= trim($line);
foreach ($this->final1 as $null => $line) $final1 .= trim($line);
foreach ($this->final2 as $null => $line) $final2 .= trim($line);

// final1 === final2
if ($final1 === $final2)
{
// We preserve the part which will be used in the merge later
$this->final2 = $this->final1;
}
// final1 === orig
else if ($final1 === $orig)
{
// Here it does not really matter what we choose, but we will use the new code
$this->orig = $this->final1;
}
// final2 === orig
else if ($final2 === $orig)
{
// Here it does not really matter too (final1 will be used), but we will use the new code
$this->orig = $this->final2;
}
}

/**
* Find code portions from $orig in $final1 and use $final2 as merged instance if provided
* @author acydburn
*/
function _compare_conflict_seq($orig, $final1, $final2 = false)
{
$result = array('merge_found' => false, 'merge' => array());

$_orig = &$this->$orig;
$_final1 = &$this->$final1;

// Ok, we basically search for $orig in $final1
$compare_seq = sizeof($_orig);

// Go through the conflict code
for ($i = 0, $j = 0, $size = sizeof($_final1); $i < $size; $i++, $j = $i)
{
$line = $_final1[$i];
$skip = 0;

for ($x = 0; $x < $compare_seq; $x++)
{
// Try to skip all matching lines
if (trim($line) === trim($_orig[$x]))
{
$line = (++$j < $size) ? $_final1[$j] : $line;
$skip++;
}
}

if ($skip === $compare_seq)
{
$result['merge_found'] = true;

if ($final2 !== false)
{
$result['merge'] = array_merge($result['merge'], $this->$final2);
}
$i += ($skip - 1);
}
else if ($final2 !== false)
{
$result['merge'][] = $line;
}
}

return $result;
}

/**
* Tries to solve conflicts aggressively based on typical "assumptions"
* @author acydburn
*/
function solve_conflict()
{
$this->_merged = false;

// CASE ONE: orig changed into final2, but modified/unknown code in final1.
// IF orig is found "as is" in final1 we replace the code directly in final1 and populate this as final2/merge
if (sizeof($this->orig) && sizeof($this->final2))
{
$result = $this->_compare_conflict_seq('orig', 'final1', 'final2');

if ($result['merge_found'])
{
$this->final2 = $result['merge'];
$this->_merged = &$this->final2;
return;
}

$result = $this->_compare_conflict_seq('final2', 'final1');

if ($result['merge_found'])
{
$this->_merged = &$this->final1;
return;
}

// Try to solve $Id$ issues. ;)
if (sizeof($this->orig) == 1 && sizeof($this->final1) == 1 && sizeof($this->final2) == 1)
{
$match = '#^' . preg_quote('* @version $Id: ', '#') . '[a-z\._\- ]+[0-9]+ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9\:Z]+ [a-z0-9_\- ]+\$$#';

if (preg_match($match, $this->orig[0]) && preg_match($match, $this->final1[0]) && preg_match($match, $this->final2[0]))
{
$this->_merged = &$this->final2;
return;
}
}

$second_run = false;

// Try to solve issues where the only reason why the above did not work is a newline being removed in the final1 code but exist in the orig/final2 code
if (trim($this->orig[0]) === '' && trim($this->final2[0]) === '')
{
unset($this->orig[0], $this->final2[0]);
$this->orig = array_values($this->orig);
$this->final2 = array_values($this->final2);

$second_run = true;
}

// The same is true for a line at the end. ;)
if (sizeof($this->orig) && sizeof($this->final2) && sizeof($this->orig) === sizeof($this->final2) && trim($this->orig[sizeof($this->orig)-1]) === '' && trim($this->final2[sizeof($this->final2)-1]) === '')
{
unset($this->orig[sizeof($this->orig)-1], $this->final2[sizeof($this->final2)-1]);
$this->orig = array_values($this->orig);
$this->final2 = array_values($this->final2);

$second_run = true;
}

if ($second_run)
{
$result = $this->_compare_conflict_seq('orig', 'final1', 'final2');

if ($result['merge_found'])
{
$this->final2 = $result['merge'];
$this->_merged = &$this->final2;
return;
}

$result = $this->_compare_conflict_seq('final2', 'final1');

if ($result['merge_found'])
{
$this->_merged = &$this->final1;
return;
}
}

return;
}

// CASE TWO: Added lines from orig to final2 but final1 had added lines too. Just merge them.
if (!sizeof($this->orig) && $this->final1 !== $this->final2 && sizeof($this->final1) && sizeof($this->final2))
{
$result = $this->_compare_conflict_seq('final2', 'final1');

if ($result['merge_found'])
{
$this->final2 = $this->final1;
$this->_merged = &$this->final1;
}
else
{
$result = $this->_compare_conflict_seq('final1', 'final2');

if (!$result['merge_found'])
{
$this->final2 = array_merge($this->final1, $this->final2);
$this->_merged = &$this->final2;
}
else
{
$this->final2 = $this->final1;
$this->_merged = &$this->final1;
}
}

return;
}

// CASE THREE: Removed lines (orig has the to-remove line(s), but final1 has additional lines which does not need to be removed). Just remove orig from final1 and then use final1 as final2/merge
if (!sizeof($this->final2) && sizeof($this->orig) && sizeof($this->final1) && $this->orig !== $this->final1)
{
$result = $this->_compare_conflict_seq('orig', 'final1');

if (!$result['merge_found'])
{
return;
}

// First of all, try to find the code in orig in final1. ;)
$compare_seq = sizeof($this->orig);
$begin = $end = -1;
$j = 0;

for ($i = 0, $size = sizeof($this->final1); $i < $size; $i++)
{
$line = $this->final1[$i];

if (trim($line) === trim($this->orig[$j]))
{
// Mark begin
if ($begin === -1)
{
$begin = $i;
}

// End is always $i, the last found line
$end = $i;

if (isset($this->orig[$j+1]))
{
$j++;
}
}
}

if ($begin !== -1 && $begin + ($compare_seq - 1) == $end)
{
foreach ($this->final1 as $i => $line)
{
if ($i < $begin || $i > $end)
{
$merged[] = $line;
}
}

$this->final2 = $merged;
$this->_merged = &$this->final2;
}

return;
}

return;

	}
}


	}
}


Line 759Line 1128
		}
else
{

		}
else
{

			$edit = &new diff3_op($this->orig, $this->final1, $this->final2);

			$edit = new diff3_op($this->orig, $this->final1, $this->final2);

			$this->_init();
return $edit;
}

			$this->_init();
return $edit;
}