3 * A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
5 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
6 * You may copy this code freely under the conditions of the GPL.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup DifferenceEngine
25 * @defgroup DifferenceEngine DifferenceEngine
29 * The base class for all other DiffOp classes.
31 * The classes that extend DiffOp are: DiffOpCopy, DiffOpDelete, DiffOpAdd and
32 * DiffOpChange. FakeDiffOp also extends DiffOp, but it is not located in this file.
35 * @ingroup DifferenceEngine
37 abstract class DiffOp
{
57 public function getType() {
64 public function getOrig() {
70 * @return string[]|string|null
72 public function getClosing( $i = null ) {
74 return $this->closing
;
76 if ( array_key_exists( $i, $this->closing
) ) {
77 return $this->closing
[$i];
82 abstract public function reverse();
87 public function norig() {
88 return $this->orig ?
count( $this->orig
) : 0;
94 public function nclosing() {
95 return $this->closing ?
count( $this->closing
) : 0;
100 * Extends DiffOp. Used to mark strings that have been
101 * copied from one string array to the other.
104 * @ingroup DifferenceEngine
106 class DiffOpCopy
extends DiffOp
{
107 public $type = 'copy';
109 public function __construct( $orig, $closing = false ) {
110 if ( !is_array( $closing ) ) {
114 $this->closing
= $closing;
120 public function reverse() {
121 return new DiffOpCopy( $this->closing
, $this->orig
);
126 * Extends DiffOp. Used to mark strings that have been
127 * deleted from the first string array.
130 * @ingroup DifferenceEngine
132 class DiffOpDelete
extends DiffOp
{
133 public $type = 'delete';
135 public function __construct( $lines ) {
136 $this->orig
= $lines;
137 $this->closing
= false;
143 public function reverse() {
144 return new DiffOpAdd( $this->orig
);
149 * Extends DiffOp. Used to mark strings that have been
150 * added from the first string array.
153 * @ingroup DifferenceEngine
155 class DiffOpAdd
extends DiffOp
{
156 public $type = 'add';
158 public function __construct( $lines ) {
159 $this->closing
= $lines;
164 * @return DiffOpDelete
166 public function reverse() {
167 return new DiffOpDelete( $this->closing
);
172 * Extends DiffOp. Used to mark strings that have been
173 * changed from the first string array (both added and subtracted).
176 * @ingroup DifferenceEngine
178 class DiffOpChange
extends DiffOp
{
179 public $type = 'change';
181 public function __construct( $orig, $closing ) {
183 $this->closing
= $closing;
187 * @return DiffOpChange
189 public function reverse() {
190 return new DiffOpChange( $this->closing
, $this->orig
);
195 * Class representing a 'diff' between two sequences of strings.
198 * @ingroup DifferenceEngine
208 * @var int If this diff complexity is exceeded, a ComplexityException is thrown
211 protected $bailoutComplexity = 0;
215 * Computes diff between sequences of strings.
217 * @param string[] $from_lines An array of strings.
218 * Typically these are lines from a file.
219 * @param string[] $to_lines An array of strings.
220 * @throws \MediaWiki\Diff\ComplexityException
222 public function __construct( $from_lines, $to_lines ) {
223 $eng = new DiffEngine
;
224 $eng->setBailoutComplexity( $this->bailoutComplexity
);
225 $this->edits
= $eng->diff( $from_lines, $to_lines );
231 public function getEdits() {
236 * Compute reversed Diff.
240 * $diff = new Diff($lines1, $lines2);
241 * $rev = $diff->reverse();
243 * @return Object A Diff object representing the inverse of the
246 public function reverse() {
249 /** @var DiffOp $edit */
250 foreach ( $this->edits
as $edit ) {
251 $rev->edits
[] = $edit->reverse();
258 * Check for empty diff.
260 * @return bool True if two sequences were identical.
262 public function isEmpty() {
263 foreach ( $this->edits
as $edit ) {
264 if ( $edit->type
!= 'copy' ) {
273 * Compute the length of the Longest Common Subsequence (LCS).
275 * This is mostly for diagnostic purposed.
277 * @return int The length of the LCS.
279 public function lcs() {
281 foreach ( $this->edits
as $edit ) {
282 if ( $edit->type
== 'copy' ) {
283 $lcs +
= count( $edit->orig
);
291 * Get the original set of lines.
293 * This reconstructs the $from_lines parameter passed to the
296 * @return string[] The original sequence of strings.
298 public function orig() {
301 foreach ( $this->edits
as $edit ) {
303 array_splice( $lines, count( $lines ), 0, $edit->orig
);
311 * Get the closing set of lines.
313 * This reconstructs the $to_lines parameter passed to the
316 * @return string[] The sequence of strings.
318 public function closing() {
321 foreach ( $this->edits
as $edit ) {
322 if ( $edit->closing
) {
323 array_splice( $lines, count( $lines ), 0, $edit->closing
);
332 * @deprecated Alias for WordAccumulator, to be soon removed
334 class HWLDFWordAccumulator
extends MediaWiki\Diff\WordAccumulator
{