Edit File by line
/home/barbar84/public_h.../wp-inclu.../Text
File: Diff.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* General API for generating and formatting diffs - the differences between
[2] Fix | Delete
* two sequences of strings.
[3] Fix | Delete
*
[4] Fix | Delete
* The original PHP version of this code was written by Geoffrey T. Dairiki
[5] Fix | Delete
* <dairiki@dairiki.org>, and is used/adapted with his permission.
[6] Fix | Delete
*
[7] Fix | Delete
* Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
[8] Fix | Delete
* Copyright 2004-2010 The Horde Project (http://www.horde.org/)
[9] Fix | Delete
*
[10] Fix | Delete
* See the enclosed file COPYING for license information (LGPL). If you did
[11] Fix | Delete
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
[12] Fix | Delete
*
[13] Fix | Delete
* @package Text_Diff
[14] Fix | Delete
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
[15] Fix | Delete
*/
[16] Fix | Delete
class Text_Diff {
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Array of changes.
[20] Fix | Delete
*
[21] Fix | Delete
* @var array
[22] Fix | Delete
*/
[23] Fix | Delete
var $_edits;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Computes diffs between sequences of strings.
[27] Fix | Delete
*
[28] Fix | Delete
* @param string $engine Name of the diffing engine to use. 'auto'
[29] Fix | Delete
* will automatically select the best.
[30] Fix | Delete
* @param array $params Parameters to pass to the diffing engine.
[31] Fix | Delete
* Normally an array of two arrays, each
[32] Fix | Delete
* containing the lines from a file.
[33] Fix | Delete
*/
[34] Fix | Delete
function __construct( $engine, $params )
[35] Fix | Delete
{
[36] Fix | Delete
// Backward compatibility workaround.
[37] Fix | Delete
if (!is_string($engine)) {
[38] Fix | Delete
$params = array($engine, $params);
[39] Fix | Delete
$engine = 'auto';
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
if ($engine == 'auto') {
[43] Fix | Delete
$engine = extension_loaded('xdiff') ? 'xdiff' : 'native';
[44] Fix | Delete
} else {
[45] Fix | Delete
$engine = basename($engine);
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
// WP #7391
[49] Fix | Delete
require_once dirname(__FILE__).'/Diff/Engine/' . $engine . '.php';
[50] Fix | Delete
$class = 'Text_Diff_Engine_' . $engine;
[51] Fix | Delete
$diff_engine = new $class();
[52] Fix | Delete
[53] Fix | Delete
$this->_edits = call_user_func_array(array($diff_engine, 'diff'), $params);
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* PHP4 constructor.
[58] Fix | Delete
*/
[59] Fix | Delete
public function Text_Diff( $engine, $params ) {
[60] Fix | Delete
self::__construct( $engine, $params );
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* Returns the array of differences.
[65] Fix | Delete
*/
[66] Fix | Delete
function getDiff()
[67] Fix | Delete
{
[68] Fix | Delete
return $this->_edits;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
* returns the number of new (added) lines in a given diff.
[73] Fix | Delete
*
[74] Fix | Delete
* @since Text_Diff 1.1.0
[75] Fix | Delete
*
[76] Fix | Delete
* @return int The number of new lines
[77] Fix | Delete
*/
[78] Fix | Delete
function countAddedLines()
[79] Fix | Delete
{
[80] Fix | Delete
$count = 0;
[81] Fix | Delete
foreach ($this->_edits as $edit) {
[82] Fix | Delete
if (is_a($edit, 'Text_Diff_Op_add') ||
[83] Fix | Delete
is_a($edit, 'Text_Diff_Op_change')) {
[84] Fix | Delete
$count += $edit->nfinal();
[85] Fix | Delete
}
[86] Fix | Delete
}
[87] Fix | Delete
return $count;
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* Returns the number of deleted (removed) lines in a given diff.
[92] Fix | Delete
*
[93] Fix | Delete
* @since Text_Diff 1.1.0
[94] Fix | Delete
*
[95] Fix | Delete
* @return int The number of deleted lines
[96] Fix | Delete
*/
[97] Fix | Delete
function countDeletedLines()
[98] Fix | Delete
{
[99] Fix | Delete
$count = 0;
[100] Fix | Delete
foreach ($this->_edits as $edit) {
[101] Fix | Delete
if (is_a($edit, 'Text_Diff_Op_delete') ||
[102] Fix | Delete
is_a($edit, 'Text_Diff_Op_change')) {
[103] Fix | Delete
$count += $edit->norig();
[104] Fix | Delete
}
[105] Fix | Delete
}
[106] Fix | Delete
return $count;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* Computes a reversed diff.
[111] Fix | Delete
*
[112] Fix | Delete
* Example:
[113] Fix | Delete
* <code>
[114] Fix | Delete
* $diff = new Text_Diff($lines1, $lines2);
[115] Fix | Delete
* $rev = $diff->reverse();
[116] Fix | Delete
* </code>
[117] Fix | Delete
*
[118] Fix | Delete
* @return Text_Diff A Diff object representing the inverse of the
[119] Fix | Delete
* original diff. Note that we purposely don't return a
[120] Fix | Delete
* reference here, since this essentially is a clone()
[121] Fix | Delete
* method.
[122] Fix | Delete
*/
[123] Fix | Delete
function reverse()
[124] Fix | Delete
{
[125] Fix | Delete
if (version_compare(zend_version(), '2', '>')) {
[126] Fix | Delete
$rev = clone($this);
[127] Fix | Delete
} else {
[128] Fix | Delete
$rev = $this;
[129] Fix | Delete
}
[130] Fix | Delete
$rev->_edits = array();
[131] Fix | Delete
foreach ($this->_edits as $edit) {
[132] Fix | Delete
$rev->_edits[] = $edit->reverse();
[133] Fix | Delete
}
[134] Fix | Delete
return $rev;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Checks for an empty diff.
[139] Fix | Delete
*
[140] Fix | Delete
* @return bool True if two sequences were identical.
[141] Fix | Delete
*/
[142] Fix | Delete
function isEmpty()
[143] Fix | Delete
{
[144] Fix | Delete
foreach ($this->_edits as $edit) {
[145] Fix | Delete
if (!is_a($edit, 'Text_Diff_Op_copy')) {
[146] Fix | Delete
return false;
[147] Fix | Delete
}
[148] Fix | Delete
}
[149] Fix | Delete
return true;
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
/**
[153] Fix | Delete
* Computes the length of the Longest Common Subsequence (LCS).
[154] Fix | Delete
*
[155] Fix | Delete
* This is mostly for diagnostic purposes.
[156] Fix | Delete
*
[157] Fix | Delete
* @return int The length of the LCS.
[158] Fix | Delete
*/
[159] Fix | Delete
function lcs()
[160] Fix | Delete
{
[161] Fix | Delete
$lcs = 0;
[162] Fix | Delete
foreach ($this->_edits as $edit) {
[163] Fix | Delete
if (is_a($edit, 'Text_Diff_Op_copy')) {
[164] Fix | Delete
$lcs += count($edit->orig);
[165] Fix | Delete
}
[166] Fix | Delete
}
[167] Fix | Delete
return $lcs;
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
/**
[171] Fix | Delete
* Gets the original set of lines.
[172] Fix | Delete
*
[173] Fix | Delete
* This reconstructs the $from_lines parameter passed to the constructor.
[174] Fix | Delete
*
[175] Fix | Delete
* @return array The original sequence of strings.
[176] Fix | Delete
*/
[177] Fix | Delete
function getOriginal()
[178] Fix | Delete
{
[179] Fix | Delete
$lines = array();
[180] Fix | Delete
foreach ($this->_edits as $edit) {
[181] Fix | Delete
if ($edit->orig) {
[182] Fix | Delete
array_splice($lines, count($lines), 0, $edit->orig);
[183] Fix | Delete
}
[184] Fix | Delete
}
[185] Fix | Delete
return $lines;
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
/**
[189] Fix | Delete
* Gets the final set of lines.
[190] Fix | Delete
*
[191] Fix | Delete
* This reconstructs the $to_lines parameter passed to the constructor.
[192] Fix | Delete
*
[193] Fix | Delete
* @return array The sequence of strings.
[194] Fix | Delete
*/
[195] Fix | Delete
function getFinal()
[196] Fix | Delete
{
[197] Fix | Delete
$lines = array();
[198] Fix | Delete
foreach ($this->_edits as $edit) {
[199] Fix | Delete
if ($edit->final) {
[200] Fix | Delete
array_splice($lines, count($lines), 0, $edit->final);
[201] Fix | Delete
}
[202] Fix | Delete
}
[203] Fix | Delete
return $lines;
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
/**
[207] Fix | Delete
* Removes trailing newlines from a line of text. This is meant to be used
[208] Fix | Delete
* with array_walk().
[209] Fix | Delete
*
[210] Fix | Delete
* @param string $line The line to trim.
[211] Fix | Delete
* @param int $key The index of the line in the array. Not used.
[212] Fix | Delete
*/
[213] Fix | Delete
static function trimNewlines(&$line, $key)
[214] Fix | Delete
{
[215] Fix | Delete
$line = str_replace(array("\n", "\r"), '', $line);
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
/**
[219] Fix | Delete
* Determines the location of the system temporary directory.
[220] Fix | Delete
*
[221] Fix | Delete
* @access protected
[222] Fix | Delete
*
[223] Fix | Delete
* @return string A directory name which can be used for temp files.
[224] Fix | Delete
* Returns false if one could not be found.
[225] Fix | Delete
*/
[226] Fix | Delete
static function _getTempDir()
[227] Fix | Delete
{
[228] Fix | Delete
$tmp_locations = array('/tmp', '/var/tmp', 'c:\WUTemp', 'c:\temp',
[229] Fix | Delete
'c:\windows\temp', 'c:\winnt\temp');
[230] Fix | Delete
[231] Fix | Delete
/* Try PHP's upload_tmp_dir directive. */
[232] Fix | Delete
$tmp = ini_get('upload_tmp_dir');
[233] Fix | Delete
[234] Fix | Delete
/* Otherwise, try to determine the TMPDIR environment variable. */
[235] Fix | Delete
if (!strlen($tmp)) {
[236] Fix | Delete
$tmp = getenv('TMPDIR');
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
/* If we still cannot determine a value, then cycle through a list of
[240] Fix | Delete
* preset possibilities. */
[241] Fix | Delete
while (!strlen($tmp) && count($tmp_locations)) {
[242] Fix | Delete
$tmp_check = array_shift($tmp_locations);
[243] Fix | Delete
if (@is_dir($tmp_check)) {
[244] Fix | Delete
$tmp = $tmp_check;
[245] Fix | Delete
}
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
/* If it is still empty, we have failed, so return false; otherwise
[249] Fix | Delete
* return the directory determined. */
[250] Fix | Delete
return strlen($tmp) ? $tmp : false;
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
/**
[254] Fix | Delete
* Checks a diff for validity.
[255] Fix | Delete
*
[256] Fix | Delete
* This is here only for debugging purposes.
[257] Fix | Delete
*/
[258] Fix | Delete
function _check($from_lines, $to_lines)
[259] Fix | Delete
{
[260] Fix | Delete
if (serialize($from_lines) != serialize($this->getOriginal())) {
[261] Fix | Delete
trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
[262] Fix | Delete
}
[263] Fix | Delete
if (serialize($to_lines) != serialize($this->getFinal())) {
[264] Fix | Delete
trigger_error("Reconstructed final doesn't match", E_USER_ERROR);
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
$rev = $this->reverse();
[268] Fix | Delete
if (serialize($to_lines) != serialize($rev->getOriginal())) {
[269] Fix | Delete
trigger_error("Reversed original doesn't match", E_USER_ERROR);
[270] Fix | Delete
}
[271] Fix | Delete
if (serialize($from_lines) != serialize($rev->getFinal())) {
[272] Fix | Delete
trigger_error("Reversed final doesn't match", E_USER_ERROR);
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
$prevtype = null;
[276] Fix | Delete
foreach ($this->_edits as $edit) {
[277] Fix | Delete
if ($edit instanceof $prevtype) {
[278] Fix | Delete
trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
[279] Fix | Delete
}
[280] Fix | Delete
$prevtype = get_class($edit);
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
return true;
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
/**
[289] Fix | Delete
* @package Text_Diff
[290] Fix | Delete
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
[291] Fix | Delete
*/
[292] Fix | Delete
class Text_MappedDiff extends Text_Diff {
[293] Fix | Delete
[294] Fix | Delete
/**
[295] Fix | Delete
* Computes a diff between sequences of strings.
[296] Fix | Delete
*
[297] Fix | Delete
* This can be used to compute things like case-insensitve diffs, or diffs
[298] Fix | Delete
* which ignore changes in white-space.
[299] Fix | Delete
*
[300] Fix | Delete
* @param array $from_lines An array of strings.
[301] Fix | Delete
* @param array $to_lines An array of strings.
[302] Fix | Delete
* @param array $mapped_from_lines This array should have the same size
[303] Fix | Delete
* number of elements as $from_lines. The
[304] Fix | Delete
* elements in $mapped_from_lines and
[305] Fix | Delete
* $mapped_to_lines are what is actually
[306] Fix | Delete
* compared when computing the diff.
[307] Fix | Delete
* @param array $mapped_to_lines This array should have the same number
[308] Fix | Delete
* of elements as $to_lines.
[309] Fix | Delete
*/
[310] Fix | Delete
function __construct($from_lines, $to_lines,
[311] Fix | Delete
$mapped_from_lines, $mapped_to_lines)
[312] Fix | Delete
{
[313] Fix | Delete
assert(count($from_lines) == count($mapped_from_lines));
[314] Fix | Delete
assert(count($to_lines) == count($mapped_to_lines));
[315] Fix | Delete
[316] Fix | Delete
parent::Text_Diff($mapped_from_lines, $mapped_to_lines);
[317] Fix | Delete
[318] Fix | Delete
$xi = $yi = 0;
[319] Fix | Delete
for ($i = 0; $i < count($this->_edits); $i++) {
[320] Fix | Delete
$orig = &$this->_edits[$i]->orig;
[321] Fix | Delete
if (is_array($orig)) {
[322] Fix | Delete
$orig = array_slice($from_lines, $xi, count($orig));
[323] Fix | Delete
$xi += count($orig);
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
$final = &$this->_edits[$i]->final;
[327] Fix | Delete
if (is_array($final)) {
[328] Fix | Delete
$final = array_slice($to_lines, $yi, count($final));
[329] Fix | Delete
$yi += count($final);
[330] Fix | Delete
}
[331] Fix | Delete
}
[332] Fix | Delete
}
[333] Fix | Delete
[334] Fix | Delete
/**
[335] Fix | Delete
* PHP4 constructor.
[336] Fix | Delete
*/
[337] Fix | Delete
public function Text_MappedDiff( $from_lines, $to_lines,
[338] Fix | Delete
$mapped_from_lines, $mapped_to_lines ) {
[339] Fix | Delete
self::__construct( $from_lines, $to_lines,
[340] Fix | Delete
$mapped_from_lines, $mapped_to_lines );
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
}
[344] Fix | Delete
[345] Fix | Delete
/**
[346] Fix | Delete
* @package Text_Diff
[347] Fix | Delete
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
[348] Fix | Delete
*
[349] Fix | Delete
* @access private
[350] Fix | Delete
*/
[351] Fix | Delete
class Text_Diff_Op {
[352] Fix | Delete
[353] Fix | Delete
var $orig;
[354] Fix | Delete
var $final;
[355] Fix | Delete
[356] Fix | Delete
function &reverse()
[357] Fix | Delete
{
[358] Fix | Delete
trigger_error('Abstract method', E_USER_ERROR);
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
function norig()
[362] Fix | Delete
{
[363] Fix | Delete
return $this->orig ? count($this->orig) : 0;
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
function nfinal()
[367] Fix | Delete
{
[368] Fix | Delete
return $this->final ? count($this->final) : 0;
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
}
[372] Fix | Delete
[373] Fix | Delete
/**
[374] Fix | Delete
* @package Text_Diff
[375] Fix | Delete
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
[376] Fix | Delete
*
[377] Fix | Delete
* @access private
[378] Fix | Delete
*/
[379] Fix | Delete
class Text_Diff_Op_copy extends Text_Diff_Op {
[380] Fix | Delete
[381] Fix | Delete
/**
[382] Fix | Delete
* PHP5 constructor.
[383] Fix | Delete
*/
[384] Fix | Delete
function __construct( $orig, $final = false )
[385] Fix | Delete
{
[386] Fix | Delete
if (!is_array($final)) {
[387] Fix | Delete
$final = $orig;
[388] Fix | Delete
}
[389] Fix | Delete
$this->orig = $orig;
[390] Fix | Delete
$this->final = $final;
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
/**
[394] Fix | Delete
* PHP4 constructor.
[395] Fix | Delete
*/
[396] Fix | Delete
public function Text_Diff_Op_copy( $orig, $final = false ) {
[397] Fix | Delete
self::__construct( $orig, $final );
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
function &reverse()
[401] Fix | Delete
{
[402] Fix | Delete
$reverse = new Text_Diff_Op_copy($this->final, $this->orig);
[403] Fix | Delete
return $reverse;
[404] Fix | Delete
}
[405] Fix | Delete
[406] Fix | Delete
}
[407] Fix | Delete
[408] Fix | Delete
/**
[409] Fix | Delete
* @package Text_Diff
[410] Fix | Delete
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
[411] Fix | Delete
*
[412] Fix | Delete
* @access private
[413] Fix | Delete
*/
[414] Fix | Delete
class Text_Diff_Op_delete extends Text_Diff_Op {
[415] Fix | Delete
[416] Fix | Delete
/**
[417] Fix | Delete
* PHP5 constructor.
[418] Fix | Delete
*/
[419] Fix | Delete
function __construct( $lines )
[420] Fix | Delete
{
[421] Fix | Delete
$this->orig = $lines;
[422] Fix | Delete
$this->final = false;
[423] Fix | Delete
}
[424] Fix | Delete
[425] Fix | Delete
/**
[426] Fix | Delete
* PHP4 constructor.
[427] Fix | Delete
*/
[428] Fix | Delete
public function Text_Diff_Op_delete( $lines ) {
[429] Fix | Delete
self::__construct( $lines );
[430] Fix | Delete
}
[431] Fix | Delete
[432] Fix | Delete
function &reverse()
[433] Fix | Delete
{
[434] Fix | Delete
$reverse = new Text_Diff_Op_add($this->orig);
[435] Fix | Delete
return $reverse;
[436] Fix | Delete
}
[437] Fix | Delete
[438] Fix | Delete
}
[439] Fix | Delete
[440] Fix | Delete
/**
[441] Fix | Delete
* @package Text_Diff
[442] Fix | Delete
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
[443] Fix | Delete
*
[444] Fix | Delete
* @access private
[445] Fix | Delete
*/
[446] Fix | Delete
class Text_Diff_Op_add extends Text_Diff_Op {
[447] Fix | Delete
[448] Fix | Delete
/**
[449] Fix | Delete
* PHP5 constructor.
[450] Fix | Delete
*/
[451] Fix | Delete
function __construct( $lines )
[452] Fix | Delete
{
[453] Fix | Delete
$this->final = $lines;
[454] Fix | Delete
$this->orig = false;
[455] Fix | Delete
}
[456] Fix | Delete
[457] Fix | Delete
/**
[458] Fix | Delete
* PHP4 constructor.
[459] Fix | Delete
*/
[460] Fix | Delete
public function Text_Diff_Op_add( $lines ) {
[461] Fix | Delete
self::__construct( $lines );
[462] Fix | Delete
}
[463] Fix | Delete
[464] Fix | Delete
function &reverse()
[465] Fix | Delete
{
[466] Fix | Delete
$reverse = new Text_Diff_Op_delete($this->final);
[467] Fix | Delete
return $reverse;
[468] Fix | Delete
}
[469] Fix | Delete
[470] Fix | Delete
}
[471] Fix | Delete
[472] Fix | Delete
/**
[473] Fix | Delete
* @package Text_Diff
[474] Fix | Delete
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
[475] Fix | Delete
*
[476] Fix | Delete
* @access private
[477] Fix | Delete
*/
[478] Fix | Delete
class Text_Diff_Op_change extends Text_Diff_Op {
[479] Fix | Delete
[480] Fix | Delete
/**
[481] Fix | Delete
* PHP5 constructor.
[482] Fix | Delete
*/
[483] Fix | Delete
function __construct( $orig, $final )
[484] Fix | Delete
{
[485] Fix | Delete
$this->orig = $orig;
[486] Fix | Delete
$this->final = $final;
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
/**
[490] Fix | Delete
* PHP4 constructor.
[491] Fix | Delete
*/
[492] Fix | Delete
public function Text_Diff_Op_change( $orig, $final ) {
[493] Fix | Delete
self::__construct( $orig, $final );
[494] Fix | Delete
}
[495] Fix | Delete
[496] Fix | Delete
function &reverse()
[497] Fix | Delete
{
[498] Fix | Delete
$reverse = new Text_Diff_Op_change($this->final, $this->orig);
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function