3 * Portions taken from phpwiki-1.3.3.
5 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
6 * You may copy this code freely under the conditions of the GPL.
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.
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.
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
24 * @ingroup DifferenceEngine
28 * MediaWiki default table style diff formatter
31 * @ingroup DifferenceEngine
33 class TableDiffFormatter
extends DiffFormatter
{
35 function __construct() {
36 $this->leadingContextLines
= 2;
37 $this->trailingContextLines
= 2;
46 public static function escapeWhiteSpace( $msg ) {
47 $msg = preg_replace( '/^ /m', '  ', $msg );
48 $msg = preg_replace( '/ $/m', '  ', $msg );
49 $msg = preg_replace( '/ /', '  ', $msg );
62 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
63 // '<!--LINE \d+ -->' get replaced by a localised line number
64 // in DifferenceEngine::localiseLineNumbers
65 $r = '<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l' .
70 '<td colspan="2" class="diff-lineno"><!--LINE ' .
78 * Writes the header to the output buffer.
80 * @param string $header
82 protected function startBlock( $header ) {
83 $this->writeOutput( $header );
86 protected function endBlock() {
90 * @param string[] $lines
91 * @param string $prefix
92 * @param string $color
94 protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
98 * HTML-escape parameter before calling this
100 * @param string $line
104 protected function addedLine( $line ) {
105 return $this->wrapLine( '+', 'diff-addedline', $line );
109 * HTML-escape parameter before calling this
111 * @param string $line
115 protected function deletedLine( $line ) {
116 return $this->wrapLine( '−', 'diff-deletedline', $line );
120 * HTML-escape parameter before calling this
122 * @param string $line
126 protected function contextLine( $line ) {
127 return $this->wrapLine( ' ', 'diff-context', $line );
131 * @param string $marker
132 * @param string $class Unused
133 * @param string $line
137 protected function wrapLine( $marker, $class, $line ) {
138 if ( $line !== '' ) {
139 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
140 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
143 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
149 protected function emptyLine() {
150 return '<td colspan="2"> </td>';
154 * Writes all lines to the output buffer, each enclosed in <tr>.
156 * @param string[] $lines
158 protected function added( $lines ) {
159 foreach ( $lines as $line ) {
160 $this->writeOutput( '<tr>' . $this->emptyLine() .
161 $this->addedLine( '<ins class="diffchange">' .
162 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n" );
167 * Writes all lines to the output buffer, each enclosed in <tr>.
169 * @param string[] $lines
171 protected function deleted( $lines ) {
172 foreach ( $lines as $line ) {
173 $this->writeOutput( '<tr>' . $this->deletedLine( '<del class="diffchange">' .
174 htmlspecialchars( $line ) . '</del>' ) .
175 $this->emptyLine() . "</tr>\n" );
180 * Writes all lines to the output buffer, each enclosed in <tr>.
182 * @param string[] $lines
184 protected function context( $lines ) {
185 foreach ( $lines as $line ) {
186 $this->writeOutput( '<tr>' .
187 $this->contextLine( htmlspecialchars( $line ) ) .
188 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n" );
193 * Writes the two sets of lines to the output buffer, each enclosed in <tr>.
195 * @param string[] $orig
196 * @param string[] $closing
198 protected function changed( $orig, $closing ) {
199 $diff = new WordLevelDiff( $orig, $closing );
200 $del = $diff->orig();
201 $add = $diff->closing();
203 # Notice that WordLevelDiff returns HTML-escaped output.
204 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
206 $ndel = count( $del );
207 $nadd = count( $add );
208 $n = max( $ndel, $nadd );
209 for ( $i = 0; $i < $n; $i++
) {
210 $delLine = $i < $ndel ?
$this->deletedLine( $del[$i] ) : $this->emptyLine();
211 $addLine = $i < $nadd ?
$this->addedLine( $add[$i] ) : $this->emptyLine();
212 $this->writeOutput( "<tr>{$delLine}{$addLine}</tr>\n" );