a96184ca59f3272171ffd01ff68096c8bcba9dc2
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 $t = $this->mTitle
->getPrefixedText();
226 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ),
227 $this->deletedIdMarker( $this->mOldid
),
228 $this->deletedIdMarker( $this->mNewid
) );
229 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
230 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", "<span class='plainlinks'>$d</span>" );
231 wfProfileOut( __METHOD__
);
235 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
237 if ( $this->mNewRev
->isCurrent() ) {
238 $wgOut->setArticleFlag( true );
241 # mOldid is false if the difference engine is called with a "vague" query for
242 # a diff between a version V and its previous version V' AND the version V
243 # is the first version of that article. In that case, V' does not exist.
244 if ( $this->mOldid
=== false ) {
245 $this->showFirstRevision();
246 $this->renderNewRevision(); // should we respect $diffOnly here or not?
247 wfProfileOut( __METHOD__
);
251 $oldTitle = $this->mOldPage
->getPrefixedText();
252 $newTitle = $this->mNewPage
->getPrefixedText();
253 if ( $oldTitle == $newTitle ) {
254 $wgOut->setPageTitle( $newTitle );
256 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
258 if ( $this->mNewPage
->equals( $this->mOldPage
) ) {
259 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
261 $wgOut->setSubtitle( wfMsgExt( 'difference-multipage', array( 'parseinline' ) ) );
263 $wgOut->setRobotPolicy( 'noindex,nofollow' );
265 if ( !$this->mOldPage
->userCanRead() ||
!$this->mNewPage
->userCanRead() ) {
266 $wgOut->loginToUse();
269 wfProfileOut( __METHOD__
);
273 $sk = $wgUser->getSkin();
274 if ( method_exists( $sk, 'suppressQuickbar' ) ) {
275 $sk->suppressQuickbar();
278 // Check if page is editable
279 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
280 if ( $editable && $this->mNewRev
->isCurrent() && $wgUser->isAllowed( 'rollback' ) ) {
281 $wgOut->preventClickjacking();
282 $rollback = '   ' . $sk->generateRollback( $this->mNewRev
);
287 // Prepare a change patrol link, if applicable
288 if ( $wgUseRCPatrol && $this->mTitle
->userCan( 'patrol' ) ) {
289 // If we've been given an explicit change identifier, use it; saves time
290 if ( $this->mRcidMarkPatrolled
) {
291 $rcid = $this->mRcidMarkPatrolled
;
292 $rc = RecentChange
::newFromId( $rcid );
293 // Already patrolled?
294 $rcid = is_object( $rc ) && !$rc->getAttribute( 'rc_patrolled' ) ?
$rcid : 0;
296 // Look for an unpatrolled change corresponding to this diff
297 $db = wfGetDB( DB_SLAVE
);
298 $change = RecentChange
::newFromConds(
300 // Redundant user,timestamp condition so we can use the existing index
301 'rc_user_text' => $this->mNewRev
->getRawUserText(),
302 'rc_timestamp' => $db->timestamp( $this->mNewRev
->getTimestamp() ),
303 'rc_this_oldid' => $this->mNewid
,
304 'rc_last_oldid' => $this->mOldid
,
309 if ( $change instanceof RecentChange
) {
310 $rcid = $change->mAttribs
['rc_id'];
311 $this->mRcidMarkPatrolled
= $rcid;
319 $wgOut->preventClickjacking();
320 $token = $wgUser->editToken( $rcid );
321 $patrol = ' <span class="patrollink">[' . $sk->link(
323 wfMsgHtml( 'markaspatrolleddiff' ),
326 'action' => 'markpatrolled',
342 # Carry over 'diffonly' param via navigation links
343 if ( $diffOnly != $wgUser->getBoolOption( 'diffonly' ) ) {
344 $query['diffonly'] = $diffOnly;
347 # Make "previous revision link"
348 $query['diff'] = 'prev';
349 $query['oldid'] = $this->mOldid
;
350 # Cascade unhide param in links for easy deletion browsing
351 if ( $this->unhide
) {
352 $query['unhide'] = 1;
354 if ( !$this->mOldRev
->getPrevious() ) {
355 $prevlink = ' ';
357 $prevlink = $sk->link(
359 wfMsgHtml( 'previousdiff' ),
361 'id' => 'differences-prevlink'
371 # Make "next revision link"
372 $query['diff'] = 'next';
373 $query['oldid'] = $this->mNewid
;
374 # Skip next link on the top revision
375 if ( $this->mNewRev
->isCurrent() ) {
376 $nextlink = ' ';
378 $nextlink = $sk->link(
380 wfMsgHtml( 'nextdiff' ),
382 'id' => 'differences-nextlink'
395 if ( $this->mOldRev
->isMinor() ) {
396 $oldminor = ChangesList
::flag( 'minor' );
398 if ( $this->mNewRev
->isMinor() ) {
399 $newminor = ChangesList
::flag( 'minor' );
402 # Handle RevisionDelete links...
403 $ldel = $this->revisionDeleteLink( $this->mOldRev
);
404 $rdel = $this->revisionDeleteLink( $this->mNewRev
);
406 $oldHeader = '<div id="mw-diff-otitle1"><strong>' . $this->mOldtitle
. '</strong></div>' .
407 '<div id="mw-diff-otitle2">' .
408 $sk->revUserTools( $this->mOldRev
, !$this->unhide
) . '</div>' .
409 '<div id="mw-diff-otitle3">' . $oldminor .
410 $sk->revComment( $this->mOldRev
, !$diffOnly, !$this->unhide
) . $ldel . '</div>' .
411 '<div id="mw-diff-otitle4">' . $prevlink . '</div>';
412 $newHeader = '<div id="mw-diff-ntitle1"><strong>' . $this->mNewtitle
. '</strong></div>' .
413 '<div id="mw-diff-ntitle2">' . $sk->revUserTools( $this->mNewRev
, !$this->unhide
) .
415 '<div id="mw-diff-ntitle3">' . $newminor .
416 $sk->revComment( $this->mNewRev
, !$diffOnly, !$this->unhide
) . $rdel . '</div>' .
417 '<div id="mw-diff-ntitle4">' . $nextlink . $patrol . '</div>';
419 # Check if this user can see the revisions
420 $allowed = $this->mOldRev
->userCan( Revision
::DELETED_TEXT
)
421 && $this->mNewRev
->userCan( Revision
::DELETED_TEXT
);
422 # Check if one of the revisions is deleted/suppressed
423 $deleted = $suppressed = false;
424 if ( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
425 $deleted = true; // old revisions text is hidden
426 if ( $this->mOldRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
427 $suppressed = true; // also suppressed
429 if ( $this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
430 $deleted = true; // new revisions text is hidden
431 if ( $this->mNewRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
432 $suppressed = true; // also suppressed
434 # If the diff cannot be shown due to a deleted revision, then output
435 # the diff header and links to unhide (if available)...
436 if ( $deleted && ( !$this->unhide ||
!$allowed ) ) {
437 $this->showDiffStyle();
438 $multi = $this->getMultiNotice();
439 $wgOut->addHTML( $this->addHeader( '', $oldHeader, $newHeader, $multi ) );
441 $msg = $suppressed ?
'rev-suppressed-no-diff' : 'rev-deleted-no-diff';
442 # Give explanation for why revision is not visible
443 $wgOut->wrapWikiMsg( "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n",
446 # Give explanation and add a link to view the diff...
447 $link = $this->mTitle
->getFullUrl( array(
448 'diff' => $this->mNewid
,
449 'oldid' => $this->mOldid
,
452 $msg = $suppressed ?
'rev-suppressed-unhide-diff' : 'rev-deleted-unhide-diff';
453 $wgOut->wrapWikiMsg( "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n", array( $msg, $link ) );
455 # Otherwise, output a regular diff...
457 # Add deletion notice if the user is viewing deleted content
460 $msg = $suppressed ?
'rev-suppressed-diff-view' : 'rev-deleted-diff-view';
461 $notice = "<div id='mw-$msg' class='mw-warning plainlinks'>\n" . wfMsgExt( $msg, 'parseinline' ) . "</div>\n";
463 $this->showDiff( $oldHeader, $newHeader, $notice );
465 $this->renderNewRevision();
468 wfProfileOut( __METHOD__
);
472 * @param $rev Revision
475 protected function revisionDeleteLink( $rev ) {
478 $canHide = $wgUser->isAllowed( 'deleterevision' );
479 // Show del/undel link if:
480 // (a) the user can delete revisions, or
481 // (b) the user can view deleted revision *and* this one is deleted
482 if ( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
483 $sk = $wgUser->getSkin();
484 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
485 $link = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
488 'type' => 'revision',
489 'target' => $rev->mTitle
->getPrefixedDbkey(),
490 'ids' => $rev->getId()
492 $link = $sk->revDeleteLink( $query,
493 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
495 $link = '   ' . $link . ' ';
501 * Show the new revision of the page.
503 function renderNewRevision() {
504 global $wgOut, $wgUser;
505 wfProfileIn( __METHOD__
);
506 # Add "current version as of X" title
507 $wgOut->addHTML( "<hr class='diff-hr' />
508 <h2 class='diff-currentversion-title'>{$this->mPagetitle}</h2>\n" );
509 # Page content may be handled by a hooked call instead...
510 if ( wfRunHooks( 'ArticleContentOnDiff', array( $this, $wgOut ) ) ) {
511 # Use the current version parser cache if applicable
513 if ( !$this->mNewRev
->isCurrent() ) {
514 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
518 $this->loadNewText();
519 $wgOut->setRevisionId( $this->mNewRev
->getId() );
521 if ( $this->mTitle
->isCssJsSubpage() ||
$this->mTitle
->isCssOrJsPage() ) {
522 // Stolen from Article::view --AG 2007-10-11
523 // Give hooks a chance to customise the output
524 // @TODO: standardize this crap into one function
525 if ( wfRunHooks( 'ShowRawCssJs', array( $this->mNewtext
, $this->mTitle
, $wgOut ) ) ) {
526 // Wrap the whole lot in a <pre> and don't parse
528 preg_match( '!\.(css|js)$!u', $this->mTitle
->getText(), $m );
529 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
530 $wgOut->addHTML( htmlspecialchars( $this->mNewtext
) );
531 $wgOut->addHTML( "\n</pre>\n" );
533 } elseif ( $pCache ) {
534 $article = new Article( $this->mTitle
, 0 );
535 $pOutput = ParserCache
::singleton()->get( $article, $wgOut->parserOptions() );
537 $wgOut->addParserOutput( $pOutput );
539 $article->doViewParse();
542 $wgOut->addWikiTextTidy( $this->mNewtext
);
545 if ( !$this->mNewRev
->isCurrent() ) {
546 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
549 # Add redundant patrol link on bottom...
550 if ( $this->mRcidMarkPatrolled
&& $this->mTitle
->quickUserCan( 'patrol' ) ) {
551 $sk = $wgUser->getSkin();
552 $token = $wgUser->editToken( $this->mRcidMarkPatrolled
);
553 $wgOut->preventClickjacking();
555 "<div class='patrollink'>[" . $sk->link(
557 wfMsgHtml( 'markaspatrolleddiff' ),
560 'action' => 'markpatrolled',
561 'rcid' => $this->mRcidMarkPatrolled
,
568 wfProfileOut( __METHOD__
);
572 * Show the first revision of an article. Uses normal diff headers in
573 * contrast to normal "old revision" display style.
575 function showFirstRevision() {
576 global $wgOut, $wgUser;
577 wfProfileIn( __METHOD__
);
579 # Get article text from the DB
581 if ( ! $this->loadNewText() ) {
582 $t = $this->mTitle
->getPrefixedText();
583 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ),
584 $this->deletedIdMarker( $this->mOldid
),
585 $this->deletedIdMarker( $this->mNewid
) );
586 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
587 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", "<span class='plainlinks'>$d</span>" );
588 wfProfileOut( __METHOD__
);
591 if ( $this->mNewRev
->isCurrent() ) {
592 $wgOut->setArticleFlag( true );
595 # Check if user is allowed to look at this page. If not, bail out.
597 if ( !$this->mTitle
->userCanRead() ) {
598 $wgOut->loginToUse();
600 wfProfileOut( __METHOD__
);
601 throw new MWException( "Permission Error: you do not have access to view this page" );
604 # Prepare the header box
606 $sk = $wgUser->getSkin();
608 $next = $this->mTitle
->getNextRevisionID( $this->mNewid
);
612 $nextlink = '<br />' . $sk->link(
614 wfMsgHtml( 'nextdiff' ),
616 'id' => 'differences-nextlink'
620 'oldid' => $this->mNewid
,
628 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\">" .
629 $sk->revUserTools( $this->mNewRev
) . "<br />" . $sk->revComment( $this->mNewRev
) . $nextlink . "</div>\n";
631 $wgOut->addHTML( $header );
633 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
634 $wgOut->setRobotPolicy( 'noindex,nofollow' );
636 wfProfileOut( __METHOD__
);
640 * Get the diff text, send it to $wgOut
641 * Returns false if the diff could not be generated, otherwise returns true
645 function showDiff( $otitle, $ntitle, $notice = '' ) {
647 $diff = $this->getDiff( $otitle, $ntitle, $notice );
648 if ( $diff === false ) {
649 $wgOut->addWikiMsg( 'missing-article', "<nowiki>(fixme, bug)</nowiki>", '' );
652 $this->showDiffStyle();
653 $wgOut->addHTML( $diff );
659 * Add style sheets and supporting JS for diff display.
661 function showDiffStyle() {
663 $wgOut->addModuleStyles( 'mediawiki.legacy.diff' );
667 * Get complete diff table, including header
669 * @param $otitle Title: old title
670 * @param $ntitle Title: new title
671 * @param $notice String: HTML between diff header and body
674 function getDiff( $otitle, $ntitle, $notice = '' ) {
675 $body = $this->getDiffBody();
676 if ( $body === false ) {
679 $multi = $this->getMultiNotice();
680 return $this->addHeader( $body, $otitle, $ntitle, $multi, $notice );
685 * Get the diff table body, without header
687 * @return mixed (string/false)
689 public function getDiffBody() {
691 wfProfileIn( __METHOD__
);
692 $this->mCacheHit
= true;
693 // Check if the diff should be hidden from this user
694 if ( !$this->loadRevisionData() ) {
695 wfProfileOut( __METHOD__
);
697 } elseif ( $this->mOldRev
&& !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
698 wfProfileOut( __METHOD__
);
700 } elseif ( $this->mNewRev
&& !$this->mNewRev
->userCan( Revision
::DELETED_TEXT
) ) {
701 wfProfileOut( __METHOD__
);
705 if ( $this->mOldRev
&& $this->mNewRev
706 && $this->mOldRev
->getID() == $this->mNewRev
->getID() )
708 wfProfileOut( __METHOD__
);
713 if ( $this->mOldid
&& $this->mNewid
) {
714 $key = wfMemcKey( 'diff', 'version', MW_DIFF_VERSION
,
715 'oldid', $this->mOldid
, 'newid', $this->mNewid
);
717 if ( !$this->mRefreshCache
) {
718 $difftext = $wgMemc->get( $key );
720 wfIncrStats( 'diff_cache_hit' );
721 $difftext = $this->localiseLineNumbers( $difftext );
722 $difftext .= "\n<!-- diff cache key $key -->\n";
723 wfProfileOut( __METHOD__
);
726 } // don't try to load but save the result
728 $this->mCacheHit
= false;
730 // Loadtext is permission safe, this just clears out the diff
731 if ( !$this->loadText() ) {
732 wfProfileOut( __METHOD__
);
736 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
738 // Save to cache for 7 days
739 if ( !wfRunHooks( 'AbortDiffCache', array( &$this ) ) ) {
740 wfIncrStats( 'diff_uncacheable' );
741 } elseif ( $key !== false && $difftext !== false ) {
742 wfIncrStats( 'diff_cache_miss' );
743 $wgMemc->set( $key, $difftext, 7 * 86400 );
745 wfIncrStats( 'diff_uncacheable' );
747 // Replace line numbers with the text in the user's language
748 if ( $difftext !== false ) {
749 $difftext = $this->localiseLineNumbers( $difftext );
751 wfProfileOut( __METHOD__
);
756 * Make sure the proper modules are loaded before we try to
759 private function initDiffEngines() {
760 global $wgExternalDiffEngine;
761 if ( $wgExternalDiffEngine == 'wikidiff' && !function_exists( 'wikidiff_do_diff' ) ) {
762 wfProfileIn( __METHOD__
. '-php_wikidiff.so' );
763 wfDl( 'php_wikidiff' );
764 wfProfileOut( __METHOD__
. '-php_wikidiff.so' );
766 elseif ( $wgExternalDiffEngine == 'wikidiff2' && !function_exists( 'wikidiff2_do_diff' ) ) {
767 wfProfileIn( __METHOD__
. '-php_wikidiff2.so' );
769 wfProfileOut( __METHOD__
. '-php_wikidiff2.so' );
774 * Generate a diff, no caching
776 * @param $otext String: old text, must be already segmented
777 * @param $ntext String: new text, must be already segmented
779 function generateDiffBody( $otext, $ntext ) {
780 global $wgExternalDiffEngine, $wgContLang;
782 $otext = str_replace( "\r\n", "\n", $otext );
783 $ntext = str_replace( "\r\n", "\n", $ntext );
785 $this->initDiffEngines();
787 if ( $wgExternalDiffEngine == 'wikidiff' && function_exists( 'wikidiff_do_diff' ) ) {
788 # For historical reasons, external diff engine expects
789 # input text to be HTML-escaped already
790 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
791 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
792 return $wgContLang->unsegmentForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) ) .
793 $this->debug( 'wikidiff1' );
796 if ( $wgExternalDiffEngine == 'wikidiff2' && function_exists( 'wikidiff2_do_diff' ) ) {
797 # Better external diff engine, the 2 may some day be dropped
798 # This one does the escaping and segmenting itself
799 wfProfileIn( 'wikidiff2_do_diff' );
800 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
801 $text .= $this->debug( 'wikidiff2' );
802 wfProfileOut( 'wikidiff2_do_diff' );
805 if ( $wgExternalDiffEngine != 'wikidiff3' && $wgExternalDiffEngine !== false ) {
807 global $wgTmpDirectory;
808 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
809 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
811 $tempFile1 = fopen( $tempName1, "w" );
813 wfProfileOut( __METHOD__
);
816 $tempFile2 = fopen( $tempName2, "w" );
818 wfProfileOut( __METHOD__
);
821 fwrite( $tempFile1, $otext );
822 fwrite( $tempFile2, $ntext );
823 fclose( $tempFile1 );
824 fclose( $tempFile2 );
825 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
826 wfProfileIn( __METHOD__
. "-shellexec" );
827 $difftext = wfShellExec( $cmd );
828 $difftext .= $this->debug( "external $wgExternalDiffEngine" );
829 wfProfileOut( __METHOD__
. "-shellexec" );
830 unlink( $tempName1 );
831 unlink( $tempName2 );
832 wfProfileOut( __METHOD__
);
837 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
838 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
839 $diffs = new Diff( $ota, $nta );
840 $formatter = new TableDiffFormatter();
841 $difftext = $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) ) .
842 wfProfileOut( __METHOD__
);
847 * Generate a debug comment indicating diff generating time,
848 * server node, and generator backend.
850 protected function debug( $generator = "internal" ) {
851 global $wgShowHostnames;
852 if ( !$this->enableDebugComment
) {
855 $data = array( $generator );
856 if ( $wgShowHostnames ) {
857 $data[] = wfHostname();
859 $data[] = wfTimestamp( TS_DB
);
860 return "<!-- diff generator: " .
869 * Replace line numbers with the text in the user's language
871 function localiseLineNumbers( $text ) {
872 return preg_replace_callback( '/<!--LINE (\d+)-->/',
873 array( &$this, 'localiseLineNumbersCb' ), $text );
876 function localiseLineNumbersCb( $matches ) {
878 if ( $matches[1] === '1' && $this->mReducedLineNumbers
) return '';
879 return wfMsgExt( 'lineno', 'escape', $wgLang->formatNum( $matches[1] ) );
884 * If there are revisions between the ones being compared, return a note saying so.
887 function getMultiNotice() {
888 if ( !is_object( $this->mOldRev
) ||
!is_object( $this->mNewRev
) ) {
890 } elseif ( !$this->mOldPage
->equals( $this->mNewPage
) ) {
891 // Comparing two different pages? Count would be meaningless.
895 if ( $this->mOldRev
->getTimestamp() > $this->mNewRev
->getTimestamp() ) {
896 $oldRev = $this->mNewRev
; // flip
897 $newRev = $this->mOldRev
; // flip
898 } else { // normal case
899 $oldRev = $this->mOldRev
;
900 $newRev = $this->mNewRev
;
903 $nEdits = $this->mTitle
->countRevisionsBetween( $oldRev, $newRev );
905 $limit = 100; // use diff-multi-manyusers if too many users
906 $numUsers = $this->mTitle
->countAuthorsBetween( $oldRev, $newRev, $limit );
907 return self
::intermediateEditsMsg( $nEdits, $numUsers, $limit );
909 return ''; // nothing
913 * Get a notice about how many intermediate edits and users there are
914 * @param $numEdits int
915 * @param $numUsers int
919 public static function intermediateEditsMsg( $numEdits, $numUsers, $limit ) {
921 if ( $numUsers > $limit ) {
922 $msg = 'diff-multi-manyusers';
927 return wfMsgExt( $msg, 'parseinline',
928 $wgLang->formatnum( $numEdits ), $wgLang->formatnum( $numUsers ) );
932 * Add the header to a diff body
936 function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) {
937 global $wgBetterDirectionality;
938 $tableClass = 'diff';
939 if( $wgBetterDirectionality ) {
940 // shared.css sets diff in interface language/dir,
941 // but the actual content should be in the page language/dir
942 $pageLang = $this->mTitle
->getPageLanguage();
943 $tableClass .= ' diff-contentalign-' . htmlspecialchars( $pageLang->alignStart() );
945 $header = "<table class='$tableClass'>";
946 if ( $diff ) { // Safari/Chrome show broken output if cols not used
948 <col class='diff-marker' />
949 <col class='diff-content' />
950 <col class='diff-marker' />
951 <col class='diff-content' />";
960 <td colspan='$colspan' class='diff-otitle'>{$otitle}</td>
961 <td colspan='$colspan' class='diff-ntitle'>{$ntitle}</td>
964 if ( $multi != '' ) {
965 $header .= "<tr><td colspan='{$multiColspan}' align='center' class='diff-multi'>{$multi}</td></tr>";
967 if ( $notice != '' ) {
968 $header .= "<tr><td colspan='{$multiColspan}' align='center'>{$notice}</td></tr>";
971 return $header . $diff . "</table>";
975 * Use specified text instead of loading from the database
977 function setText( $oldText, $newText ) {
978 $this->mOldtext
= $oldText;
979 $this->mNewtext
= $newText;
980 $this->mTextLoaded
= 2;
981 $this->mRevisionsLoaded
= true;
985 * Load revision metadata for the specified articles. If newid is 0, then compare
986 * the old article in oldid to the current article; if oldid is 0, then
987 * compare the current article to the immediately previous one (ignoring the
990 * If oldid is false, leave the corresponding revision object set
991 * to false. This is impossible via ordinary user input, and is provided for
996 function loadRevisionData() {
997 global $wgLang, $wgUser;
998 if ( $this->mRevisionsLoaded
) {
1001 // Whether it succeeds or fails, we don't want to try again
1002 $this->mRevisionsLoaded
= true;
1005 // Load the new revision object
1006 $this->mNewRev
= $this->mNewid
1007 ? Revision
::newFromId( $this->mNewid
)
1008 : Revision
::newFromTitle( $this->mTitle
);
1009 if ( !$this->mNewRev
instanceof Revision
) {
1013 // Update the new revision ID in case it was 0 (makes life easier doing UI stuff)
1014 $this->mNewid
= $this->mNewRev
->getId();
1016 // Check if page is editable
1017 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
1019 // Set assorted variables
1020 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
1021 $dateofrev = $wgLang->date( $this->mNewRev
->getTimestamp(), true );
1022 $timeofrev = $wgLang->time( $this->mNewRev
->getTimestamp(), true );
1023 $this->mNewPage
= $this->mNewRev
->getTitle();
1024 if ( $this->mNewRev
->isCurrent() ) {
1025 $newLink = $this->mNewPage
->escapeLocalUrl( array(
1026 'oldid' => $this->mNewid
1028 $this->mPagetitle
= htmlspecialchars( wfMsg(
1034 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
1038 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
1039 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
1041 $newLink = $this->mNewPage
->escapeLocalUrl( array(
1042 'oldid' => $this->mNewid
1044 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
1046 'oldid' => $this->mNewid
1048 $this->mPagetitle
= htmlspecialchars( wfMsg(
1055 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
1056 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
1058 if ( !$this->mNewRev
->userCan( Revision
::DELETED_TEXT
) ) {
1059 $this->mNewtitle
= "<span class='history-deleted'>{$this->mPagetitle}</span>";
1060 } elseif ( $this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1061 $this->mNewtitle
= "<span class='history-deleted'>{$this->mNewtitle}</span>";
1064 // Load the old revision object
1065 $this->mOldRev
= false;
1066 if ( $this->mOldid
) {
1067 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
1068 } elseif ( $this->mOldid
=== 0 ) {
1069 $rev = $this->mNewRev
->getPrevious();
1071 $this->mOldid
= $rev->getId();
1072 $this->mOldRev
= $rev;
1074 // No previous revision; mark to show as first-version only.
1075 $this->mOldid
= false;
1076 $this->mOldRev
= false;
1078 } /* elseif ( $this->mOldid === false ) leave mOldRev false; */
1080 if ( is_null( $this->mOldRev
) ) {
1084 if ( $this->mOldRev
) {
1085 $this->mOldPage
= $this->mOldRev
->getTitle();
1087 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
1088 $dateofrev = $wgLang->date( $this->mOldRev
->getTimestamp(), true );
1089 $timeofrev = $wgLang->time( $this->mOldRev
->getTimestamp(), true );
1090 $oldLink = $this->mOldPage
->escapeLocalUrl( array(
1091 'oldid' => $this->mOldid
1093 $oldEdit = $this->mOldPage
->escapeLocalUrl( array(
1095 'oldid' => $this->mOldid
1097 $this->mOldPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t, $dateofrev, $timeofrev ) );
1099 $this->mOldtitle
= "<a href='$oldLink'>{$this->mOldPagetitle}</a>"
1100 . " (<a href='$oldEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
1101 // Add an "undo" link
1102 if ( $editable && !$this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) && !$this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1103 $undoLink = Html
::element( 'a', array(
1104 'href' => $this->mNewPage
->getLocalUrl( array(
1106 'undoafter' => $this->mOldid
,
1107 'undo' => $this->mNewid
) ),
1108 'title' => $wgUser->getSkin()->titleAttrib( 'undo' )
1109 ), wfMsg( 'editundo' ) );
1110 $this->mNewtitle
.= ' (' . $undoLink . ')';
1113 if ( !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
1114 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldPagetitle
. '</span>';
1115 } elseif ( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
1116 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldtitle
. '</span>';
1124 * Load the text of the revisions, as well as revision data.
1128 function loadText() {
1129 if ( $this->mTextLoaded
== 2 ) {
1132 // Whether it succeeds or fails, we don't want to try again
1133 $this->mTextLoaded
= 2;
1136 if ( !$this->loadRevisionData() ) {
1139 if ( $this->mOldRev
) {
1140 $this->mOldtext
= $this->mOldRev
->getText( Revision
::FOR_THIS_USER
);
1141 if ( $this->mOldtext
=== false ) {
1145 if ( $this->mNewRev
) {
1146 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);
1147 if ( $this->mNewtext
=== false ) {
1155 * Load the text of the new revision, not the old one
1159 function loadNewText() {
1160 if ( $this->mTextLoaded
>= 1 ) {
1163 $this->mTextLoaded
= 1;
1165 if ( !$this->loadRevisionData() ) {
1168 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);