MediaWiki  master
TableDiffFormatter.php
Go to the documentation of this file.
1 <?php
34 
35  public function __construct() {
36  $this->leadingContextLines = 2;
37  $this->trailingContextLines = 2;
38  }
39 
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 
61  protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
62  // '<!--LINE \d+ -->' get replaced by a localised line number
63  // in DifferenceEngine::localiseLineNumbers
64  return Html::rawElement(
65  'tr',
66  [],
68  'td',
69  [ 'colspan' => '2', 'class' => 'diff-lineno', 'id' => 'mw-diff-left-l' . $xbeg ],
70  '<!--LINE ' . $xbeg . '-->'
71  ) .
72  "\n" .
74  'td',
75  [ 'colspan' => '2', 'class' => 'diff-lineno' ],
76  '<!--LINE ' . $ybeg . '-->'
77  )
78  ) . "\n";
79  }
80 
82  protected function startBlock( $header ) {
83  $this->writeOutput( $header );
84  }
85 
87  protected function endBlock() {
88  }
89 
95  protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
96  }
97 
105  protected function addedLine( $line ) {
106  return $this->wrapLine( '+', 'diff-addedline', $line );
107  }
108 
116  protected function deletedLine( $line ) {
117  return $this->wrapLine( '−', 'diff-deletedline', $line );
118  }
119 
127  protected function contextLine( $line ) {
128  return $this->wrapLine( '', 'diff-context', $line );
129  }
130 
138  protected function wrapLine( $marker, $class, $line ) {
139  if ( $line !== '' ) {
140  // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
141  $line = Html::rawElement( 'div', [], $this->escapeWhiteSpace( $line ) );
142  } else {
143  $line = Html::element( 'br' );
144  }
145 
146  $markerAttrs = [ 'class' => 'diff-marker' ];
147  if ( $marker ) {
148  $markerAttrs['data-marker'] = $marker;
149  }
150 
151  return Html::element( 'td', $markerAttrs ) .
152  Html::rawElement( 'td', [ 'class' => $class ], $line );
153  }
154 
158  protected function emptyLine() {
159  return Html::element( 'td', [ 'colspan' => '2' ] );
160  }
161 
167  protected function added( $lines ) {
168  foreach ( $lines as $line ) {
169  $this->writeOutput(
171  'tr',
172  [],
173  $this->emptyLine() .
174  $this->addedLine(
176  'ins',
177  [ 'class' => 'diffchange' ],
178  $line
179  )
180  )
181  ) .
182  "\n"
183  );
184  }
185  }
186 
192  protected function deleted( $lines ) {
193  foreach ( $lines as $line ) {
194  $this->writeOutput(
196  'tr',
197  [],
198  $this->deletedLine(
200  'del',
201  [ 'class' => 'diffchange' ],
202  $line
203  )
204  ) .
205  $this->emptyLine()
206  ) .
207  "\n"
208  );
209  }
210  }
211 
217  protected function context( $lines ) {
218  foreach ( $lines as $line ) {
219  $this->writeOutput(
221  'tr',
222  [],
223  $this->contextLine( htmlspecialchars( $line ) ) .
224  $this->contextLine( htmlspecialchars( $line ) )
225  ) .
226  "\n"
227  );
228  }
229  }
230 
237  protected function changed( $orig, $closing ) {
238  $diff = new WordLevelDiff( $orig, $closing );
239  $del = $diff->orig();
240  $add = $diff->closing();
241 
242  # Notice that WordLevelDiff returns HTML-escaped output.
243  # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
244 
245  $ndel = count( $del );
246  $nadd = count( $add );
247  $n = max( $ndel, $nadd );
248  for ( $i = 0; $i < $n; $i++ ) {
249  $delLine = $i < $ndel ? $this->deletedLine( $del[$i] ) : $this->emptyLine();
250  $addLine = $i < $nadd ? $this->addedLine( $add[$i] ) : $this->emptyLine();
251  $this->writeOutput(
253  'tr',
254  [],
255  $delLine . $addLine
256  ) .
257  "\n"
258  );
259  }
260  }
261 
262 }
TableDiffFormatter\wrapLine
wrapLine( $marker, $class, $line)
Definition: TableDiffFormatter.php:138
TableDiffFormatter\deleted
deleted( $lines)
Writes all lines to the output buffer, each enclosed in .
Definition: TableDiffFormatter.php:192
TableDiffFormatter\escapeWhiteSpace
static escapeWhiteSpace( $msg)
Definition: TableDiffFormatter.php:45
TableDiffFormatter\blockHeader
blockHeader( $xbeg, $xlen, $ybeg, $ylen)
Definition: TableDiffFormatter.php:61
DiffFormatter
Base class for diff formatters.
Definition: DiffFormatter.php:36
WordLevelDiff
Performs a word-level diff on several lines.
Definition: WordLevelDiff.php:34
TableDiffFormatter\contextLine
contextLine( $line)
HTML-escape parameter before calling this.
Definition: TableDiffFormatter.php:127
DiffFormatter\writeOutput
writeOutput( $text)
Writes a string to the output buffer.
Definition: DiffFormatter.php:159
TableDiffFormatter
MediaWiki default table style diff formatter.
Definition: TableDiffFormatter.php:33
TableDiffFormatter\__construct
__construct()
Definition: TableDiffFormatter.php:35
TableDiffFormatter\addedLine
addedLine( $line)
HTML-escape parameter before calling this.
Definition: TableDiffFormatter.php:105
TableDiffFormatter\changed
changed( $orig, $closing)
Writes the two sets of lines to the output buffer, each enclosed in .
Definition: TableDiffFormatter.php:237
TableDiffFormatter\endBlock
endBlock()
Called at the end of a block of connected edits.This default implementation does nothing.
Definition: TableDiffFormatter.php:87
TableDiffFormatter\deletedLine
deletedLine( $line)
HTML-escape parameter before calling this.
Definition: TableDiffFormatter.php:116
TableDiffFormatter\added
added( $lines)
Writes all lines to the output buffer, each enclosed in .
Definition: TableDiffFormatter.php:167
TableDiffFormatter\startBlock
startBlock( $header)
Called at the start of a block of connected edits.This default implementation writes the header and a...
Definition: TableDiffFormatter.php:82
$header
$header
Definition: updateCredits.php:37
$line
$line
Definition: mcc.php:119
TableDiffFormatter\context
context( $lines)
Writes all lines to the output buffer, each enclosed in .
Definition: TableDiffFormatter.php:217
$lines
if(!file_exists( $CREDITS)) $lines
Definition: updateCredits.php:45
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
TableDiffFormatter\lines
lines( $lines, $prefix=' ', $color='white')
Definition: TableDiffFormatter.php:95
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:234
TableDiffFormatter\emptyLine
emptyLine()
Definition: TableDiffFormatter.php:158