3 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * You may copy this code freely under the conditions of the GPL.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup DifferenceEngine
23 * @defgroup DifferenceEngine DifferenceEngine
26 use MediaWiki\Diff\WordAccumulator
;
29 * Performs a word-level diff on several lines
31 * @ingroup DifferenceEngine
33 class WordLevelDiff
extends \Diff
{
34 const MAX_LINE_LENGTH
= 10000;
37 * @param string[] $linesBefore
38 * @param string[] $linesAfter
40 public function __construct( $linesBefore, $linesAfter ) {
42 list( $wordsBefore, $wordsBeforeStripped ) = $this->split( $linesBefore );
43 list( $wordsAfter, $wordsAfterStripped ) = $this->split( $linesAfter );
45 parent
::__construct( $wordsBeforeStripped, $wordsAfterStripped );
48 $editCount = count( $this->edits
);
49 for ( $i = 0; $i < $editCount; $i++
) {
50 $orig = &$this->edits
[$i]->orig
;
51 if ( is_array( $orig ) ) {
52 $orig = array_slice( $wordsBefore, $xi, count( $orig ) );
53 $xi +
= count( $orig );
56 $closing = &$this->edits
[$i]->closing
;
57 if ( is_array( $closing ) ) {
58 $closing = array_slice( $wordsAfter, $yi, count( $closing ) );
59 $yi +
= count( $closing );
66 * @param string[] $lines
70 private function split( $lines ) {
75 foreach ( $lines as $line ) {
76 # If the line is too long, just pretend the entire line is one big word
77 # This prevents resource exhaustion problems
84 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
89 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
92 foreach ( $m[0] as $word ) {
95 foreach ( $m[1] as $stripped_word ) {
96 $stripped[] = $stripped_word;
102 return [ $words, $stripped ];
108 public function orig() {
109 $orig = new WordAccumulator
;
111 foreach ( $this->edits
as $edit ) {
112 if ( $edit->type
== 'copy' ) {
113 $orig->addWords( $edit->orig
);
114 } elseif ( $edit->orig
) {
115 $orig->addWords( $edit->orig
, 'del' );
118 $lines = $orig->getLines();
126 public function closing() {
127 $closing = new WordAccumulator
;
129 foreach ( $this->edits
as $edit ) {
130 if ( $edit->type
== 'copy' ) {
131 $closing->addWords( $edit->closing
);
132 } elseif ( $edit->closing
) {
133 $closing->addWords( $edit->closing
, 'ins' );
136 $lines = $closing->getLines();