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
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2023-06-20 04:02:04 +00:00
|
|
|
|
namespace Wikimedia\Diff;
|
|
|
|
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2023-02-16 19:27:21 +00:00
|
|
|
|
|
2013-10-29 22:25:28 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* 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
|
|
|
|
|
2021-06-23 13:55:13 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Constants for diff sides. Note: these are also used for context lines.
|
|
|
|
|
|
*/
|
2021-09-16 13:47:16 +00:00
|
|
|
|
private const SIDE_DELETED = 'deleted';
|
|
|
|
|
|
private const SIDE_ADDED = 'added';
|
2021-06-23 13:55:13 +00:00
|
|
|
|
private const SIDE_CLASSES = [
|
2021-09-16 13:47:16 +00:00
|
|
|
|
self::SIDE_DELETED => 'diff-side-deleted',
|
|
|
|
|
|
self::SIDE_ADDED => 'diff-side-added'
|
2021-06-23 13:55:13 +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
|
|
|
|
|
|
*
|
2020-11-21 01:11:54 +00:00
|
|
|
|
* @return string
|
2013-10-29 22:25:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
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
|
2023-07-19 10:17:08 +00:00
|
|
|
|
// in BaseTextDiffer::localizeLineNumbers
|
2023-06-20 04:16:09 +00:00
|
|
|
|
return $this->rawElement(
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'tr',
|
|
|
|
|
|
[],
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$this->rawElement(
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'td',
|
2022-12-15 00:21:58 +00:00
|
|
|
|
[ 'colspan' => '2', 'class' => 'diff-lineno', 'id' => 'mw-diff-left-l' . $xbeg ],
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'<!--LINE ' . $xbeg . '-->'
|
|
|
|
|
|
) .
|
|
|
|
|
|
"\n" .
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$this->rawElement(
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'td',
|
2022-12-15 00:21:58 +00:00
|
|
|
|
[ 'colspan' => '2', 'class' => 'diff-lineno' ],
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'<!--LINE ' . $ybeg . '-->'
|
|
|
|
|
|
)
|
|
|
|
|
|
) . "\n";
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-21 01:11:54 +00:00
|
|
|
|
/** @inheritDoc */
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-21 01:11:54 +00:00
|
|
|
|
/** @inheritDoc */
|
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 ) {
|
2021-09-16 13:47:16 +00:00
|
|
|
|
return $this->wrapLine( '+', [ 'diff-addedline', $this->getClassForSide( self::SIDE_ADDED ) ], $line );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 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 ) {
|
2021-09-16 13:47:16 +00:00
|
|
|
|
return $this->wrapLine( '−', [ 'diff-deletedline', $this->getClassForSide( self::SIDE_DELETED ) ], $line );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* HTML-escape parameter before calling this
|
2014-03-03 17:08:05 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param string $line
|
2021-09-16 13:47:16 +00:00
|
|
|
|
* @param string $side self::SIDE_DELETED or self::SIDE_ADDED
|
2014-03-03 17:08:05 +00:00
|
|
|
|
*
|
2013-10-29 22:25:28 +00:00
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
2021-06-23 13:55:13 +00:00
|
|
|
|
protected function contextLine( $line, string $side ) {
|
|
|
|
|
|
return $this->wrapLine( '', [ 'diff-context', $this->getClassForSide( $side ) ], $line );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* @param string $marker
|
2021-06-23 13:55:13 +00:00
|
|
|
|
* @param string|string[] $class A single class or a list of classes
|
2014-03-03 17:08:05 +00:00
|
|
|
|
* @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
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$line = $this->rawElement( 'div', [], $this->escapeWhiteSpace( $line ) );
|
2020-11-29 20:46:02 +00:00
|
|
|
|
} else {
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$line = '<br>';
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
2013-11-20 19:11:57 +00:00
|
|
|
|
|
2020-11-29 20:46:02 +00:00
|
|
|
|
$markerAttrs = [ 'class' => 'diff-marker' ];
|
|
|
|
|
|
if ( $marker ) {
|
|
|
|
|
|
$markerAttrs['data-marker'] = $marker;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-20 04:16:09 +00:00
|
|
|
|
if ( is_array( $class ) ) {
|
|
|
|
|
|
$class = implode( ' ', $class );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->element( 'td', $markerAttrs ) .
|
|
|
|
|
|
$this->rawElement( 'td', [ 'class' => $class ], $line );
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-16 13:47:16 +00:00
|
|
|
|
* @param string $side self::SIDE_DELETED or self::SIDE_ADDED
|
2013-10-29 22:25:28 +00:00
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
2021-08-12 14:45:04 +00:00
|
|
|
|
protected function emptyLine( string $side ) {
|
2023-06-20 04:16:09 +00:00
|
|
|
|
return $this->element( 'td', [ 'colspan' => '2', 'class' => $this->getClassForSide( $side ) ] );
|
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 ) {
|
2020-12-28 18:15:11 +00:00
|
|
|
|
$this->writeOutput(
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$this->rawElement(
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'tr',
|
|
|
|
|
|
[],
|
2021-09-16 13:47:16 +00:00
|
|
|
|
$this->emptyLine( self::SIDE_DELETED ) .
|
2020-12-28 18:15:11 +00:00
|
|
|
|
$this->addedLine(
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$this->element(
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'ins',
|
|
|
|
|
|
[ 'class' => 'diffchange' ],
|
|
|
|
|
|
$line
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
) .
|
|
|
|
|
|
"\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 ) {
|
2020-12-28 18:15:11 +00:00
|
|
|
|
$this->writeOutput(
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$this->rawElement(
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'tr',
|
|
|
|
|
|
[],
|
|
|
|
|
|
$this->deletedLine(
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$this->element(
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'del',
|
|
|
|
|
|
[ 'class' => 'diffchange' ],
|
|
|
|
|
|
$line
|
|
|
|
|
|
)
|
|
|
|
|
|
) .
|
2021-09-16 13:47:16 +00:00
|
|
|
|
$this->emptyLine( self::SIDE_ADDED )
|
2020-12-28 18:15:11 +00:00
|
|
|
|
) .
|
|
|
|
|
|
"\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 ) {
|
2020-12-28 18:15:11 +00:00
|
|
|
|
$this->writeOutput(
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$this->rawElement(
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'tr',
|
|
|
|
|
|
[],
|
2021-09-16 13:47:16 +00:00
|
|
|
|
$this->contextLine( htmlspecialchars( $line ), self::SIDE_DELETED ) .
|
|
|
|
|
|
$this->contextLine( htmlspecialchars( $line ), self::SIDE_ADDED )
|
2020-12-28 18:15:11 +00:00
|
|
|
|
) .
|
|
|
|
|
|
"\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++ ) {
|
2021-09-16 13:47:16 +00:00
|
|
|
|
$delLine = $i < $ndel ? $this->deletedLine( $del[$i] ) : $this->emptyLine( self::SIDE_DELETED );
|
|
|
|
|
|
$addLine = $i < $nadd ? $this->addedLine( $add[$i] ) : $this->emptyLine( self::SIDE_ADDED );
|
2020-12-28 18:15:11 +00:00
|
|
|
|
$this->writeOutput(
|
2023-06-20 04:16:09 +00:00
|
|
|
|
$this->rawElement(
|
2020-12-28 18:15:11 +00:00
|
|
|
|
'tr',
|
|
|
|
|
|
[],
|
|
|
|
|
|
$delLine . $addLine
|
|
|
|
|
|
) .
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
);
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-03-03 17:08:05 +00:00
|
|
|
|
|
2021-06-23 13:55:13 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Get a class for the given diff side, or throw if the side is invalid.
|
|
|
|
|
|
*
|
2021-09-16 13:47:16 +00:00
|
|
|
|
* @param string $side self::SIDE_DELETED or self::SIDE_ADDED
|
2021-06-23 13:55:13 +00:00
|
|
|
|
* @return string
|
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function getClassForSide( string $side ): string {
|
|
|
|
|
|
if ( !isset( self::SIDE_CLASSES[$side] ) ) {
|
|
|
|
|
|
throw new InvalidArgumentException( "Invalid diff side: $side" );
|
|
|
|
|
|
}
|
|
|
|
|
|
return self::SIDE_CLASSES[$side];
|
|
|
|
|
|
}
|
2023-06-20 04:16:09 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Serialize an HTML element, with raw contents.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $element
|
|
|
|
|
|
* @param string[] $attribs
|
|
|
|
|
|
* @param string $contents The HTML element contents
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function rawElement( $element, $attribs = [], $contents = '' ) {
|
|
|
|
|
|
$ret = "<$element";
|
|
|
|
|
|
foreach ( $attribs as $name => $value ) {
|
|
|
|
|
|
$ret .= " $name=\"" . htmlspecialchars( $value, ENT_QUOTES ) . '"';
|
|
|
|
|
|
}
|
|
|
|
|
|
$ret .= ">$contents</$element>";
|
|
|
|
|
|
return $ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Serialize an HTML element, encoding the text contents.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $element
|
|
|
|
|
|
* @param string[] $attribs
|
|
|
|
|
|
* @param string $contents The text contents
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function element( $element, $attribs = [], $contents = '' ) {
|
|
|
|
|
|
return $this->rawElement( $element, $attribs, htmlspecialchars( $contents, ENT_NOQUOTES ) );
|
|
|
|
|
|
}
|
2013-10-29 22:25:28 +00:00
|
|
|
|
}
|
2023-06-20 04:02:04 +00:00
|
|
|
|
|
2024-03-07 21:56:58 +00:00
|
|
|
|
/** @deprecated class alias since 1.41 */
|
2023-06-20 04:02:04 +00:00
|
|
|
|
class_alias( TableDiffFormatter::class, 'TableDiffFormatter' );
|