Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / diff / DairikiDiff.php
index a4c0168..6272e7e 100644 (file)
  */
 
 /**
- * @todo document
+ * The base class for all other DiffOp classes.
+ *
+ * The classes that extend DiffOp are: DiffOpCopy, DiffOpDelete, DiffOpAdd and
+ * DiffOpChange. FakeDiffOp also extends DiffOp, but it is not located in this file.
+ *
  * @private
  * @ingroup DifferenceEngine
  */
@@ -93,7 +97,9 @@ abstract class DiffOp {
 }
 
 /**
- * @todo document
+ * Extends DiffOp. Used to mark strings that have been
+ * copied from one string array to the other.
+ *
  * @private
  * @ingroup DifferenceEngine
  */
@@ -117,7 +123,9 @@ class DiffOpCopy extends DiffOp {
 }
 
 /**
- * @todo document
+ * Extends DiffOp. Used to mark strings that have been
+ * deleted from the first string array.
+ *
  * @private
  * @ingroup DifferenceEngine
  */
@@ -138,7 +146,9 @@ class DiffOpDelete extends DiffOp {
 }
 
 /**
- * @todo document
+ * Extends DiffOp. Used to mark strings that have been
+ * added from the first string array.
+ *
  * @private
  * @ingroup DifferenceEngine
  */
@@ -159,7 +169,9 @@ class DiffOpAdd extends DiffOp {
 }
 
 /**
- * @todo document
+ * Extends DiffOp. Used to mark strings that have been
+ * changed from the first string array (both added and subtracted).
+ *
  * @private
  * @ingroup DifferenceEngine
  */
