2013-10-29 22:25:28 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Portions taken from phpwiki-1.3.3.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
|
|
|
* You may copy this code freely under the conditions of the GPL.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
|
*
|
|
|
|
|
|
* @file
|
|
|
|
|
|
* @ingroup DifferenceEngine
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* MediaWiki default table style diff formatter
|
|
|
|
|
|
* @todo document
|
2020-06-30 10:13:50 +00:00
|
|
|
|
* @newable
|
2013-10-29 22:25:28 +00:00
|
|
|
|
* @ingroup DifferenceEngine
|
|
|
|
|
|
*/
|
|
|
|
|
|
class TableDiffFormatter extends DiffFormatter {
|
2014-03-03 17:08:05 +00:00
|
|
|
|
|
2019-11-30 23:03:59 +00:00
|
|
|
|
public function __construct() {
|
2013-10-29 22:25:28 +00:00
|
|
|
|
$this->leadingContextLines = 2;
|
|
|
|
|
|
$this->trailingContextLines = 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* @param string $msg
|
|
|
|
|
|
*
|
2013-10-29 22:25:28 +00:00
|
|
|
|
* @return mixed
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static function escapeWhiteSpace( $msg ) {
|
2016-12-27 21:14:16 +00:00
|
|
|
|
$msg = preg_replace( '/^ /m', "\u{00A0} ", $msg );
|
|
|
|
|
|
$msg = preg_replace( '/ $/m', " \u{00A0}", $msg );
|
|
|
|
|
|
$msg = preg_replace( '/ /', "\u{00A0} ", $msg );
|
2013-11-20 19:11:57 +00:00
|
|
|
|
|
2013-10-29 22:25:28 +00:00
|
|
|
|
return $msg;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* @param int $xbeg
|
|
|
|
|
|
* @param int $xlen
|
|
|
|
|
|
* @param int $ybeg
|
|
|
|
|
|
* @param int $ylen
|
|
|
|
|
|
*
|
2013-10-29 22:25:28 +00:00
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
|
2014-05-28 19:51:54 +00:00
|
|
|
|
// '<!--LINE \d+ -->' get replaced by a localised line number
|
|
|
|
|
|
// in DifferenceEngine::localiseLineNumbers
|
2015-09-26 16:58:43 +00:00
|
|
|
|
$r = '<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l' .
|
|
|
|
|
|
$xbeg .
|
|
|
|
|
|
'" ><!--LINE ' .
|
|
|
|
|
|
$xbeg .
|
|
|
|
|
|
"--></td>\n" .
|
|
|
|
|
|
'<td colspan="2" class="diff-lineno"><!--LINE ' .
|
|
|
|
|
|
$ybeg .
|
|
|
|
|
|
"--></td></tr>\n";
|
2013-11-20 19:11:57 +00:00
|
|
|
|
|
2013-10-29 22:25:28 +00:00
|
|
|
|
return $r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* Writes the header to the output buffer.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $header
|
2013-10-29 22:25:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
protected function startBlock( $header ) {
|
2015-10-09 03:17:50 +00:00
|
|
|
|
$this->writeOutput( $header );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function endBlock() {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-03-03 17:08:05 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @param string[] $lines
|
|
|
|
|
|
* @param string $prefix
|
|
|
|
|
|
* @param string $color
|
|
|
|
|
|
*/
|
2013-10-29 22:25:28 +00:00
|
|
|
|
protected function lines( $lines, $prefix = ' ', $color = 'white' ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* HTML-escape parameter before calling this
|
2014-03-03 17:08:05 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param string $line
|
|
|
|
|
|
*
|
2013-10-29 22:25:28 +00:00
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function addedLine( $line ) {
|
|
|
|
|
|
return $this->wrapLine( '+', 'diff-addedline', $line );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* HTML-escape parameter before calling this
|
2014-03-03 17:08:05 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param string $line
|
|
|
|
|
|
*
|
2013-10-29 22:25:28 +00:00
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function deletedLine( $line ) {
|
|
|
|
|
|
return $this->wrapLine( '−', 'diff-deletedline', $line );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* HTML-escape parameter before calling this
|
2014-03-03 17:08:05 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param string $line
|
|
|
|
|
|
*
|
2013-10-29 22:25:28 +00:00
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function contextLine( $line ) {
|
2016-12-27 21:14:16 +00:00
|
|
|
|
return $this->wrapLine( "\u{00A0}", 'diff-context', $line );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* @param string $marker
|
|
|
|
|
|
* @param string $class Unused
|
|
|
|
|
|
* @param string $line
|
|
|
|
|
|
*
|
2013-10-29 22:25:28 +00:00
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function wrapLine( $marker, $class, $line ) {
|
|
|
|
|
|
if ( $line !== '' ) {
|
|
|
|
|
|
// The <div> wrapper is needed for 'overflow: auto' style to scroll properly
|
|
|
|
|
|
$line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
|
|
|
|
|
|
}
|
2013-11-20 19:11:57 +00:00
|
|
|
|
|
2013-10-29 22:25:28 +00:00
|
|
|
|
return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function emptyLine() {
|
2016-12-27 21:14:16 +00:00
|
|
|
|
return "<td colspan=\"2\">\u{00A0}</td>";
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* Writes all lines to the output buffer, each enclosed in <tr>.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string[] $lines
|
2013-10-29 22:25:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
protected function added( $lines ) {
|
|
|
|
|
|
foreach ( $lines as $line ) {
|
2015-10-09 03:17:50 +00:00
|
|
|
|
$this->writeOutput( '<tr>' . $this->emptyLine() .
|
2013-10-29 22:25:28 +00:00
|
|
|
|
$this->addedLine( '<ins class="diffchange">' .
|
2015-10-09 03:17:50 +00:00
|
|
|
|
htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n" );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* Writes all lines to the output buffer, each enclosed in <tr>.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string[] $lines
|
2013-10-29 22:25:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
protected function deleted( $lines ) {
|
|
|
|
|
|
foreach ( $lines as $line ) {
|
2015-10-09 03:17:50 +00:00
|
|
|
|
$this->writeOutput( '<tr>' . $this->deletedLine( '<del class="diffchange">' .
|
2013-10-29 22:25:28 +00:00
|
|
|
|
htmlspecialchars( $line ) . '</del>' ) .
|
2015-10-09 03:17:50 +00:00
|
|
|
|
$this->emptyLine() . "</tr>\n" );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* Writes all lines to the output buffer, each enclosed in <tr>.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string[] $lines
|
2013-10-29 22:25:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
protected function context( $lines ) {
|
|
|
|
|
|
foreach ( $lines as $line ) {
|
2015-10-09 03:17:50 +00:00
|
|
|
|
$this->writeOutput( '<tr>' .
|
2013-10-29 22:25:28 +00:00
|
|
|
|
$this->contextLine( htmlspecialchars( $line ) ) .
|
2015-10-09 03:17:50 +00:00
|
|
|
|
$this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n" );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* Writes the two sets of lines to the output buffer, each enclosed in <tr>.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string[] $orig
|
|
|
|
|
|
* @param string[] $closing
|
2013-10-29 22:25:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
protected function changed( $orig, $closing ) {
|
|
|
|
|
|
$diff = new WordLevelDiff( $orig, $closing );
|
|
|
|
|
|
$del = $diff->orig();
|
|
|
|
|
|
$add = $diff->closing();
|
|
|
|
|
|
|
|
|
|
|
|
# Notice that WordLevelDiff returns HTML-escaped output.
|
|
|
|
|
|
# Hence, we will be calling addedLine/deletedLine without HTML-escaping.
|
|
|
|
|
|
|
2015-10-09 19:48:50 +00:00
|
|
|
|
$ndel = count( $del );
|
|
|
|
|
|
$nadd = count( $add );
|
|
|
|
|
|
$n = max( $ndel, $nadd );
|
|
|
|
|
|
for ( $i = 0; $i < $n; $i++ ) {
|
|
|
|
|
|
$delLine = $i < $ndel ? $this->deletedLine( $del[$i] ) : $this->emptyLine();
|
|
|
|
|
|
$addLine = $i < $nadd ? $this->addedLine( $add[$i] ) : $this->emptyLine();
|
|
|
|
|
|
$this->writeOutput( "<tr>{$delLine}{$addLine}</tr>\n" );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-03-03 17:08:05 +00:00
|
|
|
|
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|