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 
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 
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( $oldText, $newText ) {
140  Assert::parameterType( 'string', $oldText, '$oldText' );
141  Assert::parameterType( 'string', $newText, '$newText' );
142 
143  $diff = function () use ( $oldText, $newText ) {
144  $time = microtime( true );
145 
146  $result = $this->getTextDiffInternal( $oldText, $newText );
147 
148  $time = intval( ( microtime( true ) - $time ) * 1000 );
149  if ( $this->statsdDataFactory ) {
150  $this->statsdDataFactory->timing( 'diff_time', $time );
151  }
152 
153  // TODO reimplement this using T142313
154  /*
155  // Log requests slower than 99th percentile
156  if ( $time > 100 && $this->mOldPage && $this->mNewPage ) {
157  wfDebugLog( 'diff',
158  "$time ms diff: {$this->mOldid} -> {$this->mNewid} {$this->mNewPage}" );
159  }
160  */
161 
162  return $result;
163  };
164 
169  $error = function ( $status ) {
170  throw new FatalError( $status->getWikiText() );
171  };
172 
173  // Use PoolCounter if the diff looks like it can be expensive
174  if ( strlen( $oldText ) + strlen( $newText ) > 20000 ) {
175  $work = new PoolCounterWorkViaCallback( 'diff',
176  md5( $oldText ) . md5( $newText ),
177  [ 'doWork' => $diff, 'error' => $error ]
178  );
179  return $work->execute();
180  }
181 
182  return $diff();
183  }
184 
193  protected function getTextDiffInternal( $oldText, $newText ) {
194  // TODO move most of this into three parallel implementations of a text diff generator
195  // class, choose which one to use via dependecy injection
196 
197  $oldText = str_replace( "\r\n", "\n", $oldText );
198  $newText = str_replace( "\r\n", "\n", $newText );
199 
200  // Better external diff engine, the 2 may some day be dropped
201  // This one does the escaping and segmenting itself
202  if ( $this->engine === self::ENGINE_WIKIDIFF2 ) {
203  $wikidiff2Version = phpversion( 'wikidiff2' );
204  if (
205  $wikidiff2Version !== false &&
206  version_compare( $wikidiff2Version, '1.5.0', '>=' ) &&
207  version_compare( $wikidiff2Version, '1.8.0', '<' )
208  ) {
209  $text = wikidiff2_do_diff(
210  $oldText,
211  $newText,
212  2,
213  0
214  );
215  } else {
216  // Don't pass the 4th parameter introduced in version 1.5.0 and removed in version 1.8.0
217  $text = wikidiff2_do_diff(
218  $oldText,
219  $newText,
220  2
221  );
222  }
223 
224  return $text;
225  } elseif ( $this->engine === self::ENGINE_EXTERNAL ) {
226  # Diff via the shell
227  $tmpDir = wfTempDir();
228  $tempName1 = tempnam( $tmpDir, 'diff_' );
229  $tempName2 = tempnam( $tmpDir, 'diff_' );
230 
231  $tempFile1 = fopen( $tempName1, "w" );
232  if ( !$tempFile1 ) {
233  return false;
234  }
235  $tempFile2 = fopen( $tempName2, "w" );
236  if ( !$tempFile2 ) {
237  return false;
238  }
239  fwrite( $tempFile1, $oldText );
240  fwrite( $tempFile2, $newText );
241  fclose( $tempFile1 );
242  fclose( $tempFile2 );
243  $cmd = [ $this->externalEngine, $tempName1, $tempName2 ];
244  $result = Shell::command( $cmd )
245  ->execute();
246  $exitCode = $result->getExitCode();
247  if ( $exitCode !== 0 ) {
248  throw new Exception( "External diff command returned code {$exitCode}. Stderr: "
249  . wfEscapeWikiText( $result->getStderr() )
250  );
251  }
252  $difftext = $result->getStdout();
253  unlink( $tempName1 );
254  unlink( $tempName2 );
255 
256  return $difftext;
257  } elseif ( $this->engine === self::ENGINE_PHP ) {
258  if ( $this->language ) {
259  $oldText = $this->language->segmentForDiff( $oldText );
260  $newText = $this->language->segmentForDiff( $newText );
261  }
262  $ota = explode( "\n", $oldText );
263  $nta = explode( "\n", $newText );
264  $diffs = new Diff( $ota, $nta );
265  $formatter = new TableDiffFormatter();
266  $difftext = $formatter->format( $diffs );
267  if ( $this->language ) {
268  $difftext = $this->language->unsegmentForDiff( $difftext );
269  }
270 
271  return $difftext;
272  } elseif ( $this->engine === self::ENGINE_WIKIDIFF2_INLINE ) {
273  // Note wikidiff2_inline_diff returns an element sans table.
274  // Due to the way other diffs work (return a table with before and after), we need to wrap
275  // the output in a row that spans the 4 columns that are expected, so that our diff appears in
276  // the correct place!
277  return '<tr><td colspan="4">' . wikidiff2_inline_diff( $oldText, $newText, 2 ) . '</td></tr>';
278  }
279  throw new LogicException( 'Invalid engine: ' . $this->engine );
280  }
281 
282 }
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:44
TextSlotDiffRenderer\getExtraCacheKeys
getExtraCacheKeys()
Return any extra keys to split the diff cache by.Stable to override string[]
Definition: TextSlotDiffRenderer.php:65
TextSlotDiffRenderer\setLanguage
setLanguage(Language $language)
Definition: TextSlotDiffRenderer.php:98
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:165
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:31
SlotDiffRenderer\normalizeContents
normalizeContents(Content &$oldContent=null, Content &$newContent=null, $allowedClasses=null)
Helper method to normalize the input of getDiff().
Definition: SlotDiffRenderer.php:78
TextSlotDiffRenderer\getTextDiff
getTextDiff( $oldText, $newText)
Diff the text representations of two content objects (or just two pieces of text in general).
Definition: TextSlotDiffRenderer.php:139
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:1494
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:454
Content
Base interface for content objects.
Definition: Content.php:35
IBufferingStatsdDataFactory
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Definition: IBufferingStatsdDataFactory.php:13
wfTempDir
wfTempDir()
Tries to get the system directory for temporary files.
Definition: GlobalFunctions.php:1878
SlotDiffRenderer
Renders a diff for a single slot (that is, a diff between two content objects).
Definition: SlotDiffRenderer.php:40
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:124
FatalError
Abort the web request with a custom HTML string that will represent the entire response.
Definition: FatalError.php:37
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:78
TextSlotDiffRenderer\setEngine
setEngine( $type, $executable=null)
Set which diff engine to use.
Definition: TextSlotDiffRenderer.php:107
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:193
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
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