1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
<?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;
}
}
|