raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Class used internally by Diff to actually compute the diffs.
mp-wp_genesis 4 *
mp-wp_genesis 5 * This class uses the xdiff PECL package (http://pecl.php.net/package/xdiff)
mp-wp_genesis 6 * to compute the differences between the two input arrays.
mp-wp_genesis 7 *
mp-wp_genesis 8 * $Horde: framework/Text_Diff/Diff/Engine/xdiff.php,v 1.6 2008/01/04 10:07:50 jan Exp $
mp-wp_genesis 9 *
mp-wp_genesis 10 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
mp-wp_genesis 11 *
mp-wp_genesis 12 * See the enclosed file COPYING for license information (LGPL). If you did
mp-wp_genesis 13 * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
mp-wp_genesis 14 *
mp-wp_genesis 15 * @author Jon Parise <jon@horde.org>
mp-wp_genesis 16 * @package Text_Diff
mp-wp_genesis 17 */
mp-wp_genesis 18 class Text_Diff_Engine_xdiff {
mp-wp_genesis 19
mp-wp_genesis 20 /**
mp-wp_genesis 21 */
mp-wp_genesis 22 function diff($from_lines, $to_lines)
mp-wp_genesis 23 {
mp-wp_genesis 24 array_walk($from_lines, array('Text_Diff', 'trimNewlines'));
mp-wp_genesis 25 array_walk($to_lines, array('Text_Diff', 'trimNewlines'));
mp-wp_genesis 26
mp-wp_genesis 27 /* Convert the two input arrays into strings for xdiff processing. */
mp-wp_genesis 28 $from_string = implode("\n", $from_lines);
mp-wp_genesis 29 $to_string = implode("\n", $to_lines);
mp-wp_genesis 30
mp-wp_genesis 31 /* Diff the two strings and convert the result to an array. */
mp-wp_genesis 32 $diff = xdiff_string_diff($from_string, $to_string, count($to_lines));
mp-wp_genesis 33 $diff = explode("\n", $diff);
mp-wp_genesis 34
mp-wp_genesis 35 /* Walk through the diff one line at a time. We build the $edits
mp-wp_genesis 36 * array of diff operations by reading the first character of the
mp-wp_genesis 37 * xdiff output (which is in the "unified diff" format).
mp-wp_genesis 38 *
mp-wp_genesis 39 * Note that we don't have enough information to detect "changed"
mp-wp_genesis 40 * lines using this approach, so we can't add Text_Diff_Op_changed
mp-wp_genesis 41 * instances to the $edits array. The result is still perfectly
mp-wp_genesis 42 * valid, albeit a little less descriptive and efficient. */
mp-wp_genesis 43 $edits = array();
mp-wp_genesis 44 foreach ($diff as $line) {
mp-wp_genesis 45 switch ($line[0]) {
mp-wp_genesis 46 case ' ':
mp-wp_genesis 47 $edits[] = &new Text_Diff_Op_copy(array(substr($line, 1)));
mp-wp_genesis 48 break;
mp-wp_genesis 49
mp-wp_genesis 50 case '+':
mp-wp_genesis 51 $edits[] = &new Text_Diff_Op_add(array(substr($line, 1)));
mp-wp_genesis 52 break;
mp-wp_genesis 53
mp-wp_genesis 54 case '-':
mp-wp_genesis 55 $edits[] = &new Text_Diff_Op_delete(array(substr($line, 1)));
mp-wp_genesis 56 break;
mp-wp_genesis 57 }
mp-wp_genesis 58 }
mp-wp_genesis 59
mp-wp_genesis 60 return $edits;
mp-wp_genesis 61 }
mp-wp_genesis 62
mp-wp_genesis 63 }