@@ -189,7 +201,7 @@ class DiffOpChange extends DiffOp {
  * More ideas are taken from:
  *     http://www.ics.uci.edu/~eppstein/161/960229.html
  *
- * Some ideas are (and a bit of code) are from from analyze.c, from GNU
+ * Some ideas (and a bit of code) are from analyze.c, from GNU
  * diffutils-2.7, which can be found at:
  *     ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
  *
@@ -208,10 +220,10 @@ class DiffEngine {
 
        protected $xchanged, $ychanged;
 
-       protected $xv = array(), $yv = array();
-       protected $xind = array(), $yind = array();
+       protected $xv = [], $yv = [];
+       protected $xind = [], $yind = [];
 
-       protected $seq = array(), $in_seq = array();
+       protected $seq = [], $in_seq = [];
 
        protected $lcs = 0;
 
@@ -222,7 +234,6 @@ class DiffEngine {
         * @return DiffOp[]
         */
        public function diff( $from_lines, $to_lines ) {
-               wfProfileIn( __METHOD__ );
 
                // Diff and store locally
                $this->diffLocal( $from_lines, $to_lines );
@@ -235,14 +246,14 @@ class DiffEngine {
                $n_from = count( $from_lines );
                $n_to = count( $to_lines );
 
-               $edits = array();
+               $edits = [];
                $xi = $yi = 0;
                while ( $xi < $n_from || $yi < $n_to ) {
-                       assert( '$yi < $n_to || $this->xchanged[$xi]' );
-                       assert( '$xi < $n_from || $this->ychanged[$yi]' );
+                       assert( $yi < $n_to || $this->xchanged[$xi] );
+                       assert( $xi < $n_from || $this->ychanged[$yi] );
 
                        // Skip matching "snake".
-                       $copy = array();
+                       $copy = [];
                        while ( $xi < $n_from && $yi < $n_to
                                && !$this->xchanged[$xi] && !$this->ychanged[$yi]
                        ) {
@@ -254,12 +265,12 @@ class DiffEngine {
                        }
 
                        // Find deletes & adds.
-                       $delete = array();
+                       $delete = [];
                        while ( $xi < $n_from && $this->xchanged[$xi] ) {
                                $delete[] = $from_lines[$xi++];
                        }
 
-                       $add = array();
+                       $add = [];
                        while ( $yi < $n_to && $this->ychanged[$yi] ) {
                                $add[] = $to_lines[$yi++];
                        }
@@ -272,7 +283,6 @@ class DiffEngine {
                                $edits[] = new DiffOpAdd( $add );
                        }
                }
-               wfProfileOut( __METHOD__ );
 
                return $edits;
        }
@@ -283,7 +293,6 @@ class DiffEngine {
         */
        private function diffLocal( $from_lines, $to_lines ) {
                global $wgExternalDiffEngine;
-               wfProfileIn( __METHOD__ );
 
                if ( $wgExternalDiffEngine == 'wikidiff3' ) {
                        // wikidiff3
@@ -296,12 +305,12 @@ class DiffEngine {
                        // old diff
                        $n_from = count( $from_lines );
                        $n_to = count( $to_lines );
-                       $this->xchanged = $this->ychanged = array();
-                       $this->xv = $this->yv = array();
-                       $this->xind = $this->yind = array();
-                       unset( $this->seq );
-                       unset( $this->in_seq );
-                       unset( $this->lcs );
+                       $this->xchanged = $this->ychanged = [];
+                       $this->xv = $this->yv = [];
+                       $this->xind = $this->yind = [];
+                       $this->seq = [];
+                       $this->in_seq = [];
+                       $this->lcs = 0;
 
                        // Skip leading common lines.
                        for ( $skip = 0; $skip < $n_from && $skip < $n_to; $skip++ ) {
@@ -327,7 +336,8 @@ class DiffEngine {
 
                        for ( $yi = $skip; $yi < $n_to - $endskip; $yi++ ) {
                                $line = $to_lines[$yi];
-                               if ( ( $this->ychanged[$yi] = empty( $xhash[$this->lineHash( $line )] ) ) ) {
+                               $this->ychanged[$yi] = empty( $xhash[$this->lineHash( $line )] );
+                               if ( $this->ychanged[$yi] ) {
                                        continue;
                                }
                                $yhash[$this->lineHash( $line )] = 1;
@@ -336,7 +346,8 @@ class DiffEngine {
                        }
                        for ( $xi = $skip; $xi < $n_from - $endskip; $xi++ ) {
                                $line = $from_lines[$xi];
-                               if ( ( $this->xchanged[$xi] = empty( $yhash[$this->lineHash( $line )] ) ) ) {
+                               $this->xchanged[$xi] = empty( $yhash[$this->lineHash( $line )] );
+                               if ( $this->xchanged[$xi] ) {
                                        continue;
                                }
                                $this->xv[] = $line;
@@ -346,7 +357,6 @@ class DiffEngine {
                        // Find the LCS.
                        $this->compareSeq( 0, count( $this->xv ), 0, count( $this->yv ) );
                }
-               wfProfileOut( __METHOD__ );
        }
 
        /**
@@ -396,7 +406,7 @@ class DiffEngine {
                        // Things seems faster (I'm not sure I understand why)
                        // when the shortest sequence in X.
                        $flip = true;
-                       list( $xoff, $xlim, $yoff, $ylim ) = array( $yoff, $ylim, $xoff, $xlim );
+                       list( $xoff, $xlim, $yoff, $ylim ) = [ $yoff, $ylim, $xoff, $xlim ];
                }
 
                if ( $flip ) {
@@ -411,8 +421,8 @@ class DiffEngine {
 
                $this->lcs = 0;
                $this->seq[0] = $yoff - 1;
-               $this->in_seq = array();
-               $ymids[0] = array();
+               $this->in_seq = [];
+               $ymids[0] = [];
 
                $numer = $xlim - $xoff + $nchunks - 1;
                $x = $xoff;
@@ -424,7 +434,7 @@ class DiffEngine {
                        }
 
                        $x1 = $xoff + (int)( ( $numer + ( $xlim - $xoff ) * $chunk ) / $nchunks );
-                       // @codingStandardsIgnoreFile Ignore Squiz.WhiteSpace.SemicolonSpacing.Incorrect
+                       // @codingStandardsIgnoreStart Ignore Squiz.WhiteSpace.SemicolonSpacing.Incorrect
                        for ( ; $x < $x1; $x++ ) {
                                // @codingStandardsIgnoreEnd
                                $line = $flip ? $this->yv[$x] : $this->xv[$x];
@@ -438,7 +448,7 @@ class DiffEngine {
                                while ( list( , $y ) = each( $matches ) ) {
                                        if ( empty( $this->in_seq[$y] ) ) {
                                                $k = $this->lcsPos( $y );
-                                               assert( '$k > 0' );
+                                               assert( $k > 0 );
                                                $ymids[$k] = $ymids[$k - 1];
                                                break;
                                        }
@@ -446,31 +456,31 @@ class DiffEngine {
 
                                while ( list( , $y ) = each( $matches ) ) {
                                        if ( $y > $this->seq[$k - 1] ) {
-                                               assert( '$y < $this->seq[$k]' );
+                                               assert( $y < $this->seq[$k] );
                                                // Optimization: this is a common case:
-                                               //      next match is just replacing previous match.
+                                               // next match is just replacing previous match.
                                                $this->in_seq[$this->seq[$k]] = false;
                                                $this->seq[$k] = $y;
                                                $this->in_seq[$y] = 1;
                                        } elseif ( empty( $this->in_seq[$y] ) ) {
                                                $k = $this->lcsPos( $y );
-                                               assert( '$k > 0' );
+                                               assert( $k > 0 );
                                                $ymids[$k] = $ymids[$k - 1];
                                        }
                                }
                        }
                }
 
-               $seps[] = $flip ? array( $yoff, $xoff ) : array( $xoff, $yoff );
+               $seps[] = $flip ? [ $yoff, $xoff ] : [ $xoff, $yoff ];
                $ymid = $ymids[$this->lcs];
                for ( $n = 0; $n < $nchunks - 1; $n++ ) {
                        $x1 = $xoff + (int)( ( $numer + ( $xlim - $xoff ) * $n ) / $nchunks );
                        $y1 = $ymid[$n] + 1;
-                       $seps[] = $flip ? array( $y1, $x1 ) : array( $x1, $y1 );
+                       $seps[] = $flip ? [ $y1, $x1 ] : [ $x1, $y1 ];
                }
-               $seps[] = $flip ? array( $ylim, $xlim ) : array( $xlim, $ylim );
+               $seps[] = $flip ? [ $ylim, $xlim ] : [ $xlim, $ylim ];
 
-               return array( $this->lcs, $seps );
+               return [ $this->lcs, $seps ];
        }
 
        /**
@@ -497,7 +507,7 @@ class DiffEngine {
                        }
                }
 
-               assert( '$ypos != $this->seq[$end]' );
+               assert( $ypos != $this->seq[$end] );
 
                $this->in_seq[$this->seq[$end]] = false;
                $this->seq[$end] = $ypos;
@@ -582,11 +592,10 @@ class DiffEngine {
         * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
         */
        private function shiftBoundaries( $lines, &$changed, $other_changed ) {
-               wfProfileIn( __METHOD__ );
                $i = 0;
                $j = 0;
 
-               assert( 'count($lines) == count($changed)' );
+               assert( count( $lines ) == count( $changed ) );
                $len = count( $lines );
                $other_len = count( $other_changed );
 
@@ -607,7 +616,7 @@ class DiffEngine {
                        }
 
                        while ( $i < $len && !$changed[$i] ) {
-                               assert( '$j < $other_len && ! $other_changed[$j]' );
+                               assert( $j < $other_len && ! $other_changed[$j] );
                                $i++;
                                $j++;
                                while ( $j < $other_len && $other_changed[$j] ) {
@@ -644,11 +653,11 @@ class DiffEngine {
                                        while ( $start > 0 && $changed[$start - 1] ) {
                                                $start--;
                                        }
-                                       assert( '$j > 0' );
+                                       assert( $j > 0 );
                                        while ( $other_changed[--$j] ) {
                                                continue;
                                        }
-                                       assert( '$j >= 0 && !$other_changed[$j]' );
+                                       assert( $j >= 0 && !$other_changed[$j] );
                                }
 
                                /*
@@ -672,7 +681,7 @@ class DiffEngine {
                                                $i++;
                                        }
 
-                                       assert( '$j < $other_len && ! $other_changed[$j]' );
+                                       assert( $j < $other_len && ! $other_changed[$j] );
                                        $j++;
                                        if ( $j < $other_len && $other_changed[$j] ) {
                                                $corresponding = $i;
@@ -690,14 +699,13 @@ class DiffEngine {
                        while ( $corresponding < $i ) {
                                $changed[--$start] = 1;
                                $changed[--$i] = 0;
-                               assert( '$j > 0' );
+                               assert( $j > 0 );
                                while ( $other_changed[--$j] ) {
                                        continue;
                                }
-                               assert( '$j >= 0 && !$other_changed[$j]' );
+                               assert( $j >= 0 && !$other_changed[$j] );
                        }
                }
-               wfProfileOut( __METHOD__ );
        }
 }
 
@@ -747,7 +755,7 @@ class Diff {
         */
        public function reverse() {
                $rev = $this;
-               $rev->edits = array();
+               $rev->edits = [];
                /** @var DiffOp $edit */
                foreach ( $this->edits as $edit ) {
                        $rev->edits[] = $edit->reverse();
@@ -798,7 +806,7 @@ class Diff {
         * @return string[] The original sequence of strings.
         */
        public function orig() {
-               $lines = array();
+               $lines = [];
 
                foreach ( $this->edits as $edit ) {
                        if ( $edit->orig ) {
@@ -818,7 +826,7 @@ class Diff {
         * @return string[] The sequence of strings.
         */
        public function closing() {
-               $lines = array();
+               $lines = [];
 
                foreach ( $this->edits as $edit ) {
                        if ( $edit->closing ) {
@@ -858,10 +866,9 @@ class MappedDiff extends Diff {
         */
        public function __construct( $from_lines, $to_lines,
                $mapped_from_lines, $mapped_to_lines ) {
-               wfProfileIn( __METHOD__ );
 
-               assert( 'count( $from_lines ) == count( $mapped_from_lines )' );
-               assert( 'count( $to_lines ) == count( $mapped_to_lines )' );
+               assert( count( $from_lines ) == count( $mapped_from_lines ) );
+               assert( count( $to_lines ) == count( $mapped_to_lines ) );
 
                parent::__construct( $mapped_from_lines, $mapped_to_lines );
 
@@ -880,7 +887,6 @@ class MappedDiff extends Diff {
                                $yi += count( $closing );
                        }
                }
-               wfProfileOut( __METHOD__ );
        }
 }
 
@@ -897,7 +903,7 @@ class HWLDFWordAccumulator {
        public $insClass = ' class="diffchange diffchange-inline"';
        public $delClass = ' class="diffchange diffchange-inline"';
 
-       private $lines = array();
+       private $lines = [];
        private $line = '';
        private $group = '';
        private $tag = '';
@@ -953,7 +959,7 @@ class HWLDFWordAccumulator {
                                $this->flushLine( $tag );
                                $word = substr( $word, 1 );
                        }
-                       assert( '!strstr( $word, "\n" )' );
+                       assert( !strstr( $word, "\n" ) );
                        $this->group .= $word;
                }
        }
@@ -981,14 +987,12 @@ class WordLevelDiff extends MappedDiff {
         * @param string[] $closing_lines
         */
        public function __construct( $orig_lines, $closing_lines ) {
-               wfProfileIn( __METHOD__ );
 
                list( $orig_words, $orig_stripped ) = $this->split( $orig_lines );
                list( $closing_words, $closing_stripped ) = $this->split( $closing_lines );
 
                parent::__construct( $orig_words, $closing_words,
                        $orig_stripped, $closing_stripped );
-               wfProfileOut( __METHOD__ );
        }
 
        /**
@@ -997,10 +1001,9 @@ class WordLevelDiff extends MappedDiff {
         * @return array[]
         */
        private function split( $lines ) {
-               wfProfileIn( __METHOD__ );
 
-               $words = array();
-               $stripped = array();
+               $words = [];
+               $stripped = [];
                $first = true;
                foreach ( $lines as $line ) {
                        # If the line is too long, just pretend the entire line is one big word
@@ -1015,7 +1018,7 @@ class WordLevelDiff extends MappedDiff {
                                $words[] = $line;
                                $stripped[] = $line;
                        } else {
-                               $m = array();
+                               $m = [];
                                if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
                                        $line, $m )
                                ) {
@@ -1028,16 +1031,14 @@ class WordLevelDiff extends MappedDiff {
                                }
                        }
                }
-               wfProfileOut( __METHOD__ );
 
-               return array( $words, $stripped );
+               return [ $words, $stripped ];
        }
 
        /**
         * @return string[]
         */
        public function orig() {
-               wfProfileIn( __METHOD__ );
                $orig = new HWLDFWordAccumulator;
 
                foreach ( $this->edits as $edit ) {
@@ -1048,7 +1049,6 @@ class WordLevelDiff extends MappedDiff {
                        }
                }
                $lines = $orig->getLines();
-               wfProfileOut( __METHOD__ );
 
                return $lines;
        }
@@ -1057,7 +1057,6 @@ class WordLevelDiff extends MappedDiff {
         * @return string[]
         */
        public function closing() {
-               wfProfileIn( __METHOD__ );
                $closing = new HWLDFWordAccumulator;
 
                foreach ( $this->edits as $edit ) {
@@ -1068,7 +1067,6 @@ class WordLevelDiff extends MappedDiff {
                        }
                }
                $lines = $closing->getLines();
-               wfProfileOut( __METHOD__ );
 
                return $lines;
        }