Merge "Convert Special:WithoutInterwiki to OOUI"
[lhc/web/wiklou.git] / includes / diff / WordLevelDiff.php
1 <?php
2 /**
3 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * You may copy this code freely under the conditions of the GPL.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup DifferenceEngine
23 * @defgroup DifferenceEngine DifferenceEngine
24 */
25
26 use MediaWiki\Diff\WordAccumulator;
27
28 /**
29 * Performs a word-level diff on several lines
30 *
31 * @ingroup DifferenceEngine
32 */
33 class WordLevelDiff extends \Diff {
34 const MAX_LINE_LENGTH = 10000;
35
36 /**
37 * @param string[] $linesBefore
38 * @param string[] $linesAfter
39 */
40 public function __construct( $linesBefore, $linesAfter ) {
41
42 list( $wordsBefore, $wordsBeforeStripped ) = $this->split( $linesBefore );
43 list( $wordsAfter, $wordsAfterStripped ) = $this->split( $linesAfter );
44
45 parent::__construct( $wordsBeforeStripped, $wordsAfterStripped );
46
47 $xi = $yi = 0;
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 );
54 }
55
56 $closing = &$this->edits[$i]->closing;
57 if ( is_array( $closing ) ) {
58 $closing = array_slice( $wordsAfter, $yi, count( $closing ) );
59 $yi += count( $closing );
60 }
61 }
62
63 }
64
65 /**
66 * @param string[] $lines
67 *
68 * @return array[]
69 */
70 private function split( $lines ) {
71
72 $words = [];
73 $stripped = [];
74 $first = true;
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
78 if ( $first ) {
79 $first = false;
80 } else {
81 $words[] = "\n";
82 $stripped[] = "\n";
83 }
84 if ( strlen( $line ) > self::MAX_LINE_LENGTH ) {
85 $words[] = $line;
86 $stripped[] = $line;
87 } else {
88 $m = [];
89 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
90 $line, $m )
91 ) {
92 foreach ( $m[0] as $word ) {
93 $words[] = $word;
94 }
95 foreach ( $m[1] as $stripped_word ) {
96 $stripped[] = $stripped_word;
97 }
98 }
99 }
100 }
101
102 return [ $words, $stripped ];
103 }
104
105 /**
106 * @return string[]
107 */
108 public function orig() {
109 $orig = new WordAccumulator;
110
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' );
116 }
117 }
118 $lines = $orig->getLines();
119
120 return $lines;
121 }
122
123 /**
124 * @return string[]
125 */
126 public function closing() {
127 $closing = new WordAccumulator;
128
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' );
134 }
135 }
136 $lines = $closing->getLines();
137
138 return $lines;
139 }
140
141 }