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 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
64 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
70 * Writes the header to the output buffer.
72 * @param string $header
74 protected function startBlock( $header ) {
78 protected function endBlock() {
82 * @param string[] $lines
83 * @param string $prefix
84 * @param string $color
86 protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
90 * HTML-escape parameter before calling this
96 protected function addedLine( $line ) {
97 return $this->wrapLine( '+', 'diff-addedline', $line );
101 * HTML-escape parameter before calling this
103 * @param string $line
107 protected function deletedLine( $line ) {
108 return $this->wrapLine( '−', 'diff-deletedline', $line );
112 * HTML-escape parameter before calling this
114 * @param string $line
118 protected function contextLine( $line ) {
119 return $this->wrapLine( ' ', 'diff-context', $line );
123 * @param string $marker
124 * @param string $class Unused
125 * @param string $line
129 protected function wrapLine( $marker, $class, $line ) {
130 if ( $line !== '' ) {
131 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
132 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
135 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
141 protected function emptyLine() {
142 return '<td colspan="2"> </td>';
146 * Writes all lines to the output buffer, each enclosed in <tr>.
148 * @param string[] $lines
150 protected function added( $lines ) {
151 foreach ( $lines as $line ) {
152 echo '<tr>' . $this->emptyLine() .
153 $this->addedLine( '<ins class="diffchange">' .
154 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
159 * Writes all lines to the output buffer, each enclosed in <tr>.
161 * @param string[] $lines
163 protected function deleted( $lines ) {
164 foreach ( $lines as $line ) {
165 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
166 htmlspecialchars( $line ) . '</del>' ) .
167 $this->emptyLine() . "</tr>\n";
172 * Writes all lines to the output buffer, each enclosed in <tr>.
174 * @param string[] $lines
176 protected function context( $lines ) {
177 foreach ( $lines as $line ) {
179 $this->contextLine( htmlspecialchars( $line ) ) .
180 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
185 * Writes the two sets of lines to the output buffer, each enclosed in <tr>.
187 * @param string[] $orig
188 * @param string[] $closing
190 protected function changed( $orig, $closing ) {
191 wfProfileIn( __METHOD__
);
193 $diff = new WordLevelDiff( $orig, $closing );
194 $del = $diff->orig();
195 $add = $diff->closing();
197 # Notice that WordLevelDiff returns HTML-escaped output.
198 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
200 while ( $line = array_shift( $del ) ) {
201 $aline = array_shift( $add );
202 echo '<tr>' . $this->deletedLine( $line ) .
203 $this->addedLine( $aline ) . "</tr>\n";
205 foreach ( $add as $line ) { # If any leftovers
206 echo '<tr>' . $this->emptyLine() .
207 $this->addedLine( $line ) . "</tr>\n";
209 wfProfileOut( __METHOD__
);