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