MediaWiki REL1_39
TextSlotDiffRenderer.php
Go to the documentation of this file.
1<?php
26use Wikimedia\Assert\Assert;
27
39
41 public const ENGINE_PHP = 'php';
42
44 public const ENGINE_WIKIDIFF2 = 'wikidiff2';
45
47 public const ENGINE_WIKIDIFF2_INLINE = 'wikidiff2inline';
48
50 public const ENGINE_EXTERNAL = 'external';
51
53 private $statsdDataFactory;
54
56 private $language;
57
59 private $engine = self::ENGINE_PHP;
60
62 private $externalEngine;
63
65 public function getExtraCacheKeys() {
66 // Tell DifferenceEngine this is a different variant from the standard wikidiff2 variant
67 return $this->engine === self::ENGINE_WIKIDIFF2_INLINE ? [
68 phpversion( 'wikidiff2' ), 'inline'
69 ] : [];
70 }
71
78 public static function diff( $oldText, $newText ) {
80 $slotDiffRenderer = MediaWikiServices::getInstance()
81 ->getContentHandlerFactory()
82 ->getContentHandler( CONTENT_MODEL_TEXT )
83 ->getSlotDiffRenderer( RequestContext::getMain() );
84 '@phan-var TextSlotDiffRenderer $slotDiffRenderer';
85 return $slotDiffRenderer->getTextDiff( $oldText, $newText );
86 }
87
91 public function setStatsdDataFactory( IBufferingStatsdDataFactory $statsdDataFactory ) {
92 $this->statsdDataFactory = $statsdDataFactory;
93 }
94
98 public function setLanguage( Language $language ) {
99 $this->language = $language;
100 }
101
107 public function setEngine( $type, $executable = null ) {
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' );
115 } else {
116 Assert::parameter( $executable === null, '$executable',
117 'must not be set unless $type is ENGINE_EXTERNAL' );
118 }
119 $this->engine = $type;
120 $this->externalEngine = $executable;
121 }
122
124 public function getDiff( Content $oldContent = null, Content $newContent = null ) {
125 $this->normalizeContents( $oldContent, $newContent, TextContent::class );
126
127 $oldText = $oldContent->serialize();
128 $newText = $newContent->serialize();
129
130 return $this->getTextDiff( $oldText, $newText );
131 }
132
139 public function getTextDiff( string $oldText, string $newText ) {
140 $diff = function () use ( $oldText, $newText ) {
141 $time = microtime( true );
142
143 $result = $this->getTextDiffInternal( $oldText, $newText );
144
145 $time = intval( ( microtime( true ) - $time ) * 1000 );
146 if ( $this->statsdDataFactory ) {
147 $this->statsdDataFactory->timing( 'diff_time', $time );
148 }
149
150 // TODO reimplement this using T142313
151 /*
152 // Log requests slower than 99th percentile
153 if ( $time > 100 && $this->mOldPage && $this->mNewPage ) {
154 wfDebugLog( 'diff',
155 "$time ms diff: {$this->mOldid} -> {$this->mNewid} {$this->mNewPage}" );
156 }
157 */
158
159 return $result;
160 };
161
167 $error = static function ( $status ) {
168 throw new FatalError( $status->getWikiText() );
169 };
170
171 // Use PoolCounter if the diff looks like it can be expensive
172 if ( strlen( $oldText ) + strlen( $newText ) > 20000 ) {
173 $work = new PoolCounterWorkViaCallback( 'diff',
174 md5( $oldText ) . md5( $newText ),
175 [ 'doWork' => $diff, 'error' => $error ]
176 );
177 return $work->execute();
178 }
179
180 return $diff();
181 }
182
191 protected function getTextDiffInternal( $oldText, $newText ) {
192 // TODO move most of this into three parallel implementations of a text diff generator
193 // class, choose which one to use via dependency injection
194
195 $oldText = str_replace( "\r\n", "\n", $oldText );
196 $newText = str_replace( "\r\n", "\n", $newText );
197
198 // Better external diff engine, the 2 may some day be dropped
199 // This one does the escaping and segmenting itself
200 if ( $this->engine === self::ENGINE_WIKIDIFF2 ) {
201 $wikidiff2Version = phpversion( 'wikidiff2' );
202 if (
203 $wikidiff2Version !== false &&
204 version_compare( $wikidiff2Version, '1.5.0', '>=' ) &&
205 version_compare( $wikidiff2Version, '1.8.0', '<' )
206 ) {
207 $text = wikidiff2_do_diff(
208 $oldText,
209 $newText,
210 2,
211 0
212 );
213 } else {
214 // Don't pass the 4th parameter introduced in version 1.5.0 and removed in version 1.8.0
215 $text = wikidiff2_do_diff(
216 $oldText,
217 $newText,
218 2
219 );
220 }
221
222 return $text;
223 } elseif ( $this->engine === self::ENGINE_EXTERNAL ) {
224 # Diff via the shell
225 $tmpDir = wfTempDir();
226 $tempName1 = tempnam( $tmpDir, 'diff_' );
227 $tempName2 = tempnam( $tmpDir, 'diff_' );
228
229 $tempFile1 = fopen( $tempName1, "w" );
230 if ( !$tempFile1 ) {
231 throw new Exception( "Could not create temporary file $tempName1 for external diffing" );
232 }
233 $tempFile2 = fopen( $tempName2, "w" );
234 if ( !$tempFile2 ) {
235 throw new Exception( "Could not create temporary file $tempName2 for external diffing" );
236 }
237 fwrite( $tempFile1, $oldText );
238 fwrite( $tempFile2, $newText );
239 fclose( $tempFile1 );
240 fclose( $tempFile2 );
241 $cmd = [ $this->externalEngine, $tempName1, $tempName2 ];
242 $result = Shell::command( $cmd )
243 ->execute();
244 $exitCode = $result->getExitCode();
245 if ( $exitCode !== 0 ) {
246 throw new Exception( "External diff command returned code {$exitCode}. Stderr: "
247 . wfEscapeWikiText( $result->getStderr() )
248 );
249 }
250 $difftext = $result->getStdout();
251 unlink( $tempName1 );
252 unlink( $tempName2 );
253
254 return $difftext;
255 } elseif ( $this->engine === self::ENGINE_PHP ) {
256 if ( $this->language ) {
257 $oldText = $this->language->segmentForDiff( $oldText );
258 $newText = $this->language->segmentForDiff( $newText );
259 }
260 $ota = explode( "\n", $oldText );
261 $nta = explode( "\n", $newText );
262 $diffs = new Diff( $ota, $nta );
263 $formatter = new TableDiffFormatter();
264 $difftext = $formatter->format( $diffs );
265 if ( $this->language ) {
266 $difftext = $this->language->unsegmentForDiff( $difftext );
267 }
268
269 return $difftext;
270 } elseif ( $this->engine === self::ENGINE_WIKIDIFF2_INLINE ) {
271 // Note wikidiff2_inline_diff returns an element sans table.
272 // Due to the way other diffs work (return a table with before and after), we need to wrap
273 // the output in a row that spans the 4 columns that are expected, so that our diff appears in
274 // the correct place!
275 return '<tr><td colspan="4">' . wikidiff2_inline_diff( $oldText, $newText, 2 ) . '</td></tr>';
276 }
277 throw new LogicException( 'Invalid engine: ' . $this->engine );
278 }
279
280}
const CONTENT_MODEL_TEXT
Definition Defines.php:214
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.
Definition Diff.php:32
Abort the web request with a custom HTML string that will represent the entire response.
Base class for language-specific code.
Definition Language.php:53
Service locator for MediaWiki core services.
Executes shell commands.
Definition Shell.php:46
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().
MediaWiki default table style diff formatter.
Renders a slot diff by doing a text diff on the native representation.
setStatsdDataFactory(IBufferingStatsdDataFactory $statsdDataFactory)
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.
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.to override string[]
getTextDiff(string $oldText, string $newText)
Diff the text representations of two content objects (or just two pieces of text in general).
const ENGINE_WIKIDIFF2_INLINE
Use the wikidiff2 PHP module.
static diff( $oldText, $newText)
Convenience helper to use getTextDiff without an instance.
Base interface for content objects.
Definition Content.php:35
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.