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;
32 var $mOldPage, $mNewPage;
33 var $mRcidMarkPatrolled;
38 var $mOldRev, $mNewRev;
39 var $mRevisionsLoaded = false; // Have the revisions been loaded
40 var $mTextLoaded = 0; // How many text blobs have been loaded, 0, 1 or 2?
41 var $mCacheHit = false; // Was the diff fetched from cache?
44 * Set this to true to add debug info to the HTML output.
45 * Warning: this may cause RSS readers to spuriously mark articles as "new"
48 var $enableDebugComment = false;
50 // If true, line X is not displayed when X is 1, for example to increase
51 // readability and conserve space with many small diffs.
52 protected $mReducedLineNumbers = false;
54 protected $unhide = false; # show rev_deleted content if allowed
59 * @param $titleObj Title object that the diff is associated with
60 * @param $old Integer: old ID we want to show and diff with.
61 * @param $new String: either 'prev' or 'next'.
62 * @param $rcid Integer: ??? FIXME (default 0)
63 * @param $refreshCache boolean If set, refreshes the diff cache
64 * @param $unhide boolean If set, allow viewing deleted revs
66 function __construct( $titleObj = null, $old = 0, $new = 0, $rcid = 0,
67 $refreshCache = false, $unhide = false )
70 $this->mTitle
= $titleObj;
73 $this->mTitle
= $wgTitle; // @TODO: get rid of this
75 wfDebug( "DifferenceEngine old '$old' new '$new' rcid '$rcid'\n" );
77 if ( 'prev' === $new ) {
78 # Show diff between revision $old and the previous one.
79 # Get previous one from DB.
80 $this->mNewid
= intval( $old );
81 $this->mOldid
= $this->mTitle
->getPreviousRevisionID( $this->mNewid
);
82 } elseif ( 'next' === $new ) {
83 # Show diff between revision $old and the next one.
84 # Get next one from DB.
85 $this->mOldid
= intval( $old );
86 $this->mNewid
= $this->mTitle
->getNextRevisionID( $this->mOldid
);
87 if ( false === $this->mNewid
) {
88 # if no result, NewId points to the newest old revision. The only newer
89 # revision is cur, which is "0".
93 $this->mOldid
= intval( $old );
94 $this->mNewid
= intval( $new );
95 wfRunHooks( 'NewDifferenceEngine', array( &$titleObj, &$this->mOldid
, &$this->mNewid
, $old, $new ) );
97 $this->mRcidMarkPatrolled
= intval( $rcid ); # force it to be an integer
98 $this->mRefreshCache
= $refreshCache;
99 $this->unhide
= $unhide;
102 function setReducedLineNumbers( $value = true ) {
103 $this->mReducedLineNumbers
= $value;
106 function getTitle() {
107 return $this->mTitle
;
110 function wasCacheHit() {
111 return $this->mCacheHit
;
114 function getOldid() {
115 return $this->mOldid
;
118 function getNewid() {
119 return $this->mNewid
;
122 function showDiffPage( $diffOnly = false ) {
123 global $wgUser, $wgOut, $wgUseExternalEditor, $wgUseRCPatrol;
124 wfProfileIn( __METHOD__
);
126 # Allow frames except in certain special cases
127 $wgOut->allowClickjacking();
129 # If external diffs are enabled both globally and for the user,
130 # we'll use the application/x-external-editor interface to call
131 # an external diff tool like kompare, kdiff3, etc.
132 if ( $wgUseExternalEditor && $wgUser->getOption( 'externaldiff' ) ) {
133 global $wgInputEncoding, $wgServer, $wgScript, $wgLang;
135 header ( "Content-type: application/x-external-editor; charset=" . $wgInputEncoding );
136 $url1 = $this->mTitle
->getFullURL( array(
138 'oldid' => $this->mOldid
140 $url2 = $this->mTitle
->getFullURL( array(
142 'oldid' => $this->mNewid
144 $special = $wgLang->getNsText( NS_SPECIAL
);
145 $control = <<<CONTROL
149 Script={$wgServer}{$wgScript}
150 Special namespace={$special}
162 wfProfileOut( __METHOD__
);
166 $wgOut->setArticleFlag( false );
167 if ( !$this->loadRevisionData() ) {
168 $t = $this->mTitle
->getPrefixedText();
169 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid
, $this->mNewid
);
170 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
171 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
172 wfProfileOut( __METHOD__
);
176 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
178 if ( $this->mNewRev
->isCurrent() ) {
179 $wgOut->setArticleFlag( true );
182 # mOldid is false if the difference engine is called with a "vague" query for
183 # a diff between a version V and its previous version V' AND the version V
184 # is the first version of that article. In that case, V' does not exist.
185 if ( $this->mOldid
=== false ) {
186 $this->showFirstRevision();
187 $this->renderNewRevision(); // should we respect $diffOnly here or not?
188 wfProfileOut( __METHOD__
);
192 $oldTitle = $this->mOldPage
->getPrefixedText();
193 $newTitle = $this->mNewPage
->getPrefixedText();
194 if ( $oldTitle == $newTitle ) {
195 $wgOut->setPageTitle( $newTitle );
197 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
199 if ( $this->mNewPage
->equals( $this->mOldPage
) ) {
200 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
202 $wgOut->setSubtitle( wfMsgExt( 'difference-multipage', array( 'parseinline' ) ) );
204 $wgOut->setRobotPolicy( 'noindex,nofollow' );
206 if ( !$this->mOldPage
->userCanRead() ||
!$this->mNewPage
->userCanRead() ) {
207 $wgOut->loginToUse();
210 wfProfileOut( __METHOD__
);
214 $sk = $wgUser->getSkin();
215 if ( method_exists( $sk, 'suppressQuickbar' ) ) {
216 $sk->suppressQuickbar();
219 // Check if page is editable
220 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
221 if ( $editable && $this->mNewRev
->isCurrent() && $wgUser->isAllowed( 'rollback' ) ) {
222 $wgOut->preventClickjacking();
223 $rollback = '   ' . $sk->generateRollback( $this->mNewRev
);
228 // Prepare a change patrol link, if applicable
229 if ( $wgUseRCPatrol && $this->mTitle
->userCan( 'patrol' ) ) {
230 // If we've been given an explicit change identifier, use it; saves time
231 if ( $this->mRcidMarkPatrolled
) {
232 $rcid = $this->mRcidMarkPatrolled
;
233 $rc = RecentChange
::newFromId( $rcid );
234 // Already patrolled?
235 $rcid = is_object( $rc ) && !$rc->getAttribute( 'rc_patrolled' ) ?
$rcid : 0;
237 // Look for an unpatrolled change corresponding to this diff
238 $db = wfGetDB( DB_SLAVE
);
239 $change = RecentChange
::newFromConds(
241 // Redundant user,timestamp condition so we can use the existing index
242 'rc_user_text' => $this->mNewRev
->getRawUserText(),
243 'rc_timestamp' => $db->timestamp( $this->mNewRev
->getTimestamp() ),
244 'rc_this_oldid' => $this->mNewid
,
245 'rc_last_oldid' => $this->mOldid
,
250 if ( $change instanceof RecentChange
) {
251 $rcid = $change->mAttribs
['rc_id'];
252 $this->mRcidMarkPatrolled
= $rcid;
260 $wgOut->preventClickjacking();
261 $token = $wgUser->editToken( $rcid );
262 $patrol = ' <span class="patrollink">[' . $sk->link(
264 wfMsgHtml( 'markaspatrolleddiff' ),
267 'action' => 'markpatrolled',
283 # Carry over 'diffonly' param via navigation links
284 if ( $diffOnly != $wgUser->getBoolOption( 'diffonly' ) ) {
285 $query['diffonly'] = $diffOnly;
288 # Make "previous revision link"
289 $query['diff'] = 'prev';
290 $query['oldid'] = $this->mOldid
;
291 # Cascade unhide param in links for easy deletion browsing
292 if ( $this->unhide
) {
293 $query['unhide'] = 1;
295 if ( !$this->mOldRev
->getPrevious() ) {
296 $prevlink = ' ';
298 $prevlink = $sk->link(
300 wfMsgHtml( 'previousdiff' ),
302 'id' => 'differences-prevlink'
312 # Make "next revision link"
313 $query['diff'] = 'next';
314 $query['oldid'] = $this->mNewid
;
315 # Skip next link on the top revision
316 if ( $this->mNewRev
->isCurrent() ) {
317 $nextlink = ' ';
319 $nextlink = $sk->link(
321 wfMsgHtml( 'nextdiff' ),
323 'id' => 'differences-nextlink'
336 if ( $this->mOldRev
->isMinor() ) {
337 $oldminor = ChangesList
::flag( 'minor' );
339 if ( $this->mNewRev
->isMinor() ) {
340 $newminor = ChangesList
::flag( 'minor' );
343 # Handle RevisionDelete links...
344 $ldel = $this->revisionDeleteLink( $this->mOldRev
);
345 $rdel = $this->revisionDeleteLink( $this->mNewRev
);
347 $oldHeader = '<div id="mw-diff-otitle1"><strong>' . $this->mOldtitle
. '</strong></div>' .
348 '<div id="mw-diff-otitle2">' .
349 $sk->revUserTools( $this->mOldRev
, !$this->unhide
) . '</div>' .
350 '<div id="mw-diff-otitle3">' . $oldminor .
351 $sk->revComment( $this->mOldRev
, !$diffOnly, !$this->unhide
) . $ldel . '</div>' .
352 '<div id="mw-diff-otitle4">' . $prevlink . '</div>';
353 $newHeader = '<div id="mw-diff-ntitle1"><strong>' . $this->mNewtitle
. '</strong></div>' .
354 '<div id="mw-diff-ntitle2">' . $sk->revUserTools( $this->mNewRev
, !$this->unhide
) .
356 '<div id="mw-diff-ntitle3">' . $newminor .
357 $sk->revComment( $this->mNewRev
, !$diffOnly, !$this->unhide
) . $rdel . '</div>' .
358 '<div id="mw-diff-ntitle4">' . $nextlink . $patrol . '</div>';
360 # Check if this user can see the revisions
361 $allowed = $this->mOldRev
->userCan( Revision
::DELETED_TEXT
)
362 && $this->mNewRev
->userCan( Revision
::DELETED_TEXT
);
363 # Check if one of the revisions is deleted/suppressed
364 $deleted = $suppressed = false;
365 if ( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
366 $deleted = true; // old revisions text is hidden
367 if ( $this->mOldRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
368 $suppressed = true; // also suppressed
370 if ( $this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
371 $deleted = true; // new revisions text is hidden
372 if ( $this->mNewRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
373 $suppressed = true; // also suppressed
375 # If the diff cannot be shown due to a deleted revision, then output
376 # the diff header and links to unhide (if available)...
377 if ( $deleted && ( !$this->unhide ||
!$allowed ) ) {
378 $this->showDiffStyle();
379 $multi = $this->getMultiNotice();
380 $wgOut->addHTML( $this->addHeader( '', $oldHeader, $newHeader, $multi ) );
382 $msg = $suppressed ?
'rev-suppressed-no-diff' : 'rev-deleted-no-diff';
383 # Give explanation for why revision is not visible
384 $wgOut->wrapWikiMsg( "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n",
387 # Give explanation and add a link to view the diff...
388 $link = $this->mTitle
->getFullUrl( array(
389 'diff' => $this->mNewid
,
390 'oldid' => $this->mOldid
,
393 $msg = $suppressed ?
'rev-suppressed-unhide-diff' : 'rev-deleted-unhide-diff';
394 $wgOut->wrapWikiMsg( "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n", array( $msg, $link ) );
396 # Otherwise, output a regular diff...
398 # Add deletion notice if the user is viewing deleted content
401 $msg = $suppressed ?
'rev-suppressed-diff-view' : 'rev-deleted-diff-view';
402 $notice = "<div id='mw-$msg' class='mw-warning plainlinks'>\n" . wfMsgExt( $msg, 'parseinline' ) . "</div>\n";
404 $this->showDiff( $oldHeader, $newHeader, $notice );
406 $this->renderNewRevision();
409 wfProfileOut( __METHOD__
);
413 * @param $rev Revision
416 protected function revisionDeleteLink( $rev ) {
419 $canHide = $wgUser->isAllowed( 'deleterevision' );
420 // Show del/undel link if:
421 // (a) the user can delete revisions, or
422 // (b) the user can view deleted revision *and* this one is deleted
423 if ( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
424 $sk = $wgUser->getSkin();
425 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
426 $link = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
429 'type' => 'revision',
430 'target' => $rev->mTitle
->getPrefixedDbkey(),
431 'ids' => $rev->getId()
433 $link = $sk->revDeleteLink( $query,
434 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
436 $link = '   ' . $link . ' ';
442 * Show the new revision of the page.
444 function renderNewRevision() {
445 global $wgOut, $wgUser;
446 wfProfileIn( __METHOD__
);
447 # Add "current version as of X" title
448 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
449 # Page content may be handled by a hooked call instead...
450 if ( wfRunHooks( 'ArticleContentOnDiff', array( $this, $wgOut ) ) ) {
451 # Use the current version parser cache if applicable
453 if ( !$this->mNewRev
->isCurrent() ) {
454 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
458 $this->loadNewText();
459 $wgOut->setRevisionId( $this->mNewRev
->getId() );
461 if ( $this->mTitle
->isCssJsSubpage() ||
$this->mTitle
->isCssOrJsPage() ) {
462 // Stolen from Article::view --AG 2007-10-11
463 // Give hooks a chance to customise the output
464 // @TODO: standardize this crap into one function
465 if ( wfRunHooks( 'ShowRawCssJs', array( $this->mNewtext
, $this->mTitle
, $wgOut ) ) ) {
466 // Wrap the whole lot in a <pre> and don't parse
468 preg_match( '!\.(css|js)$!u', $this->mTitle
->getText(), $m );
469 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
470 $wgOut->addHTML( htmlspecialchars( $this->mNewtext
) );
471 $wgOut->addHTML( "\n</pre>\n" );
473 } elseif ( $pCache ) {
474 $article = new Article( $this->mTitle
, 0 );
475 $pOutput = ParserCache
::singleton()->get( $article, $wgOut->parserOptions() );
477 $wgOut->addParserOutput( $pOutput );
479 $article->doViewParse();
482 $wgOut->addWikiTextTidy( $this->mNewtext
);
485 if ( !$this->mNewRev
->isCurrent() ) {
486 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
489 # Add redundant patrol link on bottom...
490 if ( $this->mRcidMarkPatrolled
&& $this->mTitle
->quickUserCan( 'patrol' ) ) {
491 $sk = $wgUser->getSkin();
492 $token = $wgUser->editToken( $this->mRcidMarkPatrolled
);
493 $wgOut->preventClickjacking();
495 "<div class='patrollink'>[" . $sk->link(
497 wfMsgHtml( 'markaspatrolleddiff' ),
500 'action' => 'markpatrolled',
501 'rcid' => $this->mRcidMarkPatrolled
,
508 wfProfileOut( __METHOD__
);
512 * Show the first revision of an article. Uses normal diff headers in
513 * contrast to normal "old revision" display style.
515 function showFirstRevision() {
516 global $wgOut, $wgUser;
517 wfProfileIn( __METHOD__
);
519 # Get article text from the DB
521 if ( ! $this->loadNewText() ) {
522 $t = $this->mTitle
->getPrefixedText();
523 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid
, $this->mNewid
);
524 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
525 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
526 wfProfileOut( __METHOD__
);
529 if ( $this->mNewRev
->isCurrent() ) {
530 $wgOut->setArticleFlag( true );
533 # Check if user is allowed to look at this page. If not, bail out.
535 if ( !$this->mTitle
->userCanRead() ) {
536 $wgOut->loginToUse();
538 wfProfileOut( __METHOD__
);
539 throw new MWException( "Permission Error: you do not have access to view this page" );
542 # Prepare the header box
544 $sk = $wgUser->getSkin();
546 $next = $this->mTitle
->getNextRevisionID( $this->mNewid
);
550 $nextlink = '<br />' . $sk->link(
552 wfMsgHtml( 'nextdiff' ),
554 'id' => 'differences-nextlink'
558 'oldid' => $this->mNewid
,
566 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\">" .
567 $sk->revUserTools( $this->mNewRev
) . "<br />" . $sk->revComment( $this->mNewRev
) . $nextlink . "</div>\n";
569 $wgOut->addHTML( $header );
571 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
572 $wgOut->setRobotPolicy( 'noindex,nofollow' );
574 wfProfileOut( __METHOD__
);
578 * Get the diff text, send it to $wgOut
579 * Returns false if the diff could not be generated, otherwise returns true
581 function showDiff( $otitle, $ntitle, $notice = '' ) {
583 $diff = $this->getDiff( $otitle, $ntitle, $notice );
584 if ( $diff === false ) {
585 $wgOut->addWikiMsg( 'missing-article', "<nowiki>(fixme, bug)</nowiki>", '' );
588 $this->showDiffStyle();
589 $wgOut->addHTML( $diff );
595 * Add style sheets and supporting JS for diff display.
597 function showDiffStyle() {
599 $wgOut->addModuleStyles( 'mediawiki.legacy.diff' );
600 $wgOut->addModuleScripts( 'mediawiki.legacy.diff' );
604 * Get complete diff table, including header
606 * @param $otitle Title: old title
607 * @param $ntitle Title: new title
608 * @param $notice String: HTML between diff header and body
611 function getDiff( $otitle, $ntitle, $notice = '' ) {
612 $body = $this->getDiffBody();
613 if ( $body === false ) {
616 $multi = $this->getMultiNotice();
617 return $this->addHeader( $body, $otitle, $ntitle, $multi, $notice );
622 * Get the diff table body, without header
624 * @return mixed (string/false)
626 public function getDiffBody() {
628 wfProfileIn( __METHOD__
);
629 $this->mCacheHit
= true;
630 // Check if the diff should be hidden from this user
631 if ( !$this->loadRevisionData() ) {
632 wfProfileOut( __METHOD__
);
634 } elseif ( $this->mOldRev
&& !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
635 wfProfileOut( __METHOD__
);
637 } elseif ( $this->mNewRev
&& !$this->mNewRev
->userCan( Revision
::DELETED_TEXT
) ) {
638 wfProfileOut( __METHOD__
);
642 if ( $this->mOldRev
&& $this->mNewRev
643 && $this->mOldRev
->getID() == $this->mNewRev
->getID() )
645 wfProfileOut( __METHOD__
);
650 if ( $this->mOldid
&& $this->mNewid
) {
651 $key = wfMemcKey( 'diff', 'version', MW_DIFF_VERSION
,
652 'oldid', $this->mOldid
, 'newid', $this->mNewid
);
654 if ( !$this->mRefreshCache
) {
655 $difftext = $wgMemc->get( $key );
657 wfIncrStats( 'diff_cache_hit' );
658 $difftext = $this->localiseLineNumbers( $difftext );
659 $difftext .= "\n<!-- diff cache key $key -->\n";
660 wfProfileOut( __METHOD__
);
663 } // don't try to load but save the result
665 $this->mCacheHit
= false;
667 // Loadtext is permission safe, this just clears out the diff
668 if ( !$this->loadText() ) {
669 wfProfileOut( __METHOD__
);
673 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
675 // Save to cache for 7 days
676 if ( !wfRunHooks( 'AbortDiffCache', array( &$this ) ) ) {
677 wfIncrStats( 'diff_uncacheable' );
678 } elseif ( $key !== false && $difftext !== false ) {
679 wfIncrStats( 'diff_cache_miss' );
680 $wgMemc->set( $key, $difftext, 7 * 86400 );
682 wfIncrStats( 'diff_uncacheable' );
684 // Replace line numbers with the text in the user's language
685 if ( $difftext !== false ) {
686 $difftext = $this->localiseLineNumbers( $difftext );
688 wfProfileOut( __METHOD__
);
693 * Make sure the proper modules are loaded before we try to
696 private function initDiffEngines() {
697 global $wgExternalDiffEngine;
698 if ( $wgExternalDiffEngine == 'wikidiff' && !function_exists( 'wikidiff_do_diff' ) ) {
699 wfProfileIn( __METHOD__
. '-php_wikidiff.so' );
700 wfSuppressWarnings();
701 dl( 'php_wikidiff.so' );
703 wfProfileOut( __METHOD__
. '-php_wikidiff.so' );
705 else if ( $wgExternalDiffEngine == 'wikidiff2' && !function_exists( 'wikidiff2_do_diff' ) ) {
706 wfProfileIn( __METHOD__
. '-php_wikidiff2.so' );
707 wfSuppressWarnings();
710 wfProfileOut( __METHOD__
. '-php_wikidiff2.so' );
715 * Generate a diff, no caching
717 * @param $otext String: old text, must be already segmented
718 * @param $ntext String: new text, must be already segmented
720 function generateDiffBody( $otext, $ntext ) {
721 global $wgExternalDiffEngine, $wgContLang;
723 $otext = str_replace( "\r\n", "\n", $otext );
724 $ntext = str_replace( "\r\n", "\n", $ntext );
726 $this->initDiffEngines();
728 if ( $wgExternalDiffEngine == 'wikidiff' && function_exists( 'wikidiff_do_diff' ) ) {
729 # For historical reasons, external diff engine expects
730 # input text to be HTML-escaped already
731 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
732 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
733 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) ) .
734 $this->debug( 'wikidiff1' );
737 if ( $wgExternalDiffEngine == 'wikidiff2' && function_exists( 'wikidiff2_do_diff' ) ) {
738 # Better external diff engine, the 2 may some day be dropped
739 # This one does the escaping and segmenting itself
740 wfProfileIn( 'wikidiff2_do_diff' );
741 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
742 $text .= $this->debug( 'wikidiff2' );
743 wfProfileOut( 'wikidiff2_do_diff' );
746 if ( $wgExternalDiffEngine != 'wikidiff3' && $wgExternalDiffEngine !== false ) {
748 global $wgTmpDirectory;
749 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
750 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
752 $tempFile1 = fopen( $tempName1, "w" );
754 wfProfileOut( __METHOD__
);
757 $tempFile2 = fopen( $tempName2, "w" );
759 wfProfileOut( __METHOD__
);
762 fwrite( $tempFile1, $otext );
763 fwrite( $tempFile2, $ntext );
764 fclose( $tempFile1 );
765 fclose( $tempFile2 );
766 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
767 wfProfileIn( __METHOD__
. "-shellexec" );
768 $difftext = wfShellExec( $cmd );
769 $difftext .= $this->debug( "external $wgExternalDiffEngine" );
770 wfProfileOut( __METHOD__
. "-shellexec" );
771 unlink( $tempName1 );
772 unlink( $tempName2 );
773 wfProfileOut( __METHOD__
);
778 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
779 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
780 $diffs = new Diff( $ota, $nta );
781 $formatter = new TableDiffFormatter();
782 $difftext = $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) ) .
783 wfProfileOut( __METHOD__
);
788 * Generate a debug comment indicating diff generating time,
789 * server node, and generator backend.
791 protected function debug( $generator = "internal" ) {
792 global $wgShowHostnames;
793 if ( !$this->enableDebugComment
) {
796 $data = array( $generator );
797 if ( $wgShowHostnames ) {
798 $data[] = wfHostname();
800 $data[] = wfTimestamp( TS_DB
);
801 return "<!-- diff generator: " .
810 * Replace line numbers with the text in the user's language
812 function localiseLineNumbers( $text ) {
813 return preg_replace_callback( '/<!--LINE (\d+)-->/',
814 array( &$this, 'localiseLineNumbersCb' ), $text );
817 function localiseLineNumbersCb( $matches ) {
819 if ( $matches[1] === '1' && $this->mReducedLineNumbers
) return '';
820 return wfMsgExt( 'lineno', 'escape', $wgLang->formatNum( $matches[1] ) );
825 * If there are revisions between the ones being compared, return a note saying so.
828 function getMultiNotice() {
829 if ( !is_object( $this->mOldRev
) ||
!is_object( $this->mNewRev
) ) {
831 } elseif ( !$this->mOldPage
->equals( $this->mNewPage
) ) {
832 // Comparing two different pages? Count would be meaningless.
836 $oldid = $this->mOldRev
->getId();
837 $newid = $this->mNewRev
->getId();
838 if ( $oldid > $newid ) {
839 $tmp = $oldid; $oldid = $newid; $newid = $tmp;
842 $nEdits = $this->mTitle
->countRevisionsBetween( $oldid, $newid );
845 // We use ($limit + 1) so we can detect if there are > 100 authors
846 // in a given revision range. In that case, diff-multi-manyusers is used.
847 $numUsers = $this->mTitle
->countAuthorsBetween( $oldid, $newid, $limit +
1 );
848 return self
::intermediateEditsMsg( $nEdits, $numUsers, $limit );
850 return ''; // nothing
854 * Get a notice about how many intermediate edits and users there are
855 * @param $numEdits int
856 * @param $numUsers int
860 public static function intermediateEditsMsg( $numEdits, $numUsers, $limit ) {
862 if ( $numUsers > $limit ) {
863 $msg = 'diff-multi-manyusers';
868 return wfMsgExt( $msg, 'parseinline',
869 $wgLang->formatnum( $numEdits ), $wgLang->formatnum( $numUsers ) );
873 * Add the header to a diff body
875 static function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) {
876 $header = "<table class='diff'>";
877 if ( $diff ) { // Safari/Chrome show broken output if cols not used
879 <col class='diff-marker' />
880 <col class='diff-content' />
881 <col class='diff-marker' />
882 <col class='diff-content' />";
891 <td colspan='$colspan' class='diff-otitle'>{$otitle}</td>
892 <td colspan='$colspan' class='diff-ntitle'>{$ntitle}</td>
895 if ( $multi != '' ) {
896 $header .= "<tr><td colspan='{$multiColspan}' align='center' class='diff-multi'>{$multi}</td></tr>";
898 if ( $notice != '' ) {
899 $header .= "<tr><td colspan='{$multiColspan}' align='center'>{$notice}</td></tr>";
902 return $header . $diff . "</table>";
906 * Use specified text instead of loading from the database
908 function setText( $oldText, $newText ) {
909 $this->mOldtext
= $oldText;
910 $this->mNewtext
= $newText;
911 $this->mTextLoaded
= 2;
912 $this->mRevisionsLoaded
= true;
916 * Load revision metadata for the specified articles. If newid is 0, then compare
917 * the old article in oldid to the current article; if oldid is 0, then
918 * compare the current article to the immediately previous one (ignoring the
921 * If oldid is false, leave the corresponding revision object set
922 * to false. This is impossible via ordinary user input, and is provided for
925 function loadRevisionData() {
926 global $wgLang, $wgUser;
927 if ( $this->mRevisionsLoaded
) {
930 // Whether it succeeds or fails, we don't want to try again
931 $this->mRevisionsLoaded
= true;
934 // Load the new revision object
935 $this->mNewRev
= $this->mNewid
936 ? Revision
::newFromId( $this->mNewid
)
937 : Revision
::newFromTitle( $this->mTitle
);
938 if ( !$this->mNewRev
instanceof Revision
)
941 // Update the new revision ID in case it was 0 (makes life easier doing UI stuff)
942 $this->mNewid
= $this->mNewRev
->getId();
944 // Check if page is editable
945 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
947 // Set assorted variables
948 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
949 $dateofrev = $wgLang->date( $this->mNewRev
->getTimestamp(), true );
950 $timeofrev = $wgLang->time( $this->mNewRev
->getTimestamp(), true );
951 $this->mNewPage
= $this->mNewRev
->getTitle();
952 if ( $this->mNewRev
->isCurrent() ) {
953 $newLink = $this->mNewPage
->escapeLocalUrl( array(
954 'oldid' => $this->mNewid
956 $this->mPagetitle
= htmlspecialchars( wfMsg(
962 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
966 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
967 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
969 $newLink = $this->mNewPage
->escapeLocalUrl( array(
970 'oldid' => $this->mNewid
972 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
974 'oldid' => $this->mNewid
976 $this->mPagetitle
= htmlspecialchars( wfMsg(
983 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
984 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
986 if ( !$this->mNewRev
->userCan( Revision
::DELETED_TEXT
) ) {
987 $this->mNewtitle
= "<span class='history-deleted'>{$this->mPagetitle}</span>";
988 } else if ( $this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
989 $this->mNewtitle
= "<span class='history-deleted'>{$this->mNewtitle}</span>";
992 // Load the old revision object
993 $this->mOldRev
= false;
994 if ( $this->mOldid
) {
995 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
996 } elseif ( $this->mOldid
=== 0 ) {
997 $rev = $this->mNewRev
->getPrevious();
999 $this->mOldid
= $rev->getId();
1000 $this->mOldRev
= $rev;
1002 // No previous revision; mark to show as first-version only.
1003 $this->mOldid
= false;
1004 $this->mOldRev
= false;
1006 } /* elseif ( $this->mOldid === false ) leave mOldRev false; */
1008 if ( is_null( $this->mOldRev
) ) {
1012 if ( $this->mOldRev
) {
1013 $this->mOldPage
= $this->mOldRev
->getTitle();
1015 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
1016 $dateofrev = $wgLang->date( $this->mOldRev
->getTimestamp(), true );
1017 $timeofrev = $wgLang->time( $this->mOldRev
->getTimestamp(), true );
1018 $oldLink = $this->mOldPage
->escapeLocalUrl( array(
1019 'oldid' => $this->mOldid
1021 $oldEdit = $this->mOldPage
->escapeLocalUrl( array(
1023 'oldid' => $this->mOldid
1025 $this->mOldPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t, $dateofrev, $timeofrev ) );
1027 $this->mOldtitle
= "<a href='$oldLink'>{$this->mOldPagetitle}</a>"
1028 . " (<a href='$oldEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
1029 // Add an "undo" link
1030 if ( $editable && !$this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) && !$this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1031 $undoLink = Html
::element( 'a', array(
1032 'href' => $this->mNewPage
->getLocalUrl( array(
1034 'undoafter' => $this->mOldid
,
1035 'undo' => $this->mNewid
) ),
1036 'title' => $wgUser->getSkin()->titleAttrib( 'undo' )
1037 ), wfMsg( 'editundo' ) );
1038 $this->mNewtitle
.= ' (' . $undoLink . ')';
1041 if ( !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
1042 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldPagetitle
. '</span>';
1043 } else if ( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1044 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldtitle
. '</span>';
1052 * Load the text of the revisions, as well as revision data.
1054 function loadText() {
1055 if ( $this->mTextLoaded
== 2 ) {
1058 // Whether it succeeds or fails, we don't want to try again
1059 $this->mTextLoaded
= 2;
1062 if ( !$this->loadRevisionData() ) {
1065 if ( $this->mOldRev
) {
1066 $this->mOldtext
= $this->mOldRev
->getText( Revision
::FOR_THIS_USER
);
1067 if ( $this->mOldtext
=== false ) {
1071 if ( $this->mNewRev
) {
1072 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);
1073 if ( $this->mNewtext
=== false ) {
1081 * Load the text of the new revision, not the old one
1083 function loadNewText() {
1084 if ( $this->mTextLoaded
>= 1 ) {
1087 $this->mTextLoaded
= 1;
1089 if ( !$this->loadRevisionData() ) {
1092 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);