| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Diff\TextDiffer; |
| 4 |
|
| 5 |
use MediaWiki\Shell\Shell; |
| 6 |
use RuntimeException; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class ExternalTextDiffer extends BaseTextDiffer { |
| 12 |
|
| 13 |
private $externalPath; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public function __construct( $externalPath ) { |
| 19 |
$this->externalPath = $externalPath; |
| 20 |
} |
| 21 |
|
| 22 |
public function getName(): string { |
| 23 |
return 'external'; |
| 24 |
} |
| 25 |
|
| 26 |
public function getFormats(): array { |
| 27 |
return [ 'external' ]; |
| 28 |
} |
| 29 |
|
| 30 |
public function getFormatContext( string $format ) { |
| 31 |
return self::CONTEXT_ROW; |
| 32 |
} |
| 33 |
|
| 34 |
protected function doRenderBatch( string $oldText, string $newText, array $formats ): array { |
| 35 |
return [ 'external' => $this->doRender( $oldText, $newText ) ]; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private function doRender( $oldText, $newText ) { |
| 44 |
$tmpDir = wfTempDir(); |
| 45 |
$tempName1 = tempnam( $tmpDir, 'diff_' ); |
| 46 |
$tempName2 = tempnam( $tmpDir, 'diff_' ); |
| 47 |
|
| 48 |
$tempFile1 = fopen( $tempName1, "w" ); |
| 49 |
if ( !$tempFile1 ) { |
| 50 |
throw new RuntimeException( "Could not create temporary file $tempName1 for external diffing" ); |
| 51 |
} |
| 52 |
$tempFile2 = fopen( $tempName2, "w" ); |
| 53 |
if ( !$tempFile2 ) { |
| 54 |
throw new RuntimeException( "Could not create temporary file $tempName2 for external diffing" ); |
| 55 |
} |
| 56 |
fwrite( $tempFile1, $oldText ); |
| 57 |
fwrite( $tempFile2, $newText ); |
| 58 |
fclose( $tempFile1 ); |
| 59 |
fclose( $tempFile2 ); |
| 60 |
$cmd = [ $this->externalPath, $tempName1, $tempName2 ]; |
| 61 |
$result = Shell::command( $cmd ) |
| 62 |
->execute(); |
| 63 |
$exitCode = $result->getExitCode(); |
| 64 |
if ( $exitCode !== 0 ) { |
| 65 |
throw new RuntimeException( "External diff command returned code {$exitCode}. Stderr: " |
| 66 |
. wfEscapeWikiText( $result->getStderr() ) |
| 67 |
); |
| 68 |
} |
| 69 |
$diffText = $result->getStdout(); |
| 70 |
unlink( $tempName1 ); |
| 71 |
unlink( $tempName2 ); |
| 72 |
|
| 73 |
return $diffText; |
| 74 |
} |
| 75 |
} |