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;
45 public static function escapeWhiteSpace( $msg ) {
46 $msg = preg_replace( '/^ /m', '  ', $msg );
47 $msg = preg_replace( '/ $/m', '  ', $msg );
48 $msg = preg_replace( '/ /', '  ', $msg );
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' .
69 '<td colspan="2" class="diff-lineno"><!--LINE ' .
77 * Writes the header to the output buffer.
79 * @param string $header
81 protected function startBlock( $header ) {
82 $this->writeOutput( $header );
85 protected function endBlock() {
89 * @param string[] $lines
90 * @param string $prefix
91 * @param string $color
93 protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
97 * HTML-escape parameter before calling this
103 protected function addedLine( $line ) {
104 return $this->wrapLine( '+', 'diff-addedline', $line );
108 * HTML-escape parameter before calling this
110 * @param string $line
114 protected function deletedLine( $line ) {
115 return $this->wrapLine( '−', 'diff-deletedline', $line );
119 * HTML-escape parameter before calling this
121 * @param string $line
125 protected function contextLine( $line ) {
126 return $this->wrapLine( ' ', 'diff-context', $line );
130 * @param string $marker
131 * @param string $class Unused
132 * @param string $line
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 ) );
142 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
148 protected function emptyLine() {
149 return '<td colspan="2"> </td>';
153 * Writes all lines to the output buffer, each enclosed in <tr>.
155 * @param string[] $lines
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" );
166 * Writes all lines to the output buffer, each enclosed in <tr>.
168 * @param string[] $lines
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" );
179 * Writes all lines to the output buffer, each enclosed in <tr>.
181 * @param string[] $lines
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" );
192 * Writes the two sets of lines to the output buffer, each enclosed in <tr>.
194 * @param string[] $orig
195 * @param string[] $closing
197 protected function changed( $orig, $closing ) {
198 $diff = new WordLevelDiff( $orig, $closing );
199 $del = $diff->orig();
200 $add = $diff->closing();
202 # Notice that WordLevelDiff returns HTML-escaped output.
203 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
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" );