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() {
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
209 * Computes diff between sequences of strings.
211 * @param string[] $from_lines An array of strings.
212 * Typically these are lines from a file.
213 * @param string[] $to_lines An array of strings.
215 public function __construct( $from_lines, $to_lines ) {
216 $eng = new DiffEngine
;
217 $this->edits
= $eng->diff( $from_lines, $to_lines );
223 public function getEdits() {
228 * Compute reversed Diff.
232 * $diff = new Diff($lines1, $lines2);
233 * $rev = $diff->reverse();
235 * @return Object A Diff object representing the inverse of the
238 public function reverse() {
241 /** @var DiffOp $edit */
242 foreach ( $this->edits
as $edit ) {
243 $rev->edits
[] = $edit->reverse();
250 * Check for empty diff.
252 * @return bool True if two sequences were identical.
254 public function isEmpty() {
255 foreach ( $this->edits
as $edit ) {
256 if ( $edit->type
!= 'copy' ) {
265 * Compute the length of the Longest Common Subsequence (LCS).
267 * This is mostly for diagnostic purposed.
269 * @return int The length of the LCS.
271 public function lcs() {
273 foreach ( $this->edits
as $edit ) {
274 if ( $edit->type
== 'copy' ) {
275 $lcs +
= count( $edit->orig
);
283 * Get the original set of lines.
285 * This reconstructs the $from_lines parameter passed to the
288 * @return string[] The original sequence of strings.
290 public function orig() {
293 foreach ( $this->edits
as $edit ) {
295 array_splice( $lines, count( $lines ), 0, $edit->orig
);
303 * Get the closing set of lines.
305 * This reconstructs the $to_lines parameter passed to the
308 * @return string[] The sequence of strings.
310 public function closing() {
313 foreach ( $this->edits
as $edit ) {
314 if ( $edit->closing
) {
315 array_splice( $lines, count( $lines ), 0, $edit->closing
);
324 * @deprecated Alias for WordAccumulator, to be soon removed
326 class HWLDFWordAccumulator
extends MediaWiki\Diff\WordAccumulator
{