3 * User interface for the difference engine
6 * @ingroup DifferenceEngine
10 * Constant to indicate diff cache compatibility.
11 * Bump this when changing the diff formatting in a way that
12 * fixes important bugs or such to force cached diff views to
15 define( 'MW_DIFF_VERSION', '1.11a' );
19 * @ingroup DifferenceEngine
21 class DifferenceEngine
{
25 var $mOldid, $mNewid, $mTitle;
26 var $mOldtitle, $mNewtitle, $mPagetitle;
27 var $mOldtext, $mNewtext;
28 var $mOldPage, $mNewPage;
29 var $mRcidMarkPatrolled;
30 var $mOldRev, $mNewRev;
31 var $mRevisionsLoaded = false; // Have the revisions been loaded
32 var $mTextLoaded = 0; // How many text blobs have been loaded, 0, 1 or 2?
33 var $mCacheHit = false; // Was the diff fetched from cache?
36 * Set this to true to add debug info to the HTML output.
37 * Warning: this may cause RSS readers to spuriously mark articles as "new"
40 var $enableDebugComment = false;
42 // If true, line X is not displayed when X is 1, for example to increase
43 // readability and conserve space with many small diffs.
44 protected $mReducedLineNumbers = false;
46 protected $unhide = false; # show rev_deleted content if allowed
51 * @param $titleObj Title object that the diff is associated with
52 * @param $old Integer: old ID we want to show and diff with.
53 * @param $new String: either 'prev' or 'next'.
54 * @param $rcid Integer: ??? FIXME (default 0)
55 * @param $refreshCache boolean If set, refreshes the diff cache
56 * @param $unhide boolean If set, allow viewing deleted revs
58 function __construct( $titleObj = null, $old = 0, $new = 0, $rcid = 0,
59 $refreshCache = false, $unhide = false )
62 $this->mTitle
= $titleObj;
65 $this->mTitle
= $wgTitle; // @TODO: get rid of this
67 wfDebug( "DifferenceEngine old '$old' new '$new' rcid '$rcid'\n" );
69 if ( 'prev' === $new ) {
70 # Show diff between revision $old and the previous one.
71 # Get previous one from DB.
72 $this->mNewid
= intval( $old );
73 $this->mOldid
= $this->mTitle
->getPreviousRevisionID( $this->mNewid
);
74 } elseif ( 'next' === $new ) {
75 # Show diff between revision $old and the next one.
76 # Get next one from DB.
77 $this->mOldid
= intval( $old );
78 $this->mNewid
= $this->mTitle
->getNextRevisionID( $this->mOldid
);
79 if ( false === $this->mNewid
) {
80 # if no result, NewId points to the newest old revision. The only newer
81 # revision is cur, which is "0".
85 $this->mOldid
= intval( $old );
86 $this->mNewid
= intval( $new );
87 wfRunHooks( 'NewDifferenceEngine', array( &$titleObj, &$this->mOldid
, &$this->mNewid
, $old, $new ) );
89 $this->mRcidMarkPatrolled
= intval( $rcid ); # force it to be an integer
90 $this->mRefreshCache
= $refreshCache;
91 $this->unhide
= $unhide;
94 function setReducedLineNumbers( $value = true ) {
95 $this->mReducedLineNumbers
= $value;
102 function wasCacheHit() {
103 return $this->mCacheHit
;
106 function getOldid() {
107 return $this->mOldid
;
110 function getNewid() {
111 return $this->mNewid
;
114 function showDiffPage( $diffOnly = false ) {
115 global $wgUser, $wgOut, $wgUseExternalEditor, $wgUseRCPatrol;
116 wfProfileIn( __METHOD__
);
118 # Allow frames except in certain special cases
119 $wgOut->allowClickjacking();
121 # If external diffs are enabled both globally and for the user,
122 # we'll use the application/x-external-editor interface to call
123 # an external diff tool like kompare, kdiff3, etc.
124 if ( $wgUseExternalEditor && $wgUser->getOption( 'externaldiff' ) ) {
125 global $wgInputEncoding, $wgServer, $wgScript, $wgLang;
127 header ( "Content-type: application/x-external-editor; charset=" . $wgInputEncoding );
128 $url1 = $this->mTitle
->getFullURL( array(
130 'oldid' => $this->mOldid
132 $url2 = $this->mTitle
->getFullURL( array(
134 'oldid' => $this->mNewid
136 $special = $wgLang->getNsText( NS_SPECIAL
);
137 $control = <<<CONTROL
141 Script={$wgServer}{$wgScript}
142 Special namespace={$special}
156 $wgOut->setArticleFlag( false );
157 if ( !$this->loadRevisionData() ) {
158 $t = $this->mTitle
->getPrefixedText();
159 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid
, $this->mNewid
);
160 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
161 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
162 wfProfileOut( __METHOD__
);
166 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
168 if ( $this->mNewRev
->isCurrent() ) {
169 $wgOut->setArticleFlag( true );
172 # mOldid is false if the difference engine is called with a "vague" query for
173 # a diff between a version V and its previous version V' AND the version V
174 # is the first version of that article. In that case, V' does not exist.
175 if ( $this->mOldid
=== false ) {
176 $this->showFirstRevision();
177 $this->renderNewRevision(); // should we respect $diffOnly here or not?
178 wfProfileOut( __METHOD__
);
182 $wgOut->suppressQuickbar();
184 $oldTitle = $this->mOldPage
->getPrefixedText();
185 $newTitle = $this->mNewPage
->getPrefixedText();
186 if ( $oldTitle == $newTitle ) {
187 $wgOut->setPageTitle( $newTitle );
189 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
191 if ( $this->mNewPage
->equals( $this->mOldPage
) ) {
192 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
194 $wgOut->setSubtitle( wfMsgExt( 'difference-multipage', array( 'parseinline' ) ) );
196 $wgOut->setRobotPolicy( 'noindex,nofollow' );
198 if ( !$this->mOldPage
->userCanRead() ||
!$this->mNewPage
->userCanRead() ) {
199 $wgOut->loginToUse();
202 wfProfileOut( __METHOD__
);
206 $sk = $wgUser->getSkin();
208 // Check if page is editable
209 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
210 if ( $editable && $this->mNewRev
->isCurrent() && $wgUser->isAllowed( 'rollback' ) ) {
211 $wgOut->preventClickjacking();
212 $rollback = '   ' . $sk->generateRollback( $this->mNewRev
);
217 // Prepare a change patrol link, if applicable
218 if ( $wgUseRCPatrol && $this->mTitle
->userCan( 'patrol' ) ) {
219 // If we've been given an explicit change identifier, use it; saves time
220 if ( $this->mRcidMarkPatrolled
) {
221 $rcid = $this->mRcidMarkPatrolled
;
222 $rc = RecentChange
::newFromId( $rcid );
223 // Already patrolled?
224 $rcid = is_object( $rc ) && !$rc->getAttribute( 'rc_patrolled' ) ?
$rcid : 0;
226 // Look for an unpatrolled change corresponding to this diff
227 $db = wfGetDB( DB_SLAVE
);
228 $change = RecentChange
::newFromConds(
230 // Redundant user,timestamp condition so we can use the existing index
231 'rc_user_text' => $this->mNewRev
->getRawUserText(),
232 'rc_timestamp' => $db->timestamp( $this->mNewRev
->getTimestamp() ),
233 'rc_this_oldid' => $this->mNewid
,
234 'rc_last_oldid' => $this->mOldid
,
239 if ( $change instanceof RecentChange
) {
240 $rcid = $change->mAttribs
['rc_id'];
241 $this->mRcidMarkPatrolled
= $rcid;
249 $wgOut->preventClickjacking();
250 $token = $wgUser->editToken( $rcid );
251 $patrol = ' <span class="patrollink">[' . $sk->link(
253 wfMsgHtml( 'markaspatrolleddiff' ),
256 'action' => 'markpatrolled',
272 # Carry over 'diffonly' param via navigation links
273 if ( $diffOnly != $wgUser->getBoolOption( 'diffonly' ) ) {
274 $query['diffonly'] = $diffOnly;
277 # Make "previous revision link"
278 $query['diff'] = 'prev';
279 $query['oldid'] = $this->mOldid
;
280 # Cascade unhide param in links for easy deletion browsing
281 if ( $this->unhide
) {
282 $query['unhide'] = 1;
284 if ( !$this->mOldRev
->getPrevious() ) {
285 $prevlink = ' ';
287 $prevlink = $sk->link(
289 wfMsgHtml( 'previousdiff' ),
291 'id' => 'differences-prevlink'
301 # Make "next revision link"
302 $query['diff'] = 'next';
303 $query['oldid'] = $this->mNewid
;
304 # Skip next link on the top revision
305 if ( $this->mNewRev
->isCurrent() ) {
306 $nextlink = ' ';
308 $nextlink = $sk->link(
310 wfMsgHtml( 'nextdiff' ),
312 'id' => 'differences-nextlink'
325 if ( $this->mOldRev
->isMinor() ) {
326 $oldminor = ChangesList
::flag( 'minor' );
328 if ( $this->mNewRev
->isMinor() ) {
329 $newminor = ChangesList
::flag( 'minor' );
332 # Handle RevisionDelete links...
333 $ldel = $this->revisionDeleteLink( $this->mOldRev
);
334 $rdel = $this->revisionDeleteLink( $this->mNewRev
);
336 $oldHeader = '<div id="mw-diff-otitle1"><strong>' . $this->mOldtitle
. '</strong></div>' .
337 '<div id="mw-diff-otitle2">' .
338 $sk->revUserTools( $this->mOldRev
, !$this->unhide
) . '</div>' .
339 '<div id="mw-diff-otitle3">' . $oldminor .
340 $sk->revComment( $this->mOldRev
, !$diffOnly, !$this->unhide
) . $ldel . '</div>' .
341 '<div id="mw-diff-otitle4">' . $prevlink . '</div>';
342 $newHeader = '<div id="mw-diff-ntitle1"><strong>' . $this->mNewtitle
. '</strong></div>' .
343 '<div id="mw-diff-ntitle2">' . $sk->revUserTools( $this->mNewRev
, !$this->unhide
) .
345 '<div id="mw-diff-ntitle3">' . $newminor .
346 $sk->revComment( $this->mNewRev
, !$diffOnly, !$this->unhide
) . $rdel . '</div>' .
347 '<div id="mw-diff-ntitle4">' . $nextlink . $patrol . '</div>';
349 # Check if this user can see the revisions
350 $allowed = $this->mOldRev
->userCan( Revision
::DELETED_TEXT
)
351 && $this->mNewRev
->userCan( Revision
::DELETED_TEXT
);
352 # Check if one of the revisions is deleted/suppressed
353 $deleted = $suppressed = false;
354 if ( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
355 $deleted = true; // old revisions text is hidden
356 if ( $this->mOldRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
357 $suppressed = true; // also suppressed
359 if ( $this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
360 $deleted = true; // new revisions text is hidden
361 if ( $this->mNewRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
362 $suppressed = true; // also suppressed
364 # If the diff cannot be shown due to a deleted revision, then output
365 # the diff header and links to unhide (if available)...
366 if ( $deleted && ( !$this->unhide ||
!$allowed ) ) {
367 $this->showDiffStyle();
368 $multi = $this->getMultiNotice();
369 $wgOut->addHTML( $this->addHeader( '', $oldHeader, $newHeader, $multi ) );
371 $msg = $suppressed ?
'rev-suppressed-no-diff' : 'rev-deleted-no-diff';
372 # Give explanation for why revision is not visible
373 $wgOut->wrapWikiMsg( "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n",
376 # Give explanation and add a link to view the diff...
377 $link = $this->mTitle
->getFullUrl( array(
378 'diff' => $this->mNewid
,
379 'oldid' => $this->mOldid
,
382 $msg = $suppressed ?
'rev-suppressed-unhide-diff' : 'rev-deleted-unhide-diff';
383 $wgOut->wrapWikiMsg( "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n", array( $msg, $link ) );
385 # Otherwise, output a regular diff...
387 # Add deletion notice if the user is viewing deleted content
390 $msg = $suppressed ?
'rev-suppressed-diff-view' : 'rev-deleted-diff-view';
391 $notice = "<div id='mw-$msg' class='mw-warning plainlinks'>\n" . wfMsgExt( $msg, 'parseinline' ) . "</div>\n";
393 $this->showDiff( $oldHeader, $newHeader, $notice );
395 $this->renderNewRevision();
398 wfProfileOut( __METHOD__
);
401 protected function revisionDeleteLink( $rev ) {
404 $canHide = $wgUser->isAllowed( 'deleterevision' );
405 // Show del/undel link if:
406 // (a) the user can delete revisions, or
407 // (b) the user can view deleted revision *and* this one is deleted
408 if ( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
409 $sk = $wgUser->getSkin();
410 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
411 $link = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
414 'type' => 'revision',
415 'target' => $rev->mTitle
->getPrefixedDbkey(),
416 'ids' => $rev->getId()
418 $link = $sk->revDeleteLink( $query,
419 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
421 $link = '   ' . $link . ' ';
427 * Show the new revision of the page.
429 function renderNewRevision() {
430 global $wgOut, $wgUser;
431 wfProfileIn( __METHOD__
);
432 # Add "current version as of X" title
433 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
434 # Page content may be handled by a hooked call instead...
435 if ( wfRunHooks( 'ArticleContentOnDiff', array( $this, $wgOut ) ) ) {
436 # Use the current version parser cache if applicable
438 if ( !$this->mNewRev
->isCurrent() ) {
439 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
443 $this->loadNewText();
444 $wgOut->setRevisionId( $this->mNewRev
->getId() );
446 if ( $this->mTitle
->isCssJsSubpage() ||
$this->mTitle
->isCssOrJsPage() ) {
447 // Stolen from Article::view --AG 2007-10-11
448 // Give hooks a chance to customise the output
449 // @TODO: standardize this crap into one function
450 if ( wfRunHooks( 'ShowRawCssJs', array( $this->mNewtext
, $this->mTitle
, $wgOut ) ) ) {
451 // Wrap the whole lot in a <pre> and don't parse
453 preg_match( '!\.(css|js)$!u', $this->mTitle
->getText(), $m );
454 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
455 $wgOut->addHTML( htmlspecialchars( $this->mNewtext
) );
456 $wgOut->addHTML( "\n</pre>\n" );
458 } elseif ( $pCache ) {
459 $article = new Article( $this->mTitle
, 0 );
460 $pOutput = ParserCache
::singleton()->get( $article, $wgOut->parserOptions() );
462 $wgOut->addParserOutput( $pOutput );
464 $article->doViewParse();
467 $wgOut->addWikiTextTidy( $this->mNewtext
);
470 if ( !$this->mNewRev
->isCurrent() ) {
471 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
474 # Add redundant patrol link on bottom...
475 if ( $this->mRcidMarkPatrolled
&& $this->mTitle
->quickUserCan( 'patrol' ) ) {
476 $sk = $wgUser->getSkin();
477 $token = $wgUser->editToken( $this->mRcidMarkPatrolled
);
478 $wgOut->preventClickjacking();
480 "<div class='patrollink'>[" . $sk->link(
482 wfMsgHtml( 'markaspatrolleddiff' ),
485 'action' => 'markpatrolled',
486 'rcid' => $this->mRcidMarkPatrolled
,
493 wfProfileOut( __METHOD__
);
497 * Show the first revision of an article. Uses normal diff headers in
498 * contrast to normal "old revision" display style.
500 function showFirstRevision() {
501 global $wgOut, $wgUser;
502 wfProfileIn( __METHOD__
);
504 # Get article text from the DB
506 if ( ! $this->loadNewText() ) {
507 $t = $this->mTitle
->getPrefixedText();
508 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid
, $this->mNewid
);
509 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
510 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
511 wfProfileOut( __METHOD__
);
514 if ( $this->mNewRev
->isCurrent() ) {
515 $wgOut->setArticleFlag( true );
518 # Check if user is allowed to look at this page. If not, bail out.
520 if ( !$this->mTitle
->userCanRead() ) {
521 $wgOut->loginToUse();
523 wfProfileOut( __METHOD__
);
524 throw new MWException( "Permission Error: you do not have access to view this page" );
527 # Prepare the header box
529 $sk = $wgUser->getSkin();
531 $next = $this->mTitle
->getNextRevisionID( $this->mNewid
);
535 $nextlink = '<br />' . $sk->link(
537 wfMsgHtml( 'nextdiff' ),
539 'id' => 'differences-nextlink'
543 'oldid' => $this->mNewid
,
551 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\">" .
552 $sk->revUserTools( $this->mNewRev
) . "<br />" . $sk->revComment( $this->mNewRev
) . $nextlink . "</div>\n";
554 $wgOut->addHTML( $header );
556 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
557 $wgOut->setRobotPolicy( 'noindex,nofollow' );
559 wfProfileOut( __METHOD__
);
563 * Get the diff text, send it to $wgOut
564 * Returns false if the diff could not be generated, otherwise returns true
566 function showDiff( $otitle, $ntitle, $notice = '' ) {
568 $diff = $this->getDiff( $otitle, $ntitle, $notice );
569 if ( $diff === false ) {
570 $wgOut->addWikiMsg( 'missing-article', "<nowiki>(fixme, bug)</nowiki>", '' );
573 $this->showDiffStyle();
574 $wgOut->addHTML( $diff );
580 * Add style sheets and supporting JS for diff display.
582 function showDiffStyle() {
584 $wgOut->addModules( 'mediawiki.legacy.diff' );
588 * Get complete diff table, including header
590 * @param $otitle Title: old title
591 * @param $ntitle Title: new title
592 * @param $notice String: HTML between diff header and body
595 function getDiff( $otitle, $ntitle, $notice = '' ) {
596 $body = $this->getDiffBody();
597 if ( $body === false ) {
600 $multi = $this->getMultiNotice();
601 return $this->addHeader( $body, $otitle, $ntitle, $multi, $notice );
606 * Get the diff table body, without header
608 * @return mixed (string/false)
610 public function getDiffBody() {
612 wfProfileIn( __METHOD__
);
613 $this->mCacheHit
= true;
614 // Check if the diff should be hidden from this user
615 if ( !$this->loadRevisionData() ) {
617 } elseif ( $this->mOldRev
&& !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
619 } elseif ( $this->mNewRev
&& !$this->mNewRev
->userCan( Revision
::DELETED_TEXT
) ) {
623 if ( $this->mOldRev
&& $this->mNewRev
624 && $this->mOldRev
->getID() == $this->mNewRev
->getID() )
630 if ( $this->mOldid
&& $this->mNewid
) {
631 $key = wfMemcKey( 'diff', 'version', MW_DIFF_VERSION
,
632 'oldid', $this->mOldid
, 'newid', $this->mNewid
);
634 if ( !$this->mRefreshCache
) {
635 $difftext = $wgMemc->get( $key );
637 wfIncrStats( 'diff_cache_hit' );
638 $difftext = $this->localiseLineNumbers( $difftext );
639 $difftext .= "\n<!-- diff cache key $key -->\n";
640 wfProfileOut( __METHOD__
);
643 } // don't try to load but save the result
645 $this->mCacheHit
= false;
647 // Loadtext is permission safe, this just clears out the diff
648 if ( !$this->loadText() ) {
649 wfProfileOut( __METHOD__
);
653 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
655 // Save to cache for 7 days
656 if ( !wfRunHooks( 'AbortDiffCache', array( &$this ) ) ) {
657 wfIncrStats( 'diff_uncacheable' );
658 } elseif ( $key !== false && $difftext !== false ) {
659 wfIncrStats( 'diff_cache_miss' );
660 $wgMemc->set( $key, $difftext, 7 * 86400 );
662 wfIncrStats( 'diff_uncacheable' );
664 // Replace line numbers with the text in the user's language
665 if ( $difftext !== false ) {
666 $difftext = $this->localiseLineNumbers( $difftext );
668 wfProfileOut( __METHOD__
);
673 * Make sure the proper modules are loaded before we try to
676 private function initDiffEngines() {
677 global $wgExternalDiffEngine;
678 if ( $wgExternalDiffEngine == 'wikidiff' && !function_exists( 'wikidiff_do_diff' ) ) {
679 wfProfileIn( __METHOD__
. '-php_wikidiff.so' );
680 wfSuppressWarnings();
681 dl( 'php_wikidiff.so' );
683 wfProfileOut( __METHOD__
. '-php_wikidiff.so' );
685 else if ( $wgExternalDiffEngine == 'wikidiff2' && !function_exists( 'wikidiff2_do_diff' ) ) {
686 wfProfileIn( __METHOD__
. '-php_wikidiff2.so' );
687 wfSuppressWarnings();
690 wfProfileOut( __METHOD__
. '-php_wikidiff2.so' );
695 * Generate a diff, no caching
697 * @param $otext String: old text, must be already segmented
698 * @param $ntext String: new text, must be already segmented
700 function generateDiffBody( $otext, $ntext ) {
701 global $wgExternalDiffEngine, $wgContLang;
703 $otext = str_replace( "\r\n", "\n", $otext );
704 $ntext = str_replace( "\r\n", "\n", $ntext );
706 $this->initDiffEngines();
708 if ( $wgExternalDiffEngine == 'wikidiff' && function_exists( 'wikidiff_do_diff' ) ) {
709 # For historical reasons, external diff engine expects
710 # input text to be HTML-escaped already
711 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
712 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
713 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) ) .
714 $this->debug( 'wikidiff1' );
717 if ( $wgExternalDiffEngine == 'wikidiff2' && function_exists( 'wikidiff2_do_diff' ) ) {
718 # Better external diff engine, the 2 may some day be dropped
719 # This one does the escaping and segmenting itself
720 wfProfileIn( 'wikidiff2_do_diff' );
721 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
722 $text .= $this->debug( 'wikidiff2' );
723 wfProfileOut( 'wikidiff2_do_diff' );
726 if ( $wgExternalDiffEngine != 'wikidiff3' && $wgExternalDiffEngine !== false ) {
728 global $wgTmpDirectory;
729 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
730 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
732 $tempFile1 = fopen( $tempName1, "w" );
734 wfProfileOut( __METHOD__
);
737 $tempFile2 = fopen( $tempName2, "w" );
739 wfProfileOut( __METHOD__
);
742 fwrite( $tempFile1, $otext );
743 fwrite( $tempFile2, $ntext );
744 fclose( $tempFile1 );
745 fclose( $tempFile2 );
746 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
747 wfProfileIn( __METHOD__
. "-shellexec" );
748 $difftext = wfShellExec( $cmd );
749 $difftext .= $this->debug( "external $wgExternalDiffEngine" );
750 wfProfileOut( __METHOD__
. "-shellexec" );
751 unlink( $tempName1 );
752 unlink( $tempName2 );
757 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
758 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
759 $diffs = new Diff( $ota, $nta );
760 $formatter = new TableDiffFormatter();
761 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) ) .
766 * Generate a debug comment indicating diff generating time,
767 * server node, and generator backend.
769 protected function debug( $generator = "internal" ) {
770 global $wgShowHostnames;
771 if ( !$this->enableDebugComment
) {
774 $data = array( $generator );
775 if ( $wgShowHostnames ) {
776 $data[] = wfHostname();
778 $data[] = wfTimestamp( TS_DB
);
779 return "<!-- diff generator: " .
788 * Replace line numbers with the text in the user's language
790 function localiseLineNumbers( $text ) {
791 return preg_replace_callback( '/<!--LINE (\d+)-->/',
792 array( &$this, 'localiseLineNumbersCb' ), $text );
795 function localiseLineNumbersCb( $matches ) {
797 if ( $matches[1] === '1' && $this->mReducedLineNumbers
) return '';
798 return wfMsgExt( 'lineno', 'escape', $wgLang->formatNum( $matches[1] ) );
803 * If there are revisions between the ones being compared, return a note saying so.
806 function getMultiNotice() {
807 if ( !is_object( $this->mOldRev
) ||
!is_object( $this->mNewRev
) ) {
809 } elseif ( !$this->mOldPage
->equals( $this->mNewPage
) ) {
810 // Comparing two different pages? Count would be meaningless.
814 $oldid = $this->mOldRev
->getId();
815 $newid = $this->mNewRev
->getId();
816 if ( $oldid > $newid ) {
817 $tmp = $oldid; $oldid = $newid; $newid = $tmp;
820 $nEdits = $this->mTitle
->countRevisionsBetween( $oldid, $newid );
823 // We use ($limit + 1) so we can detect if there are > 100 authors
824 // in a given revision range. In that case, diff-multi-manyusers is used.
825 $numUsers = $this->mTitle
->countAuthorsBetween( $oldid, $newid, $limit +
1 );
826 return self
::intermediateEditsMsg( $nEdits, $numUsers, $limit );
828 return ''; // nothing
832 * Get a notice about how many intermediate edits and users there are
833 * @param $numEdits int
834 * @param $numUsers int
838 public static function intermediateEditsMsg( $numEdits, $numUsers, $limit ) {
840 if ( $numUsers > $limit ) {
841 $msg = 'diff-multi-manyusers';
846 return wfMsgExt( $msg, 'parseinline',
847 $wgLang->formatnum( $numEdits ), $wgLang->formatnum( $numUsers ) );
851 * Add the header to a diff body
853 static function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) {
854 $header = "<table class='diff'>";
855 if ( $diff ) { // Safari/Chrome show broken output if cols not used
857 <col class='diff-marker' />
858 <col class='diff-content' />
859 <col class='diff-marker' />
860 <col class='diff-content' />";
869 <td colspan='$colspan' class='diff-otitle'>{$otitle}</td>
870 <td colspan='$colspan' class='diff-ntitle'>{$ntitle}</td>
873 if ( $multi != '' ) {
874 $header .= "<tr><td colspan='{$multiColspan}' align='center' class='diff-multi'>{$multi}</td></tr>";
876 if ( $notice != '' ) {
877 $header .= "<tr><td colspan='{$multiColspan}' align='center'>{$notice}</td></tr>";
880 return $header . $diff . "</table>";
884 * Use specified text instead of loading from the database
886 function setText( $oldText, $newText ) {
887 $this->mOldtext
= $oldText;
888 $this->mNewtext
= $newText;
889 $this->mTextLoaded
= 2;
890 $this->mRevisionsLoaded
= true;
894 * Load revision metadata for the specified articles. If newid is 0, then compare
895 * the old article in oldid to the current article; if oldid is 0, then
896 * compare the current article to the immediately previous one (ignoring the
899 * If oldid is false, leave the corresponding revision object set
900 * to false. This is impossible via ordinary user input, and is provided for
903 function loadRevisionData() {
904 global $wgLang, $wgUser;
905 if ( $this->mRevisionsLoaded
) {
908 // Whether it succeeds or fails, we don't want to try again
909 $this->mRevisionsLoaded
= true;
912 // Load the new revision object
913 $this->mNewRev
= $this->mNewid
914 ? Revision
::newFromId( $this->mNewid
)
915 : Revision
::newFromTitle( $this->mTitle
);
916 if ( !$this->mNewRev
instanceof Revision
)
919 // Update the new revision ID in case it was 0 (makes life easier doing UI stuff)
920 $this->mNewid
= $this->mNewRev
->getId();
922 // Check if page is editable
923 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
925 // Set assorted variables
926 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
927 $dateofrev = $wgLang->date( $this->mNewRev
->getTimestamp(), true );
928 $timeofrev = $wgLang->time( $this->mNewRev
->getTimestamp(), true );
929 $this->mNewPage
= $this->mNewRev
->getTitle();
930 if ( $this->mNewRev
->isCurrent() ) {
931 $newLink = $this->mNewPage
->escapeLocalUrl( array(
932 'oldid' => $this->mNewid
934 $this->mPagetitle
= htmlspecialchars( wfMsg(
940 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
944 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
945 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
947 $newLink = $this->mNewPage
->escapeLocalUrl( array(
948 'oldid' => $this->mNewid
950 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
952 'oldid' => $this->mNewid
954 $this->mPagetitle
= htmlspecialchars( wfMsg(
961 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
962 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
964 if ( !$this->mNewRev
->userCan( Revision
::DELETED_TEXT
) ) {
965 $this->mNewtitle
= "<span class='history-deleted'>{$this->mPagetitle}</span>";
966 } else if ( $this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
967 $this->mNewtitle
= "<span class='history-deleted'>{$this->mNewtitle}</span>";
970 // Load the old revision object
971 $this->mOldRev
= false;
972 if ( $this->mOldid
) {
973 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
974 } elseif ( $this->mOldid
=== 0 ) {
975 $rev = $this->mNewRev
->getPrevious();
977 $this->mOldid
= $rev->getId();
978 $this->mOldRev
= $rev;
980 // No previous revision; mark to show as first-version only.
981 $this->mOldid
= false;
982 $this->mOldRev
= false;
984 } /* elseif ( $this->mOldid === false ) leave mOldRev false; */
986 if ( is_null( $this->mOldRev
) ) {
990 if ( $this->mOldRev
) {
991 $this->mOldPage
= $this->mOldRev
->getTitle();
993 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
994 $dateofrev = $wgLang->date( $this->mOldRev
->getTimestamp(), true );
995 $timeofrev = $wgLang->time( $this->mOldRev
->getTimestamp(), true );
996 $oldLink = $this->mOldPage
->escapeLocalUrl( array(
997 'oldid' => $this->mOldid
999 $oldEdit = $this->mOldPage
->escapeLocalUrl( array(
1001 'oldid' => $this->mOldid
1003 $this->mOldPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t, $dateofrev, $timeofrev ) );
1005 $this->mOldtitle
= "<a href='$oldLink'>{$this->mOldPagetitle}</a>"
1006 . " (<a href='$oldEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
1007 // Add an "undo" link
1008 $newUndo = $this->mNewPage
->escapeLocalUrl( array(
1010 'undoafter' => $this->mOldid
,
1011 'undo' => $this->mNewid
1013 $htmlLink = htmlspecialchars( wfMsg( 'editundo' ) );
1014 $htmlTitle = Xml
::expandAttributes( array( 'title' => $wgUser->getSkin()->titleAttrib( 'undo' ) ) );
1015 if ( $editable && !$this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) && !$this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1016 $this->mNewtitle
.= " (<a href='$newUndo' $htmlTitle>" . $htmlLink . "</a>)";
1019 if ( !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
1020 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldPagetitle
. '</span>';
1021 } else if ( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1022 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldtitle
. '</span>';
1030 * Load the text of the revisions, as well as revision data.
1032 function loadText() {
1033 if ( $this->mTextLoaded
== 2 ) {
1036 // Whether it succeeds or fails, we don't want to try again
1037 $this->mTextLoaded
= 2;
1040 if ( !$this->loadRevisionData() ) {
1043 if ( $this->mOldRev
) {
1044 $this->mOldtext
= $this->mOldRev
->getText( Revision
::FOR_THIS_USER
);
1045 if ( $this->mOldtext
=== false ) {
1049 if ( $this->mNewRev
) {
1050 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);
1051 if ( $this->mNewtext
=== false ) {
1059 * Load the text of the new revision, not the old one
1061 function loadNewText() {
1062 if ( $this->mTextLoaded
>= 1 ) {
1065 $this->mTextLoaded
= 1;
1067 if ( !$this->loadRevisionData() ) {
1070 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);