MediaWiki  1.34.0
TableDiffFormatter.php
Go to the documentation of this file.
1 <?php
34 
35  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  $r = '<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l' .
65  $xbeg .
66  '" ><!--LINE ' .
67  $xbeg .
68  "--></td>\n" .
69  '<td colspan="2" class="diff-lineno"><!--LINE ' .
70  $ybeg .
71  "--></td></tr>\n";
72 
73  return $r;
74  }
75 
81  protected function startBlock( $header ) {
82  $this->writeOutput( $header );
83  }
84 
85  protected function endBlock() {
86  }
87 
93  protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
94  }
95 
103  protected function addedLine( $line ) {
104  return $this->wrapLine( '+', 'diff-addedline', $line );
105  }
106 
114  protected function deletedLine( $line ) {
115  return $this->wrapLine( '−', 'diff-deletedline', $line );
116  }
117 
125  protected function contextLine( $line ) {
126  return $this->wrapLine( "\u{00A0}", 'diff-context', $line );
127  }
128 
136  protected function wrapLine( $marker, $class, $line ) {
137  if ( $line !== '' ) {
138  // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
139  $line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
140  }
141 
142  return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
143  }
144 
148  protected function emptyLine() {
149  return "<td colspan=\"2\">\u{00A0}</td>";
150  }
151 
157  protected function added( $lines ) {
158  foreach ( $lines as $line ) {
159  $this->writeOutput( '<tr>' . $this->emptyLine() .
160  $this->addedLine( '<ins class="diffchange">' .
161  htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n" );
162  }
163  }
164 
170  protected function deleted( $lines ) {
171  foreach ( $lines as $line ) {
172  $this->writeOutput( '<tr>' . $this->deletedLine( '<del class="diffchange">' .
173  htmlspecialchars( $line ) . '</del>' ) .
174  $this->emptyLine() . "</tr>\n" );
175  }
176  }
177 
183  protected function context( $lines ) {
184  foreach ( $lines as $line ) {
185  $this->writeOutput( '<tr>' .
186  $this->contextLine( htmlspecialchars( $line ) ) .
187  $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n" );
188  }
189  }
190 
197  protected function changed( $orig, $closing ) {
198  $diff = new WordLevelDiff( $orig, $closing );
199  $del = $diff->orig();
200  $add = $diff->closing();
201 
202  # Notice that WordLevelDiff returns HTML-escaped output.
203  # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
204 
205  $ndel = count( $del );
206  $nadd = count( $add );
207  $n = max( $ndel, $nadd );
208  for ( $i = 0; $i < $n; $i++ ) {
209  $delLine = $i < $ndel ? $this->deletedLine( $del[$i] ) : $this->emptyLine();
210  $addLine = $i < $nadd ? $this->addedLine( $add[$i] ) : $this->emptyLine();
211  $this->writeOutput( "<tr>{$delLine}{$addLine}</tr>\n" );
212  }
213  }
214 
215 }
TableDiffFormatter\wrapLine
wrapLine( $marker, $class, $line)
Definition: TableDiffFormatter.php:136
TableDiffFormatter\deleted
deleted( $lines)
Writes all lines to the output buffer, each enclosed in .
Definition: TableDiffFormatter.php:170
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:125
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:103
$lines
$lines
Definition: router.php:61
TableDiffFormatter\changed
changed( $orig, $closing)
Writes the two sets of lines to the output buffer, each enclosed in .
Definition: TableDiffFormatter.php:197
TableDiffFormatter\endBlock
endBlock()
Called at the end of a block of connected edits.
Definition: TableDiffFormatter.php:85
$line
$line
Definition: cdb.php:59
TableDiffFormatter\deletedLine
deletedLine( $line)
HTML-escape parameter before calling this.
Definition: TableDiffFormatter.php:114
TableDiffFormatter\added
added( $lines)
Writes all lines to the output buffer, each enclosed in .
Definition: TableDiffFormatter.php:157
TableDiffFormatter\startBlock
startBlock( $header)
Writes the header to the output buffer.
Definition: TableDiffFormatter.php:81
$header
$header
Definition: updateCredits.php:41
Xml\tags
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:130
TableDiffFormatter\context
context( $lines)
Writes all lines to the output buffer, each enclosed in .
Definition: TableDiffFormatter.php:183
TableDiffFormatter\lines
lines( $lines, $prefix=' ', $color='white')
Definition: TableDiffFormatter.php:93
TableDiffFormatter\emptyLine
emptyLine()
Definition: TableDiffFormatter.php:148