includes/diffengine/Engine/xdiff.php
author Dan Fuhry <dan@enanocms.org>
Thu, 02 Jun 2011 05:24:38 -0400
changeset 1342 2164d18cb10c
parent 1227 bdac73ed481e
permissions -rw-r--r--
Added database transaction API. (Untested with Postgres)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     1
<?php
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     2
/**
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     3
 * Class used internally by Diff to actually compute the diffs.  This class
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     4
 * uses the xdiff PECL package (http://pecl.php.net/package/xdiff) to compute
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     5
 * the differences between the two input arrays.
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     6
 *
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     7
 * @author  Jon Parise <jon@horde.org>
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     8
 * @package Text_Diff
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     9
 *
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    10
 * @access private
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    11
 */
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    12
class Text_Diff_Engine_xdiff {
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    13
1227
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    14
		function diff($from_lines, $to_lines)
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    15
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    16
				array_walk($from_lines, array('Text_Diff', 'trimNewlines'));
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    17
				array_walk($to_lines, array('Text_Diff', 'trimNewlines'));
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    18
1227
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    19
				/* Convert the two input arrays into strings for xdiff processing. */
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    20
				$from_string = implode("\n", $from_lines);
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    21
				$to_string = implode("\n", $to_lines);
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    22
1227
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    23
				/* Diff the two strings and convert the result to an array. */
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    24
				$diff = xdiff_string_diff($from_string, $to_string, count($to_lines));
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    25
				$diff = explode("\n", $diff);
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    26
1227
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    27
				/* Walk through the diff one line at a time.  We build the $edits
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    28
 				* array of diff operations by reading the first character of the
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    29
 				* xdiff output (which is in the "unified diff" format).
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    30
 				*
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    31
 				* Note that we don't have enough information to detect "changed"
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    32
 				* lines using this approach, so we can't add Text_Diff_Op_changed
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    33
 				* instances to the $edits array.  The result is still perfectly
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    34
 				* valid, albeit a little less descriptive and efficient. */
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    35
				$edits = array();
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    36
				foreach ($diff as $line) {
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    37
						switch ($line[0]) {
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    38
						case ' ':
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    39
								$edits[] = &new Text_Diff_Op_copy(array(substr($line, 1)));
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    40
								break;
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    41
1227
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    42
						case '+':
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    43
								$edits[] = &new Text_Diff_Op_add(array(substr($line, 1)));
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    44
								break;
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    45
1227
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    46
						case '-':
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    47
								$edits[] = &new Text_Diff_Op_delete(array(substr($line, 1)));
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    48
								break;
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    49
						}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    50
				}
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    51
1227
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    52
				return $edits;
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1
diff changeset
    53
		}
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    54
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    55
}