Page MenuHomeWickedGov Phorge

ExternalTextDiffer.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

ExternalTextDiffer.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;
}
}

File Metadata

Mime Type
text/x-php
Expires
Sat, Oct 3, 17:24 (1 d, 15 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
4f/0f/a269dcd3bec9f4c41a33ac5952e1
Default Alt Text
ExternalTextDiffer.php (1 KB)

Event Timeline