Edit File by line
/home/barbar84/public_h.../wp-inclu.../Text/Diff/Engine
File: shell.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Class used internally by Diff to actually compute the diffs.
[2] Fix | Delete
*
[3] Fix | Delete
* This class uses the Unix `diff` program via shell_exec to compute the
[4] Fix | Delete
* differences between the two input arrays.
[5] Fix | Delete
*
[6] Fix | Delete
* Copyright 2007-2010 The Horde Project (http://www.horde.org/)
[7] Fix | Delete
*
[8] Fix | Delete
* See the enclosed file COPYING for license information (LGPL). If you did
[9] Fix | Delete
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
[10] Fix | Delete
*
[11] Fix | Delete
* @author Milian Wolff <mail@milianw.de>
[12] Fix | Delete
* @package Text_Diff
[13] Fix | Delete
* @since 0.3.0
[14] Fix | Delete
*/
[15] Fix | Delete
class Text_Diff_Engine_shell {
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Path to the diff executable
[19] Fix | Delete
*
[20] Fix | Delete
* @var string
[21] Fix | Delete
*/
[22] Fix | Delete
var $_diffCommand = 'diff';
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Returns the array of differences.
[26] Fix | Delete
*
[27] Fix | Delete
* @param array $from_lines lines of text from old file
[28] Fix | Delete
* @param array $to_lines lines of text from new file
[29] Fix | Delete
*
[30] Fix | Delete
* @return array all changes made (array with Text_Diff_Op_* objects)
[31] Fix | Delete
*/
[32] Fix | Delete
function diff($from_lines, $to_lines)
[33] Fix | Delete
{
[34] Fix | Delete
array_walk($from_lines, array('Text_Diff', 'trimNewlines'));
[35] Fix | Delete
array_walk($to_lines, array('Text_Diff', 'trimNewlines'));
[36] Fix | Delete
[37] Fix | Delete
$temp_dir = Text_Diff::_getTempDir();
[38] Fix | Delete
[39] Fix | Delete
// Execute gnu diff or similar to get a standard diff file.
[40] Fix | Delete
$from_file = tempnam($temp_dir, 'Text_Diff');
[41] Fix | Delete
$to_file = tempnam($temp_dir, 'Text_Diff');
[42] Fix | Delete
$fp = fopen($from_file, 'w');
[43] Fix | Delete
fwrite($fp, implode("\n", $from_lines));
[44] Fix | Delete
fclose($fp);
[45] Fix | Delete
$fp = fopen($to_file, 'w');
[46] Fix | Delete
fwrite($fp, implode("\n", $to_lines));
[47] Fix | Delete
fclose($fp);
[48] Fix | Delete
$diff = shell_exec($this->_diffCommand . ' ' . $from_file . ' ' . $to_file);
[49] Fix | Delete
unlink($from_file);
[50] Fix | Delete
unlink($to_file);
[51] Fix | Delete
[52] Fix | Delete
if (is_null($diff)) {
[53] Fix | Delete
// No changes were made
[54] Fix | Delete
return array(new Text_Diff_Op_copy($from_lines));
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
$from_line_no = 1;
[58] Fix | Delete
$to_line_no = 1;
[59] Fix | Delete
$edits = array();
[60] Fix | Delete
[61] Fix | Delete
// Get changed lines by parsing something like:
[62] Fix | Delete
// 0a1,2
[63] Fix | Delete
// 1,2c4,6
[64] Fix | Delete
// 1,5d6
[65] Fix | Delete
preg_match_all('#^(\d+)(?:,(\d+))?([adc])(\d+)(?:,(\d+))?$#m', $diff,
[66] Fix | Delete
$matches, PREG_SET_ORDER);
[67] Fix | Delete
[68] Fix | Delete
foreach ($matches as $match) {
[69] Fix | Delete
if (!isset($match[5])) {
[70] Fix | Delete
// This paren is not set every time (see regex).
[71] Fix | Delete
$match[5] = false;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
if ($match[3] == 'a') {
[75] Fix | Delete
$from_line_no--;
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
if ($match[3] == 'd') {
[79] Fix | Delete
$to_line_no--;
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
if ($from_line_no < $match[1] || $to_line_no < $match[4]) {
[83] Fix | Delete
// copied lines
[84] Fix | Delete
assert($match[1] - $from_line_no == $match[4] - $to_line_no);
[85] Fix | Delete
array_push($edits,
[86] Fix | Delete
new Text_Diff_Op_copy(
[87] Fix | Delete
$this->_getLines($from_lines, $from_line_no, $match[1] - 1),
[88] Fix | Delete
$this->_getLines($to_lines, $to_line_no, $match[4] - 1)));
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
switch ($match[3]) {
[92] Fix | Delete
case 'd':
[93] Fix | Delete
// deleted lines
[94] Fix | Delete
array_push($edits,
[95] Fix | Delete
new Text_Diff_Op_delete(
[96] Fix | Delete
$this->_getLines($from_lines, $from_line_no, $match[2])));
[97] Fix | Delete
$to_line_no++;
[98] Fix | Delete
break;
[99] Fix | Delete
[100] Fix | Delete
case 'c':
[101] Fix | Delete
// changed lines
[102] Fix | Delete
array_push($edits,
[103] Fix | Delete
new Text_Diff_Op_change(
[104] Fix | Delete
$this->_getLines($from_lines, $from_line_no, $match[2]),
[105] Fix | Delete
$this->_getLines($to_lines, $to_line_no, $match[5])));
[106] Fix | Delete
break;
[107] Fix | Delete
[108] Fix | Delete
case 'a':
[109] Fix | Delete
// added lines
[110] Fix | Delete
array_push($edits,
[111] Fix | Delete
new Text_Diff_Op_add(
[112] Fix | Delete
$this->_getLines($to_lines, $to_line_no, $match[5])));
[113] Fix | Delete
$from_line_no++;
[114] Fix | Delete
break;
[115] Fix | Delete
}
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
if (!empty($from_lines)) {
[119] Fix | Delete
// Some lines might still be pending. Add them as copied
[120] Fix | Delete
array_push($edits,
[121] Fix | Delete
new Text_Diff_Op_copy(
[122] Fix | Delete
$this->_getLines($from_lines, $from_line_no,
[123] Fix | Delete
$from_line_no + count($from_lines) - 1),
[124] Fix | Delete
$this->_getLines($to_lines, $to_line_no,
[125] Fix | Delete
$to_line_no + count($to_lines) - 1)));
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
return $edits;
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
/**
[132] Fix | Delete
* Get lines from either the old or new text
[133] Fix | Delete
*
[134] Fix | Delete
* @access private
[135] Fix | Delete
*
[136] Fix | Delete
* @param array $text_lines Either $from_lines or $to_lines (passed by reference).
[137] Fix | Delete
* @param int $line_no Current line number (passed by reference).
[138] Fix | Delete
* @param int $end Optional end line, when we want to chop more
[139] Fix | Delete
* than one line.
[140] Fix | Delete
*
[141] Fix | Delete
* @return array The chopped lines
[142] Fix | Delete
*/
[143] Fix | Delete
function _getLines(&$text_lines, &$line_no, $end = false)
[144] Fix | Delete
{
[145] Fix | Delete
if (!empty($end)) {
[146] Fix | Delete
$lines = array();
[147] Fix | Delete
// We can shift even more
[148] Fix | Delete
while ($line_no <= $end) {
[149] Fix | Delete
array_push($lines, array_shift($text_lines));
[150] Fix | Delete
$line_no++;
[151] Fix | Delete
}
[152] Fix | Delete
} else {
[153] Fix | Delete
$lines = array(array_shift($text_lines));
[154] Fix | Delete
$line_no++;
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
return $lines;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function