26 use Wikimedia\Assert\Assert;
67 return $this->engine === self::ENGINE_WIKIDIFF2_INLINE ? [
68 phpversion(
'wikidiff2' ),
'inline'
78 public static function diff( $oldText, $newText ) {
80 $slotDiffRenderer = MediaWikiServices::getInstance()
81 ->getContentHandlerFactory()
84 '@phan-var TextSlotDiffRenderer $slotDiffRenderer';
85 return $slotDiffRenderer->getTextDiff( $oldText, $newText );
110 Assert::parameter( in_array(
$type, $engines,
true ),
'$type',
111 'must be one of the TextSlotDiffRenderer::ENGINE_* constants' );
112 if (
$type === self::ENGINE_EXTERNAL ) {
113 Assert::parameter( is_string( $executable ) && is_executable( $executable ),
'$executable',
114 'must be a path to a valid executable' );
116 Assert::parameter( $executable ===
null,
'$executable',
117 'must not be set unless $type is ENGINE_EXTERNAL' );
119 $this->engine =
$type;
120 $this->externalEngine = $executable;
127 $oldText = $oldContent->serialize();
128 $newText = $newContent->serialize();
140 Assert::parameterType(
'string', $oldText,
'$oldText' );
141 Assert::parameterType(
'string', $newText,
'$newText' );
143 $diff =
function () use ( $oldText, $newText ) {
144 $time = microtime(
true );
148 $time = intval( ( microtime(
true ) - $time ) * 1000 );
149 if ( $this->statsdDataFactory ) {
150 $this->statsdDataFactory->timing(
'diff_time', $time );
169 $error =
static function ( $status ) {
170 throw new FatalError( $status->getWikiText() );
174 if ( strlen( $oldText ) + strlen( $newText ) > 20000 ) {
176 md5( $oldText ) . md5( $newText ),
177 [
'doWork' => $diff,
'error' => $error ]
179 return $work->execute();
197 $oldText = str_replace(
"\r\n",
"\n", $oldText );
198 $newText = str_replace(
"\r\n",
"\n", $newText );
202 if ( $this->engine === self::ENGINE_WIKIDIFF2 ) {
203 $wikidiff2Version = phpversion(
'wikidiff2' );
205 $wikidiff2Version !==
false &&
206 version_compare( $wikidiff2Version,
'1.5.0',
'>=' ) &&
207 version_compare( $wikidiff2Version,
'1.8.0',
'<' )
209 $text = wikidiff2_do_diff(
217 $text = wikidiff2_do_diff(
225 } elseif ( $this->engine === self::ENGINE_EXTERNAL ) {
228 $tempName1 = tempnam( $tmpDir,
'diff_' );
229 $tempName2 = tempnam( $tmpDir,
'diff_' );
231 $tempFile1 = fopen( $tempName1,
"w" );
233 throw new Exception(
"Could not create temporary file $tempName1 for external diffing" );
235 $tempFile2 = fopen( $tempName2,
"w" );
237 throw new Exception(
"Could not create temporary file $tempName2 for external diffing" );
239 fwrite( $tempFile1, $oldText );
240 fwrite( $tempFile2, $newText );
241 fclose( $tempFile1 );
242 fclose( $tempFile2 );
244 $result = Shell::command( $cmd )
246 $exitCode = $result->getExitCode();
247 if ( $exitCode !== 0 ) {
248 throw new Exception(
"External diff command returned code {$exitCode}. Stderr: "
252 $difftext = $result->getStdout();
253 unlink( $tempName1 );
254 unlink( $tempName2 );
257 } elseif ( $this->engine === self::ENGINE_PHP ) {
258 if ( $this->language ) {
259 $oldText = $this->language->segmentForDiff( $oldText );
260 $newText = $this->language->segmentForDiff( $newText );
262 $ota = explode(
"\n", $oldText );
263 $nta = explode(
"\n", $newText );
264 $diffs =
new Diff( $ota, $nta );
266 $difftext = $formatter->format( $diffs );
267 if ( $this->language ) {
268 $difftext = $this->language->unsegmentForDiff( $difftext );
272 } elseif ( $this->engine === self::ENGINE_WIKIDIFF2_INLINE ) {
277 return '<tr><td colspan="4">' . wikidiff2_inline_diff( $oldText, $newText, 2 ) .
'</td></tr>';
279 throw new LogicException(
'Invalid engine: ' . $this->engine );