Source for file Diff.php
Documentation is available at Diff.php
* General API for generating and formatting diffs - the differences between
* two sequences of strings.
* The original PHP version of this code was written by Geoffrey T. Dairiki
* <dairiki@dairiki.org>, and is used/adapted with his permission.
* $Horde: framework/Text_Diff/Diff.php,v 1.11.2.12 2009/01/06 15:23:41 jan Exp $
* Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
* Copyright 2004-2009 The Horde Project (http://www.horde.org/)
* See the enclosed file COPYING for license information (LGPL). If you did
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
* Computes diffs between sequences of strings.
* @param string $engine Name of the diffing engine to use. 'auto'
* will automatically select the best.
* @param array $params Parameters to pass to the diffing engine.
* Normally an array of two arrays, each
* containing the lines from a file.
// Backward compatibility workaround.
$params = array ($engine, $params);
require_once 'Text/Diff/Engine/' . $engine . '.php';
$class = 'Text_Diff_Engine_' . $engine;
$diff_engine = new $class();
* Returns the array of differences.
* returns the number of new (added) lines in a given diff.
* @return integer The number of new lines
foreach ($this->_edits as $edit) {
if (is_a($edit, 'Text_Diff_Op_add') ||
is_a($edit, 'Text_Diff_Op_change')) {
$count += $edit->nfinal ();
* Returns the number of deleted (removed) lines in a given diff.
* @return integer The number of deleted lines
foreach ($this->_edits as $edit) {
if (is_a($edit, 'Text_Diff_Op_delete') ||
is_a($edit, 'Text_Diff_Op_change')) {
$count += $edit->norig ();
* Computes a reversed diff.
* $diff = new Text_Diff($lines1, $lines2);
* $rev = $diff->reverse();
* @return Text_Diff A Diff object representing the inverse of the
* original diff. Note that we purposely don't return a
* reference here, since this essentially is a clone()
foreach ($this->_edits as $edit) {
$rev->_edits [] = $edit->reverse ();
* Checks for an empty diff.
* @return boolean True if two sequences were identical.
foreach ($this->_edits as $edit) {
if (!is_a($edit, 'Text_Diff_Op_copy')) {
* Computes the length of the Longest Common Subsequence (LCS).
* This is mostly for diagnostic purposes.
* @return integer The length of the LCS.
foreach ($this->_edits as $edit) {
if (is_a($edit, 'Text_Diff_Op_copy')) {
$lcs += count($edit->orig );
* Gets the original set of lines.
* This reconstructs the $from_lines parameter passed to the constructor.
* @return array The original sequence of strings.
foreach ($this->_edits as $edit) {
* Gets the final set of lines.
* This reconstructs the $to_lines parameter passed to the constructor.
* @return array The sequence of strings.
foreach ($this->_edits as $edit) {
* Removes trailing newlines from a line of text. This is meant to be used
* @param string $line The line to trim.
* @param integer $key The index of the line in the array. Not used.
* Determines the location of the system temporary directory.
* @return string A directory name which can be used for temp files.
* Returns false if one could not be found.
$tmp_locations = array ('/tmp', '/var/tmp', 'c:\WUTemp', 'c:\temp',
'c:\windows\temp', 'c:\winnt\temp');
/* Try PHP's upload_tmp_dir directive. */
/* Otherwise, try to determine the TMPDIR environment variable. */
/* If we still cannot determine a value, then cycle through a list of
* preset possibilities. */
/* If it is still empty, we have failed, so return false; otherwise
* return the directory determined. */
return strlen($tmp) ? $tmp : false;
* Checks a diff for validity.
* This is here only for debugging purposes.
function _check ($from_lines, $to_lines)
trigger_error("Reconstructed original doesn't match", E_USER_ERROR );
trigger_error("Reconstructed final doesn't match", E_USER_ERROR );
foreach ($this->_edits as $edit) {
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
* Computes a diff between sequences of strings.
* This can be used to compute things like case-insensitve diffs, or diffs
* which ignore changes in white-space.
* @param array $from_lines An array of strings.
* @param array $to_lines An array of strings.
* @param array $mapped_from_lines This array should have the same size
* number of elements as $from_lines. The
* elements in $mapped_from_lines and
* $mapped_to_lines are what is actually
* compared when computing the diff.
* @param array $mapped_to_lines This array should have the same number
* of elements as $to_lines.
$mapped_from_lines, $mapped_to_lines)
parent ::Text_Diff ($mapped_from_lines, $mapped_to_lines);
for ($i = 0; $i < count($this->_edits); $i++ ) {
$orig = &$this->_edits[$i]->orig;
$final = &$this->_edits[$i]->final;
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
return $this->orig ? count($this->orig) : 0;
return $this->final ? count($this->final) : 0;
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
class Text_Diff_Op_copy extends Text_Diff_Op {
function Text_Diff_Op_copy ($orig, $final = false )
$reverse = new Text_Diff_Op_copy ($this->final, $this->orig);
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
class Text_Diff_Op_delete extends Text_Diff_Op {
function Text_Diff_Op_delete ($lines)
$reverse = new Text_Diff_Op_add ($this->orig);
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
class Text_Diff_Op_add extends Text_Diff_Op {
function Text_Diff_Op_add ($lines)
$reverse = new Text_Diff_Op_delete ($this->final);
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
class Text_Diff_Op_change extends Text_Diff_Op {
function Text_Diff_Op_change ($orig, $final)
$reverse = new Text_Diff_Op_change ($this->final, $this->orig);
Documentation generated on Mon, 11 Mar 2019 16:03:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|