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 __construct( $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);
61 public function Text_Diff( $engine, $params ) {
62 self::__construct( $engine, $params );
66 * Returns the array of differences.
74 * returns the number of new (added) lines in a given diff.
76 * @since Text_Diff 1.1.0
78 * @return integer The number of new lines
80 function countAddedLines()
83 foreach ($this->_edits as $edit) {
84 if (is_a($edit, 'Text_Diff_Op_add') ||
85 is_a($edit, 'Text_Diff_Op_change')) {
86 $count += $edit->nfinal();
93 * Returns the number of deleted (removed) lines in a given diff.
95 * @since Text_Diff 1.1.0
97 * @return integer The number of deleted lines
99 function countDeletedLines()
102 foreach ($this->_edits as $edit) {
103 if (is_a($edit, 'Text_Diff_Op_delete') ||
104 is_a($edit, 'Text_Diff_Op_change')) {
105 $count += $edit->norig();
112 * Computes a reversed diff.
116 * $diff = new Text_Diff($lines1, $lines2);
117 * $rev = $diff->reverse();
120 * @return Text_Diff A Diff object representing the inverse of the
121 * original diff. Note that we purposely don't return a
122 * reference here, since this essentially is a clone()
127 if (version_compare(zend_version(), '2', '>')) {
132 $rev->_edits = array();
133 foreach ($this->_edits as $edit) {
134 $rev->_edits[] = $edit->reverse();
140 * Checks for an empty diff.
142 * @return boolean True if two sequences were identical.
146 foreach ($this->_edits as $edit) {
147 if (!is_a($edit, 'Text_Diff_Op_copy')) {
155 * Computes the length of the Longest Common Subsequence (LCS).
157 * This is mostly for diagnostic purposes.
159 * @return integer The length of the LCS.
164 foreach ($this->_edits as $edit) {
165 if (is_a($edit, 'Text_Diff_Op_copy')) {
166 $lcs += count($edit->orig);
173 * Gets the original set of lines.
175 * This reconstructs the $from_lines parameter passed to the constructor.
177 * @return array The original sequence of strings.
179 function getOriginal()
182 foreach ($this->_edits as $edit) {
184 array_splice($lines, count($lines), 0, $edit->orig);
191 * Gets the final set of lines.
193 * This reconstructs the $to_lines parameter passed to the constructor.
195 * @return array The sequence of strings.
200 foreach ($this->_edits as $edit) {
202 array_splice($lines, count($lines), 0, $edit->final);
209 * Removes trailing newlines from a line of text. This is meant to be used
212 * @param string $line The line to trim.
213 * @param integer $key The index of the line in the array. Not used.
215 static function trimNewlines(&$line, $key)
217 $line = str_replace(array("\n", "\r"), '', $line);
221 * Determines the location of the system temporary directory.
227 * @return string A directory name which can be used for temp files.
228 * Returns false if one could not be found.
230 function _getTempDir()
232 $tmp_locations = array('/tmp', '/var/tmp', 'c:\WUTemp', 'c:\temp',
233 'c:\windows\temp', 'c:\winnt\temp');
235 /* Try PHP's upload_tmp_dir directive. */
236 $tmp = ini_get('upload_tmp_dir');
238 /* Otherwise, try to determine the TMPDIR environment variable. */
240 $tmp = getenv('TMPDIR');
243 /* If we still cannot determine a value, then cycle through a list of
244 * preset possibilities. */
245 while (!strlen($tmp) && count($tmp_locations)) {
246 $tmp_check = array_shift($tmp_locations);
247 if (@is_dir($tmp_check)) {
252 /* If it is still empty, we have failed, so return false; otherwise
253 * return the directory determined. */
254 return strlen($tmp) ? $tmp : false;
258 * Checks a diff for validity.
260 * This is here only for debugging purposes.
262 function _check($from_lines, $to_lines)
264 if (serialize($from_lines) != serialize($this->getOriginal())) {
265 trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
267 if (serialize($to_lines) != serialize($this->getFinal())) {
268 trigger_error("Reconstructed final doesn't match", E_USER_ERROR);
271 $rev = $this->reverse();
272 if (serialize($to_lines) != serialize($rev->getOriginal())) {
273 trigger_error("Reversed original doesn't match", E_USER_ERROR);
275 if (serialize($from_lines) != serialize($rev->getFinal())) {
276 trigger_error("Reversed final doesn't match", E_USER_ERROR);
280 foreach ($this->_edits as $edit) {
281 if ($prevtype == get_class($edit)) {
282 trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
284 $prevtype = get_class($edit);
294 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
296 class Text_MappedDiff extends Text_Diff {
299 * Computes a diff between sequences of strings.
301 * This can be used to compute things like case-insensitve diffs, or diffs
302 * which ignore changes in white-space.
304 * @param array $from_lines An array of strings.
305 * @param array $to_lines An array of strings.
306 * @param array $mapped_from_lines This array should have the same size
307 * number of elements as $from_lines. The
308 * elements in $mapped_from_lines and
309 * $mapped_to_lines are what is actually
310 * compared when computing the diff.
311 * @param array $mapped_to_lines This array should have the same number
312 * of elements as $to_lines.
314 function __construct($from_lines, $to_lines,
315 $mapped_from_lines, $mapped_to_lines)
317 assert(count($from_lines) == count($mapped_from_lines));
318 assert(count($to_lines) == count($mapped_to_lines));
320 parent::Text_Diff($mapped_from_lines, $mapped_to_lines);
323 for ($i = 0; $i < count($this->_edits); $i++) {
324 $orig = &$this->_edits[$i]->orig;
325 if (is_array($orig)) {
326 $orig = array_slice($from_lines, $xi, count($orig));
330 $final = &$this->_edits[$i]->final;
331 if (is_array($final)) {
332 $final = array_slice($to_lines, $yi, count($final));
333 $yi += count($final);
341 public function Text_MappedDiff( $from_lines, $to_lines,
342 $mapped_from_lines, $mapped_to_lines ) {
343 self::__construct( $from_lines, $to_lines,
344 $mapped_from_lines, $mapped_to_lines );
351 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
362 trigger_error('Abstract method', E_USER_ERROR);
367 return $this->orig ? count($this->orig) : 0;
372 return $this->final ? count($this->final) : 0;
379 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
383 class Text_Diff_Op_copy extends Text_Diff_Op {
388 function __construct( $orig, $final = false )
390 if (!is_array($final)) {
394 $this->final = $final;
400 public function Text_Diff_Op_copy( $orig, $final = false ) {
401 self::__construct( $orig, $final );
406 $reverse = new Text_Diff_Op_copy($this->final, $this->orig);
414 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
418 class Text_Diff_Op_delete extends Text_Diff_Op {
423 function __construct( $lines )
425 $this->orig = $lines;
426 $this->final = false;
432 public function Text_Diff_Op_delete( $lines ) {
433 self::__construct( $lines );
438 $reverse = new Text_Diff_Op_add($this->orig);
446 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
450 class Text_Diff_Op_add extends Text_Diff_Op {
455 function __construct( $lines )
457 $this->final = $lines;
464 public function Text_Diff_Op_add( $lines ) {
465 self::__construct( $lines );
470 $reverse = new Text_Diff_Op_delete($this->final);
478 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
482 class Text_Diff_Op_change extends Text_Diff_Op {
487 function __construct( $orig, $final )
490 $this->final = $final;
496 public function Text_Diff_Op_change( $orig, $final ) {
497 self::__construct( $orig, $final );
502 $reverse = new Text_Diff_Op_change($this->final, $this->orig);