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;
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__
);
119 # If external diffs are enabled both globally and for the user,
120 # we'll use the application/x-external-editor interface to call
121 # an external diff tool like kompare, kdiff3, etc.
122 if($wgUseExternalEditor && $wgUser->getOption('externaldiff')) {
123 global $wgInputEncoding,$wgServer,$wgScript,$wgLang;
125 header ( "Content-type: application/x-external-editor; charset=".$wgInputEncoding );
126 $url1=$this->mTitle
->getFullURL( array(
128 'oldid' => $this->mOldid
130 $url2=$this->mTitle
->getFullURL( array(
132 'oldid' => $this->mNewid
134 $special=$wgLang->getNsText(NS_SPECIAL
);
139 Script={$wgServer}{$wgScript}
140 Special namespace={$special}
154 $wgOut->setArticleFlag( false );
155 if ( !$this->loadRevisionData() ) {
156 $t = $this->mTitle
->getPrefixedText();
157 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid
, $this->mNewid
);
158 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
159 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
160 wfProfileOut( __METHOD__
);
164 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
166 if ( $this->mNewRev
->isCurrent() ) {
167 $wgOut->setArticleFlag( true );
170 # mOldid is false if the difference engine is called with a "vague" query for
171 # a diff between a version V and its previous version V' AND the version V
172 # is the first version of that article. In that case, V' does not exist.
173 if ( $this->mOldid
=== false ) {
174 $this->showFirstRevision();
175 $this->renderNewRevision(); // should we respect $diffOnly here or not?
176 wfProfileOut( __METHOD__
);
180 $wgOut->suppressQuickbar();
182 $oldTitle = $this->mOldPage
->getPrefixedText();
183 $newTitle = $this->mNewPage
->getPrefixedText();
184 if( $oldTitle == $newTitle ) {
185 $wgOut->setPageTitle( $newTitle );
187 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
189 if ( $this->mNewPage
->equals( $this->mOldPage
) ) {
190 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
192 $wgOut->setSubtitle( wfMsgExt( 'difference-multipage', array( 'parseinline' ) ) );
194 $wgOut->setRobotPolicy( 'noindex,nofollow' );
196 if ( !$this->mOldPage
->userCanRead() ||
!$this->mNewPage
->userCanRead() ) {
197 $wgOut->loginToUse();
200 wfProfileOut( __METHOD__
);
204 $sk = $wgUser->getSkin();
206 // Check if page is editable
207 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
208 if ( $editable && $this->mNewRev
->isCurrent() && $wgUser->isAllowed( 'rollback' ) ) {
209 $rollback = '   ' . $sk->generateRollback( $this->mNewRev
);
214 // Prepare a change patrol link, if applicable
215 if( $wgUseRCPatrol && $this->mTitle
->userCan('patrol') ) {
216 // If we've been given an explicit change identifier, use it; saves time
217 if( $this->mRcidMarkPatrolled
) {
218 $rcid = $this->mRcidMarkPatrolled
;
219 $rc = RecentChange
::newFromId( $rcid );
220 // Already patrolled?
221 $rcid = is_object($rc) && !$rc->getAttribute('rc_patrolled') ?
$rcid : 0;
223 // Look for an unpatrolled change corresponding to this diff
224 $db = wfGetDB( DB_SLAVE
);
225 $change = RecentChange
::newFromConds(
227 // Redundant user,timestamp condition so we can use the existing index
228 'rc_user_text' => $this->mNewRev
->getRawUserText(),
229 'rc_timestamp' => $db->timestamp( $this->mNewRev
->getTimestamp() ),
230 'rc_this_oldid' => $this->mNewid
,
231 'rc_last_oldid' => $this->mOldid
,
236 if( $change instanceof RecentChange
) {
237 $rcid = $change->mAttribs
['rc_id'];
238 $this->mRcidMarkPatrolled
= $rcid;
246 $token = $wgUser->editToken( $rcid );
247 $patrol = ' <span class="patrollink">[' . $sk->link(
249 wfMsgHtml( 'markaspatrolleddiff' ),
252 'action' => 'markpatrolled',
268 # Carry over 'diffonly' param via navigation links
269 if( $diffOnly != $wgUser->getBoolOption('diffonly') ) {
270 $query['diffonly'] = $diffOnly;
273 # Make "previous revision link"
274 $query['diff'] = 'prev';
275 $query['oldid'] = $this->mOldid
;
276 # Cascade unhide param in links for easy deletion browsing
277 if( $this->unhide
) {
278 $query['unhide'] = 1;
280 if( !$this->mOldRev
->getPrevious() ) {
281 $prevlink = ' ';
283 $prevlink = $sk->link(
285 wfMsgHtml( 'previousdiff' ),
287 'id' => 'differences-prevlink'
297 # Make "next revision link"
298 $query['diff'] = 'next';
299 $query['oldid'] = $this->mNewid
;
300 # Skip next link on the top revision
301 if( $this->mNewRev
->isCurrent() ) {
302 $nextlink = ' ';
304 $nextlink = $sk->link(
306 wfMsgHtml( 'nextdiff' ),
308 'id' => 'differences-nextlink'
321 if( $this->mOldRev
->isMinor() ) {
322 $oldminor = ChangesList
::flag( 'minor' );
324 if( $this->mNewRev
->isMinor() ) {
325 $newminor = ChangesList
::flag( 'minor' );
328 # Handle RevisionDelete links...
329 $ldel = $this->revisionDeleteLink( $this->mOldRev
);
330 $rdel = $this->revisionDeleteLink( $this->mNewRev
);
332 $oldHeader = '<div id="mw-diff-otitle1"><strong>'.$this->mOldtitle
.'</strong></div>' .
333 '<div id="mw-diff-otitle2">' .
334 $sk->revUserTools( $this->mOldRev
, !$this->unhide
).'</div>' .
335 '<div id="mw-diff-otitle3">' . $oldminor .
336 $sk->revComment( $this->mOldRev
, !$diffOnly, !$this->unhide
).$ldel.'</div>' .
337 '<div id="mw-diff-otitle4">' . $prevlink .'</div>';
338 $newHeader = '<div id="mw-diff-ntitle1"><strong>'.$this->mNewtitle
.'</strong></div>' .
339 '<div id="mw-diff-ntitle2">' . $sk->revUserTools( $this->mNewRev
, !$this->unhide
) .
341 '<div id="mw-diff-ntitle3">' . $newminor .
342 $sk->revComment( $this->mNewRev
, !$diffOnly, !$this->unhide
).$rdel.'</div>' .
343 '<div id="mw-diff-ntitle4">' . $nextlink . $patrol . '</div>';
345 # Check if this user can see the revisions
346 $allowed = $this->mOldRev
->userCan(Revision
::DELETED_TEXT
)
347 && $this->mNewRev
->userCan(Revision
::DELETED_TEXT
);
348 # Check if one of the revisions is deleted/suppressed
349 $deleted = $suppressed = false;
350 if( $this->mOldRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
351 $deleted = true; // old revisions text is hidden
352 if( $this->mOldRev
->isDeleted(Revision
::DELETED_RESTRICTED
) )
353 $suppressed = true; // also suppressed
355 if( $this->mNewRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
356 $deleted = true; // new revisions text is hidden
357 if( $this->mNewRev
->isDeleted(Revision
::DELETED_RESTRICTED
) )
358 $suppressed = true; // also suppressed
360 # If the diff cannot be shown due to a deleted revision, then output
361 # the diff header and links to unhide (if available)...
362 if( $deleted && (!$this->unhide ||
!$allowed) ) {
363 $this->showDiffStyle();
364 $multi = $this->getMultiNotice();
365 $wgOut->addHTML( $this->addHeader( '', $oldHeader, $newHeader, $multi ) );
367 $msg = $suppressed ?
'rev-suppressed-no-diff' : 'rev-deleted-no-diff';
368 # Give explanation for why revision is not visible
369 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
372 # Give explanation and add a link to view the diff...
373 $link = $this->mTitle
->getFullUrl( array(
374 'diff' => $this->mNewid
,
375 'oldid' => $this->mOldid
,
378 $msg = $suppressed ?
'rev-suppressed-unhide-diff' : 'rev-deleted-unhide-diff';
379 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", array( $msg, $link ) );
381 # Otherwise, output a regular diff...
383 # Add deletion notice if the user is viewing deleted content
386 $msg = $suppressed ?
'rev-suppressed-diff-view' : 'rev-deleted-diff-view';
387 $notice = "<div class='mw-warning plainlinks'>\n".wfMsgExt($msg,'parseinline')."</div>\n";
389 $this->showDiff( $oldHeader, $newHeader, $notice );
391 $this->renderNewRevision();
394 wfProfileOut( __METHOD__
);
397 protected function revisionDeleteLink( $rev ) {
400 $canHide = $wgUser->isAllowed( 'deleterevision' );
401 // Show del/undel link if:
402 // (a) the user can delete revisions, or
403 // (b) the user can view deleted revision *and* this one is deleted
404 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' )) ) {
405 $sk = $wgUser->getSkin();
406 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
407 $link = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
410 'type' => 'revision',
411 'target' => $rev->mTitle
->getPrefixedDbkey(),
412 'ids' => $rev->getId()
414 $link = $sk->revDeleteLink( $query,
415 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
417 $link = '   ' . $link . ' ';
423 * Show the new revision of the page.
425 function renderNewRevision() {
426 global $wgOut, $wgUser;
427 wfProfileIn( __METHOD__
);
429 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
430 # Add deleted rev tag if needed
431 if( !$this->mNewRev
->userCan(Revision
::DELETED_TEXT
) ) {
432 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
433 } else if( $this->mNewRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
434 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
438 if( !$this->mNewRev
->isCurrent() ) {
439 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
443 $this->loadNewText();
444 if( is_object( $this->mNewRev
) ) {
445 $wgOut->setRevisionId( $this->mNewRev
->getId() );
448 if( $this->mTitle
->isCssJsSubpage() ||
$this->mTitle
->isCssOrJsPage() ) {
449 // Stolen from Article::view --AG 2007-10-11
450 // Give hooks a chance to customise the output
451 if( wfRunHooks( 'ShowRawCssJs', array( $this->mNewtext
, $this->mTitle
, $wgOut ) ) ) {
452 // Wrap the whole lot in a <pre> and don't parse
454 preg_match( '!\.(css|js)$!u', $this->mTitle
->getText(), $m );
455 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
456 $wgOut->addHTML( htmlspecialchars( $this->mNewtext
) );
457 $wgOut->addHTML( "\n</pre>\n" );
459 } elseif( wfRunHooks( 'ArticleContentOnDiff', array( $this, $wgOut ) ) ) {
461 $article = new Article( $this->mTitle
, 0 );
462 $pOutput = ParserCache
::singleton()->get( $article, $wgOut->parserOptions() );
464 $wgOut->addParserOutput( $pOutput );
466 $article->doViewParse();
469 $wgOut->addWikiTextTidy( $this->mNewtext
);
473 if( is_object( $this->mNewRev
) && !$this->mNewRev
->isCurrent() ) {
474 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
477 # Add redundant patrol link on bottom...
478 if( $this->mRcidMarkPatrolled
&& $this->mTitle
->quickUserCan('patrol') ) {
479 $sk = $wgUser->getSkin();
480 $token = $wgUser->editToken( $this->mRcidMarkPatrolled
);
482 "<div class='patrollink'>[" . $sk->link(
484 wfMsgHtml( 'markaspatrolleddiff' ),
487 'action' => 'markpatrolled',
488 'rcid' => $this->mRcidMarkPatrolled
,
495 wfProfileOut( __METHOD__
);
499 * Show the first revision of an article. Uses normal diff headers in
500 * contrast to normal "old revision" display style.
502 function showFirstRevision() {
503 global $wgOut, $wgUser;
504 wfProfileIn( __METHOD__
);
506 # Get article text from the DB
508 if ( ! $this->loadNewText() ) {
509 $t = $this->mTitle
->getPrefixedText();
510 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid
, $this->mNewid
);
511 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
512 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
513 wfProfileOut( __METHOD__
);
516 if ( $this->mNewRev
->isCurrent() ) {
517 $wgOut->setArticleFlag( true );
520 # Check if user is allowed to look at this page. If not, bail out.
522 if ( !$this->mTitle
->userCanRead() ) {
523 $wgOut->loginToUse();
525 wfProfileOut( __METHOD__
);
526 throw new MWException("Permission Error: you do not have access to view this page");
529 # Prepare the header box
531 $sk = $wgUser->getSkin();
533 $next = $this->mTitle
->getNextRevisionID( $this->mNewid
);
537 $nextlink = '<br />' . $sk->link(
539 wfMsgHtml( 'nextdiff' ),
541 'id' => 'differences-nextlink'
545 'oldid' => $this->mNewid
,
553 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\">" .
554 $sk->revUserTools( $this->mNewRev
) . "<br />" . $sk->revComment( $this->mNewRev
) . $nextlink . "</div>\n";
556 $wgOut->addHTML( $header );
558 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
559 $wgOut->setRobotPolicy( 'noindex,nofollow' );
561 wfProfileOut( __METHOD__
);
565 * Get the diff text, send it to $wgOut
566 * Returns false if the diff could not be generated, otherwise returns true
568 function showDiff( $otitle, $ntitle, $notice = '' ) {
570 $diff = $this->getDiff( $otitle, $ntitle, $notice );
571 if ( $diff === false ) {
572 $wgOut->addWikiMsg( 'missing-article', "<nowiki>(fixme, bug)</nowiki>", '' );
575 $this->showDiffStyle();
576 $wgOut->addHTML( $diff );
582 * Add style sheets and supporting JS for diff display.
584 function showDiffStyle() {
586 $wgOut->addModules( 'mediawiki.legacy.diff' );
590 * Get complete diff table, including header
592 * @param $otitle Title: old title
593 * @param $ntitle Title: new title
594 * @param $notice String: HTML between diff header and body
597 function getDiff( $otitle, $ntitle, $notice = '' ) {
598 $body = $this->getDiffBody();
599 if ( $body === false ) {
602 $multi = $this->getMultiNotice();
603 return $this->addHeader( $body, $otitle, $ntitle, $multi, $notice );
608 * Get the diff table body, without header
612 function getDiffBody() {
614 wfProfileIn( __METHOD__
);
615 $this->mCacheHit
= true;
616 // Check if the diff should be hidden from this user
617 if ( !$this->loadRevisionData() )
619 if ( $this->mOldRev
&& !$this->mOldRev
->userCan(Revision
::DELETED_TEXT
) ) {
621 } else if ( $this->mNewRev
&& !$this->mNewRev
->userCan(Revision
::DELETED_TEXT
) ) {
623 } else if ( $this->mOldRev
&& $this->mNewRev
&& $this->mOldRev
->getID() == $this->mNewRev
->getID() ) {
628 if ( $this->mOldid
&& $this->mNewid
) {
629 $key = wfMemcKey( 'diff', 'version', MW_DIFF_VERSION
, 'oldid', $this->mOldid
, 'newid', $this->mNewid
);
631 if ( !$this->mRefreshCache
) {
632 $difftext = $wgMemc->get( $key );
634 wfIncrStats( 'diff_cache_hit' );
635 $difftext = $this->localiseLineNumbers( $difftext );
636 $difftext .= "\n<!-- diff cache key $key -->\n";
637 wfProfileOut( __METHOD__
);
640 } // don't try to load but save the result
642 $this->mCacheHit
= false;
644 // Loadtext is permission safe, this just clears out the diff
645 if ( !$this->loadText() ) {
646 wfProfileOut( __METHOD__
);
650 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
652 // Save to cache for 7 days
653 if ( !wfRunHooks( 'AbortDiffCache', array( &$this ) ) ) {
654 wfIncrStats( 'diff_uncacheable' );
655 } else if ( $key !== false && $difftext !== false ) {
656 wfIncrStats( 'diff_cache_miss' );
657 $wgMemc->set( $key, $difftext, 7*86400 );
659 wfIncrStats( 'diff_uncacheable' );
661 // Replace line numbers with the text in the user's language
662 if ( $difftext !== false ) {
663 $difftext = $this->localiseLineNumbers( $difftext );
665 wfProfileOut( __METHOD__
);
670 * Make sure the proper modules are loaded before we try to
673 private function initDiffEngines() {
674 global $wgExternalDiffEngine;
675 if ( $wgExternalDiffEngine == 'wikidiff' && !function_exists( 'wikidiff_do_diff' ) ) {
676 wfProfileIn( __METHOD__
. '-php_wikidiff.so' );
677 wfSuppressWarnings();
678 dl( 'php_wikidiff.so' );
680 wfProfileOut( __METHOD__
. '-php_wikidiff.so' );
682 else if ( $wgExternalDiffEngine == 'wikidiff2' && !function_exists( 'wikidiff2_do_diff' ) ) {
683 wfProfileIn( __METHOD__
. '-php_wikidiff2.so' );
684 wfSuppressWarnings();
687 wfProfileOut( __METHOD__
. '-php_wikidiff2.so' );
692 * Generate a diff, no caching
694 * @param $otext String: old text, must be already segmented
695 * @param $ntext String: new text, must be already segmented
697 function generateDiffBody( $otext, $ntext ) {
698 global $wgExternalDiffEngine, $wgContLang;
700 $otext = str_replace( "\r\n", "\n", $otext );
701 $ntext = str_replace( "\r\n", "\n", $ntext );
703 $this->initDiffEngines();
705 if ( $wgExternalDiffEngine == 'wikidiff' && function_exists( 'wikidiff_do_diff' ) ) {
706 # For historical reasons, external diff engine expects
707 # input text to be HTML-escaped already
708 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
709 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
710 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) ) .
711 $this->debug( 'wikidiff1' );
714 if ( $wgExternalDiffEngine == 'wikidiff2' && function_exists( 'wikidiff2_do_diff' ) ) {
715 # Better external diff engine, the 2 may some day be dropped
716 # This one does the escaping and segmenting itself
717 wfProfileIn( 'wikidiff2_do_diff' );
718 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
719 $text .= $this->debug( 'wikidiff2' );
720 wfProfileOut( 'wikidiff2_do_diff' );
723 if ( $wgExternalDiffEngine != 'wikidiff3' && $wgExternalDiffEngine !== false ) {
725 global $wgTmpDirectory;
726 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
727 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
729 $tempFile1 = fopen( $tempName1, "w" );
731 wfProfileOut( __METHOD__
);
734 $tempFile2 = fopen( $tempName2, "w" );
736 wfProfileOut( __METHOD__
);
739 fwrite( $tempFile1, $otext );
740 fwrite( $tempFile2, $ntext );
741 fclose( $tempFile1 );
742 fclose( $tempFile2 );
743 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
744 wfProfileIn( __METHOD__
. "-shellexec" );
745 $difftext = wfShellExec( $cmd );
746 $difftext .= $this->debug( "external $wgExternalDiffEngine" );
747 wfProfileOut( __METHOD__
. "-shellexec" );
748 unlink( $tempName1 );
749 unlink( $tempName2 );
754 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
755 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
756 $diffs = new Diff( $ota, $nta );
757 $formatter = new TableDiffFormatter();
758 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) ) .
763 * Generate a debug comment indicating diff generating time,
764 * server node, and generator backend.
766 protected function debug( $generator="internal" ) {
767 global $wgShowHostnames;
768 if ( !$this->enableDebugComment
) {
771 $data = array( $generator );
772 if( $wgShowHostnames ) {
773 $data[] = wfHostname();
775 $data[] = wfTimestamp( TS_DB
);
776 return "<!-- diff generator: " .
785 * Replace line numbers with the text in the user's language
787 function localiseLineNumbers( $text ) {
788 return preg_replace_callback( '/<!--LINE (\d+)-->/',
789 array( &$this, 'localiseLineNumbersCb' ), $text );
792 function localiseLineNumbersCb( $matches ) {
794 if ( $matches[1] === '1' && $this->mReducedLineNumbers
) return '';
795 return wfMsgExt( 'lineno', 'escape', $wgLang->formatNum( $matches[1] ) );
800 * If there are revisions between the ones being compared, return a note saying so.
802 function getMultiNotice() {
803 if ( !is_object($this->mOldRev
) ||
!is_object($this->mNewRev
) )
806 if( !$this->mOldPage
->equals( $this->mNewPage
) ) {
807 // Comparing two different pages? Count would be meaningless.
811 $oldid = $this->mOldRev
->getId();
812 $newid = $this->mNewRev
->getId();
813 if ( $oldid > $newid ) {
814 $tmp = $oldid; $oldid = $newid; $newid = $tmp;
817 $n = $this->mTitle
->countRevisionsBetween( $oldid, $newid );
822 $dbr = wfGetDB( DB_SLAVE
);
824 // Actually, the limit is $limit + 1. We do this so we can detect
825 // if there are > 100 authors in a given revision range. If they
826 // are, $limit will be passed to diff-multi-manyusers for l10n.
828 $res = $dbr->select( 'revision', 'DISTINCT rev_user_text',
830 'rev_page = ' . $this->mOldRev
->getPage(),
831 'rev_id > ' . $this->mOldRev
->getId(),
832 'rev_id < ' . $this->mNewRev
->getId()
834 array( 'LIMIT' => $limit +
1 )
836 $numUsers = $dbr->numRows( $res );
837 if( $numUsers > $limit ) {
838 $msg = 'diff-multi-manyusers';
843 return wfMsgExt( $msg, array( 'parseinline' ), $wgLang->formatnum( $n ),
844 $wgLang->formatnum( $numUsers ) );
850 * Add the header to a diff body
852 static function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) {
853 $header = "<table class='diff'>";
854 if( $diff ) { // Safari/Chrome show broken output if cols not used
856 <col class='diff-marker' />
857 <col class='diff-content' />
858 <col class='diff-marker' />
859 <col class='diff-content' />";
868 <td colspan='$colspan' class='diff-otitle'>{$otitle}</td>
869 <td colspan='$colspan' class='diff-ntitle'>{$ntitle}</td>
872 if ( $multi != '' ) {
873 $header .= "<tr><td colspan='{$multiColspan}' align='center' class='diff-multi'>{$multi}</td></tr>";
875 if ( $notice != '' ) {
876 $header .= "<tr><td colspan='{$multiColspan}' align='center'>{$notice}</td></tr>";
879 return $header . $diff . "</table>";
883 * Use specified text instead of loading from the database
885 function setText( $oldText, $newText ) {
886 $this->mOldtext
= $oldText;
887 $this->mNewtext
= $newText;
888 $this->mTextLoaded
= 2;
889 $this->mRevisionsLoaded
= true;
893 * Load revision metadata for the specified articles. If newid is 0, then compare
894 * the old article in oldid to the current article; if oldid is 0, then
895 * compare the current article to the immediately previous one (ignoring the
898 * If oldid is false, leave the corresponding revision object set
899 * to false. This is impossible via ordinary user input, and is provided for
902 function loadRevisionData() {
903 global $wgLang, $wgUser;
904 if ( $this->mRevisionsLoaded
) {
907 // Whether it succeeds or fails, we don't want to try again
908 $this->mRevisionsLoaded
= true;
911 // Load the new revision object
912 $this->mNewRev
= $this->mNewid
913 ? Revision
::newFromId( $this->mNewid
)
914 : Revision
::newFromTitle( $this->mTitle
);
915 if( !$this->mNewRev
instanceof Revision
)
918 // Update the new revision ID in case it was 0 (makes life easier doing UI stuff)
919 $this->mNewid
= $this->mNewRev
->getId();
921 // Check if page is editable
922 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
924 // Set assorted variables
925 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
926 $dateofrev = $wgLang->date( $this->mNewRev
->getTimestamp(), true );
927 $timeofrev = $wgLang->time( $this->mNewRev
->getTimestamp(), true );
928 $this->mNewPage
= $this->mNewRev
->getTitle();
929 if( $this->mNewRev
->isCurrent() ) {
930 $newLink = $this->mNewPage
->escapeLocalUrl( array(
931 'oldid' => $this->mNewid
933 $this->mPagetitle
= htmlspecialchars( wfMsg(
939 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
943 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
944 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
946 $newLink = $this->mNewPage
->escapeLocalUrl( array(
947 'oldid' => $this->mNewid
949 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
951 'oldid' => $this->mNewid
953 $this->mPagetitle
= htmlspecialchars( wfMsg(
960 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
961 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
963 if( !$this->mNewRev
->userCan(Revision
::DELETED_TEXT
) ) {
964 $this->mNewtitle
= "<span class='history-deleted'>{$this->mPagetitle}</span>";
965 } else if ( $this->mNewRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
966 $this->mNewtitle
= "<span class='history-deleted'>{$this->mNewtitle}</span>";
969 // Load the old revision object
970 $this->mOldRev
= false;
971 if( $this->mOldid
) {
972 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
973 } elseif ( $this->mOldid
=== 0 ) {
974 $rev = $this->mNewRev
->getPrevious();
976 $this->mOldid
= $rev->getId();
977 $this->mOldRev
= $rev;
979 // No previous revision; mark to show as first-version only.
980 $this->mOldid
= false;
981 $this->mOldRev
= false;
983 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
985 if( is_null( $this->mOldRev
) ) {
989 if ( $this->mOldRev
) {
990 $this->mOldPage
= $this->mOldRev
->getTitle();
992 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
993 $dateofrev = $wgLang->date( $this->mOldRev
->getTimestamp(), true );
994 $timeofrev = $wgLang->time( $this->mOldRev
->getTimestamp(), true );
995 $oldLink = $this->mOldPage
->escapeLocalUrl( array(
996 'oldid' => $this->mOldid
998 $oldEdit = $this->mOldPage
->escapeLocalUrl( array(
1000 'oldid' => $this->mOldid
1002 $this->mOldPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t, $dateofrev, $timeofrev ) );
1004 $this->mOldtitle
= "<a href='$oldLink'>{$this->mOldPagetitle}</a>"
1005 . " (<a href='$oldEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
1006 // Add an "undo" link
1007 $newUndo = $this->mNewPage
->escapeLocalUrl( array(
1009 'undoafter' => $this->mOldid
,
1010 'undo' => $this->mNewid
1012 $htmlLink = htmlspecialchars( wfMsg( 'editundo' ) );
1013 $htmlTitle = $wgUser->getSkin()->titleAttrib( 'undo' );
1014 if( $editable && !$this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) && !$this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1015 $this->mNewtitle
.= " (<a href='$newUndo' $htmlTitle>" . $htmlLink . "</a>)";
1018 if( !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
1019 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldPagetitle
. '</span>';
1020 } else if( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1021 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldtitle
. '</span>';
1029 * Load the text of the revisions, as well as revision data.
1031 function loadText() {
1032 if ( $this->mTextLoaded
== 2 ) {
1035 // Whether it succeeds or fails, we don't want to try again
1036 $this->mTextLoaded
= 2;
1039 if ( !$this->loadRevisionData() ) {
1042 if ( $this->mOldRev
) {
1043 $this->mOldtext
= $this->mOldRev
->getText( Revision
::FOR_THIS_USER
);
1044 if ( $this->mOldtext
=== false ) {
1048 if ( $this->mNewRev
) {
1049 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);
1050 if ( $this->mNewtext
=== false ) {
1058 * Load the text of the new revision, not the old one
1060 function loadNewText() {
1061 if ( $this->mTextLoaded
>= 1 ) {
1064 $this->mTextLoaded
= 1;
1066 if ( !$this->loadRevisionData() ) {
1069 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);