2010-11-07 16:28:11 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
|
|
* You may copy this code freely under the conditions of the GPL.
|
|
|
|
|
*
|
2012-11-18 14:34:00 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-11-07 16:28:11 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup DifferenceEngine
|
|
|
|
|
* @defgroup DifferenceEngine DifferenceEngine
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-18 00:40:55 +00:00
|
|
|
* 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.
|
|
|
|
|
*
|
2010-11-07 16:28:11 +00:00
|
|
|
* @private
|
|
|
|
|
* @ingroup DifferenceEngine
|
|
|
|
|
*/
|
2013-10-29 22:25:28 +00:00
|
|
|
abstract class DiffOp {
|
2014-02-26 21:47:40 +00:00
|
|
|
|
2014-03-03 17:08:05 +00:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2013-10-29 22:25:28 +00:00
|
|
|
public $type;
|
2014-03-03 17:08:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
2013-10-29 22:25:28 +00:00
|
|
|
public $orig;
|
2014-03-03 17:08:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
2013-10-29 22:25:28 +00:00
|
|
|
public $closing;
|
2010-11-07 16:28:11 +00:00
|
|
|
|
2014-03-03 17:08:05 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function getType() {
|
|
|
|
|
return $this->type;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-03 17:08:05 +00:00
|
|
|
/**
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function getOrig() {
|
|
|
|
|
return $this->orig;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-03 17:08:05 +00:00
|
|
|
/**
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param int|null $i
|
2016-12-08 05:04:53 +00:00
|
|
|
* @return string[]|string|null
|
2014-03-03 17:08:05 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function getClosing( $i = null ) {
|
2014-03-20 18:59:20 +00:00
|
|
|
if ( $i === null ) {
|
2014-02-26 21:47:40 +00:00
|
|
|
return $this->closing;
|
|
|
|
|
}
|
2014-03-20 18:59:20 +00:00
|
|
|
if ( array_key_exists( $i, $this->closing ) ) {
|
2014-02-26 21:47:40 +00:00
|
|
|
return $this->closing[$i];
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-20 19:32:53 +00:00
|
|
|
abstract public function reverse();
|
2010-11-07 16:28:11 +00:00
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function norig() {
|
2013-01-26 21:20:04 +00:00
|
|
|
return $this->orig ? count( $this->orig ) : 0;
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function nclosing() {
|
2013-01-26 21:20:04 +00:00
|
|
|
return $this->closing ? count( $this->closing ) : 0;
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-18 00:40:55 +00:00
|
|
|
* Extends DiffOp. Used to mark strings that have been
|
|
|
|
|
* copied from one string array to the other.
|
|
|
|
|
*
|
2010-11-07 16:28:11 +00:00
|
|
|
* @private
|
|
|
|
|
* @ingroup DifferenceEngine
|
|
|
|
|
*/
|
2013-11-20 19:28:14 +00:00
|
|
|
class DiffOpCopy extends DiffOp {
|
2013-10-29 22:25:28 +00:00
|
|
|
public $type = 'copy';
|
2010-11-07 16:28:11 +00:00
|
|
|
|
2014-02-26 21:47:40 +00:00
|
|
|
public function __construct( $orig, $closing = false ) {
|
2011-02-20 19:04:58 +00:00
|
|
|
if ( !is_array( $closing ) ) {
|
|
|
|
|
$closing = $orig;
|
|
|
|
|
}
|
2010-11-07 16:28:11 +00:00
|
|
|
$this->orig = $orig;
|
|
|
|
|
$this->closing = $closing;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
2013-11-20 19:28:14 +00:00
|
|
|
* @return DiffOpCopy
|
2011-10-26 04:15:09 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function reverse() {
|
2013-11-20 19:28:14 +00:00
|
|
|
return new DiffOpCopy( $this->closing, $this->orig );
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-18 00:40:55 +00:00
|
|
|
* Extends DiffOp. Used to mark strings that have been
|
|
|
|
|
* deleted from the first string array.
|
|
|
|
|
*
|
2010-11-07 16:28:11 +00:00
|
|
|
* @private
|
|
|
|
|
* @ingroup DifferenceEngine
|
|
|
|
|
*/
|
2013-11-20 19:28:14 +00:00
|
|
|
class DiffOpDelete extends DiffOp {
|
2013-10-29 22:25:28 +00:00
|
|
|
public $type = 'delete';
|
2010-11-07 16:28:11 +00:00
|
|
|
|
2014-02-26 21:47:40 +00:00
|
|
|
public function __construct( $lines ) {
|
2010-11-07 16:28:11 +00:00
|
|
|
$this->orig = $lines;
|
|
|
|
|
$this->closing = false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
2013-11-20 19:28:14 +00:00
|
|
|
* @return DiffOpAdd
|
2011-10-26 04:15:09 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function reverse() {
|
2013-11-20 19:28:14 +00:00
|
|
|
return new DiffOpAdd( $this->orig );
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-18 00:40:55 +00:00
|
|
|
* Extends DiffOp. Used to mark strings that have been
|
|
|
|
|
* added from the first string array.
|
|
|
|
|
*
|
2010-11-07 16:28:11 +00:00
|
|
|
* @private
|
|
|
|
|
* @ingroup DifferenceEngine
|
|
|
|
|
*/
|
2013-11-20 19:28:14 +00:00
|
|
|
class DiffOpAdd extends DiffOp {
|
2013-10-29 22:25:28 +00:00
|
|
|
public $type = 'add';
|
2010-11-07 16:28:11 +00:00
|
|
|
|
2014-02-26 21:47:40 +00:00
|
|
|
public function __construct( $lines ) {
|
2010-11-07 16:28:11 +00:00
|
|
|
$this->closing = $lines;
|
|
|
|
|
$this->orig = false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
2013-11-20 19:28:14 +00:00
|
|
|
* @return DiffOpDelete
|
2011-10-26 04:15:09 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function reverse() {
|
2013-11-20 19:28:14 +00:00
|
|
|
return new DiffOpDelete( $this->closing );
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-18 00:40:55 +00:00
|
|
|
* Extends DiffOp. Used to mark strings that have been
|
|
|
|
|
* changed from the first string array (both added and subtracted).
|
|
|
|
|
*
|
2010-11-07 16:28:11 +00:00
|
|
|
* @private
|
|
|
|
|
* @ingroup DifferenceEngine
|
|
|
|
|
*/
|
2013-11-20 19:28:14 +00:00
|
|
|
class DiffOpChange extends DiffOp {
|
2013-10-29 22:25:28 +00:00
|
|
|
public $type = 'change';
|
2010-11-07 16:28:11 +00:00
|
|
|
|
2014-02-26 21:47:40 +00:00
|
|
|
public function __construct( $orig, $closing ) {
|
2010-11-07 16:28:11 +00:00
|
|
|
$this->orig = $orig;
|
|
|
|
|
$this->closing = $closing;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
2013-11-20 19:28:14 +00:00
|
|
|
* @return DiffOpChange
|
2011-10-26 04:15:09 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function reverse() {
|
2013-11-20 19:28:14 +00:00
|
|
|
return new DiffOpChange( $this->closing, $this->orig );
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class representing a 'diff' between two sequences of strings.
|
|
|
|
|
* @todo document
|
|
|
|
|
* @private
|
|
|
|
|
* @ingroup DifferenceEngine
|
|
|
|
|
*/
|
2011-02-20 19:04:58 +00:00
|
|
|
class Diff {
|
2014-02-26 21:47:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var DiffOp[]
|
|
|
|
|
*/
|
2013-10-29 22:25:28 +00:00
|
|
|
public $edits;
|
2010-11-07 16:28:11 +00:00
|
|
|
|
2016-04-27 01:10:26 +00:00
|
|
|
/**
|
|
|
|
|
* @var int If this diff complexity is exceeded, a ComplexityException is thrown
|
|
|
|
|
* 0 means no limit.
|
|
|
|
|
*/
|
|
|
|
|
protected $bailoutComplexity = 0;
|
|
|
|
|
|
2010-11-07 16:28:11 +00:00
|
|
|
/**
|
|
|
|
|
* Computes diff between sequences of strings.
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @param string[] $from_lines An array of strings.
|
2013-11-20 19:11:57 +00:00
|
|
|
* Typically these are lines from a file.
|
2014-03-03 17:08:05 +00:00
|
|
|
* @param string[] $to_lines An array of strings.
|
2016-04-27 01:10:26 +00:00
|
|
|
* @throws \MediaWiki\Diff\ComplexityException
|
2010-11-07 16:28:11 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function __construct( $from_lines, $to_lines ) {
|
2013-10-29 22:25:28 +00:00
|
|
|
$eng = new DiffEngine;
|
2016-04-27 01:10:26 +00:00
|
|
|
$eng->setBailoutComplexity( $this->bailoutComplexity );
|
2010-11-27 19:01:51 +00:00
|
|
|
$this->edits = $eng->diff( $from_lines, $to_lines );
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
2014-02-26 21:47:40 +00:00
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return DiffOp[]
|
2014-02-26 21:47:40 +00:00
|
|
|
*/
|
|
|
|
|
public function getEdits() {
|
|
|
|
|
return $this->edits;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-07 16:28:11 +00:00
|
|
|
/**
|
|
|
|
|
* Compute reversed Diff.
|
|
|
|
|
*
|
|
|
|
|
* SYNOPSIS:
|
|
|
|
|
*
|
2013-11-20 19:11:57 +00:00
|
|
|
* $diff = new Diff($lines1, $lines2);
|
|
|
|
|
* $rev = $diff->reverse();
|
2014-03-03 17:08:05 +00:00
|
|
|
*
|
2012-02-10 15:37:33 +00:00
|
|
|
* @return Object A Diff object representing the inverse of the
|
2013-11-20 19:11:57 +00:00
|
|
|
* original diff.
|
2010-11-07 16:28:11 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function reverse() {
|
2010-11-07 16:28:11 +00:00
|
|
|
$rev = $this;
|
2016-02-17 09:09:32 +00:00
|
|
|
$rev->edits = [];
|
2013-10-29 22:25:28 +00:00
|
|
|
/** @var DiffOp $edit */
|
2010-11-27 19:01:51 +00:00
|
|
|
foreach ( $this->edits as $edit ) {
|
2010-11-07 16:28:11 +00:00
|
|
|
$rev->edits[] = $edit->reverse();
|
|
|
|
|
}
|
2013-11-20 19:11:57 +00:00
|
|
|
|
2010-11-07 16:28:11 +00:00
|
|
|
return $rev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check for empty diff.
|
|
|
|
|
*
|
2013-06-11 23:01:52 +00:00
|
|
|
* @return bool True if two sequences were identical.
|
2010-11-07 16:28:11 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function isEmpty() {
|
2010-11-27 19:01:51 +00:00
|
|
|
foreach ( $this->edits as $edit ) {
|
2011-02-19 21:34:44 +00:00
|
|
|
if ( $edit->type != 'copy' ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
2013-11-20 19:11:57 +00:00
|
|
|
|
2010-11-07 16:28:11 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Compute the length of the Longest Common Subsequence (LCS).
|
|
|
|
|
*
|
|
|
|
|
* This is mostly for diagnostic purposed.
|
|
|
|
|
*
|
|
|
|
|
* @return int The length of the LCS.
|
|
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function lcs() {
|
2010-11-07 16:28:11 +00:00
|
|
|
$lcs = 0;
|
2010-11-27 19:01:51 +00:00
|
|
|
foreach ( $this->edits as $edit ) {
|
2011-02-19 21:34:44 +00:00
|
|
|
if ( $edit->type == 'copy' ) {
|
2013-01-26 21:20:04 +00:00
|
|
|
$lcs += count( $edit->orig );
|
2011-02-19 21:34:44 +00:00
|
|
|
}
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
2013-11-20 19:11:57 +00:00
|
|
|
|
2010-11-07 16:28:11 +00:00
|
|
|
return $lcs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the original set of lines.
|
|
|
|
|
*
|
|
|
|
|
* This reconstructs the $from_lines parameter passed to the
|
|
|
|
|
* constructor.
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return string[] The original sequence of strings.
|
2010-11-07 16:28:11 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function orig() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$lines = [];
|
2010-11-07 16:28:11 +00:00
|
|
|
|
2010-11-27 19:01:51 +00:00
|
|
|
foreach ( $this->edits as $edit ) {
|
2011-02-19 21:34:44 +00:00
|
|
|
if ( $edit->orig ) {
|
2013-01-26 21:20:04 +00:00
|
|
|
array_splice( $lines, count( $lines ), 0, $edit->orig );
|
2011-02-19 21:34:44 +00:00
|
|
|
}
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
2013-11-20 19:11:57 +00:00
|
|
|
|
2010-11-07 16:28:11 +00:00
|
|
|
return $lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the closing set of lines.
|
|
|
|
|
*
|
|
|
|
|
* This reconstructs the $to_lines parameter passed to the
|
|
|
|
|
* constructor.
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return string[] The sequence of strings.
|
2010-11-07 16:28:11 +00:00
|
|
|
*/
|
2014-02-26 21:47:40 +00:00
|
|
|
public function closing() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$lines = [];
|
2010-11-07 16:28:11 +00:00
|
|
|
|
2010-11-27 19:01:51 +00:00
|
|
|
foreach ( $this->edits as $edit ) {
|
2011-02-19 21:34:44 +00:00
|
|
|
if ( $edit->closing ) {
|
2013-01-26 21:20:04 +00:00
|
|
|
array_splice( $lines, count( $lines ), 0, $edit->closing );
|
2011-02-19 21:34:44 +00:00
|
|
|
}
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|
2013-11-20 19:11:57 +00:00
|
|
|
|
2010-11-07 16:28:11 +00:00
|
|
|
return $lines;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-26 18:57:44 +00:00
|
|
|
* @deprecated Alias for WordAccumulator, to be soon removed
|
2010-11-07 16:28:11 +00:00
|
|
|
*/
|
2016-04-26 18:57:44 +00:00
|
|
|
class HWLDFWordAccumulator extends MediaWiki\Diff\WordAccumulator {
|
2010-11-07 16:28:11 +00:00
|
|
|
}
|