Debian Codegraph @

includes/libs/Diff/TableDiffFormatter.php

raw symbols SCIP
1 <?php
2 /**
3 * Portions taken from phpwiki-1.3.3.
4 *
5 * Copyright © 2000, 2001 Geoffrey T. Dairiki <[email protected]>
6 * You may copy this code freely under the conditions of the GPL.
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @ingroup DifferenceEngine
25 */
26
27 namespace Wikimedia\Diff;
28
29 use InvalidArgumentException;
30
31 /**
32 * MediaWiki default table style diff formatter
33 * @todo document
34 * @newable
35 * @ingroup DifferenceEngine
36 */
37 class TableDiffFormatter extends DiffFormatter {
38
39 /**
40 * Constants for diff sides. Note: these are also used for context lines.
41 */
42 private const SIDE_DELETED = 'deleted';
43 private const SIDE_ADDED = 'added';
44 private const SIDE_CLASSES = [
45 self::SIDE_DELETED => 'diff-side-deleted',
46 self::SIDE_ADDED => 'diff-side-added'
47 ];
48
49 public function __construct() {
50 $this->leadingContextLines = 2;
51 $this->trailingContextLines = 2;
52 }
53
54 /**
55 * @param string $msg
56 *
57 * @return string
58 */
59 public static function escapeWhiteSpace( $msg ) {
60 $msg = preg_replace( '/^ /m', "\u{00A0} ", $msg );
61 $msg = preg_replace( '/ $/m', " \u{00A0}", $msg );
62 $msg = preg_replace( '/ /', "\u{00A0} ", $msg );
63
64 return $msg;
65 }
66
67 /**
68 * @param int $xbeg
69 * @param int $xlen
70 * @param int $ybeg
71 * @param int $ylen
72 *
73 * @return string
74 */
75 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
76 // '<!--LINE \d+ -->' get replaced by a localised line number
77 // in BaseTextDiffer::localizeLineNumbers
78 return $this->rawElement(
79 'tr',
80 [],
81 $this->rawElement(
82 'td',
83 [ 'colspan' => '2', 'class' => 'diff-lineno', 'id' => 'mw-diff-left-l' . $xbeg ],
84 '<!--LINE ' . $xbeg . '-->'
85 ) .
86 "\n" .
87 $this->rawElement(
88 'td',
89 [ 'colspan' => '2', 'class' => 'diff-lineno' ],
90 '<!--LINE ' . $ybeg . '-->'
91 )
92 ) . "\n";
93 }
94
95 /** @inheritDoc */
96 protected function startBlock( $header ) {
97 $this->writeOutput( $header );
98 }
99
100 /** @inheritDoc */
101 protected function endBlock() {
102 }
103
104 /**
105 * @param string[] $lines
106 * @param string $prefix
107 * @param string $color
108 */
109 protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
110 }
111
112 /**
113 * HTML-escape parameter before calling this
114 *
115 * @param string $line
116 *
117 * @return string
118 */
119 protected function addedLine( $line ) {
120 return $this->wrapLine( '+', [ 'diff-addedline', $this->getClassForSide( self::SIDE_ADDED ) ], $line );
121 }
122
123 /**
124 * HTML-escape parameter before calling this
125 *
126 * @param string $line
127 *
128 * @return string
129 */
130 protected function deletedLine( $line ) {
131 return $this->wrapLine( '−', [ 'diff-deletedline', $this->getClassForSide( self::SIDE_DELETED ) ], $line );
132 }
133
134 /**
135 * HTML-escape parameter before calling this
136 *
137 * @param string $line
138 * @param string $side self::SIDE_DELETED or self::SIDE_ADDED
139 *
140 * @return string
141 */
142 protected function contextLine( $line, string $side ) {
143 return $this->wrapLine( '', [ 'diff-context', $this->getClassForSide( $side ) ], $line );
144 }
145
146 /**
147 * @param string $marker
148 * @param string|string[] $class A single class or a list of classes
149 * @param string $line
150 *
151 * @return string
152 */
153 protected function wrapLine( $marker, $class, $line ) {
154 if ( $line !== '' ) {
155 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
156 $line = $this->rawElement( 'div', [], $this->escapeWhiteSpace( $line ) );
157 } else {
158 $line = '<br>';
159 }
160
161 $markerAttrs = [ 'class' => 'diff-marker' ];
162 if ( $marker ) {
163 $markerAttrs['data-marker'] = $marker;
164 }
165
166 if ( is_array( $class ) ) {
167 $class = implode( ' ', $class );
168 }
169
170 return $this->element( 'td', $markerAttrs ) .
171 $this->rawElement( 'td', [ 'class' => $class ], $line );
172 }
173
174 /**
175 * @param string $side self::SIDE_DELETED or self::SIDE_ADDED
176 * @return string
177 */
178 protected function emptyLine( string $side ) {
179 return $this->element( 'td', [ 'colspan' => '2', 'class' => $this->getClassForSide( $side ) ] );
180 }
181
182 /**
183 * Writes all lines to the output buffer, each enclosed in <tr>.
184 *
185 * @param string[] $lines
186 */
187 protected function added( $lines ) {
188 foreach ( $lines as $line ) {
189 $this->writeOutput(
190 $this->rawElement(
191 'tr',
192 [],
193 $this->emptyLine( self::SIDE_DELETED ) .
194 $this->addedLine(
195 $this->element(
196 'ins',
197 [ 'class' => 'diffchange' ],
198 $line
199 )
200 )
201 ) .
202 "\n"
203 );
204 }
205 }
206
207 /**
208 * Writes all lines to the output buffer, each enclosed in <tr>.
209 *
210 * @param string[] $lines
211 */
212 protected function deleted( $lines ) {
213 foreach ( $lines as $line ) {
214 $this->writeOutput(
215 $this->rawElement(
216 'tr',
217 [],
218 $this->deletedLine(
219 $this->element(
220 'del',
221 [ 'class' => 'diffchange' ],
222 $line
223 )
224 ) .
225 $this->emptyLine( self::SIDE_ADDED )
226 ) .
227 "\n"
228 );
229 }
230 }
231
232 /**
233 * Writes all lines to the output buffer, each enclosed in <tr>.
234 *
235 * @param string[] $lines
236 */
237 protected function context( $lines ) {
238 foreach ( $lines as $line ) {
239 $this->writeOutput(
240 $this->rawElement(
241 'tr',
242 [],
243 $this->contextLine( htmlspecialchars( $line ), self::SIDE_DELETED ) .
244 $this->contextLine( htmlspecialchars( $line ), self::SIDE_ADDED )
245 ) .
246 "\n"
247 );
248 }
249 }
250
251 /**
252 * Writes the two sets of lines to the output buffer, each enclosed in <tr>.
253 *
254 * @param string[] $orig
255 * @param string[] $closing
256 */
257 protected function changed( $orig, $closing ) {
258 $diff = new WordLevelDiff( $orig, $closing );
259 $del = $diff->orig();
260 $add = $diff->closing();
261
262 # Notice that WordLevelDiff returns HTML-escaped output.
263 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
264
265 $ndel = count( $del );
266 $nadd = count( $add );
267 $n = max( $ndel, $nadd );
268 for ( $i = 0; $i < $n; $i++ ) {
269 $delLine = $i < $ndel ? $this->deletedLine( $del[$i] ) : $this->emptyLine( self::SIDE_DELETED );
270 $addLine = $i < $nadd ? $this->addedLine( $add[$i] ) : $this->emptyLine( self::SIDE_ADDED );
271 $this->writeOutput(
272 $this->rawElement(
273 'tr',
274 [],
275 $delLine . $addLine
276 ) .
277 "\n"
278 );
279 }
280 }
281
282 /**
283 * Get a class for the given diff side, or throw if the side is invalid.
284 *
285 * @param string $side self::SIDE_DELETED or self::SIDE_ADDED
286 * @return string
287 * @throws InvalidArgumentException
288 */
289 private function getClassForSide( string $side ): string {
290 if ( !isset( self::SIDE_CLASSES[$side] ) ) {
291 throw new InvalidArgumentException( "Invalid diff side: $side" );
292 }
293 return self::SIDE_CLASSES[$side];
294 }
295
296 /**
297 * Serialize an HTML element, with raw contents.
298 *
299 * @param string $element
300 * @param string[] $attribs
301 * @param string $contents The HTML element contents
302 * @return string
303 */
304 private function rawElement( $element, $attribs = [], $contents = '' ) {
305 $ret = "<$element";
306 foreach ( $attribs as $name => $value ) {
307 $ret .= " $name=\"" . htmlspecialchars( $value, ENT_QUOTES ) . '"';
308 }
309 $ret .= ">$contents</$element>";
310 return $ret;
311 }
312
313 /**
314 * Serialize an HTML element, encoding the text contents.
315 *
316 * @param string $element
317 * @param string[] $attribs
318 * @param string $contents The text contents
319 * @return string
320 */
321 private function element( $element, $attribs = [], $contents = '' ) {
322 return $this->rawElement( $element, $attribs, htmlspecialchars( $contents, ENT_NOQUOTES ) );
323 }
324 }
325
326 /** @deprecated class alias since 1.41 */
327 class_alias( TableDiffFormatter::class, 'TableDiffFormatter' );