Merge "Add option to expose original sha1 in thumb url"
[lhc/web/wiklou.git] / includes / diff / DairikiDiff.php
index c47eced..d327433 100644 (file)
  * @ingroup DifferenceEngine
  */
 abstract class DiffOp {
+
+       /**
+        * @var string
+        */
        public $type;
+
+       /**
+        * @var string[]
+        */
        public $orig;
+
+       /**
+        * @var string[]
+        */
        public $closing;
 
+       /**
+        * @return string
+        */
+       public function getType() {
+               return $this->type;
+       }
+
+       /**
+        * @return string[]
+        */
+       public function getOrig() {
+               return $this->orig;
+       }
+
+       /**
+        * @param int $i
+        * @return string|null
+        */
+       public function getClosing( $i = null ) {
+               if ( $i === null ) {
+                       return $this->closing;
+               }
+               if ( array_key_exists( $i, $this->closing ) ) {
+                       return $this->closing[$i];
+               }
+               return null;
+       }
+
        abstract public function reverse();
 
        /**
         * @return int
         */
-       function norig() {
+       public function norig() {
                return $this->orig ? count( $this->orig ) : 0;
        }
 
        /**
         * @return int
         */
-       function nclosing() {
+       public function nclosing() {
                return $this->closing ? count( $this->closing ) : 0;
        }
 }
@@ -60,7 +100,7 @@ abstract class DiffOp {
 class DiffOpCopy extends DiffOp {
        public $type = 'copy';
 
-       function __construct( $orig, $closing = false ) {
+       public function __construct( $orig, $closing = false ) {
                if ( !is_array( $closing ) ) {
                        $closing = $orig;
                }
@@ -71,7 +111,7 @@ class DiffOpCopy extends DiffOp {
        /**
         * @return DiffOpCopy
         */
-       function reverse() {
+       public function reverse() {
                return new DiffOpCopy( $this->closing, $this->orig );
        }
 }
@@ -84,7 +124,7 @@ class DiffOpCopy extends DiffOp {
 class DiffOpDelete extends DiffOp {
        public $type = 'delete';
 
-       function __construct( $lines ) {
+       public function __construct( $lines ) {
                $this->orig = $lines;
                $this->closing = false;
        }
@@ -92,7 +132,7 @@ class DiffOpDelete extends DiffOp {
        /**
         * @return DiffOpAdd
         */
-       function reverse() {
+       public function reverse() {
                return new DiffOpAdd( $this->orig );
        }
 }
@@ -105,7 +145,7 @@ class DiffOpDelete extends DiffOp {
 class DiffOpAdd extends DiffOp {
        public $type = 'add';
 
-       function __construct( $lines ) {
+       public function __construct( $lines ) {
                $this->closing = $lines;
                $this->orig = false;
        }
@@ -113,7 +153,7 @@ class DiffOpAdd extends DiffOp {
        /**
         * @return DiffOpDelete
         */
-       function reverse() {
+       public function reverse() {
                return new DiffOpDelete( $this->closing );
        }
 }
@@ -126,7 +166,7 @@ class DiffOpAdd extends DiffOp {
 class DiffOpChange extends DiffOp {
        public $type = 'change';
 
-       function __construct( $orig, $closing ) {
+       public function __construct( $orig, $closing ) {
                $this->orig = $orig;
                $this->closing = $closing;
        }
@@ -134,7 +174,7 @@ class DiffOpChange extends DiffOp {
        /**
         * @return DiffOpChange
         */
-       function reverse() {
+       public function reverse() {
                return new DiffOpChange( $this->closing, $this->orig );
        }
 }
@@ -149,7 +189,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
  *
@@ -176,12 +216,12 @@ class DiffEngine {
        protected $lcs = 0;
 
        /**
-        * @param $from_lines
-        * @param $to_lines
-        * @return array
+        * @param string[] $from_lines
+        * @param string[] $to_lines
+        *
+        * @return DiffOp[]
         */
-       function diff( $from_lines, $to_lines ) {
-               wfProfileIn( __METHOD__ );
+       public function diff( $from_lines, $to_lines ) {
 
                // Diff and store locally
                $this->diffLocal( $from_lines, $to_lines );
@@ -231,18 +271,16 @@ class DiffEngine {
                                $edits[] = new DiffOpAdd( $add );
                        }
                }
-               wfProfileOut( __METHOD__ );
 
                return $edits;
        }
 
        /**
-        * @param $from_lines
-        * @param $to_lines
+        * @param string[] $from_lines
+        * @param string[] $to_lines
         */
        private function diffLocal( $from_lines, $to_lines ) {
                global $wgExternalDiffEngine;
-               wfProfileIn( __METHOD__ );
 
                if ( $wgExternalDiffEngine == 'wikidiff3' ) {
                        // wikidiff3
@@ -305,12 +343,13 @@ class DiffEngine {
                        // Find the LCS.
                        $this->compareSeq( 0, count( $this->xv ), 0, count( $this->yv ) );
                }
-               wfProfileOut( __METHOD__ );
        }
 
        /**
         * Returns the whole line if it's small enough, or the MD5 hash otherwise
-        * @param $line string
+        *
+        * @param string $line
+        *
         * @return string
         */
        private function lineHash( $line ) {
@@ -337,12 +376,14 @@ class DiffEngine {
         * of the two files do not match, and likewise that the last lines do not
         * match.  The caller must trim matching lines from the beginning and end
         * of the portions it is going to specify.
-        * @param $xoff
-        * @param $xlim
-        * @param $yoff
-        * @param $ylim
-        * @param $nchunks
-        * @return array
+        *
+        * @param int $xoff
+        * @param int $xlim
+        * @param int $yoff
+        * @param int $ylim
+        * @param int $nchunks
+        *
+        * @return array List of two elements, integer and array[].
         */
        private function diag( $xoff, $xlim, $yoff, $ylim, $nchunks ) {
                $flip = false;
@@ -379,7 +420,9 @@ class DiffEngine {
                        }
 
                        $x1 = $xoff + (int)( ( $numer + ( $xlim - $xoff ) * $chunk ) / $nchunks );
+                       // @codingStandardsIgnoreFile Ignore Squiz.WhiteSpace.SemicolonSpacing.Incorrect
                        for ( ; $x < $x1; $x++ ) {
+                               // @codingStandardsIgnoreEnd
                                $line = $flip ? $this->yv[$x] : $this->xv[$x];
                                if ( empty( $ymatches[$line] ) ) {
                                        continue;
@@ -427,7 +470,8 @@ class DiffEngine {
        }
 
        /**
-        * @param $ypos
+        * @param int $ypos
+        *
         * @return int
         */
        private function lcsPos( $ypos ) {
@@ -469,10 +513,11 @@ class DiffEngine {
         *
         * Note that XLIM, YLIM are exclusive bounds.
         * All line numbers are origin-0 and discarded lines are not counted.
-        * @param $xoff
-        * @param $xlim
-        * @param $yoff
-        * @param $ylim
+        *
+        * @param int $xoff
+        * @param int $xlim
+        * @param int $yoff
+        * @param int $ylim
         */
        private function compareSeq( $xoff, $xlim, $yoff, $ylim ) {
                // Slide down the bottom initial diagonal.
@@ -533,7 +578,6 @@ 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;
 
@@ -648,7 +692,6 @@ class DiffEngine {
                                assert( '$j >= 0 && !$other_changed[$j]' );
                        }
                }
-               wfProfileOut( __METHOD__ );
        }
 }
 
@@ -659,21 +702,32 @@ class DiffEngine {
  * @ingroup DifferenceEngine
  */
 class Diff {
+
+       /**
+        * @var DiffOp[]
+        */
        public $edits;
 
        /**
         * Constructor.
         * Computes diff between sequences of strings.
         *
-        * @param $from_lines array An array of strings.
+        * @param string[] $from_lines An array of strings.
         *   Typically these are lines from a file.
-        * @param $to_lines array An array of strings.
+        * @param string[] $to_lines An array of strings.
         */
-       function __construct( $from_lines, $to_lines ) {
+       public function __construct( $from_lines, $to_lines ) {
                $eng = new DiffEngine;
                $this->edits = $eng->diff( $from_lines, $to_lines );
        }
 
+       /**
+        * @return DiffOp[]
+        */
+       public function getEdits() {
+               return $this->edits;
+       }
+
        /**
         * Compute reversed Diff.
         *
@@ -681,10 +735,11 @@ class Diff {
         *
         *    $diff = new Diff($lines1, $lines2);
         *    $rev = $diff->reverse();
+        *
         * @return Object A Diff object representing the inverse of the
         *   original diff.
         */
-       function reverse() {
+       public function reverse() {
                $rev = $this;
                $rev->edits = array();
                /** @var DiffOp $edit */
@@ -700,7 +755,7 @@ class Diff {
         *
         * @return bool True if two sequences were identical.
         */
-       function isEmpty() {
+       public function isEmpty() {
                foreach ( $this->edits as $edit ) {
                        if ( $edit->type != 'copy' ) {
                                return false;
@@ -717,7 +772,7 @@ class Diff {
         *
         * @return int The length of the LCS.
         */
-       function lcs() {
+       public function lcs() {
                $lcs = 0;
                foreach ( $this->edits as $edit ) {
                        if ( $edit->type == 'copy' ) {
@@ -734,9 +789,9 @@ class Diff {
         * This reconstructs the $from_lines parameter passed to the
         * constructor.
         *
-        * @return array The original sequence of strings.
+        * @return string[] The original sequence of strings.
         */
-       function orig() {
+       public function orig() {
                $lines = array();
 
                foreach ( $this->edits as $edit ) {
@@ -754,9 +809,9 @@ class Diff {
         * This reconstructs the $to_lines parameter passed to the
         * constructor.
         *
-        * @return array The sequence of strings.
+        * @return string[] The sequence of strings.
         */
-       function closing() {
+       public function closing() {
                $lines = array();
 
                foreach ( $this->edits as $edit ) {
@@ -784,23 +839,19 @@ class MappedDiff extends Diff {
         * case-insensitve diffs, or diffs which ignore
         * changes in white-space.
         *
-        * @param $from_lines array An array of strings.
+        * @param string[] $from_lines An array of strings.
         *   Typically these are lines from a file.
-        *
-        * @param $to_lines array An array of strings.
-        *
-        * @param $mapped_from_lines array This array should
+        * @param string[] $to_lines An array of strings.
+        * @param string[] $mapped_from_lines This array should
         *   have the same size number of elements as $from_lines.
         *   The elements in $mapped_from_lines and
         *   $mapped_to_lines are what is actually compared
         *   when computing the diff.
-        *
-        * @param $mapped_to_lines array This array should
+        * @param string[] $mapped_to_lines This array should
         *   have the same number of elements as $to_lines.
         */
-       function __construct( $from_lines, $to_lines,
+       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 )' );
@@ -822,7 +873,6 @@ class MappedDiff extends Diff {
                                $yi += count( $closing );
                        }
                }
-               wfProfileOut( __METHOD__ );
        }
 }
 
@@ -845,7 +895,7 @@ class HWLDFWordAccumulator {
        private $tag = '';
 
        /**
-        * @param $new_tag
+        * @param string $new_tag
         */
        private function flushGroup( $new_tag ) {
                if ( $this->group !== '' ) {
@@ -864,7 +914,7 @@ class HWLDFWordAccumulator {
        }
 
        /**
-        * @param $new_tag
+        * @param string $new_tag
         */
        private function flushLine( $new_tag ) {
                $this->flushGroup( $new_tag );
@@ -878,8 +928,8 @@ class HWLDFWordAccumulator {
        }
 
        /**
-        * @param $words
-        * @param $tag string
+        * @param string[] $words
+        * @param string $tag
         */
        public function addWords( $words, $tag = '' ) {
                if ( $tag != $this->tag ) {
@@ -901,7 +951,7 @@ class HWLDFWordAccumulator {
        }
 
        /**
-        * @return array
+        * @return string[]
         */
        public function getLines() {
                $this->flushLine( '~done' );
@@ -919,26 +969,24 @@ class WordLevelDiff extends MappedDiff {
        const MAX_LINE_LENGTH = 10000;
 
        /**
-        * @param $orig_lines
-        * @param $closing_lines
+        * @param string[] $orig_lines
+        * @param string[] $closing_lines
         */
-       function __construct( $orig_lines, $closing_lines ) {
-               wfProfileIn( __METHOD__ );
+       public function __construct( $orig_lines, $closing_lines ) {
 
                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__ );
        }
 
        /**
-        * @param $lines
-        * @return array
+        * @param string[] $lines
+        *
+        * @return array[]
         */
        private function split( $lines ) {
-               wfProfileIn( __METHOD__ );
 
                $words = array();
                $stripped = array();
@@ -969,16 +1017,14 @@ class WordLevelDiff extends MappedDiff {
                                }
                        }
                }
-               wfProfileOut( __METHOD__ );
 
                return array( $words, $stripped );
        }
 
        /**
-        * @return array
+        * @return string[]
         */
        public function orig() {
-               wfProfileIn( __METHOD__ );
                $orig = new HWLDFWordAccumulator;
 
                foreach ( $this->edits as $edit ) {
@@ -989,16 +1035,14 @@ class WordLevelDiff extends MappedDiff {
                        }
                }
                $lines = $orig->getLines();
-               wfProfileOut( __METHOD__ );
 
                return $lines;
        }
 
        /**
-        * @return array
+        * @return string[]
         */
        public function closing() {
-               wfProfileIn( __METHOD__ );
                $closing = new HWLDFWordAccumulator;
 
                foreach ( $this->edits as $edit ) {
@@ -1009,8 +1053,8 @@ class WordLevelDiff extends MappedDiff {
                        }
                }
                $lines = $closing->getLines();
-               wfProfileOut( __METHOD__ );
 
                return $lines;
        }
+
 }