wiki.techinc.nl/includes/diff/TextDiffer/ExternalTextDiffer.php
Tim Starling 2aa87cdf2c Factor out TextDiffer hierarchy from TextSlotDiffRenderer
* 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
2023-07-19 12:38:18 +10:00

75 lines
1.9 KiB
PHP

<?php
namespace MediaWiki\Diff\TextDiffer;
use MediaWiki\Shell\Shell;
use RuntimeException;
/**
* @since 1.41
*/
class ExternalTextDiffer extends BaseTextDiffer {
/** @var string */
private $externalPath;
/**
* @param string $externalPath The path to the executable file which provides the diff
*/
public function __construct( $externalPath ) {
$this->externalPath = $externalPath;
}
public function getName(): string {
return 'external';
}
public function getFormats(): array {
return [ 'external' ];
}
public function getFormatContext( string $format ) {
return self::CONTEXT_ROW;
}
protected function doRenderBatch( string $oldText, string $newText, array $formats ): array {
return [ 'external' => $this->doRender( $oldText, $newText ) ];
}
/**
* @param string $oldText
* @param string $newText
* @return string
*/
private function doRender( $oldText, $newText ) {
$tmpDir = wfTempDir();
$tempName1 = tempnam( $tmpDir, 'diff_' );
$tempName2 = tempnam( $tmpDir, 'diff_' );
$tempFile1 = fopen( $tempName1, "w" );
if ( !$tempFile1 ) {
throw new RuntimeException( "Could not create temporary file $tempName1 for external diffing" );
}
$tempFile2 = fopen( $tempName2, "w" );
if ( !$tempFile2 ) {
throw new RuntimeException( "Could not create temporary file $tempName2 for external diffing" );
}
fwrite( $tempFile1, $oldText );
fwrite( $tempFile2, $newText );
fclose( $tempFile1 );
fclose( $tempFile2 );
$cmd = [ $this->externalPath, $tempName1, $tempName2 ];
$result = Shell::command( $cmd )
->execute();
$exitCode = $result->getExitCode();
if ( $exitCode !== 0 ) {
throw new RuntimeException( "External diff command returned code {$exitCode}. Stderr: "
. wfEscapeWikiText( $result->getStderr() )
);
}
$diffText = $result->getStdout();
unlink( $tempName1 );
unlink( $tempName2 );
return $diffText;
}
}