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
28 * Class representing a 'diff' between two sequences of strings.
30 * @ingroup DifferenceEngine
40 * @var int If this diff complexity is exceeded, a ComplexityException is thrown
43 protected $bailoutComplexity = 0;
46 * Computes diff between sequences of strings.
48 * @param string[] $from_lines An array of strings.
49 * Typically these are lines from a file.
50 * @param string[] $to_lines An array of strings.
51 * @throws \MediaWiki\Diff\ComplexityException
53 public function __construct( $from_lines, $to_lines ) {
54 $eng = new DiffEngine
;
55 $eng->setBailoutComplexity( $this->bailoutComplexity
);
56 $this->edits
= $eng->diff( $from_lines, $to_lines );
62 public function getEdits() {
67 * Compute reversed Diff.
71 * $diff = new Diff($lines1, $lines2);
72 * $rev = $diff->reverse();
74 * @return Object A Diff object representing the inverse of the
77 public function reverse() {
80 /** @var DiffOp $edit */
81 foreach ( $this->edits
as $edit ) {
82 $rev->edits
[] = $edit->reverse();
89 * Check for empty diff.
91 * @return bool True if two sequences were identical.
93 public function isEmpty() {
94 foreach ( $this->edits
as $edit ) {
95 if ( $edit->type
!= 'copy' ) {
104 * Compute the length of the Longest Common Subsequence (LCS).
106 * This is mostly for diagnostic purposed.
108 * @return int The length of the LCS.
110 public function lcs() {
112 foreach ( $this->edits
as $edit ) {
113 if ( $edit->type
== 'copy' ) {
114 $lcs +
= count( $edit->orig
);
122 * Get the original set of lines.
124 * This reconstructs the $from_lines parameter passed to the
127 * @return string[] The original sequence of strings.
129 public function orig() {
132 foreach ( $this->edits
as $edit ) {
134 array_splice( $lines, count( $lines ), 0, $edit->orig
);
142 * Get the closing set of lines.
144 * This reconstructs the $to_lines parameter passed to the
147 * @return string[] The sequence of strings.
149 public function closing() {
152 foreach ( $this->edits
as $edit ) {
153 if ( $edit->closing
) {
154 array_splice( $lines, count( $lines ), 0, $edit->closing
);