* Follow the TODO comment in TextSlotDiffRenderer ::getTextDiffInternal() by moving the code out to three parallel implementations, namely ExternalTextDiffer, PhpTextDiffer and Wikidiff2TextDiffer. * Add a container/factory class ManifoldTextDiffer to glue them together and collate available formats. * Move the inline legend to Wikidiff2TextDiffer. Not the toggle since the ability to toggle depends on the available format, not the current format. * Update the diff cache keys so that ManifoldTextDiffer can store the engine=>format map it used to generate the diff. * Drop support for the second parameter to TextSlotDiffRenderer ::setEngine(), since nothing used it anymore. * Provide a format batch API, since some engines are able to efficiently generate multiple formats. This might be used by DifferenceEngine in future. Needs risky change notification for the cache key change. Bug: T339184 Depends-On: I8a35b9b8ec1622c9a36d2496bdd24f51bc52c85f Change-Id: I5c506e39162855aff53dd420dd8145156739059c
62 lines
1.6 KiB
PHP
62 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Diff\TextDiffer;
|
|
|
|
use Language;
|
|
use Wikimedia\Diff\Diff;
|
|
use Wikimedia\Diff\TableDiffFormatter;
|
|
|
|
/**
|
|
* @since 1.41
|
|
*/
|
|
class PhpTextDiffer extends BaseTextDiffer {
|
|
/** @var Language|null */
|
|
private $contentLanguage;
|
|
|
|
public function __construct( ?Language $contentLanguage ) {
|
|
$this->contentLanguage = $contentLanguage;
|
|
}
|
|
|
|
public function getName(): string {
|
|
return 'php';
|
|
}
|
|
|
|
public function getFormats(): array {
|
|
return [ 'table' ];
|
|
}
|
|
|
|
public function getFormatContext( string $format ) {
|
|
return $format === 'table' ? self::CONTEXT_ROW : self::CONTEXT_PLAIN;
|
|
}
|
|
|
|
protected function doRenderBatch( string $oldText, string $newText, array $formats ): array {
|
|
$language = $this->contentLanguage;
|
|
if ( $language ) {
|
|
$oldText = $language->segmentForDiff( $oldText );
|
|
$newText = $language->segmentForDiff( $newText );
|
|
}
|
|
$oldLines = explode( "\n", $oldText );
|
|
$newLines = explode( "\n", $newText );
|
|
$diff = new Diff( $oldLines, $newLines );
|
|
$result = [];
|
|
foreach ( $formats as $format ) {
|
|
// @phan-suppress-next-line PhanNoopSwitchCases -- rectified in followup commit
|
|
switch ( $format ) {
|
|
default: // 'table':
|
|
$formatter = new TableDiffFormatter();
|
|
$diffText = $formatter->format( $diff );
|
|
break;
|
|
}
|
|
if ( $language ) {
|
|
$diffText = $language->unsegmentForDiff( $diffText );
|
|
}
|
|
$result[$format] = $diffText;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function localize( string $format, string $diff, array $options = [] ): string {
|
|
return $this->localizeLineNumbers( $diff, $options['reducedLineNumbers'] ?? false );
|
|
}
|
|
}
|