|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @version $Id: diff.php 8765 2008-08-16 22:18:25Z aptx $
|
|
|
4c79b5 |
* @copyright (c) 2006 phpBB Group
|
|
|
4c79b5 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @ignore
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
if (!defined('IN_PHPBB'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
exit;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Code from pear.php.net, Text_Diff-1.0.0 package
|
|
|
4c79b5 |
* http://pear.php.net/package/Text_Diff/
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* Modified by phpBB Group to meet our coding standards
|
|
|
4c79b5 |
* and being able to integrate into phpBB
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* General API for generating and formatting diffs - the differences between
|
|
|
4c79b5 |
* two sequences of strings.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Array of changes.
|
|
|
4c79b5 |
* @var array
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
var $_edits;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Computes diffs between sequences of strings.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @param array $from_lines An array of strings. Typically these are lines from a file.
|
|
|
4c79b5 |
* @param array $to_lines An array of strings.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function diff(&$from_content, &$to_content, $preserve_cr = true)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$diff_engine = new diff_engine();
|
|
|
4c79b5 |
$this->_edits = $diff_engine->diff($from_content, $to_content, $preserve_cr);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Returns the array of differences.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function get_diff()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $this->_edits;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Computes a reversed diff.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* Example:
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* $diff = new diff($lines1, $lines2);
|
|
|
4c79b5 |
* $rev = $diff->reverse();
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @return diff A Diff object representing the inverse of the original diff.
|
|
|
4c79b5 |
* Note that we purposely don't return a reference here, since
|
|
|
4c79b5 |
* this essentially is a clone() method.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function reverse()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (version_compare(zend_version(), '2', '>'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$rev = clone($this);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$rev = $this;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$rev->_edits = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$rev->_edits[] = $edit->reverse();
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $rev;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Checks for an empty diff.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @return boolean True if two sequences were identical.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function is_empty()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!is_a($edit, 'diff_op_copy'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Computes the length of the Longest Common Subsequence (LCS).
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* This is mostly for diagnostic purposes.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @return integer The length of the LCS.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function lcs()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lcs = 0;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (is_a($edit, 'diff_op_copy'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lcs += sizeof($edit->orig);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
return $lcs;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Gets the original set of lines.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* This reconstructs the $from_lines parameter passed to the constructor.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @return array The original sequence of strings.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function get_original()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($edit->orig)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
array_splice($lines, sizeof($lines), 0, $edit->orig);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
return $lines;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Gets the final set of lines.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* This reconstructs the $to_lines parameter passed to the constructor.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @return array The sequence of strings.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function get_final()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($edit->final)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
array_splice($lines, sizeof($lines), 0, $edit->final);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
return $lines;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Removes trailing newlines from a line of text. This is meant to be used with array_walk().
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @param string &$line The line to trim.
|
|
|
4c79b5 |
* @param integer $key The index of the line in the array. Not used.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function trim_newlines(&$line, $key)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$line = str_replace(array("\n", "\r"), '', $line);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Checks a diff for validity.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* This is here only for debugging purposes.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _check($from_lines, $to_lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (serialize($from_lines) != serialize($this->get_original()))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
trigger_error("[diff] Reconstructed original doesn't match", E_USER_ERROR);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (serialize($to_lines) != serialize($this->get_final()))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
trigger_error("[diff] Reconstructed final doesn't match", E_USER_ERROR);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$rev = $this->reverse();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (serialize($to_lines) != serialize($rev->get_original()))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
trigger_error("[diff] Reversed original doesn't match", E_USER_ERROR);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (serialize($from_lines) != serialize($rev->get_final()))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
trigger_error("[diff] Reversed final doesn't match", E_USER_ERROR);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$prevtype = null;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($prevtype == get_class($edit))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
trigger_error("[diff] Edit sequence is non-optimal", E_USER_ERROR);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$prevtype = get_class($edit);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class mapped_diff extends diff
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Computes a diff between sequences of strings.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* This can be used to compute things like case-insensitve diffs, or diffs
|
|
|
4c79b5 |
* which ignore changes in white-space.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @param array $from_lines An array of strings.
|
|
|
4c79b5 |
* @param array $to_lines An array of strings.
|
|
|
4c79b5 |
* @param array $mapped_from_lines This array should have the same size number of elements as $from_lines.
|
|
|
4c79b5 |
* The elements in $mapped_from_lines and $mapped_to_lines are what is actually
|
|
|
4c79b5 |
* compared when computing the diff.
|
|
|
4c79b5 |
* @param array $mapped_to_lines This array should have the same number of elements as $to_lines.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function mapped_diff(&$from_lines, &$to_lines, &$mapped_from_lines, &$mapped_to_lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (sizeof($from_lines) != sizeof($mapped_from_lines) || sizeof($to_lines) != sizeof($mapped_to_lines))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
parent::diff($mapped_from_lines, $mapped_to_lines);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$xi = $yi = 0;
|
|
|
4c79b5 |
for ($i = 0; $i < sizeof($this->_edits); $i++)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$orig = &$this->_edits[$i]->orig;
|
|
|
4c79b5 |
if (is_array($orig))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$orig = array_slice($from_lines, $xi, sizeof($orig));
|
|
|
4c79b5 |
$xi += sizeof($orig);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$final = &$this->_edits[$i]->final;
|
|
|
4c79b5 |
if (is_array($final))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$final = array_slice($to_lines, $yi, sizeof($final));
|
|
|
4c79b5 |
$yi += sizeof($final);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff_op
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
var $orig;
|
|
|
4c79b5 |
var $final;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function &reverse()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
trigger_error('[diff] Abstract method', E_USER_ERROR);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function norig()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return ($this->orig) ? sizeof($this->orig) : 0;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function nfinal()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return ($this->final) ? sizeof($this->final) : 0;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff_op_copy extends diff_op
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
function diff_op_copy($orig, $final = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!is_array($final))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$final = $orig;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->orig = $orig;
|
|
|
4c79b5 |
$this->final = $final;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function &reverse()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$reverse = new diff_op_copy($this->final, $this->orig);
|
|
|
4c79b5 |
return $reverse;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff_op_delete extends diff_op
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
function diff_op_delete($lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->orig = $lines;
|
|
|
4c79b5 |
$this->final = false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function &reverse()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$reverse = new diff_op_add($this->orig);
|
|
|
4c79b5 |
return $reverse;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff_op_add extends diff_op
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
function diff_op_add($lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->final = $lines;
|
|
|
4c79b5 |
$this->orig = false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function &reverse()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$reverse = new diff_op_delete($this->final);
|
|
|
4c79b5 |
return $reverse;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff_op_change extends diff_op
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
function diff_op_change($orig, $final)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->orig = $orig;
|
|
|
4c79b5 |
$this->final = $final;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function &reverse()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$reverse = new diff_op_change($this->final, $this->orig);
|
|
|
4c79b5 |
return $reverse;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* A class for computing three way diffs.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff3 extends diff
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Conflict counter.
|
|
|
4c79b5 |
* @var integer
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
var $_conflicting_blocks = 0;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Computes diff between 3 sequences of strings.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @param array $orig The original lines to use.
|
|
|
4c79b5 |
* @param array $final1 The first version to compare to.
|
|
|
4c79b5 |
* @param array $final2 The second version to compare to.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function diff3(&$orig, &$final1, &$final2)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$diff_engine = new diff_engine();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$diff_1 = $diff_engine->diff($orig, $final1);
|
|
|
4c79b5 |
$diff_2 = $diff_engine->diff($orig, $final2);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
unset($engine);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->_edits = $this->_diff3($diff_1, $diff_2);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Return merged output
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @param string $label1 the cvs file version/label from the original set of lines
|
|
|
4c79b5 |
* @param string $label2 the cvs file version/label from the new set of lines
|
|
|
4c79b5 |
* @param string $label_sep the explanation between label1 and label2 - more of a helper for the user
|
|
|
4c79b5 |
* @param bool $get_conflicts if set to true only the number of conflicts is returned
|
|
|
4c79b5 |
* @param bool $merge_new if set to true the merged output will have the new file contents on a conflicting merge
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @return mixed the merged output
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function merged_output($label1 = 'CURRENT_FILE', $label2 = 'NEW_FILE', $label_sep = 'DIFF_SEP_EXPLAIN', $get_conflicts = false, $merge_new = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $user;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($get_conflicts)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($edit->is_conflict())
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->_conflicting_blocks++;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->_conflicting_blocks;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$label1 = (!empty($user->lang[$label1])) ? $user->lang[$label1] : $label1;
|
|
|
4c79b5 |
$label2 = (!empty($user->lang[$label2])) ? $user->lang[$label2] : $label2;
|
|
|
4c79b5 |
$label_sep = (!empty($user->lang[$label_sep])) ? $user->lang[$label_sep] : $label_sep;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$lines = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($edit->is_conflict())
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!$merge_new)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array_merge($lines, array('<<<<<<<' . ($label1 ? ' ' . $label1 : '')), $edit->final1, array('=======' . ($label_sep ? ' ' . $label_sep : '')), $edit->final2, array('>>>>>>>' . ($label2 ? ' ' . $label2 : '')));
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array_merge($lines, $edit->final1);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->_conflicting_blocks++;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array_merge($lines, $edit->merged());
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $lines;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Merge the output and use the new file code for conflicts
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function merged_new_output()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($edit->is_conflict())
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array_merge($lines, $edit->final2);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array_merge($lines, $edit->merged());
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $lines;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Merge the output and use the original file code for conflicts
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function merged_orig_output()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($edit->is_conflict())
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array_merge($lines, $edit->final1);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$lines = array_merge($lines, $edit->merged());
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $lines;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Get conflicting block(s)
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function get_conflicts()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$conflicts = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($this->_edits as $edit)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($edit->is_conflict())
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$conflicts[] = array($edit->final1, $edit->final2);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $conflicts;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _diff3(&$edits1, &$edits2)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$edits = array();
|
|
|
4c79b5 |
$bb = new diff3_block_builder();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$e1 = current($edits1);
|
|
|
4c79b5 |
$e2 = current($edits2);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
while ($e1 || $e2)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($e1 && $e2 && is_a($e1, 'diff_op_copy') && is_a($e2, 'diff_op_copy'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// We have copy blocks from both diffs. This is the (only) time we want to emit a diff3 copy block.
|
|
|
4c79b5 |
// Flush current diff3 diff block, if any.
|
|
|
4c79b5 |
if ($edit = $bb->finish())
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$edits[] = $edit;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$ncopy = min($e1->norig(), $e2->norig());
|
|
|
4c79b5 |
$edits[] = new diff3_op_copy(array_slice($e1->orig, 0, $ncopy));
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($e1->norig() > $ncopy)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
array_splice($e1->orig, 0, $ncopy);
|
|
|
4c79b5 |
array_splice($e1->final, 0, $ncopy);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$e1 = next($edits1);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($e2->norig() > $ncopy)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
array_splice($e2->orig, 0, $ncopy);
|
|
|
4c79b5 |
array_splice($e2->final, 0, $ncopy);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$e2 = next($edits2);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($e1 && $e2)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($e1->orig && $e2->orig)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$norig = min($e1->norig(), $e2->norig());
|
|
|
4c79b5 |
$orig = array_splice($e1->orig, 0, $norig);
|
|
|
4c79b5 |
array_splice($e2->orig, 0, $norig);
|
|
|
4c79b5 |
$bb->input($orig);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$norig = 0;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (is_a($e1, 'diff_op_copy'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$bb->out1(array_splice($e1->final, 0, $norig));
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (is_a($e2, 'diff_op_copy'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$bb->out2(array_splice($e2->final, 0, $norig));
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($e1 && ! $e1->orig)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$bb->out1($e1->final);
|
|
|
4c79b5 |
$e1 = next($edits1);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($e2 && ! $e2->orig)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$bb->out2($e2->final);
|
|
|
4c79b5 |
$e2 = next($edits2);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($edit = $bb->finish())
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$edits[] = $edit;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $edits;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff3_op
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
function diff3_op($orig = false, $final1 = false, $final2 = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->orig = $orig ? $orig : array();
|
|
|
4c79b5 |
$this->final1 = $final1 ? $final1 : array();
|
|
|
4c79b5 |
$this->final2 = $final2 ? $final2 : array();
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function merged()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!isset($this->_merged))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($this->final1 === $this->final2)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->_merged = &$this->final1;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if ($this->final1 === $this->orig)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->_merged = &$this->final2;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if ($this->final2 === $this->orig)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->_merged = &$this->final1;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->_merged = false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->_merged;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function is_conflict()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return ($this->merged() === false) ? true : false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff3_op_copy extends diff3_op
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
function diff3_op_copy($lines = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->orig = $lines ? $lines : array();
|
|
|
4c79b5 |
$this->final1 = &$this->orig;
|
|
|
4c79b5 |
$this->final2 = &$this->orig;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function merged()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $this->orig;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function is_conflict()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @package diff
|
|
|
4c79b5 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class diff3_block_builder
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
function diff3_block_builder()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->_init();
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function input($lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->_append($this->orig, $lines);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function out1($lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->_append($this->final1, $lines);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function out2($lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->_append($this->final2, $lines);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function is_empty()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return !$this->orig && !$this->final1 && !$this->final2;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function finish()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($this->is_empty())
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$edit = new diff3_op($this->orig, $this->final1, $this->final2);
|
|
|
4c79b5 |
$this->_init();
|
|
|
4c79b5 |
return $edit;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function _init()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->orig = $this->final1 = $this->final2 = array();
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function _append(&$array, $lines)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
array_splice($array, sizeof($array), 0, $lines);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
?>
|