MediaWiki  master
TextSlotDiffRenderer.php
Go to the documentation of this file.
1 <?php
26 use 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 
54 
56  private $language;
57 
60 
62  private $externalEngine;
63 
68  public function getExtraCacheKeys() {
69  // Tell DifferenceEngine this is a different variant from the standard wikidiff2 variant
70  return $this->engine === self::ENGINE_WIKIDIFF2_INLINE ? [
71  phpversion( 'wikidiff2' ), 'inline'
72  ] : [];
73  }
74 
81  public static function diff( $oldText, $newText ) {
83  $slotDiffRenderer = MediaWikiServices::getInstance()
84  ->getContentHandlerFactory()
85  ->getContentHandler( CONTENT_MODEL_TEXT )
86  ->getSlotDiffRenderer( RequestContext::getMain() );
87  '@phan-var TextSlotDiffRenderer $slotDiffRenderer';
88  return $slotDiffRenderer->getTextDiff( $oldText, $newText );
89  }
90 
92  $this->statsdDataFactory = $statsdDataFactory;
93  }
94 
95  public function setLanguage( Language $language ) {
96  $this->language = $language;
97  }
98 
104  public function setEngine( $type, $executable = null ) {
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' );
112  } else {
113  Assert::parameter( $executable === null, '$executable',
114  'must not be set unless $type is ENGINE_EXTERNAL' );
115  }
116  $this->engine = $type;
117  $this->externalEngine = $executable;
118  }
119 
121  public function getDiff( Content $oldContent = null, Content $newContent = null ) {
122  $this->normalizeContents( $oldContent, $newContent, TextContent::class );
123 
124  $oldText = $oldContent->serialize();
125  $newText = $newContent->serialize();
126 
127  return $this->getTextDiff( $oldText, $newText );
128  }
129 
136  public function getTextDiff( $oldText, $newText ) {
137  Assert::parameterType( 'string', $oldText, '$oldText' );
138  Assert::parameterType( 'string', $newText, '$newText' );
139 
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 
166  $error = function ( $status ) {
167  throw new FatalError( $status->getWikiText() );
168  };
169 
170  // Use PoolCounter if the diff looks like it can be expensive
171  if ( strlen( $oldText ) + strlen( $newText ) > 20000 ) {
172  $work = new PoolCounterWorkViaCallback( 'diff',
173  md5( $oldText ) . md5( $newText ),
174  [ 'doWork' => $diff, 'error' => $error ]
175  );
176  return $work->execute();
177  }
178 
179  return $diff();
180  }
181 
190  protected function getTextDiffInternal( $oldText, $newText ) {
191  // TODO move most of this into three parallel implementations of a text diff generator
192  // class, choose which one to use via dependecy injection
193 
194  $oldText = str_replace( "\r\n", "\n", $oldText );
195  $newText = str_replace( "\r\n", "\n", $newText );
196 
197  // Better external diff engine, the 2 may some day be dropped
198  // This one does the escaping and segmenting itself
199  if ( $this->engine === self::ENGINE_WIKIDIFF2 ) {
200  $wikidiff2Version = phpversion( 'wikidiff2' );
201  if (
202  $wikidiff2Version !== false &&
203  version_compare( $wikidiff2Version, '1.5.0', '>=' ) &&
204  version_compare( $wikidiff2Version, '1.8.0', '<' )
205  ) {
206  $text = wikidiff2_do_diff(
207  $oldText,
208  $newText,
209  2,
210  0
211  );
212  } else {
213  // Don't pass the 4th parameter introduced in version 1.5.0 and removed in version 1.8.0
214  $text = wikidiff2_do_diff(
215  $oldText,
216  $newText,
217  2
218  );
219  }
220 
221  return $text;
222  } elseif ( $this->engine === self::ENGINE_EXTERNAL ) {
223  # Diff via the shell
224  $tmpDir = wfTempDir();
225  $tempName1 = tempnam( $tmpDir, 'diff_' );
226  $tempName2 = tempnam( $tmpDir, 'diff_' );
227 
228  $tempFile1 = fopen( $tempName1, "w" );
229  if ( !$tempFile1 ) {
230  return false;
231  }
232  $tempFile2 = fopen( $tempName2, "w" );
233  if ( !$tempFile2 ) {
234  return false;
235  }
236  fwrite( $tempFile1, $oldText );
237  fwrite( $tempFile2, $newText );
238  fclose( $tempFile1 );
239  fclose( $tempFile2 );
240  $cmd = [ $this->externalEngine, $tempName1, $tempName2 ];
241  $result = Shell::command( $cmd )
242  ->execute();
243  $exitCode = $result->getExitCode();
244  if ( $exitCode !== 0 ) {
245  throw new Exception( "External diff command returned code {$exitCode}. Stderr: "
246  . wfEscapeWikiText( $result->getStderr() )
247  );
248  }
249  $difftext = $result->getStdout();
250  unlink( $tempName1 );
251  unlink( $tempName2 );
252 
253  return $difftext;
254  } elseif ( $this->engine === self::ENGINE_PHP ) {
255  if ( $this->language ) {
256  $oldText = $this->language->segmentForDiff( $oldText );
257  $newText = $this->language->segmentForDiff( $newText );
258  }
259  $ota = explode( "\n", $oldText );
260  $nta = explode( "\n", $newText );
261  $diffs = new Diff( $ota, $nta );
262  $formatter = new TableDiffFormatter();
263  $difftext = $formatter->format( $diffs );
264  if ( $this->language ) {
265  $difftext = $this->language->unsegmentForDiff( $difftext );
266  }
267 
268  return $difftext;
269  } elseif ( $this->engine === self::ENGINE_WIKIDIFF2_INLINE ) {
270  // Note wikidiff2_inline_diff returns an element sans table.
271  // Due to the way other diffs work (return a table with before and after), we need to wrap
272  // the output in a row that spans the 4 columns that are expected, so that our diff appears in
273  // the correct place!
274  return '<tr><td colspan="4">' . wikidiff2_inline_diff( $oldText, $newText, 2 ) . '</td></tr>';
275  }
276  throw new LogicException( 'Invalid engine: ' . $this->engine );
277  }
278 
279 }
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:44
TextSlotDiffRenderer\getExtraCacheKeys
getExtraCacheKeys()
Return any extra keys to split the diff cache by.array
Definition: TextSlotDiffRenderer.php:68
TextSlotDiffRenderer\setLanguage
setLanguage(Language $language)
Definition: TextSlotDiffRenderer.php:95
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
TextSlotDiffRenderer\$statsdDataFactory
IBufferingStatsdDataFactory null $statsdDataFactory
Definition: TextSlotDiffRenderer.php:53
TextSlotDiffRenderer\$engine
string $engine
One of the ENGINE_* constants.
Definition: TextSlotDiffRenderer.php:59
TextSlotDiffRenderer\ENGINE_EXTERNAL
const ENGINE_EXTERNAL
Use an external executable.
Definition: TextSlotDiffRenderer.php:50
TableDiffFormatter
MediaWiki default table style diff formatter.
Definition: TableDiffFormatter.php:33
TextSlotDiffRenderer\ENGINE_WIKIDIFF2
const ENGINE_WIKIDIFF2
Use the wikidiff2 PHP module.
Definition: TextSlotDiffRenderer.php:44
PoolCounterWorkViaCallback
Convenience class for dealing with PoolCounters using callbacks.
Definition: PoolCounterWorkViaCallback.php:28
SlotDiffRenderer\normalizeContents
normalizeContents(Content &$oldContent=null, Content &$newContent=null, $allowedClasses=null)
Helper method to normalize the input of getDiff().
Definition: SlotDiffRenderer.php:75
TextSlotDiffRenderer\getTextDiff
getTextDiff( $oldText, $newText)
Diff the text representations of two content objects (or just two pieces of text in general).
Definition: TextSlotDiffRenderer.php:136
TextSlotDiffRenderer\setStatsdDataFactory
setStatsdDataFactory(IBufferingStatsdDataFactory $statsdDataFactory)
Definition: TextSlotDiffRenderer.php:91
TextSlotDiffRenderer\ENGINE_PHP
const ENGINE_PHP
Use the PHP diff implementation (DiffEngine).
Definition: TextSlotDiffRenderer.php:41
TextSlotDiffRenderer\ENGINE_WIKIDIFF2_INLINE
const ENGINE_WIKIDIFF2_INLINE
Use the wikidiff2 PHP module.
Definition: TextSlotDiffRenderer.php:47
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1485
TextSlotDiffRenderer\$externalEngine
string $externalEngine
Path to an executable to be used as the diff engine.
Definition: TextSlotDiffRenderer.php:62
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:451
Content
Base interface for content objects.
Definition: Content.php:34
IBufferingStatsdDataFactory
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Definition: IBufferingStatsdDataFactory.php:11
wfTempDir
wfTempDir()
Tries to get the system directory for temporary files.
Definition: GlobalFunctions.php:1873
SlotDiffRenderer
Renders a diff for a single slot (that is, a diff between two content objects).
Definition: SlotDiffRenderer.php:39
TextSlotDiffRenderer\getDiff
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...
Definition: TextSlotDiffRenderer.php:121
FatalError
Abort the web request with a custom HTML string that will represent the entire response.
Definition: FatalError.php:35
TextSlotDiffRenderer\$language
Language null $language
The language this content is in.
Definition: TextSlotDiffRenderer.php:56
TextSlotDiffRenderer\diff
static diff( $oldText, $newText)
Convenience helper to use getTextDiff without an instance.
Definition: TextSlotDiffRenderer.php:81
TextSlotDiffRenderer\setEngine
setEngine( $type, $executable=null)
Set which diff engine to use.
Definition: TextSlotDiffRenderer.php:104
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:227
TextSlotDiffRenderer\getTextDiffInternal
getTextDiffInternal( $oldText, $newText)
Diff the text representations of two content objects (or just two pieces of text in general).
Definition: TextSlotDiffRenderer.php:190
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:41
Diff
Class representing a 'diff' between two sequences of strings.
Definition: Diff.php:32
TextSlotDiffRenderer
Renders a slot diff by doing a text diff on the native representation.
Definition: TextSlotDiffRenderer.php:38
$type
$type
Definition: testCompression.php:52