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