26use Wikimedia\Assert\Assert;
70 return $this->engine === self::ENGINE_WIKIDIFF2_INLINE ? [
71 phpversion(
'wikidiff2' ),
'inline'
81 public static function diff( $oldText, $newText ) {
83 $slotDiffRenderer = MediaWikiServices::getInstance()
84 ->getContentHandlerFactory()
86 ->getSlotDiffRenderer( RequestContext::getMain() );
87 '@phan-var TextSlotDiffRenderer $slotDiffRenderer';
88 return $slotDiffRenderer->getTextDiff( $oldText, $newText );
107 Assert::parameter( in_array(
$type, $engines,
true ),
'$type',
108 'must be one of the TextSlotDiffRenderer::ENGINE_* constants' );
109 if (
$type === self::ENGINE_EXTERNAL ) {
110 Assert::parameter( is_string( $executable ) && is_executable( $executable ),
'$executable',
111 'must be a path to a valid executable' );
113 Assert::parameter( $executable ===
null,
'$executable',
114 'must not be set unless $type is ENGINE_EXTERNAL' );
116 $this->engine =
$type;
117 $this->externalEngine = $executable;
124 $oldText = $oldContent->serialize();
125 $newText = $newContent->serialize();
137 Assert::parameterType(
'string', $oldText,
'$oldText' );
138 Assert::parameterType(
'string', $newText,
'$newText' );
140 $diff =
function () use ( $oldText, $newText ) {
141 $time = microtime(
true );
145 $time = intval( ( microtime(
true ) - $time ) * 1000 );
146 if ( $this->statsdDataFactory ) {
147 $this->statsdDataFactory->timing(
'diff_time', $time );
166 $error =
function ( $status ) {
167 throw new FatalError( $status->getWikiText() );
171 if ( strlen( $oldText ) + strlen( $newText ) > 20000 ) {
173 md5( $oldText ) . md5( $newText ),
174 [
'doWork' => $diff,
'error' => $error ]
176 return $work->execute();
194 $oldText = str_replace(
"\r\n",
"\n", $oldText );
195 $newText = str_replace(
"\r\n",
"\n", $newText );
199 if ( $this->engine === self::ENGINE_WIKIDIFF2 ) {
200 $wikidiff2Version = phpversion(
'wikidiff2' );
202 $wikidiff2Version !==
false &&
203 version_compare( $wikidiff2Version,
'1.5.0',
'>=' ) &&
204 version_compare( $wikidiff2Version,
'1.8.0',
'<' )
206 $text = wikidiff2_do_diff(
214 $text = wikidiff2_do_diff(
222 } elseif ( $this->engine === self::ENGINE_EXTERNAL ) {
225 $tempName1 = tempnam( $tmpDir,
'diff_' );
226 $tempName2 = tempnam( $tmpDir,
'diff_' );
228 $tempFile1 = fopen( $tempName1,
"w" );
232 $tempFile2 = fopen( $tempName2,
"w" );
236 fwrite( $tempFile1, $oldText );
237 fwrite( $tempFile2, $newText );
238 fclose( $tempFile1 );
239 fclose( $tempFile2 );
241 $result = Shell::command( $cmd )
243 $exitCode = $result->getExitCode();
244 if ( $exitCode !== 0 ) {
245 throw new Exception(
"External diff command returned code {$exitCode}. Stderr: "
249 $difftext = $result->getStdout();
250 unlink( $tempName1 );
251 unlink( $tempName2 );
254 } elseif ( $this->engine === self::ENGINE_PHP ) {
255 if ( $this->language ) {
256 $oldText = $this->language->segmentForDiff( $oldText );
257 $newText = $this->language->segmentForDiff( $newText );
259 $ota = explode(
"\n", $oldText );
260 $nta = explode(
"\n", $newText );
261 $diffs =
new Diff( $ota, $nta );
263 $difftext = $formatter->format( $diffs );
264 if ( $this->language ) {
265 $difftext = $this->language->unsegmentForDiff( $difftext );
269 } elseif ( $this->engine === self::ENGINE_WIKIDIFF2_INLINE ) {
274 return '<tr><td colspan="4">' . wikidiff2_inline_diff( $oldText, $newText, 2 ) .
'</td></tr>';
276 throw new LogicException(
'Invalid engine: ' . $this->engine );
wfTempDir()
Tries to get the system directory for temporary files.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Class representing a 'diff' between two sequences of strings.
Abort the web request with a custom HTML string that will represent the entire response.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Convenience class for dealing with PoolCounters using callbacks.
Renders a diff for a single slot (that is, a diff between two content objects).
normalizeContents(Content &$oldContent=null, Content &$newContent=null, $allowedClasses=null)
Helper method to normalize the input of getDiff().
Renders a slot diff by doing a text diff on the native representation.
setStatsdDataFactory(IBufferingStatsdDataFactory $statsdDataFactory)
Language null $language
The language this content is in.
setEngine( $type, $executable=null)
Set which diff engine to use.
const ENGINE_PHP
Use the PHP diff implementation (DiffEngine).
const ENGINE_EXTERNAL
Use an external executable.
getTextDiffInternal( $oldText, $newText)
Diff the text representations of two content objects (or just two pieces of text in general).
const ENGINE_WIKIDIFF2
Use the wikidiff2 PHP module.
string $externalEngine
Path to an executable to be used as the diff engine.
getDiff(Content $oldContent=null, Content $newContent=null)
Get a diff between two content objects.One of them might be null (meaning a slot was created or remov...
setLanguage(Language $language)
getExtraCacheKeys()
Return any extra keys to split the diff cache by.Stable to override array
IBufferingStatsdDataFactory null $statsdDataFactory
const ENGINE_WIKIDIFF2_INLINE
Use the wikidiff2 PHP module.
static diff( $oldText, $newText)
Convenience helper to use getTextDiff without an instance.
string $engine
One of the ENGINE_* constants.
getTextDiff( $oldText, $newText)
Diff the text representations of two content objects (or just two pieces of text in general).
Base interface for content objects.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.