Update Text_Diff. Props simek. Fixes #9467
git-svn-id: http://svn.automattic.com/wordpress/trunk@13211 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
df6d56b0ce
commit
7d258d93af
|
@ -6,10 +6,8 @@
|
||||||
* The original PHP version of this code was written by Geoffrey T. Dairiki
|
* The original PHP version of this code was written by Geoffrey T. Dairiki
|
||||||
* <dairiki@dairiki.org>, and is used/adapted with his permission.
|
* <dairiki@dairiki.org>, and is used/adapted with his permission.
|
||||||
*
|
*
|
||||||
* $Horde: framework/Text_Diff/Diff.php,v 1.26 2008/01/04 10:07:49 jan Exp $
|
|
||||||
*
|
|
||||||
* Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
|
* Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
|
||||||
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
* Copyright 2004-2010 The Horde Project (http://www.horde.org/)
|
||||||
*
|
*
|
||||||
* See the enclosed file COPYING for license information (LGPL). If you did
|
* See the enclosed file COPYING for license information (LGPL). If you did
|
||||||
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
||||||
|
@ -65,6 +63,44 @@ class Text_Diff {
|
||||||
return $this->_edits;
|
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 countAddedLines()
|
||||||
|
{
|
||||||
|
$count = 0;
|
||||||
|
foreach ($this->_edits as $edit) {
|
||||||
|
if (is_a($edit, 'Text_Diff_Op_add') ||
|
||||||
|
is_a($edit, 'Text_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 countDeletedLines()
|
||||||
|
{
|
||||||
|
$count = 0;
|
||||||
|
foreach ($this->_edits as $edit) {
|
||||||
|
if (is_a($edit, 'Text_Diff_Op_delete') ||
|
||||||
|
is_a($edit, 'Text_Diff_Op_change')) {
|
||||||
|
$count += $edit->norig();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $count;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes a reversed diff.
|
* Computes a reversed diff.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* $Horde: framework/Text_Diff/Diff/Engine/native.php,v 1.10 2008/01/04 10:27:53 jan Exp $
|
* Class used internally by Text_Diff to actually compute the diffs.
|
||||||
*
|
*
|
||||||
* Class used internally by Text_Diff to actually compute the diffs. This
|
* This class is implemented using native PHP code.
|
||||||
* class is implemented using native PHP code.
|
|
||||||
*
|
*
|
||||||
* The algorithm used here is mostly lifted from the perl module
|
* The algorithm used here is mostly lifted from the perl module
|
||||||
* Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
|
* Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
|
||||||
|
@ -19,7 +18,7 @@
|
||||||
* Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
|
* Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
|
||||||
* code was written by him, and is used/adapted with his permission.
|
* code was written by him, and is used/adapted with his permission.
|
||||||
*
|
*
|
||||||
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
* Copyright 2004-2010 The Horde Project (http://www.horde.org/)
|
||||||
*
|
*
|
||||||
* See the enclosed file COPYING for license information (LGPL). If you did
|
* See the enclosed file COPYING for license information (LGPL). If you did
|
||||||
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
||||||
|
|
|
@ -5,9 +5,7 @@
|
||||||
* This class uses the Unix `diff` program via shell_exec to compute the
|
* This class uses the Unix `diff` program via shell_exec to compute the
|
||||||
* differences between the two input arrays.
|
* differences between the two input arrays.
|
||||||
*
|
*
|
||||||
* $Horde: framework/Text_Diff/Diff/Engine/shell.php,v 1.8 2008/01/04 10:07:50 jan Exp $
|
* Copyright 2007-2010 The Horde Project (http://www.horde.org/)
|
||||||
*
|
|
||||||
* Copyright 2007-2008 The Horde Project (http://www.horde.org/)
|
|
||||||
*
|
*
|
||||||
* See the enclosed file COPYING for license information (LGPL). If you did
|
* See the enclosed file COPYING for license information (LGPL). If you did
|
||||||
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
||||||
|
|
|
@ -10,10 +10,8 @@
|
||||||
* echo $renderer->render($diff);
|
* echo $renderer->render($diff);
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* $Horde: framework/Text_Diff/Diff/Engine/string.php,v 1.7 2008/01/04 10:07:50 jan Exp $
|
|
||||||
*
|
|
||||||
* Copyright 2005 Örjan Persson <o@42mm.org>
|
* Copyright 2005 Örjan Persson <o@42mm.org>
|
||||||
* Copyright 2005-2008 The Horde Project (http://www.horde.org/)
|
* Copyright 2005-2010 The Horde Project (http://www.horde.org/)
|
||||||
*
|
*
|
||||||
* See the enclosed file COPYING for license information (LGPL). If you did
|
* See the enclosed file COPYING for license information (LGPL). If you did
|
||||||
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
||||||
|
@ -39,6 +37,19 @@ class Text_Diff_Engine_string {
|
||||||
*/
|
*/
|
||||||
function diff($diff, $mode = 'autodetect')
|
function diff($diff, $mode = 'autodetect')
|
||||||
{
|
{
|
||||||
|
// Detect line breaks.
|
||||||
|
$lnbr = "\n";
|
||||||
|
if (strpos($diff, "\r\n") !== false) {
|
||||||
|
$lnbr = "\r\n";
|
||||||
|
} elseif (strpos($diff, "\r") !== false) {
|
||||||
|
$lnbr = "\r";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure we have a line break at the EOF.
|
||||||
|
if (substr($diff, -strlen($lnbr)) != $lnbr) {
|
||||||
|
$diff .= $lnbr;
|
||||||
|
}
|
||||||
|
|
||||||
if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') {
|
if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') {
|
||||||
return PEAR::raiseError('Type of diff is unsupported');
|
return PEAR::raiseError('Type of diff is unsupported');
|
||||||
}
|
}
|
||||||
|
@ -48,17 +59,20 @@ class Text_Diff_Engine_string {
|
||||||
$unified = strpos($diff, '---');
|
$unified = strpos($diff, '---');
|
||||||
if ($context === $unified) {
|
if ($context === $unified) {
|
||||||
return PEAR::raiseError('Type of diff could not be detected');
|
return PEAR::raiseError('Type of diff could not be detected');
|
||||||
} elseif ($context === false || $context === false) {
|
} elseif ($context === false || $unified === false) {
|
||||||
$mode = $context !== false ? 'context' : 'unified';
|
$mode = $context !== false ? 'context' : 'unified';
|
||||||
} else {
|
} else {
|
||||||
$mode = $context < $unified ? 'context' : 'unified';
|
$mode = $context < $unified ? 'context' : 'unified';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// split by new line and remove the diff header
|
// Split by new line and remove the diff header, if there is one.
|
||||||
$diff = explode("\n", $diff);
|
$diff = explode($lnbr, $diff);
|
||||||
array_shift($diff);
|
if (($mode == 'context' && strpos($diff[0], '***') === 0) ||
|
||||||
array_shift($diff);
|
($mode == 'unified' && strpos($diff[0], '---') === 0)) {
|
||||||
|
array_shift($diff);
|
||||||
|
array_shift($diff);
|
||||||
|
}
|
||||||
|
|
||||||
if ($mode == 'context') {
|
if ($mode == 'context') {
|
||||||
return $this->parseContextDiff($diff);
|
return $this->parseContextDiff($diff);
|
||||||
|
|
|
@ -5,9 +5,7 @@
|
||||||
* This class uses the xdiff PECL package (http://pecl.php.net/package/xdiff)
|
* This class uses the xdiff PECL package (http://pecl.php.net/package/xdiff)
|
||||||
* to compute the differences between the two input arrays.
|
* to compute the differences between the two input arrays.
|
||||||
*
|
*
|
||||||
* $Horde: framework/Text_Diff/Diff/Engine/xdiff.php,v 1.6 2008/01/04 10:07:50 jan Exp $
|
* Copyright 2004-2010 The Horde Project (http://www.horde.org/)
|
||||||
*
|
|
||||||
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
|
||||||
*
|
*
|
||||||
* See the enclosed file COPYING for license information (LGPL). If you did
|
* See the enclosed file COPYING for license information (LGPL). If you did
|
||||||
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
||||||
|
@ -42,6 +40,9 @@ class Text_Diff_Engine_xdiff {
|
||||||
* valid, albeit a little less descriptive and efficient. */
|
* valid, albeit a little less descriptive and efficient. */
|
||||||
$edits = array();
|
$edits = array();
|
||||||
foreach ($diff as $line) {
|
foreach ($diff as $line) {
|
||||||
|
if (!strlen($line)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
switch ($line[0]) {
|
switch ($line[0]) {
|
||||||
case ' ':
|
case ' ':
|
||||||
$edits[] = &new Text_Diff_Op_copy(array(substr($line, 1)));
|
$edits[] = &new Text_Diff_Op_copy(array(substr($line, 1)));
|
||||||
|
|
|
@ -5,9 +5,7 @@
|
||||||
* This class renders the diff in classic diff format. It is intended that
|
* This class renders the diff in classic diff format. It is intended that
|
||||||
* this class be customized via inheritance, to obtain fancier outputs.
|
* this class be customized via inheritance, to obtain fancier outputs.
|
||||||
*
|
*
|
||||||
* $Horde: framework/Text_Diff/Diff/Renderer.php,v 1.21 2008/01/04 10:07:50 jan Exp $
|
* Copyright 2004-2010 The Horde Project (http://www.horde.org/)
|
||||||
*
|
|
||||||
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
|
||||||
*
|
*
|
||||||
* See the enclosed file COPYING for license information (LGPL). If you did
|
* See the enclosed file COPYING for license information (LGPL). If you did
|
||||||
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
/**
|
/**
|
||||||
* "Inline" diff renderer.
|
* "Inline" diff renderer.
|
||||||
*
|
*
|
||||||
* $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.21 2008/01/04 10:07:51 jan Exp $
|
* Copyright 2004-2010 The Horde Project (http://www.horde.org/)
|
||||||
*
|
|
||||||
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
|
||||||
*
|
*
|
||||||
* See the enclosed file COPYING for license information (LGPL). If you did
|
* See the enclosed file COPYING for license information (LGPL). If you did
|
||||||
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
|
||||||
|
@ -30,42 +28,65 @@ class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Number of leading context "lines" to preserve.
|
* Number of leading context "lines" to preserve.
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
*/
|
*/
|
||||||
var $_leading_context_lines = 10000;
|
var $_leading_context_lines = 10000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Number of trailing context "lines" to preserve.
|
* Number of trailing context "lines" to preserve.
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
*/
|
*/
|
||||||
var $_trailing_context_lines = 10000;
|
var $_trailing_context_lines = 10000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prefix for inserted text.
|
* Prefix for inserted text.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
var $_ins_prefix = '<ins>';
|
var $_ins_prefix = '<ins>';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Suffix for inserted text.
|
* Suffix for inserted text.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
var $_ins_suffix = '</ins>';
|
var $_ins_suffix = '</ins>';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prefix for deleted text.
|
* Prefix for deleted text.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
var $_del_prefix = '<del>';
|
var $_del_prefix = '<del>';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Suffix for deleted text.
|
* Suffix for deleted text.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
var $_del_suffix = '</del>';
|
var $_del_suffix = '</del>';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Header for each change block.
|
* Header for each change block.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
var $_block_header = '';
|
var $_block_header = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to split down to character-level.
|
||||||
|
*
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
|
var $_split_characters = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* What are we currently splitting on? Used to recurse to show word-level
|
* What are we currently splitting on? Used to recurse to show word-level
|
||||||
* changes.
|
* or character-level changes.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
var $_split_level = 'lines';
|
var $_split_level = 'lines';
|
||||||
|
|
||||||
|
@ -85,10 +106,10 @@ class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
|
||||||
array_walk($lines, array(&$this, '_encode'));
|
array_walk($lines, array(&$this, '_encode'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->_split_level == 'words') {
|
if ($this->_split_level == 'lines') {
|
||||||
return implode('', $lines);
|
|
||||||
} else {
|
|
||||||
return implode("\n", $lines) . "\n";
|
return implode("\n", $lines) . "\n";
|
||||||
|
} else {
|
||||||
|
return implode('', $lines);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,8 +131,13 @@ class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
|
||||||
|
|
||||||
function _changed($orig, $final)
|
function _changed($orig, $final)
|
||||||
{
|
{
|
||||||
/* If we've already split on words, don't try to do so again - just
|
/* If we've already split on characters, just display. */
|
||||||
* display. */
|
if ($this->_split_level == 'characters') {
|
||||||
|
return $this->_deleted($orig)
|
||||||
|
. $this->_added($final);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If we've already split on words, just display. */
|
||||||
if ($this->_split_level == 'words') {
|
if ($this->_split_level == 'words') {
|
||||||
$prefix = '';
|
$prefix = '';
|
||||||
while ($orig[0] !== false && $final[0] !== false &&
|
while ($orig[0] !== false && $final[0] !== false &&
|
||||||
|
@ -130,15 +156,23 @@ class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
|
||||||
/* Non-printing newline marker. */
|
/* Non-printing newline marker. */
|
||||||
$nl = "\0";
|
$nl = "\0";
|
||||||
|
|
||||||
/* We want to split on word boundaries, but we need to
|
if ($this->_split_characters) {
|
||||||
* preserve whitespace as well. Therefore we split on words,
|
$diff = new Text_Diff('native',
|
||||||
* but include all blocks of whitespace in the wordlist. */
|
array(preg_split('//', $text1),
|
||||||
$diff = new Text_Diff($this->_splitOnWords($text1, $nl),
|
preg_split('//', $text2)));
|
||||||
$this->_splitOnWords($text2, $nl));
|
} else {
|
||||||
|
/* We want to split on word boundaries, but we need to preserve
|
||||||
|
* whitespace as well. Therefore we split on words, but include
|
||||||
|
* all blocks of whitespace in the wordlist. */
|
||||||
|
$diff = new Text_Diff('native',
|
||||||
|
array($this->_splitOnWords($text1, $nl),
|
||||||
|
$this->_splitOnWords($text2, $nl)));
|
||||||
|
}
|
||||||
|
|
||||||
/* Get the diff in inline format. */
|
/* Get the diff in inline format. */
|
||||||
$renderer = new Text_Diff_Renderer_inline(array_merge($this->getParams(),
|
$renderer = new Text_Diff_Renderer_inline
|
||||||
array('split_level' => 'words')));
|
(array_merge($this->getParams(),
|
||||||
|
array('split_level' => $this->_split_characters ? 'characters' : 'words')));
|
||||||
|
|
||||||
/* Run the diff and get the output. */
|
/* Run the diff and get the output. */
|
||||||
return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
|
return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
|
||||||
|
|
Loading…
Reference in New Issue