3
* General API for generating and formatting diffs - the differences between
4
* two sequences of strings.
6
* The original PHP version of this code was written by Geoffrey T. Dairiki
7
* <dairiki@dairiki.org>, and is used/adapted with his permission.
9
* Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
10
* Copyright 2004-2010 The Horde Project (http://www.horde.org/)
12
* See the enclosed file COPYING for license information (LGPL). If you did
13
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
16
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
28
* Computes diffs between sequences of strings.
30
* @param string $engine Name of the diffing engine to use. 'auto'
31
* will automatically select the best.
32
* @param array $params Parameters to pass to the diffing engine.
33
* Normally an array of two arrays, each
34
* containing the lines from a file.
36
function Text_Diff($engine, $params)
38
// Backward compatibility workaround.
39
if (!is_string($engine)) {
40
$params = array($engine, $params);
44
if ($engine == 'auto') {
45
$engine = extension_loaded('xdiff') ? 'xdiff' : 'native';
47
$engine = basename($engine);
51
require_once dirname(__FILE__).'/Diff/Engine/' . $engine . '.php';
52
$class = 'Text_Diff_Engine_' . $engine;
53
$diff_engine = new $class();
55
$this->_edits = call_user_func_array(array($diff_engine, 'diff'), $params);
59
* Returns the array of differences.
67
* returns the number of new (added) lines in a given diff.
69
* @since Text_Diff 1.1.0
71
* @return integer The number of new lines
73
function countAddedLines()
76
foreach ($this->_edits as $edit) {
77
if (is_a($edit, 'Text_Diff_Op_add') ||
78
is_a($edit, 'Text_Diff_Op_change')) {
79
$count += $edit->nfinal();
86
* Returns the number of deleted (removed) lines in a given diff.
88
* @since Text_Diff 1.1.0
90
* @return integer The number of deleted lines
92
function countDeletedLines()
95
foreach ($this->_edits as $edit) {
96
if (is_a($edit, 'Text_Diff_Op_delete') ||
97
is_a($edit, 'Text_Diff_Op_change')) {
98
$count += $edit->norig();
105
* Computes a reversed diff.
109
* $diff = new Text_Diff($lines1, $lines2);
110
* $rev = $diff->reverse();
113
* @return Text_Diff A Diff object representing the inverse of the
114
* original diff. Note that we purposely don't return a
115
* reference here, since this essentially is a clone()
120
if (version_compare(zend_version(), '2', '>')) {
125
$rev->_edits = array();
126
foreach ($this->_edits as $edit) {
127
$rev->_edits[] = $edit->reverse();
133
* Checks for an empty diff.
135
* @return boolean True if two sequences were identical.
139
foreach ($this->_edits as $edit) {
140
if (!is_a($edit, 'Text_Diff_Op_copy')) {
148
* Computes the length of the Longest Common Subsequence (LCS).
150
* This is mostly for diagnostic purposes.
152
* @return integer The length of the LCS.
157
foreach ($this->_edits as $edit) {
158
if (is_a($edit, 'Text_Diff_Op_copy')) {
159
$lcs += count($edit->orig);
166
* Gets the original set of lines.
168
* This reconstructs the $from_lines parameter passed to the constructor.
170
* @return array The original sequence of strings.
172
function getOriginal()
175
foreach ($this->_edits as $edit) {
177
array_splice($lines, count($lines), 0, $edit->orig);
184
* Gets the final set of lines.
186
* This reconstructs the $to_lines parameter passed to the constructor.
188
* @return array The sequence of strings.
193
foreach ($this->_edits as $edit) {
195
array_splice($lines, count($lines), 0, $edit->final);
202
* Removes trailing newlines from a line of text. This is meant to be used
205
* @param string $line The line to trim.
206
* @param integer $key The index of the line in the array. Not used.
208
static function trimNewlines(&$line, $key)
210
$line = str_replace(array("\n", "\r"), '', $line);
214
* Determines the location of the system temporary directory.
220
* @return string A directory name which can be used for temp files.
221
* Returns false if one could not be found.
223
function _getTempDir()
225
$tmp_locations = array('/tmp', '/var/tmp', 'c:\WUTemp', 'c:\temp',
226
'c:\windows\temp', 'c:\winnt\temp');
228
/* Try PHP's upload_tmp_dir directive. */
229
$tmp = ini_get('upload_tmp_dir');
231
/* Otherwise, try to determine the TMPDIR environment variable. */
233
$tmp = getenv('TMPDIR');
236
/* If we still cannot determine a value, then cycle through a list of
237
* preset possibilities. */
238
while (!strlen($tmp) && count($tmp_locations)) {
239
$tmp_check = array_shift($tmp_locations);
240
if (@is_dir($tmp_check)) {
245
/* If it is still empty, we have failed, so return false; otherwise
246
* return the directory determined. */
247
return strlen($tmp) ? $tmp : false;
251
* Checks a diff for validity.
253
* This is here only for debugging purposes.
255
function _check($from_lines, $to_lines)
257
if (serialize($from_lines) != serialize($this->getOriginal())) {
258
trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
260
if (serialize($to_lines) != serialize($this->getFinal())) {
261
trigger_error("Reconstructed final doesn't match", E_USER_ERROR);
264
$rev = $this->reverse();
265
if (serialize($to_lines) != serialize($rev->getOriginal())) {
266
trigger_error("Reversed original doesn't match", E_USER_ERROR);
268
if (serialize($from_lines) != serialize($rev->getFinal())) {
269
trigger_error("Reversed final doesn't match", E_USER_ERROR);
273
foreach ($this->_edits as $edit) {
274
if ($prevtype == get_class($edit)) {
275
trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
277
$prevtype = get_class($edit);
287
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
289
class Text_MappedDiff extends Text_Diff {
292
* Computes a diff between sequences of strings.
294
* This can be used to compute things like case-insensitve diffs, or diffs
295
* which ignore changes in white-space.
297
* @param array $from_lines An array of strings.
298
* @param array $to_lines An array of strings.
299
* @param array $mapped_from_lines This array should have the same size
300
* number of elements as $from_lines. The
301
* elements in $mapped_from_lines and
302
* $mapped_to_lines are what is actually
303
* compared when computing the diff.
304
* @param array $mapped_to_lines This array should have the same number
305
* of elements as $to_lines.
307
function Text_MappedDiff($from_lines, $to_lines,
308
$mapped_from_lines, $mapped_to_lines)
310
assert(count($from_lines) == count($mapped_from_lines));
311
assert(count($to_lines) == count($mapped_to_lines));
313
parent::Text_Diff($mapped_from_lines, $mapped_to_lines);
316
for ($i = 0; $i < count($this->_edits); $i++) {
317
$orig = &$this->_edits[$i]->orig;
318
if (is_array($orig)) {
319
$orig = array_slice($from_lines, $xi, count($orig));
323
$final = &$this->_edits[$i]->final;
324
if (is_array($final)) {
325
$final = array_slice($to_lines, $yi, count($final));
326
$yi += count($final);
335
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
346
trigger_error('Abstract method', E_USER_ERROR);
351
return $this->orig ? count($this->orig) : 0;
356
return $this->final ? count($this->final) : 0;
363
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
367
class Text_Diff_Op_copy extends Text_Diff_Op {
369
function Text_Diff_Op_copy($orig, $final = false)
371
if (!is_array($final)) {
375
$this->final = $final;
380
$reverse = new Text_Diff_Op_copy($this->final, $this->orig);
388
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
392
class Text_Diff_Op_delete extends Text_Diff_Op {
394
function Text_Diff_Op_delete($lines)
396
$this->orig = $lines;
397
$this->final = false;
402
$reverse = new Text_Diff_Op_add($this->orig);
410
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
414
class Text_Diff_Op_add extends Text_Diff_Op {
416
function Text_Diff_Op_add($lines)
418
$this->final = $lines;
424
$reverse = new Text_Diff_Op_delete($this->final);
432
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
436
class Text_Diff_Op_change extends Text_Diff_Op {
438
function Text_Diff_Op_change($orig, $final)
441
$this->final = $final;
446
$reverse = new Text_Diff_Op_change($this->final, $this->orig);