Add unified format

The code is there, so we may as well expose it.

Change-Id: Ic4cf5c0d4a5b46f0f0f25454815dcea1f69970eb
This commit is contained in:
Tim Starling 2023-07-06 15:38:16 +10:00
parent 3f948a0335
commit 191ec35340
6 changed files with 40 additions and 9 deletions

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Diff\TextDiffer;
use MediaWiki\Html\Html;
use MessageLocalizer;
use OutputPage;
@ -40,8 +41,13 @@ abstract class BaseTextDiffer implements TextDiffer {
}
public function addRowWrapper( string $format, string $diffText ): string {
if ( $this->getFormatContext( $format ) === self::CONTEXT_PLAIN ) {
$context = $this->getFormatContext( $format );
if ( $context === self::CONTEXT_PLAIN ) {
return "<tr><td colspan=\"4\">$diffText</td></tr>";
} elseif ( $context === self::CONTEXT_PRE ) {
return '<tr><td colspan="4">' .
Html::element( 'pre', [], $diffText ) .
'</td></tr>';
} else {
return $diffText;
}

View file

@ -5,6 +5,7 @@ namespace MediaWiki\Diff\TextDiffer;
use Language;
use Wikimedia\Diff\Diff;
use Wikimedia\Diff\TableDiffFormatter;
use Wikimedia\Diff\UnifiedDiffFormatter;
/**
* @since 1.41
@ -22,11 +23,11 @@ class PhpTextDiffer extends BaseTextDiffer {
}
public function getFormats(): array {
return [ 'table' ];
return [ 'table', 'unified' ];
}
public function getFormatContext( string $format ) {
return $format === 'table' ? self::CONTEXT_ROW : self::CONTEXT_PLAIN;
return $format === 'table' ? self::CONTEXT_ROW : self::CONTEXT_PRE;
}
protected function doRenderBatch( string $oldText, string $newText, array $formats ): array {
@ -40,8 +41,12 @@ class PhpTextDiffer extends BaseTextDiffer {
$diff = new Diff( $oldLines, $newLines );
$result = [];
foreach ( $formats as $format ) {
// @phan-suppress-next-line PhanNoopSwitchCases -- rectified in followup commit
switch ( $format ) {
case 'unified':
$formatter = new UnifiedDiffFormatter();
$diffText = $formatter->format( $diff );
break;
default: // 'table':
$formatter = new TableDiffFormatter();
$diffText = $formatter->format( $diff );

View file

@ -20,6 +20,11 @@ interface TextDiffer {
*/
public const CONTEXT_ROW = 'row';
/**
* The return value is plain text and should be wrapped in a <pre>
*/
public const CONTEXT_PRE = 'pre';
/**
* Get a stable unique name to identify this differ in a cache key.
*/

View file

@ -24,9 +24,9 @@ class ManifoldTextDifferTest extends MediaWikiIntegrationTestCase {
public function testGetFormats() {
if ( extension_loaded( 'wikidiff2' ) ) {
$formats = [ 'table', 'inline' ];
$formats = [ 'table', 'inline', 'unified' ];
} else {
$formats = [ 'table' ];
$formats = [ 'table', 'unified' ];
}
$this->assertSame(
$formats,
@ -85,12 +85,14 @@ class ManifoldTextDifferTest extends MediaWikiIntegrationTestCase {
$differ = $this->createDiffer( [
'ExternalDiffEngine' => __DIR__ . '/externalDiffTest.sh'
] );
$result = $differ->renderBatch( 'foo', 'bar', [ 'table', 'inline', 'external' ] );
$result = $differ->renderBatch( 'foo', 'bar',
[ 'table', 'inline', 'external', 'unified' ] );
$this->assertSame(
[
'table' => TextDifferData::WIKIDIFF2_TABLE,
'inline' => TextDifferData::WIKIDIFF2_INLINE,
'external' => TextDifferData::EXTERNAL
'external' => TextDifferData::EXTERNAL,
'unified' => TextDifferData::PHP_UNIFIED
],
$result
);
@ -100,6 +102,7 @@ class ManifoldTextDifferTest extends MediaWikiIntegrationTestCase {
return [
[ 'table', false ],
[ 'external', false ],
[ 'unified', true ]
];
}
@ -114,7 +117,7 @@ class ManifoldTextDifferTest extends MediaWikiIntegrationTestCase {
] );
$result = $differ->addRowWrapper( $format, 'foo' );
if ( $isWrap ) {
$this->assertSame( '<tr><td colspan="4">foo</td></tr>', $result );
$this->assertSame( '<tr><td colspan="4"><pre>foo</pre></td></tr>', $result );
} else {
$this->assertSame( 'foo', $result );
}

View file

@ -39,6 +39,13 @@ class PhpTextDifferTest extends MediaWikiIntegrationTestCase {
'table' => TextDifferData::PHP_TABLE,
]
],
'multiple formats' => [
[ 'table', 'unified' ],
[
'table' => TextDifferData::PHP_TABLE,
'unified' => TextDifferData::PHP_UNIFIED,
]
],
];
}

View file

@ -8,6 +8,11 @@ class TextDifferData {
public const PHP_TABLE = '<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1"><!--LINE 1--></td>
<td colspan="2" class="diff-lineno"><!--LINE 1--></td></tr>
<tr><td class="diff-marker" data-marker=""></td><td class="diff-deletedline diff-side-deleted"><div><del class="diffchange diffchange-inline">foo</del></div></td><td class="diff-marker" data-marker="+"></td><td class="diff-addedline diff-side-added"><div><ins class="diffchange diffchange-inline">bar</ins></div></td></tr>
';
public const PHP_UNIFIED = '@@ -1,1 +1,1 @@
-foo
+bar
';
public const WIKIDIFF2_TABLE = '<tr>