Make WordPress Core

Changeset 13211


Ignore:
Timestamp:
02/19/2010 01:25:26 AM (15 years ago)
Author:
nacin
Message:

Update Text_Diff. Props simek. Fixes #9467

Location:
trunk/wp-includes/Text
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/Text/Diff.php

    r8581 r13211  
    77 * <dairiki@dairiki.org>, and is used/adapted with his permission.
    88 *
    9  * $Horde: framework/Text_Diff/Diff.php,v 1.26 2008/01/04 10:07:49 jan Exp $
    10  *
    119 * Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
    12  * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
     10 * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
    1311 *
    1412 * See the enclosed file COPYING for license information (LGPL). If you did
     
    6462    {
    6563        return $this->_edits;
     64    }
     65
     66    /**
     67     * returns the number of new (added) lines in a given diff.
     68     *
     69     * @since Text_Diff 1.1.0
     70     *
     71     * @return integer The number of new lines
     72     */
     73    function countAddedLines()
     74    {
     75        $count = 0;
     76        foreach ($this->_edits as $edit) {
     77            if (is_a($edit, 'Text_Diff_Op_add') ||
     78                is_a($edit, 'Text_Diff_Op_change')) {
     79                $count += $edit->nfinal();
     80            }
     81        }
     82        return $count;
     83    }
     84
     85    /**
     86     * Returns the number of deleted (removed) lines in a given diff.
     87     *
     88     * @since Text_Diff 1.1.0
     89     *
     90     * @return integer The number of deleted lines
     91     */
     92    function countDeletedLines()
     93    {
     94        $count = 0;
     95        foreach ($this->_edits as $edit) {
     96            if (is_a($edit, 'Text_Diff_Op_delete') ||
     97                is_a($edit, 'Text_Diff_Op_change')) {
     98                $count += $edit->norig();
     99            }
     100        }
     101        return $count;
    66102    }
    67103
  • trunk/wp-includes/Text/Diff/Engine/native.php

    r7747 r13211  
    11<?php
    22/**
    3  * $Horde: framework/Text_Diff/Diff/Engine/native.php,v 1.10 2008/01/04 10:27:53 jan Exp $
    4  *
    5  * Class used internally by Text_Diff to actually compute the diffs. This
    6  * class is implemented using native PHP code.
     3 * Class used internally by Text_Diff to actually compute the diffs.
     4 *
     5 * This class is implemented using native PHP code.
    76 *
    87 * The algorithm used here is mostly lifted from the perl module
     
    2019 * code was written by him, and is used/adapted with his permission.
    2120 *
    22  * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
     21 * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
    2322 *
    2423 * See the enclosed file COPYING for license information (LGPL). If you did
  • trunk/wp-includes/Text/Diff/Engine/shell.php

    r7747 r13211  
    66 * differences between the two input arrays.
    77 *
    8  * $Horde: framework/Text_Diff/Diff/Engine/shell.php,v 1.8 2008/01/04 10:07:50 jan Exp $
    9  *
    10  * Copyright 2007-2008 The Horde Project (http://www.horde.org/)
     8 * Copyright 2007-2010 The Horde Project (http://www.horde.org/)
    119 *
    1210 * See the enclosed file COPYING for license information (LGPL). If you did
  • trunk/wp-includes/Text/Diff/Engine/string.php

    r7747 r13211  
    1111 * </code>
    1212 *
    13  * $Horde: framework/Text_Diff/Diff/Engine/string.php,v 1.7 2008/01/04 10:07:50 jan Exp $
    14  *
    1513 * Copyright 2005 Örjan Persson <o@42mm.org>
    16  * Copyright 2005-2008 The Horde Project (http://www.horde.org/)
     14 * Copyright 2005-2010 The Horde Project (http://www.horde.org/)
    1715 *
    1816 * See the enclosed file COPYING for license information (LGPL). If you did
     
    4038    function diff($diff, $mode = 'autodetect')
    4139    {
     40        // Detect line breaks.
     41        $lnbr = "\n";
     42        if (strpos($diff, "\r\n") !== false) {
     43            $lnbr = "\r\n";
     44        } elseif (strpos($diff, "\r") !== false) {
     45            $lnbr = "\r";
     46        }
     47
     48        // Make sure we have a line break at the EOF.
     49        if (substr($diff, -strlen($lnbr)) != $lnbr) {
     50            $diff .= $lnbr;
     51        }
     52
    4253        if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') {
    4354            return PEAR::raiseError('Type of diff is unsupported');
     
    4960            if ($context === $unified) {
    5061                return PEAR::raiseError('Type of diff could not be detected');
    51             } elseif ($context === false || $context === false) {
     62            } elseif ($context === false || $unified === false) {
    5263                $mode = $context !== false ? 'context' : 'unified';
    5364            } else {
     
    5667        }
    5768
    58         // split by new line and remove the diff header
    59         $diff = explode("\n", $diff);
    60         array_shift($diff);
    61         array_shift($diff);
     69        // Split by new line and remove the diff header, if there is one.
     70        $diff = explode($lnbr, $diff);
     71        if (($mode == 'context' && strpos($diff[0], '***') === 0) ||
     72            ($mode == 'unified' && strpos($diff[0], '---') === 0)) {
     73            array_shift($diff);
     74            array_shift($diff);
     75        }
    6276
    6377        if ($mode == 'context') {
  • trunk/wp-includes/Text/Diff/Engine/xdiff.php

    r7747 r13211  
    66 * to compute the differences between the two input arrays.
    77 *
    8  * $Horde: framework/Text_Diff/Diff/Engine/xdiff.php,v 1.6 2008/01/04 10:07:50 jan Exp $
    9  *
    10  * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
     8 * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
    119 *
    1210 * See the enclosed file COPYING for license information (LGPL). If you did
     
    4341        $edits = array();
    4442        foreach ($diff as $line) {
     43            if (!strlen($line)) {
     44                continue;
     45            }
    4546            switch ($line[0]) {
    4647            case ' ':
  • trunk/wp-includes/Text/Diff/Renderer.php

    r7747 r13211  
    66 * this class be customized via inheritance, to obtain fancier outputs.
    77 *
    8  * $Horde: framework/Text_Diff/Diff/Renderer.php,v 1.21 2008/01/04 10:07:50 jan Exp $
    9  *
    10  * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
     8 * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
    119 *
    1210 * See the enclosed file COPYING for license information (LGPL). If you did
  • trunk/wp-includes/Text/Diff/Renderer/inline.php

    r8581 r13211  
    33 * "Inline" diff renderer.
    44 *
    5  * $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.21 2008/01/04 10:07:51 jan Exp $
    6  *
    7  * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
     5 * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
    86 *
    97 * See the enclosed file COPYING for license information (LGPL). If you did
     
    3129    /**
    3230     * Number of leading context "lines" to preserve.
     31     *
     32     * @var integer
    3333     */
    3434    var $_leading_context_lines = 10000;
     
    3636    /**
    3737     * Number of trailing context "lines" to preserve.
     38     *
     39     * @var integer
    3840     */
    3941    var $_trailing_context_lines = 10000;
     
    4143    /**
    4244     * Prefix for inserted text.
     45     *
     46     * @var string
    4347     */
    4448    var $_ins_prefix = '<ins>';
     
    4650    /**
    4751     * Suffix for inserted text.
     52     *
     53     * @var string
    4854     */
    4955    var $_ins_suffix = '</ins>';
     
    5157    /**
    5258     * Prefix for deleted text.
     59     *
     60     * @var string
    5361     */
    5462    var $_del_prefix = '<del>';
     
    5664    /**
    5765     * Suffix for deleted text.
     66     *
     67     * @var string
    5868     */
    5969    var $_del_suffix = '</del>';
     
    6171    /**
    6272     * Header for each change block.
     73     *
     74     * @var string
    6375     */
    6476    var $_block_header = '';
    6577
    6678    /**
     79     * Whether to split down to character-level.
     80     *
     81     * @var boolean
     82     */
     83    var $_split_characters = false;
     84
     85    /**
    6786     * What are we currently splitting on? Used to recurse to show word-level
    68      * changes.
     87     * or character-level changes.
     88     *
     89     * @var string
    6990     */
    7091    var $_split_level = 'lines';
     
    86107        }
    87108
    88         if ($this->_split_level == 'words') {
     109        if ($this->_split_level == 'lines') {
     110            return implode("\n", $lines) . "\n";
     111        } else {
    89112            return implode('', $lines);
    90         } else {
    91             return implode("\n", $lines) . "\n";
    92113        }
    93114    }
     
    111132    function _changed($orig, $final)
    112133    {
    113         /* If we've already split on words, don't try to do so again - just
    114          * display. */
     134        /* If we've already split on characters, just display. */
     135        if ($this->_split_level == 'characters') {
     136            return $this->_deleted($orig)
     137                . $this->_added($final);
     138        }
     139
     140        /* If we've already split on words, just display. */
    115141        if ($this->_split_level == 'words') {
    116142            $prefix = '';
     
    131157        $nl = "\0";
    132158
    133         /* We want to split on word boundaries, but we need to
    134          * preserve whitespace as well. Therefore we split on words,
    135          * but include all blocks of whitespace in the wordlist. */
    136         $diff = new Text_Diff($this->_splitOnWords($text1, $nl),
    137                               $this->_splitOnWords($text2, $nl));
     159        if ($this->_split_characters) {
     160            $diff = new Text_Diff('native',
     161                                  array(preg_split('//', $text1),
     162                                        preg_split('//', $text2)));
     163        } else {
     164            /* We want to split on word boundaries, but we need to preserve
     165             * whitespace as well. Therefore we split on words, but include
     166             * all blocks of whitespace in the wordlist. */
     167            $diff = new Text_Diff('native',
     168                                  array($this->_splitOnWords($text1, $nl),
     169                                        $this->_splitOnWords($text2, $nl)));
     170        }
    138171
    139172        /* Get the diff in inline format. */
    140         $renderer = new Text_Diff_Renderer_inline(array_merge($this->getParams(),
    141                                                               array('split_level' => 'words')));
     173        $renderer = new Text_Diff_Renderer_inline
     174            (array_merge($this->getParams(),
     175                         array('split_level' => $this->_split_characters ? 'characters' : 'words')));
    142176
    143177        /* Run the diff and get the output. */
Note: See TracChangeset for help on using the changeset viewer.