bb210eca89ec61b4deafa51f627259d446924e8e
3 * @defgroup DifferenceEngine DifferenceEngine
7 * Constant to indicate diff cache compatibility.
8 * Bump this when changing the diff formatting in a way that
9 * fixes important bugs or such to force cached diff views to
12 define( 'MW_DIFF_VERSION', '1.11a' );
16 * @ingroup DifferenceEngine
18 class DifferenceEngine
{
22 var $mOldid, $mNewid, $mTitle;
23 var $mOldtitle, $mNewtitle, $mPagetitle;
24 var $mOldtext, $mNewtext;
25 var $mOldPage, $mNewPage;
26 var $mRcidMarkPatrolled;
27 var $mOldRev, $mNewRev;
28 var $mRevisionsLoaded = false; // Have the revisions been loaded
29 var $mTextLoaded = 0; // How many text blobs have been loaded, 0, 1 or 2?
30 var $mCacheHit = false; // Was the diff fetched from cache?
33 * Set this to true to add debug info to the HTML output.
34 * Warning: this may cause RSS readers to spuriously mark articles as "new"
37 var $enableDebugComment = false;
39 // If true, line X is not displayed when X is 1, for example to increase
40 // readability and conserve space with many small diffs.
41 protected $mReducedLineNumbers = false;
43 protected $unhide = false; # show rev_deleted content if allowed
48 * @param $titleObj Title object that the diff is associated with
49 * @param $old Integer: old ID we want to show and diff with.
50 * @param $new String: either 'prev' or 'next'.
51 * @param $rcid Integer: ??? FIXME (default 0)
52 * @param $refreshCache boolean If set, refreshes the diff cache
53 * @param $unhide boolean If set, allow viewing deleted revs
55 function __construct( $titleObj = null, $old = 0, $new = 0, $rcid = 0,
56 $refreshCache = false, $unhide = false )
59 $this->mTitle
= $titleObj;
62 $this->mTitle
= $wgTitle;
64 wfDebug("DifferenceEngine old '$old' new '$new' rcid '$rcid'\n");
66 if ( 'prev' === $new ) {
67 # Show diff between revision $old and the previous one.
68 # Get previous one from DB.
69 $this->mNewid
= intval($old);
70 $this->mOldid
= $this->mTitle
->getPreviousRevisionID( $this->mNewid
);
71 } elseif ( 'next' === $new ) {
72 # Show diff between revision $old and the next one.
73 # Get next one from DB.
74 $this->mOldid
= intval($old);
75 $this->mNewid
= $this->mTitle
->getNextRevisionID( $this->mOldid
);
76 if ( false === $this->mNewid
) {
77 # if no result, NewId points to the newest old revision. The only newer
78 # revision is cur, which is "0".
82 $this->mOldid
= intval($old);
83 $this->mNewid
= intval($new);
84 wfRunHooks( 'NewDifferenceEngine', array(&$titleObj, &$this->mOldid
, &$this->mNewid
, $old, $new) );
86 $this->mRcidMarkPatrolled
= intval($rcid); # force it to be an integer
87 $this->mRefreshCache
= $refreshCache;
88 $this->unhide
= $unhide;
91 function setReducedLineNumbers( $value = true ) {
92 $this->mReducedLineNumbers
= $value;
99 function wasCacheHit() {
100 return $this->mCacheHit
;
103 function getOldid() {
104 return $this->mOldid
;
107 function getNewid() {
108 return $this->mNewid
;
111 function showDiffPage( $diffOnly = false ) {
112 global $wgUser, $wgOut, $wgUseExternalEditor, $wgUseRCPatrol;
113 wfProfileIn( __METHOD__
);
116 # If external diffs are enabled both globally and for the user,
117 # we'll use the application/x-external-editor interface to call
118 # an external diff tool like kompare, kdiff3, etc.
119 if($wgUseExternalEditor && $wgUser->getOption('externaldiff')) {
120 global $wgInputEncoding,$wgServer,$wgScript,$wgLang;
122 header ( "Content-type: application/x-external-editor; charset=".$wgInputEncoding );
123 $url1=$this->mTitle
->getFullURL( array(
125 'oldid' => $this->mOldid
127 $url2=$this->mTitle
->getFullURL( array(
129 'oldid' => $this->mNewid
131 $special=$wgLang->getNsText(NS_SPECIAL
);
136 Script={$wgServer}{$wgScript}
137 Special namespace={$special}
151 $wgOut->setArticleFlag( false );
152 if ( !$this->loadRevisionData() ) {
153 $t = $this->mTitle
->getPrefixedText();
154 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid
, $this->mNewid
);
155 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
156 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
157 wfProfileOut( __METHOD__
);
161 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
163 if ( $this->mNewRev
->isCurrent() ) {
164 $wgOut->setArticleFlag( true );
167 # mOldid is false if the difference engine is called with a "vague" query for
168 # a diff between a version V and its previous version V' AND the version V
169 # is the first version of that article. In that case, V' does not exist.
170 if ( $this->mOldid
=== false ) {
171 $this->showFirstRevision();
172 $this->renderNewRevision(); // should we respect $diffOnly here or not?
173 wfProfileOut( __METHOD__
);
177 $wgOut->suppressQuickbar();
179 $oldTitle = $this->mOldPage
->getPrefixedText();
180 $newTitle = $this->mNewPage
->getPrefixedText();
181 if( $oldTitle == $newTitle ) {
182 $wgOut->setPageTitle( $newTitle );
184 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
186 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
187 $wgOut->setRobotPolicy( 'noindex,nofollow' );
189 if ( !$this->mOldPage
->userCanRead() ||
!$this->mNewPage
->userCanRead() ) {
190 $wgOut->loginToUse();
193 wfProfileOut( __METHOD__
);
197 $sk = $wgUser->getSkin();
199 // Check if page is editable
200 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
201 if ( $editable && $this->mNewRev
->isCurrent() && $wgUser->isAllowed( 'rollback' ) ) {
202 $rollback = '   ' . $sk->generateRollback( $this->mNewRev
);
207 // Prepare a change patrol link, if applicable
208 if( $wgUseRCPatrol && $this->mTitle
->userCan('patrol') ) {
209 // If we've been given an explicit change identifier, use it; saves time
210 if( $this->mRcidMarkPatrolled
) {
211 $rcid = $this->mRcidMarkPatrolled
;
212 $rc = RecentChange
::newFromId( $rcid );
213 // Already patrolled?
214 $rcid = is_object($rc) && !$rc->getAttribute('rc_patrolled') ?
$rcid : 0;
216 // Look for an unpatrolled change corresponding to this diff
217 $db = wfGetDB( DB_SLAVE
);
218 $change = RecentChange
::newFromConds(
220 // Redundant user,timestamp condition so we can use the existing index
221 'rc_user_text' => $this->mNewRev
->getRawUserText(),
222 'rc_timestamp' => $db->timestamp( $this->mNewRev
->getTimestamp() ),
223 'rc_this_oldid' => $this->mNewid
,
224 'rc_last_oldid' => $this->mOldid
,
229 if( $change instanceof RecentChange
) {
230 $rcid = $change->mAttribs
['rc_id'];
231 $this->mRcidMarkPatrolled
= $rcid;
239 $token = $wgUser->editToken();
240 $patrol = ' <span class="patrollink">[' . $sk->link(
242 wfMsgHtml( 'markaspatrolleddiff' ),
245 'action' => 'markpatrolled',
261 # Carry over 'diffonly' param via navigation links
262 if( $diffOnly != $wgUser->getBoolOption('diffonly') ) {
263 $query['diffonly'] = $diffOnly;
266 # Make "previous revision link"
267 $query['diff'] = 'prev';
268 $query['oldid'] = $this->mOldid
;
269 # Cascade unhide param in links for easy deletion browsing
270 if( $this->unhide
) {
271 $query['unhide'] = 1;
273 $prevlink = $sk->link(
275 wfMsgHtml( 'previousdiff' ),
277 'id' => 'differences-prevlink'
286 # Make "next revision link"
287 $query['diff'] = 'next';
288 $query['oldid'] = $this->mNewid
;
289 # Skip next link on the top revision
290 if( $this->mNewRev
->isCurrent() ) {
291 $nextlink = ' ';
293 $nextlink = $sk->link(
295 wfMsgHtml( 'nextdiff' ),
297 'id' => 'differences-nextlink'
310 if( $this->mOldRev
->isMinor() ) {
311 $oldminor = ChangesList
::flag( 'minor' );
313 if( $this->mNewRev
->isMinor() ) {
314 $newminor = ChangesList
::flag( 'minor' );
317 # Handle RevisionDelete links...
318 $ldel = $this->revisionDeleteLink( $this->mOldRev
);
319 $rdel = $this->revisionDeleteLink( $this->mNewRev
);
321 $oldHeader = '<div id="mw-diff-otitle1"><strong>'.$this->mOldtitle
.'</strong></div>' .
322 '<div id="mw-diff-otitle2">' .
323 $sk->revUserTools( $this->mOldRev
, !$this->unhide
).'</div>' .
324 '<div id="mw-diff-otitle3">' . $oldminor .
325 $sk->revComment( $this->mOldRev
, !$diffOnly, !$this->unhide
).$ldel.'</div>' .
326 '<div id="mw-diff-otitle4">' . $prevlink .'</div>';
327 $newHeader = '<div id="mw-diff-ntitle1"><strong>'.$this->mNewtitle
.'</strong></div>' .
328 '<div id="mw-diff-ntitle2">' . $sk->revUserTools( $this->mNewRev
, !$this->unhide
) .
330 '<div id="mw-diff-ntitle3">' . $newminor .
331 $sk->revComment( $this->mNewRev
, !$diffOnly, !$this->unhide
).$rdel.'</div>' .
332 '<div id="mw-diff-ntitle4">' . $nextlink . $patrol . '</div>';
334 # Check if this user can see the revisions
335 $allowed = $this->mOldRev
->userCan(Revision
::DELETED_TEXT
)
336 && $this->mNewRev
->userCan(Revision
::DELETED_TEXT
);
337 # Check if one of the revisions is deleted/suppressed
338 $deleted = $suppressed = false;
339 if( $this->mOldRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
340 $deleted = true; // old revisions text is hidden
341 if( $this->mOldRev
->isDeleted(Revision
::DELETED_RESTRICTED
) )
342 $suppressed = true; // also suppressed
344 if( $this->mNewRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
345 $deleted = true; // new revisions text is hidden
346 if( $this->mNewRev
->isDeleted(Revision
::DELETED_RESTRICTED
) )
347 $suppressed = true; // also suppressed
349 # If the diff cannot be shown due to a deleted revision, then output
350 # the diff header and links to unhide (if available)...
351 if( $deleted && (!$this->unhide ||
!$allowed) ) {
352 $this->showDiffStyle();
353 $multi = $this->getMultiNotice();
354 $wgOut->addHTML( $this->addHeader( '', $oldHeader, $newHeader, $multi ) );
356 $msg = $suppressed ?
'rev-suppressed-no-diff' : 'rev-deleted-no-diff';
357 # Give explanation for why revision is not visible
358 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
361 # Give explanation and add a link to view the diff...
362 $link = $this->mTitle
->getFullUrl( array(
363 'diff' => $this->mNewid
,
364 'oldid' => $this->mOldid
,
367 $msg = $suppressed ?
'rev-suppressed-unhide-diff' : 'rev-deleted-unhide-diff';
368 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", array( $msg, $link ) );
370 # Otherwise, output a regular diff...
372 # Add deletion notice if the user is viewing deleted content
375 $msg = $suppressed ?
'rev-suppressed-diff-view' : 'rev-deleted-diff-view';
376 $notice = "<div class='mw-warning plainlinks'>\n".wfMsgExt($msg,'parseinline')."</div>\n";
378 $this->showDiff( $oldHeader, $newHeader, $notice );
380 $this->renderNewRevision();
383 wfProfileOut( __METHOD__
);
386 protected function revisionDeleteLink( $rev ) {
389 $canHide = $wgUser->isAllowed( 'deleterevision' );
390 // Show del/undel link if:
391 // (a) the user can delete revisions, or
392 // (b) the user can view deleted revision *and* this one is deleted
393 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' )) ) {
394 $sk = $wgUser->getSkin();
395 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
396 $link = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
399 'type' => 'revision',
400 'target' => $rev->mTitle
->getPrefixedDbkey(),
401 'ids' => $rev->getId()
403 $link = $sk->revDeleteLink( $query,
404 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
406 $link = '   ' . $link . ' ';
412 * Show the new revision of the page.
414 function renderNewRevision() {
415 global $wgOut, $wgUser;
416 wfProfileIn( __METHOD__
);
418 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
419 # Add deleted rev tag if needed
420 if( !$this->mNewRev
->userCan(Revision
::DELETED_TEXT
) ) {
421 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
422 } else if( $this->mNewRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
423 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
427 if( !$this->mNewRev
->isCurrent() ) {
428 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
432 $this->loadNewText();
433 if( is_object( $this->mNewRev
) ) {
434 $wgOut->setRevisionId( $this->mNewRev
->getId() );
437 if( $this->mTitle
->isCssJsSubpage() ||
$this->mTitle
->isCssOrJsPage() ) {
438 // Stolen from Article::view --AG 2007-10-11
439 // Give hooks a chance to customise the output
440 if( wfRunHooks( 'ShowRawCssJs', array( $this->mNewtext
, $this->mTitle
, $wgOut ) ) ) {
441 // Wrap the whole lot in a <pre> and don't parse
443 preg_match( '!\.(css|js)$!u', $this->mTitle
->getText(), $m );
444 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
445 $wgOut->addHTML( htmlspecialchars( $this->mNewtext
) );
446 $wgOut->addHTML( "\n</pre>\n" );
448 } elseif( $pCache ) {
449 $article = new Article( $this->mTitle
, 0 );
450 $pOutput = ParserCache
::singleton()->get( $article, $wgUser );
452 $wgOut->addParserOutput( $pOutput );
454 $article->doViewParse();
457 $wgOut->addWikiTextTidy( $this->mNewtext
);
460 if( is_object( $this->mNewRev
) && !$this->mNewRev
->isCurrent() ) {
461 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
463 # Add redundant patrol link on bottom...
464 if( $this->mRcidMarkPatrolled
&& $this->mTitle
->quickUserCan('patrol') ) {
465 $sk = $wgUser->getSkin();
466 $token = $wgUser->editToken();
468 "<div class='patrollink'>[" . $sk->link(
470 wfMsgHtml( 'markaspatrolleddiff' ),
473 'action' => 'markpatrolled',
474 'rcid' => $this->mRcidMarkPatrolled
,
481 wfProfileOut( __METHOD__
);
485 * Show the first revision of an article. Uses normal diff headers in
486 * contrast to normal "old revision" display style.
488 function showFirstRevision() {
489 global $wgOut, $wgUser;
490 wfProfileIn( __METHOD__
);
492 # Get article text from the DB
494 if ( ! $this->loadNewText() ) {
495 $t = $this->mTitle
->getPrefixedText();
496 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid
, $this->mNewid
);
497 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
498 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
499 wfProfileOut( __METHOD__
);
502 if ( $this->mNewRev
->isCurrent() ) {
503 $wgOut->setArticleFlag( true );
506 # Check if user is allowed to look at this page. If not, bail out.
508 if ( !$this->mTitle
->userCanRead() ) {
509 $wgOut->loginToUse();
511 wfProfileOut( __METHOD__
);
512 throw new MWException("Permission Error: you do not have access to view this page");
515 # Prepare the header box
517 $sk = $wgUser->getSkin();
519 $next = $this->mTitle
->getNextRevisionID( $this->mNewid
);
523 $nextlink = '<br />' . $sk->link(
525 wfMsgHtml( 'nextdiff' ),
527 'id' => 'differences-nextlink'
531 'oldid' => $this->mNewid
,
539 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\">" .
540 $sk->revUserTools( $this->mNewRev
) . "<br />" . $sk->revComment( $this->mNewRev
) . $nextlink . "</div>\n";
542 $wgOut->addHTML( $header );
544 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
545 $wgOut->setRobotPolicy( 'noindex,nofollow' );
547 wfProfileOut( __METHOD__
);
551 * Get the diff text, send it to $wgOut
552 * Returns false if the diff could not be generated, otherwise returns true
554 function showDiff( $otitle, $ntitle, $notice = '' ) {
556 $diff = $this->getDiff( $otitle, $ntitle, $notice );
557 if ( $diff === false ) {
558 $wgOut->addWikiMsg( 'missing-article', "<nowiki>(fixme, bug)</nowiki>", '' );
561 $this->showDiffStyle();
562 $wgOut->addHTML( $diff );
568 * Add style sheets and supporting JS for diff display.
570 function showDiffStyle() {
571 global $wgStylePath, $wgStyleVersion, $wgOut;
572 $wgOut->addStyle( 'common/diff.css' );
574 // JS is needed to detect old versions of Mozilla to work around an annoyance bug.
575 $wgOut->addScript( "<script type=\"text/javascript\" src=\"$wgStylePath/common/diff.js?$wgStyleVersion\"></script>" );
579 * Get complete diff table, including header
581 * @param $otitle Title: old title
582 * @param $ntitle Title: new title
583 * @param $notice String: HTML between diff header and body
586 function getDiff( $otitle, $ntitle, $notice = '' ) {
587 $body = $this->getDiffBody();
588 if ( $body === false ) {
591 $multi = $this->getMultiNotice();
592 return $this->addHeader( $body, $otitle, $ntitle, $multi, $notice );
597 * Get the diff table body, without header
601 function getDiffBody() {
603 wfProfileIn( __METHOD__
);
604 $this->mCacheHit
= true;
605 // Check if the diff should be hidden from this user
606 if ( !$this->loadRevisionData() )
608 if ( $this->mOldRev
&& !$this->mOldRev
->userCan(Revision
::DELETED_TEXT
) ) {
610 } else if ( $this->mNewRev
&& !$this->mNewRev
->userCan(Revision
::DELETED_TEXT
) ) {
612 } else if ( $this->mOldRev
&& $this->mNewRev
&& $this->mOldRev
->getID() == $this->mNewRev
->getID() ) {
617 if ( $this->mOldid
&& $this->mNewid
) {
618 $key = wfMemcKey( 'diff', 'version', MW_DIFF_VERSION
, 'oldid', $this->mOldid
, 'newid', $this->mNewid
);
620 if ( !$this->mRefreshCache
) {
621 $difftext = $wgMemc->get( $key );
623 wfIncrStats( 'diff_cache_hit' );
624 $difftext = $this->localiseLineNumbers( $difftext );
625 $difftext .= "\n<!-- diff cache key $key -->\n";
626 wfProfileOut( __METHOD__
);
629 } // don't try to load but save the result
631 $this->mCacheHit
= false;
633 // Loadtext is permission safe, this just clears out the diff
634 if ( !$this->loadText() ) {
635 wfProfileOut( __METHOD__
);
639 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
641 // Save to cache for 7 days
642 if ( !wfRunHooks( 'AbortDiffCache', array( &$this ) ) ) {
643 wfIncrStats( 'diff_uncacheable' );
644 } else if ( $key !== false && $difftext !== false ) {
645 wfIncrStats( 'diff_cache_miss' );
646 $wgMemc->set( $key, $difftext, 7*86400 );
648 wfIncrStats( 'diff_uncacheable' );
650 // Replace line numbers with the text in the user's language
651 if ( $difftext !== false ) {
652 $difftext = $this->localiseLineNumbers( $difftext );
654 wfProfileOut( __METHOD__
);
659 * Make sure the proper modules are loaded before we try to
662 private function initDiffEngines() {
663 global $wgExternalDiffEngine;
664 if ( $wgExternalDiffEngine == 'wikidiff' && !function_exists( 'wikidiff_do_diff' ) ) {
665 wfProfileIn( __METHOD__
. '-php_wikidiff.so' );
666 wfSuppressWarnings();
667 dl( 'php_wikidiff.so' );
669 wfProfileOut( __METHOD__
. '-php_wikidiff.so' );
671 else if ( $wgExternalDiffEngine == 'wikidiff2' && !function_exists( 'wikidiff2_do_diff' ) ) {
672 wfProfileIn( __METHOD__
. '-php_wikidiff2.so' );
673 wfSuppressWarnings();
676 wfProfileOut( __METHOD__
. '-php_wikidiff2.so' );
681 * Generate a diff, no caching
683 * @param $otext String: old text, must be already segmented
684 * @param $ntext String: new text, must be already segmented
686 function generateDiffBody( $otext, $ntext ) {
687 global $wgExternalDiffEngine, $wgContLang;
689 $otext = str_replace( "\r\n", "\n", $otext );
690 $ntext = str_replace( "\r\n", "\n", $ntext );
692 $this->initDiffEngines();
694 if ( $wgExternalDiffEngine == 'wikidiff' && function_exists( 'wikidiff_do_diff' ) ) {
695 # For historical reasons, external diff engine expects
696 # input text to be HTML-escaped already
697 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
698 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
699 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) ) .
700 $this->debug( 'wikidiff1' );
703 if ( $wgExternalDiffEngine == 'wikidiff2' && function_exists( 'wikidiff2_do_diff' ) ) {
704 # Better external diff engine, the 2 may some day be dropped
705 # This one does the escaping and segmenting itself
706 wfProfileIn( 'wikidiff2_do_diff' );
707 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
708 $text .= $this->debug( 'wikidiff2' );
709 wfProfileOut( 'wikidiff2_do_diff' );
712 if ( $wgExternalDiffEngine != 'wikidiff3' && $wgExternalDiffEngine !== false ) {
714 global $wgTmpDirectory;
715 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
716 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
718 $tempFile1 = fopen( $tempName1, "w" );
720 wfProfileOut( __METHOD__
);
723 $tempFile2 = fopen( $tempName2, "w" );
725 wfProfileOut( __METHOD__
);
728 fwrite( $tempFile1, $otext );
729 fwrite( $tempFile2, $ntext );
730 fclose( $tempFile1 );
731 fclose( $tempFile2 );
732 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
733 wfProfileIn( __METHOD__
. "-shellexec" );
734 $difftext = wfShellExec( $cmd );
735 $difftext .= $this->debug( "external $wgExternalDiffEngine" );
736 wfProfileOut( __METHOD__
. "-shellexec" );
737 unlink( $tempName1 );
738 unlink( $tempName2 );
743 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
744 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
745 $diffs = new Diff( $ota, $nta );
746 $formatter = new TableDiffFormatter();
747 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) ) .
752 * Generate a debug comment indicating diff generating time,
753 * server node, and generator backend.
755 protected function debug( $generator="internal" ) {
756 global $wgShowHostnames;
757 if ( !$this->enableDebugComment
) {
760 $data = array( $generator );
761 if( $wgShowHostnames ) {
762 $data[] = wfHostname();
764 $data[] = wfTimestamp( TS_DB
);
765 return "<!-- diff generator: " .
774 * Replace line numbers with the text in the user's language
776 function localiseLineNumbers( $text ) {
777 return preg_replace_callback( '/<!--LINE (\d+)-->/',
778 array( &$this, 'localiseLineNumbersCb' ), $text );
781 function localiseLineNumbersCb( $matches ) {
783 if ( $matches[1] === '1' && $this->mReducedLineNumbers
) return '';
784 return wfMsgExt( 'lineno', 'escape', $wgLang->formatNum( $matches[1] ) );
789 * If there are revisions between the ones being compared, return a note saying so.
791 function getMultiNotice() {
792 if ( !is_object($this->mOldRev
) ||
!is_object($this->mNewRev
) )
795 if( !$this->mOldPage
->equals( $this->mNewPage
) ) {
796 // Comparing two different pages? Count would be meaningless.
800 $oldid = $this->mOldRev
->getId();
801 $newid = $this->mNewRev
->getId();
802 if ( $oldid > $newid ) {
803 $tmp = $oldid; $oldid = $newid; $newid = $tmp;
806 $n = $this->mTitle
->countRevisionsBetween( $oldid, $newid );
810 return wfMsgExt( 'diff-multi', array( 'parseinline' ), $n );
815 * Add the header to a diff body
817 static function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) {
818 $header = "<table class='diff'>";
819 if( $diff ) { // Safari/Chrome show broken output if cols not used
821 <col class='diff-marker' />
822 <col class='diff-content' />
823 <col class='diff-marker' />
824 <col class='diff-content' />";
833 <td colspan='$colspan' class='diff-otitle'>{$otitle}</td>
834 <td colspan='$colspan' class='diff-ntitle'>{$ntitle}</td>
837 if ( $multi != '' ) {
838 $header .= "<tr><td colspan='{$multiColspan}' align='center' class='diff-multi'>{$multi}</td></tr>";
840 if ( $notice != '' ) {
841 $header .= "<tr><td colspan='{$multiColspan}' align='center'>{$notice}</td></tr>";
844 return $header . $diff . "</table>";
848 * Use specified text instead of loading from the database
850 function setText( $oldText, $newText ) {
851 $this->mOldtext
= $oldText;
852 $this->mNewtext
= $newText;
853 $this->mTextLoaded
= 2;
854 $this->mRevisionsLoaded
= true;
858 * Load revision metadata for the specified articles. If newid is 0, then compare
859 * the old article in oldid to the current article; if oldid is 0, then
860 * compare the current article to the immediately previous one (ignoring the
863 * If oldid is false, leave the corresponding revision object set
864 * to false. This is impossible via ordinary user input, and is provided for
867 function loadRevisionData() {
868 global $wgLang, $wgUser;
869 if ( $this->mRevisionsLoaded
) {
872 // Whether it succeeds or fails, we don't want to try again
873 $this->mRevisionsLoaded
= true;
876 // Load the new revision object
877 $this->mNewRev
= $this->mNewid
878 ? Revision
::newFromId( $this->mNewid
)
879 : Revision
::newFromTitle( $this->mTitle
);
880 if( !$this->mNewRev
instanceof Revision
)
883 // Update the new revision ID in case it was 0 (makes life easier doing UI stuff)
884 $this->mNewid
= $this->mNewRev
->getId();
886 // Check if page is editable
887 $editable = $this->mNewRev
->getTitle()->userCan( 'edit' );
889 // Set assorted variables
890 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
891 $dateofrev = $wgLang->date( $this->mNewRev
->getTimestamp(), true );
892 $timeofrev = $wgLang->time( $this->mNewRev
->getTimestamp(), true );
893 $this->mNewPage
= $this->mNewRev
->getTitle();
894 if( $this->mNewRev
->isCurrent() ) {
895 $newLink = $this->mNewPage
->escapeLocalUrl( array(
896 'oldid' => $this->mNewid
898 $this->mPagetitle
= htmlspecialchars( wfMsg(
904 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
908 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
909 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
911 $newLink = $this->mNewPage
->escapeLocalUrl( array(
912 'oldid' => $this->mNewid
914 $newEdit = $this->mNewPage
->escapeLocalUrl( array(
916 'oldid' => $this->mNewid
918 $this->mPagetitle
= htmlspecialchars( wfMsg(
925 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
926 $this->mNewtitle
.= " (<a href='$newEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
928 if( !$this->mNewRev
->userCan(Revision
::DELETED_TEXT
) ) {
929 $this->mNewtitle
= "<span class='history-deleted'>{$this->mPagetitle}</span>";
930 } else if ( $this->mNewRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
931 $this->mNewtitle
= "<span class='history-deleted'>{$this->mNewtitle}</span>";
934 // Load the old revision object
935 $this->mOldRev
= false;
936 if( $this->mOldid
) {
937 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
938 } elseif ( $this->mOldid
=== 0 ) {
939 $rev = $this->mNewRev
->getPrevious();
941 $this->mOldid
= $rev->getId();
942 $this->mOldRev
= $rev;
944 // No previous revision; mark to show as first-version only.
945 $this->mOldid
= false;
946 $this->mOldRev
= false;
948 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
950 if( is_null( $this->mOldRev
) ) {
954 if ( $this->mOldRev
) {
955 $this->mOldPage
= $this->mOldRev
->getTitle();
957 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
958 $dateofrev = $wgLang->date( $this->mOldRev
->getTimestamp(), true );
959 $timeofrev = $wgLang->time( $this->mOldRev
->getTimestamp(), true );
960 $oldLink = $this->mOldPage
->escapeLocalUrl( array(
961 'oldid' => $this->mOldid
963 $oldEdit = $this->mOldPage
->escapeLocalUrl( array(
965 'oldid' => $this->mOldid
967 $this->mOldPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t, $dateofrev, $timeofrev ) );
969 $this->mOldtitle
= "<a href='$oldLink'>{$this->mOldPagetitle}</a>"
970 . " (<a href='$oldEdit'>" . wfMsgHtml( $editable ?
'editold' : 'viewsourceold' ) . "</a>)";
971 // Add an "undo" link
972 $newUndo = $this->mNewPage
->escapeLocalUrl( array(
974 'undoafter' => $this->mOldid
,
975 'undo' => $this->mNewid
977 $htmlLink = htmlspecialchars( wfMsg( 'editundo' ) );
978 $htmlTitle = $wgUser->getSkin()->tooltip( 'undo' );
979 if( $editable && !$this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) && !$this->mNewRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
980 $this->mNewtitle
.= " (<a href='$newUndo' $htmlTitle>" . $htmlLink . "</a>)";
983 if( !$this->mOldRev
->userCan( Revision
::DELETED_TEXT
) ) {
984 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldPagetitle
. '</span>';
985 } else if( $this->mOldRev
->isDeleted( Revision
::DELETED_TEXT
) ) {
986 $this->mOldtitle
= '<span class="history-deleted">' . $this->mOldtitle
. '</span>';
994 * Load the text of the revisions, as well as revision data.
996 function loadText() {
997 if ( $this->mTextLoaded
== 2 ) {
1000 // Whether it succeeds or fails, we don't want to try again
1001 $this->mTextLoaded
= 2;
1004 if ( !$this->loadRevisionData() ) {
1007 if ( $this->mOldRev
) {
1008 $this->mOldtext
= $this->mOldRev
->getText( Revision
::FOR_THIS_USER
);
1009 if ( $this->mOldtext
=== false ) {
1013 if ( $this->mNewRev
) {
1014 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);
1015 if ( $this->mNewtext
=== false ) {
1023 * Load the text of the new revision, not the old one
1025 function loadNewText() {
1026 if ( $this->mTextLoaded
>= 1 ) {
1029 $this->mTextLoaded
= 1;
1031 if ( !$this->loadRevisionData() ) {
1034 $this->mNewtext
= $this->mNewRev
->getText( Revision
::FOR_THIS_USER
);