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;
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__ );
115 # Allow frames except in certain special cases
116 $wgOut->allowClickjacking();
118 # If external diffs are enabled both globally and for the user,
119 # we'll use the application/x-external-editor interface to call
120 # an external diff tool like kompare, kdiff3, etc.
121 if($wgUseExternalEditor && $wgUser->getOption('externaldiff')) {
122 global $wgInputEncoding,$wgServer,$wgScript,$wgLang;
124 header ( "Content-type: application/x-external-editor; charset=".$wgInputEncoding );
125 $url1=$this->mTitle->getFullURL( array(
127 'oldid' => $this->mOldid
129 $url2=$this->mTitle->getFullURL( array(
131 'oldid' => $this->mNewid
133 $special=$wgLang->getNsText(NS_SPECIAL);
138 Script={$wgServer}{$wgScript}
139 Special namespace={$special}
153 $wgOut->setArticleFlag( false );
154 if ( !$this->loadRevisionData() ) {
155 $t = $this->mTitle->getPrefixedText();
156 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid, $this->mNewid );
157 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
158 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
159 wfProfileOut( __METHOD__ );
163 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev, $this->mNewRev ) );
165 if ( $this->mNewRev->isCurrent() ) {
166 $wgOut->setArticleFlag( true );
169 # mOldid is false if the difference engine is called with a "vague" query for
170 # a diff between a version V and its previous version V' AND the version V
171 # is the first version of that article. In that case, V' does not exist.
172 if ( $this->mOldid === false ) {
173 $this->showFirstRevision();
174 $this->renderNewRevision(); // should we respect $diffOnly here or not?
175 wfProfileOut( __METHOD__ );
179 $wgOut->suppressQuickbar();
181 $oldTitle = $this->mOldPage->getPrefixedText();
182 $newTitle = $this->mNewPage->getPrefixedText();
183 if( $oldTitle == $newTitle ) {
184 $wgOut->setPageTitle( $newTitle );
186 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
188 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
189 $wgOut->setRobotPolicy( 'noindex,nofollow' );
191 if ( !$this->mOldPage->userCanRead() || !$this->mNewPage->userCanRead() ) {
192 $wgOut->loginToUse();
195 wfProfileOut( __METHOD__ );
199 $sk = $wgUser->getSkin();
201 // Check if page is editable
202 $editable = $this->mNewRev->getTitle()->userCan( 'edit' );
203 if ( $editable && $this->mNewRev->isCurrent() && $wgUser->isAllowed( 'rollback' ) ) {
204 $wgOut->preventClickjacking();
205 $rollback = ' ' . $sk->generateRollback( $this->mNewRev );
210 // Prepare a change patrol link, if applicable
211 if( $wgUseRCPatrol && $this->mTitle->userCan('patrol') ) {
212 // If we've been given an explicit change identifier, use it; saves time
213 if( $this->mRcidMarkPatrolled ) {
214 $rcid = $this->mRcidMarkPatrolled;
215 $rc = RecentChange::newFromId( $rcid );
216 // Already patrolled?
217 $rcid = is_object($rc) && !$rc->getAttribute('rc_patrolled') ? $rcid : 0;
219 // Look for an unpatrolled change corresponding to this diff
220 $db = wfGetDB( DB_SLAVE );
221 $change = RecentChange::newFromConds(
223 // Redundant user,timestamp condition so we can use the existing index
224 'rc_user_text' => $this->mNewRev->getRawUserText(),
225 'rc_timestamp' => $db->timestamp( $this->mNewRev->getTimestamp() ),
226 'rc_this_oldid' => $this->mNewid,
227 'rc_last_oldid' => $this->mOldid,
232 if( $change instanceof RecentChange ) {
233 $rcid = $change->mAttribs['rc_id'];
234 $this->mRcidMarkPatrolled = $rcid;
242 $patrol = ' <span class="patrollink">[' . $sk->link(
244 wfMsgHtml( 'markaspatrolleddiff' ),
247 'action' => 'markpatrolled',
262 # Carry over 'diffonly' param via navigation links
263 if( $diffOnly != $wgUser->getBoolOption('diffonly') ) {
264 $query['diffonly'] = $diffOnly;
267 # Make "previous revision link"
268 $query['diff'] = 'prev';
269 $query['oldid'] = $this->mOldid;
270 # Cascade unhide param in links for easy deletion browsing
271 if( $this->unhide ) {
272 $query['unhide'] = 1;
274 $prevlink = $sk->link(
276 wfMsgHtml( 'previousdiff' ),
278 'id' => 'differences-prevlink'
287 # Make "next revision link"
288 $query['diff'] = 'next';
289 $query['oldid'] = $this->mNewid;
290 # Skip next link on the top revision
291 if( $this->mNewRev->isCurrent() ) {
292 $nextlink = ' ';
294 $nextlink = $sk->link(
296 wfMsgHtml( 'nextdiff' ),
298 'id' => 'differences-nextlink'
311 if( $this->mOldRev->isMinor() ) {
312 $oldminor = ChangesList::flag( 'minor' );
314 if( $this->mNewRev->isMinor() ) {
315 $newminor = ChangesList::flag( 'minor' );
318 # Handle RevisionDelete links...
319 $ldel = $this->revisionDeleteLink( $this->mOldRev );
320 $rdel = $this->revisionDeleteLink( $this->mNewRev );
322 $oldHeader = '<div id="mw-diff-otitle1"><strong>'.$this->mOldtitle.'</strong></div>' .
323 '<div id="mw-diff-otitle2">' .
324 $sk->revUserTools( $this->mOldRev, !$this->unhide ).'</div>' .
325 '<div id="mw-diff-otitle3">' . $oldminor .
326 $sk->revComment( $this->mOldRev, !$diffOnly, !$this->unhide ).$ldel.'</div>' .
327 '<div id="mw-diff-otitle4">' . $prevlink .'</div>';
328 $newHeader = '<div id="mw-diff-ntitle1"><strong>'.$this->mNewtitle.'</strong></div>' .
329 '<div id="mw-diff-ntitle2">' . $sk->revUserTools( $this->mNewRev, !$this->unhide ) .
331 '<div id="mw-diff-ntitle3">' . $newminor .
332 $sk->revComment( $this->mNewRev, !$diffOnly, !$this->unhide ).$rdel.'</div>' .
333 '<div id="mw-diff-ntitle4">' . $nextlink . $patrol . '</div>';
335 # Check if this user can see the revisions
336 $allowed = $this->mOldRev->userCan(Revision::DELETED_TEXT)
337 && $this->mNewRev->userCan(Revision::DELETED_TEXT);
338 # Check if one of the revisions is deleted/suppressed
339 $deleted = $suppressed = false;
340 if( $this->mOldRev->isDeleted(Revision::DELETED_TEXT) ) {
341 $deleted = true; // old revisions text is hidden
342 if( $this->mOldRev->isDeleted(Revision::DELETED_RESTRICTED) )
343 $suppressed = true; // also suppressed
345 if( $this->mNewRev->isDeleted(Revision::DELETED_TEXT) ) {
346 $deleted = true; // new revisions text is hidden
347 if( $this->mNewRev->isDeleted(Revision::DELETED_RESTRICTED) )
348 $suppressed = true; // also suppressed
350 # If the diff cannot be shown due to a deleted revision, then output
351 # the diff header and links to unhide (if available)...
352 if( $deleted && (!$this->unhide || !$allowed) ) {
353 $this->showDiffStyle();
354 $multi = $this->getMultiNotice();
355 $wgOut->addHTML( $this->addHeader( '', $oldHeader, $newHeader, $multi ) );
357 $msg = $suppressed ? 'rev-suppressed-no-diff' : 'rev-deleted-no-diff';
358 # Give explanation for why revision is not visible
359 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n",
362 # Give explanation and add a link to view the diff...
363 $link = $this->mTitle->getFullUrl( array(
364 'diff' => $this->mNewid,
365 'oldid' => $this->mOldid,
368 $msg = $suppressed ? 'rev-suppressed-unhide-diff' : 'rev-deleted-unhide-diff';
369 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", array( $msg, $link ) );
371 # Otherwise, output a regular diff...
373 # Add deletion notice if the user is viewing deleted content
376 $msg = $suppressed ? 'rev-suppressed-diff-view' : 'rev-deleted-diff-view';
377 $notice = "<div class='mw-warning plainlinks'>\n".wfMsgExt($msg,'parseinline')."</div>\n";
379 $this->showDiff( $oldHeader, $newHeader, $notice );
381 $this->renderNewRevision();
384 wfProfileOut( __METHOD__ );
387 protected function revisionDeleteLink( $rev ) {
390 $canHide = $wgUser->isAllowed( 'deleterevision' );
391 // Show del/undel link if:
392 // (a) the user can delete revisions, or
393 // (b) the user can view deleted revision *and* this one is deleted
394 if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' )) ) {
395 $sk = $wgUser->getSkin();
396 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
397 $link = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
400 'type' => 'revision',
401 'target' => $rev->mTitle->getPrefixedDbkey(),
402 'ids' => $rev->getId()
404 $link = $sk->revDeleteLink( $query,
405 $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide );
407 $link = ' ' . $link . ' ';
413 * Show the new revision of the page.
415 function renderNewRevision() {
416 global $wgOut, $wgUser;
417 wfProfileIn( __METHOD__ );
419 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
420 # Add deleted rev tag if needed
421 if( !$this->mNewRev->userCan(Revision::DELETED_TEXT) ) {
422 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-permission' );
423 } else if( $this->mNewRev->isDeleted(Revision::DELETED_TEXT) ) {
424 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-view' );
427 if( !$this->mNewRev->isCurrent() ) {
428 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
431 $this->loadNewText();
432 if( is_object( $this->mNewRev ) ) {
433 $wgOut->setRevisionId( $this->mNewRev->getId() );
436 if( $this->mTitle->isCssJsSubpage() || $this->mTitle->isCssOrJsPage() ) {
437 // Stolen from Article::view --AG 2007-10-11
438 // Give hooks a chance to customise the output
439 if( wfRunHooks( 'ShowRawCssJs', array( $this->mNewtext, $this->mTitle, $wgOut ) ) ) {
440 // Wrap the whole lot in a <pre> and don't parse
442 preg_match( '!\.(css|js)$!u', $this->mTitle->getText(), $m );
443 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
444 $wgOut->addHTML( htmlspecialchars( $this->mNewtext ) );
445 $wgOut->addHTML( "\n</pre>\n" );
448 $wgOut->addWikiTextTidy( $this->mNewtext );
451 if( is_object( $this->mNewRev ) && !$this->mNewRev->isCurrent() ) {
452 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
454 # Add redundant patrol link on bottom...
455 if( $this->mRcidMarkPatrolled && $this->mTitle->quickUserCan('patrol') ) {
456 $sk = $wgUser->getSkin();
458 "<div class='patrollink'>[" . $sk->link(
460 wfMsgHtml( 'markaspatrolleddiff' ),
463 'action' => 'markpatrolled',
464 'rcid' => $this->mRcidMarkPatrolled
470 wfProfileOut( __METHOD__ );
474 * Show the first revision of an article. Uses normal diff headers in
475 * contrast to normal "old revision" display style.
477 function showFirstRevision() {
478 global $wgOut, $wgUser;
479 wfProfileIn( __METHOD__ );
481 # Get article text from the DB
483 if ( ! $this->loadNewText() ) {
484 $t = $this->mTitle->getPrefixedText();
485 $d = wfMsgExt( 'missingarticle-diff', array( 'escape' ), $this->mOldid, $this->mNewid );
486 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
487 $wgOut->addWikiMsg( 'missing-article', "<nowiki>$t</nowiki>", $d );
488 wfProfileOut( __METHOD__ );
491 if ( $this->mNewRev->isCurrent() ) {
492 $wgOut->setArticleFlag( true );
495 # Check if user is allowed to look at this page. If not, bail out.
497 if ( !$this->mTitle->userCanRead() ) {
498 $wgOut->loginToUse();
500 wfProfileOut( __METHOD__ );
501 throw new MWException("Permission Error: you do not have access to view this page");
504 # Prepare the header box
506 $sk = $wgUser->getSkin();
508 $next = $this->mTitle->getNextRevisionID( $this->mNewid );
512 $nextlink = '<br />' . $sk->link(
514 wfMsgHtml( 'nextdiff' ),
516 'id' => 'differences-nextlink'
520 'oldid' => $this->mNewid,
528 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\">" .
529 $sk->revUserTools( $this->mNewRev ) . "<br />" . $sk->revComment( $this->mNewRev ) . $nextlink . "</div>\n";
531 $wgOut->addHTML( $header );
533 $wgOut->setSubtitle( wfMsgExt( 'difference', array( 'parseinline' ) ) );
534 $wgOut->setRobotPolicy( 'noindex,nofollow' );
536 wfProfileOut( __METHOD__ );
540 * Get the diff text, send it to $wgOut
541 * Returns false if the diff could not be generated, otherwise returns true
543 function showDiff( $otitle, $ntitle, $notice = '' ) {
545 $diff = $this->getDiff( $otitle, $ntitle, $notice );
546 if ( $diff === false ) {
547 $wgOut->addWikiMsg( 'missing-article', "<nowiki>(fixme, bug)</nowiki>", '' );
550 $this->showDiffStyle();
551 $wgOut->addHTML( $diff );
557 * Add style sheets and supporting JS for diff display.
559 function showDiffStyle() {
560 global $wgStylePath, $wgStyleVersion, $wgOut;
561 $wgOut->addStyle( 'common/diff.css' );
563 // JS is needed to detect old versions of Mozilla to work around an annoyance bug.
564 $wgOut->addScript( "<script type=\"text/javascript\" src=\"$wgStylePath/common/diff.js?$wgStyleVersion\"></script>" );
568 * Get complete diff table, including header
570 * @param Title $otitle Old title
571 * @param Title $ntitle New title
572 * @param string $notice HTML between diff header and body
575 function getDiff( $otitle, $ntitle, $notice = '' ) {
576 $body = $this->getDiffBody();
577 if ( $body === false ) {
580 $multi = $this->getMultiNotice();
581 return $this->addHeader( $body, $otitle, $ntitle, $multi, $notice );
586 * Get the diff table body, without header
590 function getDiffBody() {
592 wfProfileIn( __METHOD__ );
593 $this->mCacheHit = true;
594 // Check if the diff should be hidden from this user
595 if ( !$this->loadRevisionData() )
597 if ( $this->mOldRev && !$this->mOldRev->userCan(Revision::DELETED_TEXT) ) {
599 } else if ( $this->mNewRev && !$this->mNewRev->userCan(Revision::DELETED_TEXT) ) {
601 } else if ( $this->mOldRev && $this->mNewRev && $this->mOldRev->getID() == $this->mNewRev->getID() ) {
606 if ( $this->mOldid && $this->mNewid ) {
607 $key = wfMemcKey( 'diff', 'version', MW_DIFF_VERSION, 'oldid', $this->mOldid, 'newid', $this->mNewid );
609 if ( !$this->mRefreshCache ) {
610 $difftext = $wgMemc->get( $key );
612 wfIncrStats( 'diff_cache_hit' );
613 $difftext = $this->localiseLineNumbers( $difftext );
614 $difftext .= "\n<!-- diff cache key $key -->\n";
615 wfProfileOut( __METHOD__ );
618 } // don't try to load but save the result
620 $this->mCacheHit = false;
622 // Loadtext is permission safe, this just clears out the diff
623 if ( !$this->loadText() ) {
624 wfProfileOut( __METHOD__ );
628 $difftext = $this->generateDiffBody( $this->mOldtext, $this->mNewtext );
630 // Save to cache for 7 days
631 if ( !wfRunHooks( 'AbortDiffCache', array( &$this ) ) ) {
632 wfIncrStats( 'diff_uncacheable' );
633 } else if ( $key !== false && $difftext !== false ) {
634 wfIncrStats( 'diff_cache_miss' );
635 $wgMemc->set( $key, $difftext, 7*86400 );
637 wfIncrStats( 'diff_uncacheable' );
639 // Replace line numbers with the text in the user's language
640 if ( $difftext !== false ) {
641 $difftext = $this->localiseLineNumbers( $difftext );
643 wfProfileOut( __METHOD__ );
648 * Make sure the proper modules are loaded before we try to
651 private function initDiffEngines() {
652 global $wgExternalDiffEngine;
653 if ( $wgExternalDiffEngine == 'wikidiff' && !function_exists( 'wikidiff_do_diff' ) ) {
654 wfProfileIn( __METHOD__ . '-php_wikidiff.so' );
655 wfSuppressWarnings();
656 dl( 'php_wikidiff.so' );
658 wfProfileOut( __METHOD__ . '-php_wikidiff.so' );
660 else if ( $wgExternalDiffEngine == 'wikidiff2' && !function_exists( 'wikidiff2_do_diff' ) ) {
661 wfProfileIn( __METHOD__ . '-php_wikidiff2.so' );
662 wfSuppressWarnings();
663 dl( 'php_wikidiff2.so' );
665 wfProfileOut( __METHOD__ . '-php_wikidiff2.so' );
670 * Generate a diff, no caching
671 * $otext and $ntext must be already segmented
673 function generateDiffBody( $otext, $ntext ) {
674 global $wgExternalDiffEngine, $wgContLang;
676 $otext = str_replace( "\r\n", "\n", $otext );
677 $ntext = str_replace( "\r\n", "\n", $ntext );
679 $this->initDiffEngines();
681 if ( $wgExternalDiffEngine == 'wikidiff' && function_exists( 'wikidiff_do_diff' ) ) {
682 # For historical reasons, external diff engine expects
683 # input text to be HTML-escaped already
684 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
685 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
686 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) ) .
687 $this->debug( 'wikidiff1' );
690 if ( $wgExternalDiffEngine == 'wikidiff2' && function_exists( 'wikidiff2_do_diff' ) ) {
691 # Better external diff engine, the 2 may some day be dropped
692 # This one does the escaping and segmenting itself
693 wfProfileIn( 'wikidiff2_do_diff' );
694 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
695 $text .= $this->debug( 'wikidiff2' );
696 wfProfileOut( 'wikidiff2_do_diff' );
699 if ( $wgExternalDiffEngine != 'wikidiff3' && $wgExternalDiffEngine !== false ) {
701 global $wgTmpDirectory;
702 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
703 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
705 $tempFile1 = fopen( $tempName1, "w" );
707 wfProfileOut( __METHOD__ );
710 $tempFile2 = fopen( $tempName2, "w" );
712 wfProfileOut( __METHOD__ );
715 fwrite( $tempFile1, $otext );
716 fwrite( $tempFile2, $ntext );
717 fclose( $tempFile1 );
718 fclose( $tempFile2 );
719 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
720 wfProfileIn( __METHOD__ . "-shellexec" );
721 $difftext = wfShellExec( $cmd );
722 $difftext .= $this->debug( "external $wgExternalDiffEngine" );
723 wfProfileOut( __METHOD__ . "-shellexec" );
724 unlink( $tempName1 );
725 unlink( $tempName2 );
730 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
731 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
732 $diffs = new Diff( $ota, $nta );
733 $formatter = new TableDiffFormatter();
734 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) ) .
739 * Generate a debug comment indicating diff generating time,
740 * server node, and generator backend.
742 protected function debug( $generator="internal" ) {
743 global $wgShowHostnames;
744 if ( !$this->enableDebugComment ) {
747 $data = array( $generator );
748 if( $wgShowHostnames ) {
749 $data[] = wfHostname();
751 $data[] = wfTimestamp( TS_DB );
752 return "<!-- diff generator: " .
761 * Replace line numbers with the text in the user's language
763 function localiseLineNumbers( $text ) {
764 return preg_replace_callback( '/<!--LINE (\d+)-->/',
765 array( &$this, 'localiseLineNumbersCb' ), $text );
768 function localiseLineNumbersCb( $matches ) {
770 if ( $matches[1] === '1' && $this->mReducedLineNumbers ) return '';
771 return wfMsgExt( 'lineno', 'escape', $wgLang->formatNum( $matches[1] ) );
776 * If there are revisions between the ones being compared, return a note saying so.
778 function getMultiNotice() {
779 if ( !is_object($this->mOldRev) || !is_object($this->mNewRev) )
782 if( !$this->mOldPage->equals( $this->mNewPage ) ) {
783 // Comparing two different pages? Count would be meaningless.
787 $oldid = $this->mOldRev->getId();
788 $newid = $this->mNewRev->getId();
789 if ( $oldid > $newid ) {
790 $tmp = $oldid; $oldid = $newid; $newid = $tmp;
793 $n = $this->mTitle->countRevisionsBetween( $oldid, $newid );
797 return wfMsgExt( 'diff-multi', array( 'parseinline' ), $n );
802 * Add the header to a diff body
804 static function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) {
805 $header = "<table class='diff'>";
806 if( $diff ) { // Safari/Chrome show broken output if cols not used
808 <col class='diff-marker' />
809 <col class='diff-content' />
810 <col class='diff-marker' />
811 <col class='diff-content' />";
820 <td colspan='$colspan' class='diff-otitle'>{$otitle}</td>
821 <td colspan='$colspan' class='diff-ntitle'>{$ntitle}</td>
824 if ( $multi != '' ) {
825 $header .= "<tr><td colspan='{$multiColspan}' align='center' class='diff-multi'>{$multi}</td></tr>";
827 if ( $notice != '' ) {
828 $header .= "<tr><td colspan='{$multiColspan}' align='center'>{$notice}</td></tr>";
831 return $header . $diff . "</table>";
835 * Use specified text instead of loading from the database
837 function setText( $oldText, $newText ) {
838 $this->mOldtext = $oldText;
839 $this->mNewtext = $newText;
840 $this->mTextLoaded = 2;
841 $this->mRevisionsLoaded = true;
845 * Load revision metadata for the specified articles. If newid is 0, then compare
846 * the old article in oldid to the current article; if oldid is 0, then
847 * compare the current article to the immediately previous one (ignoring the
850 * If oldid is false, leave the corresponding revision object set
851 * to false. This is impossible via ordinary user input, and is provided for
854 function loadRevisionData() {
855 global $wgLang, $wgUser;
856 if ( $this->mRevisionsLoaded ) {
859 // Whether it succeeds or fails, we don't want to try again
860 $this->mRevisionsLoaded = true;
863 // Load the new revision object
864 $this->mNewRev = $this->mNewid
865 ? Revision::newFromId( $this->mNewid )
866 : Revision::newFromTitle( $this->mTitle );
867 if( !$this->mNewRev instanceof Revision )
870 // Update the new revision ID in case it was 0 (makes life easier doing UI stuff)
871 $this->mNewid = $this->mNewRev->getId();
873 // Check if page is editable
874 $editable = $this->mNewRev->getTitle()->userCan( 'edit' );
876 // Set assorted variables
877 $timestamp = $wgLang->timeanddate( $this->mNewRev->getTimestamp(), true );
878 $dateofrev = $wgLang->date( $this->mNewRev->getTimestamp(), true );
879 $timeofrev = $wgLang->time( $this->mNewRev->getTimestamp(), true );
880 $this->mNewPage = $this->mNewRev->getTitle();
881 if( $this->mNewRev->isCurrent() ) {
882 $newLink = $this->mNewPage->escapeLocalUrl( array(
883 'oldid' => $this->mNewid
885 $this->mPagetitle = htmlspecialchars( wfMsg(
891 $newEdit = $this->mNewPage->escapeLocalUrl( array(
895 $this->mNewtitle = "<a href='$newLink'>{$this->mPagetitle}</a>";
896 $this->mNewtitle .= " (<a href='$newEdit'>" . wfMsgHtml( $editable ? 'editold' : 'viewsourceold' ) . "</a>)";
898 $newLink = $this->mNewPage->escapeLocalUrl( array(
899 'oldid' => $this->mNewid
901 $newEdit = $this->mNewPage->escapeLocalUrl( array(
903 'oldid' => $this->mNewid
905 $this->mPagetitle = htmlspecialchars( wfMsg(
912 $this->mNewtitle = "<a href='$newLink'>{$this->mPagetitle}</a>";
913 $this->mNewtitle .= " (<a href='$newEdit'>" . wfMsgHtml( $editable ? 'editold' : 'viewsourceold' ) . "</a>)";
915 if( !$this->mNewRev->userCan(Revision::DELETED_TEXT) ) {
916 $this->mNewtitle = "<span class='history-deleted'>{$this->mPagetitle}</span>";
917 } else if ( $this->mNewRev->isDeleted(Revision::DELETED_TEXT) ) {
918 $this->mNewtitle = "<span class='history-deleted'>{$this->mNewtitle}</span>";
921 // Load the old revision object
922 $this->mOldRev = false;
923 if( $this->mOldid ) {
924 $this->mOldRev = Revision::newFromId( $this->mOldid );
925 } elseif ( $this->mOldid === 0 ) {
926 $rev = $this->mNewRev->getPrevious();
928 $this->mOldid = $rev->getId();
929 $this->mOldRev = $rev;
931 // No previous revision; mark to show as first-version only.
932 $this->mOldid = false;
933 $this->mOldRev = false;
935 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
937 if( is_null( $this->mOldRev ) ) {
941 if ( $this->mOldRev ) {
942 $this->mOldPage = $this->mOldRev->getTitle();
944 $t = $wgLang->timeanddate( $this->mOldRev->getTimestamp(), true );
945 $dateofrev = $wgLang->date( $this->mOldRev->getTimestamp(), true );
946 $timeofrev = $wgLang->time( $this->mOldRev->getTimestamp(), true );
947 $oldLink = $this->mOldPage->escapeLocalUrl( array(
948 'oldid' => $this->mOldid
950 $oldEdit = $this->mOldPage->escapeLocalUrl( array(
952 'oldid' => $this->mOldid
954 $this->mOldPagetitle = htmlspecialchars( wfMsg( 'revisionasof', $t, $dateofrev, $timeofrev ) );
956 $this->mOldtitle = "<a href='$oldLink'>{$this->mOldPagetitle}</a>"
957 . " (<a href='$oldEdit'>" . wfMsgHtml( $editable ? 'editold' : 'viewsourceold' ) . "</a>)";
958 // Add an "undo" link
959 $newUndo = $this->mNewPage->escapeLocalUrl( array(
961 'undoafter' => $this->mOldid,
962 'undo' => $this->mNewid
964 $htmlLink = htmlspecialchars( wfMsg( 'editundo' ) );
965 $htmlTitle = $wgUser->getSkin()->tooltip( 'undo' );
966 if( $editable && !$this->mOldRev->isDeleted( Revision::DELETED_TEXT ) && !$this->mNewRev->isDeleted( Revision::DELETED_TEXT ) ) {
967 $this->mNewtitle .= " (<a href='$newUndo' $htmlTitle>" . $htmlLink . "</a>)";
970 if( !$this->mOldRev->userCan( Revision::DELETED_TEXT ) ) {
971 $this->mOldtitle = '<span class="history-deleted">' . $this->mOldPagetitle . '</span>';
972 } else if( $this->mOldRev->isDeleted( Revision::DELETED_TEXT ) ) {
973 $this->mOldtitle = '<span class="history-deleted">' . $this->mOldtitle . '</span>';
981 * Load the text of the revisions, as well as revision data.
983 function loadText() {
984 if ( $this->mTextLoaded == 2 ) {
987 // Whether it succeeds or fails, we don't want to try again
988 $this->mTextLoaded = 2;
991 if ( !$this->loadRevisionData() ) {
994 if ( $this->mOldRev ) {
995 $this->mOldtext = $this->mOldRev->getText( Revision::FOR_THIS_USER );
996 if ( $this->mOldtext === false ) {
1000 if ( $this->mNewRev ) {
1001 $this->mNewtext = $this->mNewRev->getText( Revision::FOR_THIS_USER );
1002 if ( $this->mNewtext === false ) {
1010 * Load the text of the new revision, not the old one
1012 function loadNewText() {
1013 if ( $this->mTextLoaded >= 1 ) {
1016 $this->mTextLoaded = 1;
1018 if ( !$this->loadRevisionData() ) {
1021 $this->mNewtext = $this->mNewRev->getText( Revision::FOR_THIS_USER );