getChangeSetFromEmptyLeft( $right ); } $diffs = new Diff( explode( "\n", $left ), explode( "\n", $right ) ); $format = new UnifiedDiffFormatter(); $diff = $format->format( $diffs ); return $this->parse( $diff, $left, $right ); } /** * If we add content to an empty page the changeSet can be composed straightaway * * @param string $right * @return array[] See {@see getChangeSet} */ private function getChangeSetFromEmptyLeft( $right ) { $rightLines = explode( "\n", $right ); return [ '_info' => [ 'lhs-length' => 1, 'rhs-length' => count( $rightLines ), 'lhs' => [ '' ], 'rhs' => $rightLines ], [ 'right-pos' => 1, 'left-pos' => 1, 'action' => 'add', 'content' => $right, ] ]; } /** * Duplicates the check from the global wfDiff function to determine * if we are using internal or external diff utilities * * @deprecated since 1.29, the internal diff parser is always used * @return bool */ protected static function usingInternalDiff() { return true; } /** * Parse the unified diff output into an array of changes to individual groups of the text * * @param string $diff The unified diff output * @param string $left The left side of the diff used for sanity checks * @param string $right The right side of the diff used for sanity checks * * @return array[] */ protected function parse( $diff, $left, $right ) { $this->left = explode( "\n", $left ); $this->right = explode( "\n", $right ); $diff = explode( "\n", $diff ); $this->leftPos = 0; $this->rightPos = 0; $this->changeSet = [ '_info' => [ 'lhs-length' => count( $this->left ), 'rhs-length' => count( $this->right ), 'lhs' => $this->left, 'rhs' => $this->right, ], ]; $change = null; foreach ( $diff as $line ) { $change = $this->parseLine( $line, $change ); } if ( $change === null ) { return $this->changeSet; } else { return array_merge( $this->changeSet, $change->getChangeSet() ); } } /** * Parse the next line of the unified diff output * * @param string $line The next line of the unified diff * @param EchoDiffGroup|null $change Changes the the immediately previous lines * * @throws MWException * @return EchoDiffGroup Changes to this line and any changed lines immediately previous */ protected function parseLine( $line, EchoDiffGroup $change = null ) { if ( $line ) { $op = $line[0]; if ( strlen( $line ) > $this->prefixLength ) { $line = substr( $line, $this->prefixLength ); } else { $line = ''; } } else { $op = ' '; } switch ( $op ) { case '@': // metadata if ( $change !== null ) { $this->changeSet = array_merge( $this->changeSet, $change->getChangeSet() ); $change = null; } // @@ -start,numLines +start,numLines @@ list( , $left, $right ) = explode( ' ', $line ); list( $this->leftPos ) = explode( ',', substr( $left, 1 ) ); list( $this->rightPos ) = explode( ',', substr( $right, 1 ) ); // -1 because diff is 1 indexed and we are 0 indexed $this->leftPos--; $this->rightPos--; break; case ' ': // No changes if ( $change !== null ) { $this->changeSet = array_merge( $this->changeSet, $change->getChangeSet() ); $change = null; } $this->leftPos++; $this->rightPos++; break; case '-': // subtract if ( $this->left[$this->leftPos] !== $line ) { throw new MWException( 'Positional error: left' ); } if ( $change === null ) { $change = new EchoDiffGroup( $this->leftPos, $this->rightPos ); } $change->subtract( $line ); $this->leftPos++; break; case '+': // add if ( $this->right[$this->rightPos] !== $line ) { throw new MWException( 'Positional error: right' ); } if ( $change === null ) { $change = new EchoDiffGroup( $this->leftPos, $this->rightPos ); } $change->add( $line ); $this->rightPos++; break; default: throw new MWException( 'Unknown Diff Operation: ' . $op ); } return $change; } }