3 * Renders a slot diff by doing a text diff on the native representation.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup DifferenceEngine
24 use MediaWiki\Shell\Shell
;
25 use Wikimedia\Assert\Assert
;
28 * Renders a slot diff by doing a text diff on the native representation.
30 * If you want to use this without content objects (to call getTextDiff() on some
31 * non-content-related texts), obtain an instance with
32 * ContentHandler::getForModelID( CONTENT_MODEL_TEXT )
33 * ->getSlotDiffRenderer( RequestContext::getMain() )
35 * @ingroup DifferenceEngine
37 class TextSlotDiffRenderer
extends SlotDiffRenderer
{
39 /** Use the PHP diff implementation (DiffEngine). */
40 const ENGINE_PHP
= 'php';
42 /** Use the wikidiff2 PHP module. */
43 const ENGINE_WIKIDIFF2
= 'wikidiff2';
45 /** Use an external executable. */
46 const ENGINE_EXTERNAL
= 'external';
48 /** @var IBufferingStatsdDataFactory|null */
49 private $statsdDataFactory;
51 /** @var Language|null The language this content is in. */
54 /** @var string One of the ENGINE_* constants. */
55 private $engine = self
::ENGINE_PHP
;
57 /** @var string Path to an executable to be used as the diff engine. */
58 private $externalEngine;
61 * Convenience helper to use getTextDiff without an instance.
62 * @param string $oldText
63 * @param string $newText
66 public static function diff( $oldText, $newText ) {
67 /** @var TextSlotDiffRenderer $slotDiffRenderer */
68 $slotDiffRenderer = ContentHandler
::getForModelID( CONTENT_MODEL_TEXT
)
69 ->getSlotDiffRenderer( RequestContext
::getMain() );
70 '@phan-var TextSlotDiffRenderer $slotDiffRenderer';
71 return $slotDiffRenderer->getTextDiff( $oldText, $newText );
74 public function setStatsdDataFactory( IBufferingStatsdDataFactory
$statsdDataFactory ) {
75 $this->statsdDataFactory
= $statsdDataFactory;
78 public function setLanguage( Language
$language ) {
79 $this->language
= $language;
83 * Set which diff engine to use.
84 * @param string $type One of the ENGINE_* constants.
85 * @param string|null $executable Path to an external exectable, only when type is ENGINE_EXTERNAL.
87 public function setEngine( $type, $executable = null ) {
88 $engines = [ self
::ENGINE_PHP
, self
::ENGINE_WIKIDIFF2
, self
::ENGINE_EXTERNAL
];
89 Assert
::parameter( in_array( $type, $engines, true ), '$type',
90 'must be one of the TextSlotDiffRenderer::ENGINE_* constants' );
91 if ( $type === self
::ENGINE_EXTERNAL
) {
92 Assert
::parameter( is_string( $executable ) && is_executable( $executable ), '$executable',
93 'must be a path to a valid executable' );
95 Assert
::parameter( is_null( $executable ), '$executable',
96 'must not be set unless $type is ENGINE_EXTERNAL' );
98 $this->engine
= $type;
99 $this->externalEngine
= $executable;
103 public function getDiff( Content
$oldContent = null, Content
$newContent = null ) {
104 $this->normalizeContents( $oldContent, $newContent, TextContent
::class );
106 $oldText = $oldContent->serialize();
107 $newText = $newContent->serialize();
109 return $this->getTextDiff( $oldText, $newText );
113 * Diff the text representations of two content objects (or just two pieces of text in general).
114 * @param string $oldText
115 * @param string $newText
116 * @return string HTML, one or more <tr> tags.
118 public function getTextDiff( $oldText, $newText ) {
119 Assert
::parameterType( 'string', $oldText, '$oldText' );
120 Assert
::parameterType( 'string', $newText, '$newText' );
122 $diff = function () use ( $oldText, $newText ) {
123 $time = microtime( true );
125 $result = $this->getTextDiffInternal( $oldText, $newText );
127 $time = intval( ( microtime( true ) - $time ) * 1000 );
128 if ( $this->statsdDataFactory
) {
129 $this->statsdDataFactory
->timing( 'diff_time', $time );
132 // TODO reimplement this using T142313
134 // Log requests slower than 99th percentile
135 if ( $time > 100 && $this->mOldPage && $this->mNewPage ) {
137 "$time ms diff: {$this->mOldid} -> {$this->mNewid} {$this->mNewPage}" );
145 * @param Status $status
148 $error = function ( $status ) {
149 throw new FatalError( $status->getWikiText() );
152 // Use PoolCounter if the diff looks like it can be expensive
153 if ( strlen( $oldText ) +
strlen( $newText ) > 20000 ) {
154 $work = new PoolCounterWorkViaCallback( 'diff',
155 md5( $oldText ) . md5( $newText ),
156 [ 'doWork' => $diff, 'error' => $error ]
158 return $work->execute();
165 * Diff the text representations of two content objects (or just two pieces of text in general).
166 * This does the actual diffing, getTextDiff() wraps it with logging and resource limiting.
167 * @param string $oldText
168 * @param string $newText
172 protected function getTextDiffInternal( $oldText, $newText ) {
173 // TODO move most of this into three parallel implementations of a text diff generator
174 // class, choose which one to use via dependecy injection
176 $oldText = str_replace( "\r\n", "\n", $oldText );
177 $newText = str_replace( "\r\n", "\n", $newText );
179 // Better external diff engine, the 2 may some day be dropped
180 // This one does the escaping and segmenting itself
181 if ( $this->engine
=== self
::ENGINE_WIKIDIFF2
) {
182 $wikidiff2Version = phpversion( 'wikidiff2' );
184 $wikidiff2Version !== false &&
185 version_compare( $wikidiff2Version, '1.5.0', '>=' ) &&
186 version_compare( $wikidiff2Version, '1.8.0', '<' )
188 $text = wikidiff2_do_diff(
195 // Don't pass the 4th parameter introduced in version 1.5.0 and removed in version 1.8.0
196 $text = wikidiff2_do_diff(
204 } elseif ( $this->engine
=== self
::ENGINE_EXTERNAL
) {
206 $tmpDir = wfTempDir();
207 $tempName1 = tempnam( $tmpDir, 'diff_' );
208 $tempName2 = tempnam( $tmpDir, 'diff_' );
210 $tempFile1 = fopen( $tempName1, "w" );
214 $tempFile2 = fopen( $tempName2, "w" );
218 fwrite( $tempFile1, $oldText );
219 fwrite( $tempFile2, $newText );
220 fclose( $tempFile1 );
221 fclose( $tempFile2 );
222 $cmd = [ $this->externalEngine
, $tempName1, $tempName2 ];
223 $result = Shell
::command( $cmd )
225 $exitCode = $result->getExitCode();
226 if ( $exitCode !== 0 ) {
227 throw new Exception( "External diff command returned code {$exitCode}. Stderr: "
228 . wfEscapeWikiText( $result->getStderr() )
231 $difftext = $result->getStdout();
232 unlink( $tempName1 );
233 unlink( $tempName2 );
236 } elseif ( $this->engine
=== self
::ENGINE_PHP
) {
237 if ( $this->language
) {
238 $oldText = $this->language
->segmentForDiff( $oldText );
239 $newText = $this->language
->segmentForDiff( $newText );
241 $ota = explode( "\n", $oldText );
242 $nta = explode( "\n", $newText );
243 $diffs = new Diff( $ota, $nta );
244 $formatter = new TableDiffFormatter();
245 $difftext = $formatter->format( $diffs );
246 if ( $this->language
) {
247 $difftext = $this->language
->unsegmentForDiff( $difftext );
252 throw new LogicException( 'Invalid engine: ' . $this->engine
);