a43eba98c996f53a05cee5929ba1148e2a14762f
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
{
26 var $mOldtitle, $mNewtitle, $mPagetitle;
27 var $mOldtext, $mNewtext;
32 var $mOldPage, $mNewPage, $mTitle;
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;
105 function setReducedLineNumbers( $value = true ) {
106 $this->mReducedLineNumbers
= $value;
112 function getTitle() {
113 return $this->mTitle
;
119 function wasCacheHit() {
120 return $this->mCacheHit
;
126 function getOldid() {
127 return $this->mOldid
;
133 function getNewid() {
134 return $this->mNewid
;
138 * Look up a special:Undelete link to the given deleted revision id,
139 * as a workaround for being unable to load deleted diffs in currently.
141 * @param int $id revision ID
142 * @return mixed URL or false
144 function deletedLink( $id ) {
146 if ( $wgUser->isAllowed( 'deletedhistory' ) ) {
147 $dbr = wfGetDB( DB_SLAVE
);
148 $row = $dbr->selectRow('archive', '*',
149 array( 'ar_rev_id' => $id ),
152 $rev = Revision
::newFromArchiveRow( $row );
153 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
154 return SpecialPage
::getTitleFor( 'Undelete' )->getFullURL( array(
155 'target' => $title->getPrefixedText(),
156 'timestamp' => $rev->getTimestamp()
164 * Build a wikitext link toward a deleted revision, if viewable.
166 * @param int $id revision ID
167 * @return string wikitext fragment
169 function deletedIdMarker( $id ) {
170 $link = $this->deletedLink( $id );
172 return "[$link $id]";
178 function showDiffPage( $diffOnly = false ) {
179 global $wgUser, $wgOut, $wgUseExternalEditor, $wgUseRCPatrol;
180 wfProfileIn( __METHOD__
);
182 # Allow frames except in certain special cases
183 $wgOut->allowClickjacking();
185 # If external diffs are enabled both globally and for the user,
186 # we'll use the application/x-external-editor interface to call
187 # an external diff tool like kompare, kdiff3, etc.
188 if ( $wgUseExternalEditor && $wgUser->getOption( 'externaldiff' ) ) {
189 global $wgServer, $wgScript, $wgLang;
191 header ( "Content-type: application/x-external-editor; charset=UTF-8" );
192 $url1 = $this->mTitle
->getFullURL( array(
194 'oldid' => $this->mOldid
196 $url2 = $this->mTitle
->getFullURL( array(
198 'oldid' => $this->mNewid
200 $special = $wgLang->getNsText( NS_SPECIAL
);
201 $control = <<<CONTROL
205 Script={$wgServer}{$wgScript}
206 Special namespace={$special}
218 wfProfileOut( __METHOD__
);
222 $wgOut->setArticleFlag( false );
223 if ( !$this->loadRevisionData() ) {
224 // Sounds like a deleted revision... Let's see what we can do.
225 $deletedLink = $this->deletedLink( $this->mNewid
);
227 $t = $this->mTitle
->getPrefixedText();
228 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ),
229 $this->deletedIdMarker( $this->mOldid
),
230 $this->deletedIdMarker( $this->mNewid
) );
231 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
232 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", "<span class='plainlinks'>$d</span>" );
233 wfProfileOut( __METHOD__
);
237 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
239 if ( $this->mNewRev
->isCurrent() ) {
240 $wgOut->setArticleFlag( true );
243 # mOldid is false if the difference engine is called with a "vague" query for
244 # a diff between a version V and its previous version V' AND the version V
245 # is the first version of that article. In that case, V' does not exist.
246 if ( $this->mOldid
=== false ) {
247 $this->showFirstRevision();
248 $this->renderNewRevision(); // should we respect $diffOnly here or not?
249 wfProfileOut( __METHOD__
);
253 $oldTitle = $this->mOldPage
->getPrefixedText();
254 $newTitle = $this->mNewPage
->getPrefixedText();
255 if ( $oldTitle == $newTitle ) {
256 $wgOut->setPageTitle( $newTitle );
258 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
260 if ( $this->mNewPage
->equals( $this->mOldPage
) ) {
261 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
263 $wgOut->setSubtitle( wfMsgExt( 'difference-multipage', array( 'parseinline' ) ) );
265 $wgOut->setRobotPolicy( 'noindex,nofollow' );
267 if ( !$this->mOldPage
->userCanRead() ||
!$this->mNewPage
->userCanRead() ) {
268 $wgOut->loginToUse();
271 wfProfileOut( __METHOD__
);
275 $sk = $wgUser->getSkin();
276 if ( method_exists( $sk, 'suppressQuickbar' ) ) {
277 $sk->suppressQuickbar();
280 // Check if page is editable
281 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
282 if ( $editable && $this->mNewRev
->isCurrent() && $wgUser->isAllowed( 'rollback' ) ) {
283 $wgOut->preventClickjacking();
284 $rollback = '   ' . $sk->generateRollback( $this->mNewRev
);
289 // Prepare a change patrol link, if applicable
290 if ( $wgUseRCPatrol && $this->mTitle
->userCan( 'patrol' ) ) {
291 // If we've been given an explicit change identifier, use it; saves time
292 if ( $this->mRcidMarkPatrolled
) {
293 $rcid = $this->mRcidMarkPatrolled
;
294 $rc = RecentChange
::newFromId( $rcid );
295 // Already patrolled?
296 $rcid = is_object( $rc ) && !$rc->getAttribute( 'rc_patrolled' ) ?
$rcid : 0;
298 // Look for an unpatrolled change corresponding to this diff
299 $db = wfGetDB( DB_SLAVE
);
300 $change = RecentChange
::newFromConds(
302 // Redundant user,timestamp condition so we can use the existing index
303 'rc_user_text' => $this->mNewRev
->getRawUserText(),
304 'rc_timestamp' => $db->timestamp( $this->mNewRev
->getTimestamp() ),
305 'rc_this_oldid' => $this->mNewid
,
306 'rc_last_oldid' => $this->mOldid
,
311 if ( $change instanceof RecentChange
) {
312 $rcid = $change->mAttribs
['rc_id'];
313 $this->mRcidMarkPatrolled
= $rcid;
321 $wgOut->preventClickjacking();
322 $token = $wgUser->editToken( $rcid );
323 $patrol = ' <span class="patrollink">[' . $sk->link(
325 wfMsgHtml( 'markaspatrolleddiff' ),
328 'action' => 'markpatrolled',
344 # Carry over 'diffonly' param via navigation links
345 if ( $diffOnly != $wgUser->getBoolOption( 'diffonly' ) ) {
346 $query['diffonly'] = $diffOnly;
349 # Make "previous revision link"
350 $query['diff'] = 'prev';
351 $query['oldid'] = $this->mOldid
;
352 # Cascade unhide param in links for easy deletion browsing
353 if ( $this->unhide
) {
354 $query['unhide'] = 1;
356 if ( !$this->mOldRev
->getPrevious() ) {
357 $prevlink = ' ';
359 $prevlink = $sk->link(
361 wfMsgHtml( 'previousdiff' ),
363 'id' => 'differences-prevlink'
373 # Make "next revision link"
374 $query['diff'] = 'next';
375 $query['oldid'] = $this->mNewid
;
376 # Skip next link on the top revision
377 if ( $this->mNewRev
->isCurrent() ) {
378 $nextlink = ' ';
380 $nextlink = $sk->link(
382 wfMsgHtml( 'nextdiff' ),
384 'id' => 'differences-nextlink'
397 if ( $this->mOldRev
->isMinor() ) {
398 $oldminor = ChangesList
::flag( 'minor' );
400 if ( $this->mNewRev
->isMinor() ) {
401 $newminor = ChangesList
::flag( 'minor' );
404 # Handle RevisionDelete links...
405 $ldel = $this->revisionDeleteLink( $this->mOldRev
);
406 $rdel = $this->revisionDeleteLink( $this->mNewRev
);
408 $oldHeader = '<div id="mw-diff-otitle1"><strong>' . $this->mOldtitle
. '</strong></div>' .
409 '<div id="mw-diff-otitle2">' .
410 $sk->revUserTools( $this->mOldRev
, !$this->unhide
) . '</div>' .
411 '<div id="mw-diff-otitle3">' . $oldminor .
412 $sk->revComment( $this->mOldRev
, !$diffOnly, !$this->unhide
) . $ldel . '</div>' .
413 '<div id="mw-diff-otitle4">' . $prevlink . '</div>';
414 $newHeader = '<div id="mw-diff-ntitle1"><strong>' . $this->mNewtitle
. '</strong></div>' .
415 '<div id="mw-diff-ntitle2">' . $sk->revUserTools( $this->mNewRev
, !$this->unhide
) .
417 '<div id="mw-diff-ntitle3">' . $newminor .
418 $sk->revComment( $this->mNewRev
, !$diffOnly, !$this->unhide
) . $rdel . '</div>' .
419 '<div id="mw-diff-ntitle4">' . $nextlink . $patrol . '</div>';
421 # Check if this user can see the revisions
422 $allowed = $this->mOldRev
->userCan( Revision
::DELETED_TEXT
)
423 && $this->mNewRev
->userCan( Revision
::DELETED_TEXT
);
424 # Check if one of the revisions is deleted/suppressed
425 $deleted = $suppressed = false;
426 if ( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
427 $deleted = true; // old revisions text is hidden
428 if ( $this->mOldRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
429 $suppressed = true; // also suppressed
431 if ( $this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
432 $deleted = true; // new revisions text is hidden
433 if ( $this->mNewRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
434 $suppressed = true; // also suppressed
436 # If the diff cannot be shown due to a deleted revision, then output
437 # the diff header and links to unhide (if available)...
438 if ( $deleted && ( !$this->unhide ||
!$allowed ) ) {
439 $this->showDiffStyle();
440 $multi = $this->getMultiNotice();
441 $wgOut->addHTML( $this->addHeader( '', $oldHeader, $newHeader, $multi ) );
443 $msg = $suppressed ?
'rev-suppressed-no-diff' : 'rev-deleted-no-diff';
444 # Give explanation for why revision is not visible
445 $wgOut->wrapWikiMsg( "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n",
448 # Give explanation and add a link to view the diff...
449 $link = $this->mTitle
->getFullUrl( array(
450 'diff' => $this->mNewid
,
451 'oldid' => $this->mOldid
,
454 $msg = $suppressed ?
'rev-suppressed-unhide-diff' : 'rev-deleted-unhide-diff';
455 $wgOut->wrapWikiMsg( "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n", array( $msg, $link ) );
457 # Otherwise, output a regular diff...
459 # Add deletion notice if the user is viewing deleted content
462 $msg = $suppressed ?
'rev-suppressed-diff-view' : 'rev-deleted-diff-view';
463 $notice = "<div id='mw-$msg' class='mw-warning plainlinks'>\n" . wfMsgExt( $msg, 'parseinline' ) . "</div>\n";
465 $this->showDiff( $oldHeader, $newHeader, $notice );
467 $this->renderNewRevision();
470 wfProfileOut( __METHOD__
);
474 * @param $rev Revision
477 protected function revisionDeleteLink( $rev ) {
480 $canHide = $wgUser->isAllowed( 'deleterevision' );
481 // Show del/undel link if:
482 // (a) the user can delete revisions, or
483 // (b) the user can view deleted revision *and* this one is deleted
484 if ( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
485 $sk = $wgUser->getSkin();
486 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
487 $link = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
490 'type' => 'revision',
491 'target' => $rev->mTitle
->getPrefixedDbkey(),
492 'ids' => $rev->getId()
494 $link = $sk->revDeleteLink( $query,
495 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
497 $link = '   ' . $link . ' ';
503 * Show the new revision of the page.
505 function renderNewRevision() {
506 global $wgOut, $wgUser;
507 wfProfileIn( __METHOD__
);
508 # Add "current version as of X" title
509 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
510 # Page content may be handled by a hooked call instead...
511 if ( wfRunHooks( 'ArticleContentOnDiff', array( $this, $wgOut ) ) ) {
512 # Use the current version parser cache if applicable
514 if ( !$this->mNewRev
->isCurrent() ) {
515 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
519 $this->loadNewText();
520 $wgOut->setRevisionId( $this->mNewRev
->getId() );
522 if ( $this->mTitle
->isCssJsSubpage() ||
$this->mTitle
->isCssOrJsPage() ) {
523 // Stolen from Article::view --AG 2007-10-11
524 // Give hooks a chance to customise the output
525 // @TODO: standardize this crap into one function
526 if ( wfRunHooks( 'ShowRawCssJs', array( $this->mNewtext
, $this->mTitle
, $wgOut ) ) ) {
527 // Wrap the whole lot in a <pre> and don't parse
529 preg_match( '!\.(css|js)$!u', $this->mTitle
->getText(), $m );
530 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
531 $wgOut->addHTML( htmlspecialchars( $this->mNewtext
) );
532 $wgOut->addHTML( "\n</pre>\n" );
534 } elseif ( $pCache ) {
535 $article = new Article( $this->mTitle
, 0 );
536 $pOutput = ParserCache
::singleton()->get( $article, $wgOut->parserOptions() );
538 $wgOut->addParserOutput( $pOutput );
540 $article->doViewParse();
543 $wgOut->addWikiTextTidy( $this->mNewtext
);
546 if ( !$this->mNewRev
->isCurrent() ) {
547 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
550 # Add redundant patrol link on bottom...
551 if ( $this->mRcidMarkPatrolled
&& $this->mTitle
->quickUserCan( 'patrol' ) ) {
552 $sk = $wgUser->getSkin();
553 $token = $wgUser->editToken( $this->mRcidMarkPatrolled
);
554 $wgOut->preventClickjacking();
556 "<div class='patrollink'>[" . $sk->link(
558 wfMsgHtml( 'markaspatrolleddiff' ),
561 'action' => 'markpatrolled',
562 'rcid' => $this->mRcidMarkPatrolled
,
569 wfProfileOut( __METHOD__
);
573 * Show the first revision of an article. Uses normal diff headers in
574 * contrast to normal "old revision" display style.
576 function showFirstRevision() {
577 global $wgOut, $wgUser;
578 wfProfileIn( __METHOD__
);
580 # Get article text from the DB
582 if ( ! $this->loadNewText() ) {
583 $t = $this->mTitle
->getPrefixedText();
584 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ),
585 $this->deletedIdMarker( $this->mOldid
),
586 $this->deletedIdMarker( $this->mNewid
) );
587 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
588 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", "<span class='plainlinks'>$d</span>" );
589 wfProfileOut( __METHOD__
);
592 if ( $this->mNewRev
->isCurrent() ) {
593 $wgOut->setArticleFlag( true );
596 # Check if user is allowed to look at this page. If not, bail out.
598 if ( !$this->mTitle
->userCanRead() ) {
599 $wgOut->loginToUse();
601 wfProfileOut( __METHOD__
);
602 throw new MWException( "Permission Error: you do not have access to view this page" );
605 # Prepare the header box
607 $sk = $wgUser->getSkin();
609 $next = $this->mTitle
->getNextRevisionID( $this->mNewid
);
613 $nextlink = '<br />' . $sk->link(
615 wfMsgHtml( 'nextdiff' ),
617 'id' => 'differences-nextlink'
621 'oldid' => $this->mNewid
,
629 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\">" .
630 $sk->revUserTools( $this->mNewRev
) . "<br />" . $sk->revComment( $this->mNewRev
) . $nextlink . "</div>\n";
632 $wgOut->addHTML( $header );
634 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
635 $wgOut->setRobotPolicy( 'noindex,nofollow' );
637 wfProfileOut( __METHOD__
);
641 * Get the diff text, send it to $wgOut
642 * Returns false if the diff could not be generated, otherwise returns true
646 function showDiff( $otitle, $ntitle, $notice = '' ) {
648 $diff = $this->getDiff( $otitle, $ntitle, $notice );
649 if ( $diff === false ) {
650 $wgOut->addWikiMsg( 'missing-article', "<nowiki>(fixme, bug)</nowiki>", '' );
653 $this->showDiffStyle();
654 $wgOut->addHTML( $diff );
660 * Add style sheets and supporting JS for diff display.
662 function showDiffStyle() {
664 $wgOut->addModuleStyles( 'mediawiki.legacy.diff' );
668 * Get complete diff table, including header
670 * @param $otitle Title: old title
671 * @param $ntitle Title: new title
672 * @param $notice String: HTML between diff header and body
675 function getDiff( $otitle, $ntitle, $notice = '' ) {
676 $body = $this->getDiffBody();
677 if ( $body === false ) {
680 $multi = $this->getMultiNotice();
681 return $this->addHeader( $body, $otitle, $ntitle, $multi, $notice );
686 * Get the diff table body, without header
688 * @return mixed (string/false)
690 public function getDiffBody() {
692 wfProfileIn( __METHOD__
);
693 $this->mCacheHit
= true;
694 // Check if the diff should be hidden from this user
695 if ( !$this->loadRevisionData() ) {
696 wfProfileOut( __METHOD__
);
698 } elseif ( $this->mOldRev
&& !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
699 wfProfileOut( __METHOD__
);
701 } elseif ( $this->mNewRev
&& !$this->mNewRev
->userCan( Revision
::DELETED_TEXT
) ) {
702 wfProfileOut( __METHOD__
);
706 if ( $this->mOldRev
&& $this->mNewRev
707 && $this->mOldRev
->getID() == $this->mNewRev
->getID() )
709 wfProfileOut( __METHOD__
);
714 if ( $this->mOldid
&& $this->mNewid
) {
715 $key = wfMemcKey( 'diff', 'version', MW_DIFF_VERSION
,
716 'oldid', $this->mOldid
, 'newid', $this->mNewid
);
718 if ( !$this->mRefreshCache
) {
719 $difftext = $wgMemc->get( $key );
721 wfIncrStats( 'diff_cache_hit' );
722 $difftext = $this->localiseLineNumbers( $difftext );
723 $difftext .= "\n<!-- diff cache key $key -->\n";
724 wfProfileOut( __METHOD__
);
727 } // don't try to load but save the result
729 $this->mCacheHit
= false;
731 // Loadtext is permission safe, this just clears out the diff
732 if ( !$this->loadText() ) {
733 wfProfileOut( __METHOD__
);
737 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
739 // Save to cache for 7 days
740 if ( !wfRunHooks( 'AbortDiffCache', array( &$this ) ) ) {
741 wfIncrStats( 'diff_uncacheable' );
742 } elseif ( $key !== false && $difftext !== false ) {
743 wfIncrStats( 'diff_cache_miss' );
744 $wgMemc->set( $key, $difftext, 7 * 86400 );
746 wfIncrStats( 'diff_uncacheable' );
748 // Replace line numbers with the text in the user's language
749 if ( $difftext !== false ) {
750 $difftext = $this->localiseLineNumbers( $difftext );
752 wfProfileOut( __METHOD__
);
757 * Make sure the proper modules are loaded before we try to
760 private function initDiffEngines() {
761 global $wgExternalDiffEngine;
762 if ( $wgExternalDiffEngine == 'wikidiff' && !function_exists( 'wikidiff_do_diff' ) ) {
763 wfProfileIn( __METHOD__
. '-php_wikidiff.so' );
764 wfDl( 'php_wikidiff' );
765 wfProfileOut( __METHOD__
. '-php_wikidiff.so' );
767 elseif ( $wgExternalDiffEngine == 'wikidiff2' && !function_exists( 'wikidiff2_do_diff' ) ) {
768 wfProfileIn( __METHOD__
. '-php_wikidiff2.so' );
770 wfProfileOut( __METHOD__
. '-php_wikidiff2.so' );
775 * Generate a diff, no caching
777 * @param $otext String: old text, must be already segmented
778 * @param $ntext String: new text, must be already segmented
780 function generateDiffBody( $otext, $ntext ) {
781 global $wgExternalDiffEngine, $wgContLang;
783 $otext = str_replace( "\r\n", "\n", $otext );
784 $ntext = str_replace( "\r\n", "\n", $ntext );
786 $this->initDiffEngines();
788 if ( $wgExternalDiffEngine == 'wikidiff' && function_exists( 'wikidiff_do_diff' ) ) {
789 # For historical reasons, external diff engine expects
790 # input text to be HTML-escaped already
791 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
792 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
793 return $wgContLang->unsegmentForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) ) .
794 $this->debug( 'wikidiff1' );
797 if ( $wgExternalDiffEngine == 'wikidiff2' && function_exists( 'wikidiff2_do_diff' ) ) {
798 # Better external diff engine, the 2 may some day be dropped
799 # This one does the escaping and segmenting itself
800 wfProfileIn( 'wikidiff2_do_diff' );
801 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
802 $text .= $this->debug( 'wikidiff2' );
803 wfProfileOut( 'wikidiff2_do_diff' );
806 if ( $wgExternalDiffEngine != 'wikidiff3' && $wgExternalDiffEngine !== false ) {
808 global $wgTmpDirectory;
809 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
810 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
812 $tempFile1 = fopen( $tempName1, "w" );
814 wfProfileOut( __METHOD__
);
817 $tempFile2 = fopen( $tempName2, "w" );
819 wfProfileOut( __METHOD__
);
822 fwrite( $tempFile1, $otext );
823 fwrite( $tempFile2, $ntext );
824 fclose( $tempFile1 );
825 fclose( $tempFile2 );
826 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
827 wfProfileIn( __METHOD__
. "-shellexec" );
828 $difftext = wfShellExec( $cmd );
829 $difftext .= $this->debug( "external $wgExternalDiffEngine" );
830 wfProfileOut( __METHOD__
. "-shellexec" );
831 unlink( $tempName1 );
832 unlink( $tempName2 );
833 wfProfileOut( __METHOD__
);
838 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
839 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
840 $diffs = new Diff( $ota, $nta );
841 $formatter = new TableDiffFormatter();
842 $difftext = $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) ) .
843 wfProfileOut( __METHOD__
);
848 * Generate a debug comment indicating diff generating time,
849 * server node, and generator backend.
851 protected function debug( $generator = "internal" ) {
852 global $wgShowHostnames;
853 if ( !$this->enableDebugComment
) {
856 $data = array( $generator );
857 if ( $wgShowHostnames ) {
858 $data[] = wfHostname();
860 $data[] = wfTimestamp( TS_DB
);
861 return "<!-- diff generator: " .
870 * Replace line numbers with the text in the user's language
872 function localiseLineNumbers( $text ) {
873 return preg_replace_callback( '/<!--LINE (\d+)-->/',
874 array( &$this, 'localiseLineNumbersCb' ), $text );
877 function localiseLineNumbersCb( $matches ) {
879 if ( $matches[1] === '1' && $this->mReducedLineNumbers
) return '';
880 return wfMsgExt( 'lineno', 'escape', $wgLang->formatNum( $matches[1] ) );
885 * If there are revisions between the ones being compared, return a note saying so.
888 function getMultiNotice() {
889 if ( !is_object( $this->mOldRev
) ||
!is_object( $this->mNewRev
) ) {
891 } elseif ( !$this->mOldPage
->equals( $this->mNewPage
) ) {
892 // Comparing two different pages? Count would be meaningless.
896 if ( $this->mOldRev
->getTimestamp() > $this->mNewRev
->getTimestamp() ) {
897 $oldRev = $this->mNewRev
; // flip
898 $newRev = $this->mOldRev
; // flip
899 } else { // normal case
900 $oldRev = $this->mOldRev
;
901 $newRev = $this->mNewRev
;
904 $nEdits = $this->mTitle
->countRevisionsBetween( $oldRev, $newRev );
906 $limit = 100; // use diff-multi-manyusers if too many users
907 $numUsers = $this->mTitle
->countAuthorsBetween( $oldRev, $newRev, $limit );
908 return self
::intermediateEditsMsg( $nEdits, $numUsers, $limit );
910 return ''; // nothing
914 * Get a notice about how many intermediate edits and users there are
915 * @param $numEdits int
916 * @param $numUsers int
920 public static function intermediateEditsMsg( $numEdits, $numUsers, $limit ) {
922 if ( $numUsers > $limit ) {
923 $msg = 'diff-multi-manyusers';
928 return wfMsgExt( $msg, 'parseinline',
929 $wgLang->formatnum( $numEdits ), $wgLang->formatnum( $numUsers ) );
933 * Add the header to a diff body
937 static function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) {
938 $header = "<table class='diff'>";
939 if ( $diff ) { // Safari/Chrome show broken output if cols not used
941 <col class='diff-marker' />
942 <col class='diff-content' />
943 <col class='diff-marker' />
944 <col class='diff-content' />";
953 <td colspan='$colspan' class='diff-otitle'>{$otitle}</td>
954 <td colspan='$colspan' class='diff-ntitle'>{$ntitle}</td>
957 if ( $multi != '' ) {
958 $header .= "<tr><td colspan='{$multiColspan}' align='center' class='diff-multi'>{$multi}</td></tr>";
960 if ( $notice != '' ) {
961 $header .= "<tr><td colspan='{$multiColspan}' align='center'>{$notice}</td></tr>";
964 return $header . $diff . "</table>";
968 * Use specified text instead of loading from the database
970 function setText( $oldText, $newText ) {
971 $this->mOldtext
= $oldText;
972 $this->mNewtext
= $newText;
973 $this->mTextLoaded
= 2;
974 $this->mRevisionsLoaded
= true;
978 * Load revision metadata for the specified articles. If newid is 0, then compare
979 * the old article in oldid to the current article; if oldid is 0, then
980 * compare the current article to the immediately previous one (ignoring the
983 * If oldid is false, leave the corresponding revision object set
984 * to false. This is impossible via ordinary user input, and is provided for
989 function loadRevisionData() {
990 global $wgLang, $wgUser;
991 if ( $this->mRevisionsLoaded
) {
994 // Whether it succeeds or fails, we don't want to try again
995 $this->mRevisionsLoaded
= true;
998 // Load the new revision object
999 $this->mNewRev
= $this->mNewid
1000 ? Revision
::newFromId( $this->mNewid
)
1001 : Revision
::newFromTitle( $this->mTitle
);
1002 if ( !$this->mNewRev
instanceof Revision
) {
1006 // Update the new revision ID in case it was 0 (makes life easier doing UI stuff)
1007 $this->mNewid
= $this->mNewRev
->getId();
1009 // Check if page is editable
1010 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
1012 // Set assorted variables
1013 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
1014 $dateofrev = $wgLang->date( $this->mNewRev
->getTimestamp(), true );
1015 $timeofrev = $wgLang->time( $this->mNewRev
->getTimestamp(), true );
1016 $this->mNewPage
= $this->mNewRev
->getTitle();
1017 if ( $this->mNewRev
->isCurrent() ) {
1018 $newLink = $this->mNewPage
->escapeLocalUrl( array(
1019 'oldid' => $this->mNewid
1021 $this->mPagetitle
= htmlspecialchars( wfMsg(
1027 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
1031 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
1032 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
1034 $newLink = $this->mNewPage
->escapeLocalUrl( array(
1035 'oldid' => $this->mNewid
1037 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
1039 'oldid' => $this->mNewid
1041 $this->mPagetitle
= htmlspecialchars( wfMsg(
1048 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
1049 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
1051 if ( !$this->mNewRev
->userCan( Revision
::DELETED_TEXT
) ) {
1052 $this->mNewtitle
= "<span class='history-deleted'>{$this->mPagetitle}</span>";
1053 } elseif ( $this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1054 $this->mNewtitle
= "<span class='history-deleted'>{$this->mNewtitle}</span>";
1057 // Load the old revision object
1058 $this->mOldRev
= false;
1059 if ( $this->mOldid
) {
1060 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
1061 } elseif ( $this->mOldid
=== 0 ) {
1062 $rev = $this->mNewRev
->getPrevious();
1064 $this->mOldid
= $rev->getId();
1065 $this->mOldRev
= $rev;
1067 // No previous revision; mark to show as first-version only.
1068 $this->mOldid
= false;
1069 $this->mOldRev
= false;
1071 } /* elseif ( $this->mOldid === false ) leave mOldRev false; */
1073 if ( is_null( $this->mOldRev
) ) {
1077 if ( $this->mOldRev
) {
1078 $this->mOldPage
= $this->mOldRev
->getTitle();
1080 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
1081 $dateofrev = $wgLang->date( $this->mOldRev
->getTimestamp(), true );
1082 $timeofrev = $wgLang->time( $this->mOldRev
->getTimestamp(), true );
1083 $oldLink = $this->mOldPage
->escapeLocalUrl( array(
1084 'oldid' => $this->mOldid
1086 $oldEdit = $this->mOldPage
->escapeLocalUrl( array(
1088 'oldid' => $this->mOldid
1090 $this->mOldPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t, $dateofrev, $timeofrev ) );
1092 $this->mOldtitle
= "<a href='$oldLink'>{$this->mOldPagetitle}</a>"
1093 . " (<a href='$oldEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
1094 // Add an "undo" link
1095 if ( $editable && !$this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) && !$this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1096 $undoLink = Html
::element( 'a', array(
1097 'href' => $this->mNewPage
->getLocalUrl( array(
1099 'undoafter' => $this->mOldid
,
1100 'undo' => $this->mNewid
) ),
1101 'title' => $wgUser->getSkin()->titleAttrib( 'undo' )
1102 ), wfMsg( 'editundo' ) );
1103 $this->mNewtitle
.= ' (' . $undoLink . ')';
1106 if ( !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
1107 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldPagetitle
. '</span>';
1108 } elseif ( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1109 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldtitle
. '</span>';
1117 * Load the text of the revisions, as well as revision data.
1121 function loadText() {
1122 if ( $this->mTextLoaded
== 2 ) {
1125 // Whether it succeeds or fails, we don't want to try again
1126 $this->mTextLoaded
= 2;
1129 if ( !$this->loadRevisionData() ) {
1132 if ( $this->mOldRev
) {
1133 $this->mOldtext
= $this->mOldRev
->getText( Revision
::FOR_THIS_USER
);
1134 if ( $this->mOldtext
=== false ) {
1138 if ( $this->mNewRev
) {
1139 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);
1140 if ( $this->mNewtext
=== false ) {
1148 * Load the text of the new revision, not the old one
1152 function loadNewText() {
1153 if ( $this->mTextLoaded
>= 1 ) {
1156 $this->mTextLoaded
= 1;
1158 if ( !$this->loadRevisionData() ) {
1161 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);