4 * @todo indicate where diff.doc can be found.
5 * @addtogroup DifferenceEngine
9 * Constant to indicate diff cache compatibility.
10 * Bump this when changing the diff formatting in a way that
11 * fixes important bugs or such to force cached diff views to
14 define( 'MW_DIFF_VERSION', '1.11a' );
19 * @addtogroup DifferenceEngine
21 class DifferenceEngine
{
25 var $mOldid, $mNewid, $mTitle;
26 var $mOldtitle, $mNewtitle, $mPagetitle;
27 var $mOldtext, $mNewtext;
28 var $mOldPage, $mNewPage;
29 var $mRcidMarkPatrolled;
30 var $mOldRev, $mNewRev;
31 var $mRevisionsLoaded = false; // Have the revisions been loaded
32 var $mTextLoaded = 0; // How many text blobs have been loaded, 0, 1 or 2?
37 * @param $titleObj Title object that the diff is associated with
38 * @param $old Integer: old ID we want to show and diff with.
39 * @param $new String: either 'prev' or 'next'.
40 * @param $rcid Integer: ??? FIXME (default 0)
41 * @param $refreshCache boolean If set, refreshes the diff cache
43 function DifferenceEngine( $titleObj = null, $old = 0, $new = 0, $rcid = 0, $refreshCache = false ) {
44 $this->mTitle
= $titleObj;
45 wfDebug("DifferenceEngine old '$old' new '$new' rcid '$rcid'\n");
47 if ( 'prev' === $new ) {
48 # Show diff between revision $old and the previous one.
49 # Get previous one from DB.
51 $this->mNewid
= intval($old);
53 $this->mOldid
= $this->mTitle
->getPreviousRevisionID( $this->mNewid
);
55 } elseif ( 'next' === $new ) {
56 # Show diff between revision $old and the previous one.
57 # Get previous one from DB.
59 $this->mOldid
= intval($old);
60 $this->mNewid
= $this->mTitle
->getNextRevisionID( $this->mOldid
);
61 if ( false === $this->mNewid
) {
62 # if no result, NewId points to the newest old revision. The only newer
63 # revision is cur, which is "0".
68 $this->mOldid
= intval($old);
69 $this->mNewid
= intval($new);
71 $this->mRcidMarkPatrolled
= intval($rcid); # force it to be an integer
72 $this->mRefreshCache
= $refreshCache;
75 function showDiffPage( $diffOnly = false ) {
76 global $wgUser, $wgOut, $wgUseExternalEditor, $wgUseRCPatrol;
77 $fname = 'DifferenceEngine::showDiffPage';
78 wfProfileIn( $fname );
80 # If external diffs are enabled both globally and for the user,
81 # we'll use the application/x-external-editor interface to call
82 # an external diff tool like kompare, kdiff3, etc.
83 if($wgUseExternalEditor && $wgUser->getOption('externaldiff')) {
84 global $wgInputEncoding,$wgServer,$wgScript,$wgLang;
86 header ( "Content-type: application/x-external-editor; charset=".$wgInputEncoding );
87 $url1=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mOldid
);
88 $url2=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mNewid
);
89 $special=$wgLang->getNsText(NS_SPECIAL
);
94 Script={$wgServer}{$wgScript}
95 Special namespace={$special}
109 $wgOut->setArticleFlag( false );
110 if ( ! $this->loadRevisionData() ) {
111 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, {$this->mNewid})";
112 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
113 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
114 $wgOut->addWikitext( $mtext );
115 wfProfileOut( $fname );
119 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
121 if ( $this->mNewRev
->isCurrent() ) {
122 $wgOut->setArticleFlag( true );
125 # mOldid is false if the difference engine is called with a "vague" query for
126 # a diff between a version V and its previous version V' AND the version V
127 # is the first version of that article. In that case, V' does not exist.
128 if ( $this->mOldid
=== false ) {
129 $this->showFirstRevision();
130 $this->renderNewRevision(); // should we respect $diffOnly here or not?
131 wfProfileOut( $fname );
135 $wgOut->suppressQuickbar();
137 $oldTitle = $this->mOldPage
->getPrefixedText();
138 $newTitle = $this->mNewPage
->getPrefixedText();
139 if( $oldTitle == $newTitle ) {
140 $wgOut->setPageTitle( $newTitle );
142 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
144 $wgOut->setSubtitle( wfMsg( 'difference' ) );
145 $wgOut->setRobotpolicy( 'noindex,nofollow' );
147 if ( !( $this->mOldPage
->userCanRead() && $this->mNewPage
->userCanRead() ) ) {
148 $wgOut->loginToUse();
150 wfProfileOut( $fname );
154 $sk = $wgUser->getSkin();
156 if ( $this->mNewRev
->isCurrent() && $wgUser->isAllowed('rollback') ) {
157 $rollback = ' ' . $sk->generateRollback( $this->mNewRev
);
162 // Prepare a change patrol link, if applicable
163 if( $wgUseRCPatrol && $wgUser->isAllowed( 'patrol' ) ) {
164 // If we've been given an explicit change identifier, use it; saves time
165 if( $this->mRcidMarkPatrolled
) {
166 $rcid = $this->mRcidMarkPatrolled
;
168 // Look for an unpatrolled change corresponding to this diff
169 $db = wfGetDB( DB_SLAVE
);
170 $change = RecentChange
::newFromConds(
172 // Add redundant timestamp condition so we can use the
174 'rc_timestamp' => $db->timestamp( $this->mNewRev
->getTimestamp() ),
175 'rc_this_oldid' => $this->mNewid
,
176 'rc_last_oldid' => $this->mOldid
,
181 if( $change instanceof RecentChange
) {
182 $rcid = $change->mAttribs
['rc_id'];
190 $patrol = ' [' . $sk->makeKnownLinkObj(
192 wfMsgHtml( 'markaspatrolleddiff' ),
193 "action=markpatrolled&rcid={$rcid}"
202 $prevlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'previousdiff' ),
203 'diff=prev&oldid='.$this->mOldid
, '', '', 'id="differences-prevlink"' );
204 if ( $this->mNewRev
->isCurrent() ) {
205 $nextlink = ' ';
207 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ),
208 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
214 if ($this->mOldRev
->mMinorEdit
== 1) {
215 $oldminor = wfElement( 'span', array( 'class' => 'minor' ),
216 wfMsg( 'minoreditletter') ) . ' ';
219 if ($this->mNewRev
->mMinorEdit
== 1) {
220 $newminor = wfElement( 'span', array( 'class' => 'minor' ),
221 wfMsg( 'minoreditletter') ) . ' ';
224 $rdel = ''; $ldel = '';
225 if( $wgUser->isAllowed( 'deleterevision' ) ) {
226 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
227 if( !$this->mOldRev
->userCan( Revision
::DELETED_RESTRICTED
) ) {
228 // If revision was hidden from sysops
229 $ldel = wfMsgHtml('rev-delundel');
231 $ldel = $sk->makeKnownLinkObj( $revdel,
232 wfMsgHtml('rev-delundel'),
233 'target=' . urlencode( $this->mOldRev
->mTitle
->getPrefixedDbkey() ) .
234 '&oldid=' . urlencode( $this->mOldRev
->getId() ) );
235 // Bolden oversighted content
236 if( $this->mOldRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
237 $ldel = "<strong>$ldel</strong>";
239 $ldel = " <tt>(<small>$ldel</small>)</tt> ";
240 // We don't currently handle well changing the top revision's settings
241 if( $this->mNewRev
->isCurrent() ) {
242 // If revision was hidden from sysops
243 $rdel = wfMsgHtml('rev-delundel');
244 } else if( !$this->mNewRev
->userCan( Revision
::DELETED_RESTRICTED
) ) {
245 // If revision was hidden from sysops
246 $rdel = wfMsgHtml('rev-delundel');
248 $rdel = $sk->makeKnownLinkObj( $revdel,
249 wfMsgHtml('rev-delundel'),
250 'target=' . urlencode( $this->mNewRev
->mTitle
->getPrefixedDbkey() ) .
251 '&oldid=' . urlencode( $this->mNewRev
->getId() ) );
252 // Bolden oversighted content
253 if( $this->mNewRev
->isDeleted( Revision
::DELETED_RESTRICTED
) )
254 $rdel = "<strong>$rdel</strong>";
256 $rdel = " <tt>(<small>$rdel</small>)</tt> ";
259 $oldHeader = '<div id="mw-diff-otitle1"><strong>'.$this->mOldtitle
.'</strong></div>' .
260 '<div id="mw-diff-otitle2">' . $sk->revUserTools( $this->mOldRev
, true ) . "</div>" .
261 '<div id="mw-diff-otitle3">' . $oldminor . $sk->revComment( $this->mOldRev
, !$diffOnly, true ) . $ldel . "</div>" .
262 '<div id="mw-diff-otitle4">' . $prevlink .'</div>';
263 $newHeader = '<div id="mw-diff-ntitle1"><strong>'.$this->mNewtitle
.'</strong></div>' .
264 '<div id="mw-diff-ntitle2">' . $sk->revUserTools( $this->mNewRev
, true ) . " $rollback</div>" .
265 '<div id="mw-diff-ntitle3">' . $newminor . $sk->revComment( $this->mNewRev
, !$diffOnly, true ) . $rdel . "</div>" .
266 '<div id="mw-diff-ntitle4">' . $nextlink . $patrol . '</div>';
268 $this->showDiff( $oldHeader, $newHeader );
271 $this->renderNewRevision();
273 wfProfileOut( $fname );
277 * Show the new revision of the page.
279 function renderNewRevision() {
281 $fname = 'DifferenceEngine::renderNewRevision';
282 wfProfileIn( $fname );
284 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
285 #add deleted rev tag if needed
286 if( !$this->mNewRev
->userCan(Revision
::DELETED_TEXT
) ) {
287 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-permission' ) );
288 } else if( $this->mNewRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
289 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-view' ) );
292 if( !$this->mNewRev
->isCurrent() ) {
293 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
296 $this->loadNewText();
297 if( is_object( $this->mNewRev
) ) {
298 $wgOut->setRevisionId( $this->mNewRev
->getId() );
301 if ($this->mTitle
->isCssJsSubpage() ||
$this->mTitle
->isCssOrJsPage()) {
302 // Stolen from Article::view --AG 2007-10-11
304 // Give hooks a chance to customise the output
305 if( wfRunHooks( 'ShowRawCssJs', array( $this->mNewtext
, $this->mTitle
, $wgOut ) ) ) {
306 // Wrap the whole lot in a <pre> and don't parse
308 preg_match( '!\.(css|js)$!u', $this->mTitle
->getText(), $m );
309 $wgOut->addHtml( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
310 $wgOut->addHtml( htmlspecialchars( $this->mNewtext
) );
311 $wgOut->addHtml( "\n</pre>\n" );
314 $wgOut->addWikiTextTidy( $this->mNewtext
);
316 if( !$this->mNewRev
->isCurrent() ) {
317 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
320 wfProfileOut( $fname );
324 * Show the first revision of an article. Uses normal diff headers in
325 * contrast to normal "old revision" display style.
327 function showFirstRevision() {
328 global $wgOut, $wgUser;
330 $fname = 'DifferenceEngine::showFirstRevision';
331 wfProfileIn( $fname );
333 # Get article text from the DB
335 if ( ! $this->loadNewText() ) {
336 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
338 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
339 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
340 $wgOut->addWikitext( $mtext );
341 wfProfileOut( $fname );
344 if ( $this->mNewRev
->isCurrent() ) {
345 $wgOut->setArticleFlag( true );
348 # Check if user is allowed to look at this page. If not, bail out.
350 if ( !( $this->mTitle
->userCanRead() ) ) {
351 $wgOut->loginToUse();
353 wfProfileOut( $fname );
357 # Prepare the header box
359 $sk = $wgUser->getSkin();
361 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
362 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />" .
363 $sk->revUserTools( $this->mNewRev
) . "<br />" .
364 $sk->revComment( $this->mNewRev
) . "<br />" .
365 $nextlink . "</div>\n";
367 $wgOut->addHTML( $header );
369 $wgOut->setSubtitle( wfMsg( 'difference' ) );
370 $wgOut->setRobotpolicy( 'noindex,nofollow' );
372 wfProfileOut( $fname );
376 * Get the diff text, send it to $wgOut
377 * Returns false if the diff could not be generated, otherwise returns true
379 function showDiff( $otitle, $ntitle ) {
381 $diff = $this->getDiff( $otitle, $ntitle );
382 if ( $diff === false ) {
383 $wgOut->addWikitext( wfMsg( 'missingarticle', "<nowiki>(fixme, bug)</nowiki>" ) );
386 $this->showDiffStyle();
387 $wgOut->addHTML( $diff );
393 * Add style sheets and supporting JS for diff display.
395 function showDiffStyle() {
396 global $wgStylePath, $wgStyleVersion, $wgOut;
397 $wgOut->addStyle( 'common/diff.css' );
399 // JS is needed to detect old versions of Mozilla to work around an annoyance bug.
400 $wgOut->addScript( "<script type=\"text/javascript\" src=\"$wgStylePath/common/diff.js?$wgStyleVersion\"></script>" );
404 * Get complete diff table, including header
406 * @param Title $otitle Old title
407 * @param Title $ntitle New title
410 function getDiff( $otitle, $ntitle ) {
411 $body = $this->getDiffBody();
412 if ( $body === false ) {
415 $multi = $this->getMultiNotice();
416 return $this->addHeader( $body, $otitle, $ntitle, $multi );
421 * Get the diff table body, without header
425 function getDiffBody() {
427 $fname = 'DifferenceEngine::getDiffBody';
428 wfProfileIn( $fname );
432 if ( $this->mOldid
&& $this->mNewid
) {
433 $key = wfMemcKey( 'diff', 'version', MW_DIFF_VERSION
, 'oldid', $this->mOldid
, 'newid', $this->mNewid
);
435 if ( !$this->mRefreshCache
) {
436 $difftext = $wgMemc->get( $key );
438 wfIncrStats( 'diff_cache_hit' );
439 $difftext = $this->localiseLineNumbers( $difftext );
440 $difftext .= "\n<!-- diff cache key $key -->\n";
441 wfProfileOut( $fname );
444 } // don't try to load but save the result
447 // Loadtext is permission safe, this just clears out the diff
448 if ( !$this->loadText() ) {
449 wfProfileOut( $fname );
451 } else if ( $this->mOldRev
&& !$this->mOldRev
->userCan(Revision
::DELETED_TEXT
) ) {
453 } else if ( $this->mNewRev
&& !$this->mNewRev
->userCan(Revision
::DELETED_TEXT
) ) {
457 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
459 // Save to cache for 7 days
460 // Only do this for public revs, otherwise an admin can view the diff and a non-admin can nab it!
461 if ( $this->mOldRev
&& $this->mOldRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
462 wfIncrStats( 'diff_uncacheable' );
463 } else if ( $this->mNewRev
&& $this->mNewRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
464 wfIncrStats( 'diff_uncacheable' );
465 } else if ( $key !== false && $difftext !== false ) {
466 wfIncrStats( 'diff_cache_miss' );
467 $wgMemc->set( $key, $difftext, 7*86400 );
469 wfIncrStats( 'diff_uncacheable' );
471 // Replace line numbers with the text in the user's language
472 if ( $difftext !== false ) {
473 $difftext = $this->localiseLineNumbers( $difftext );
475 wfProfileOut( $fname );
480 * Generate a diff, no caching
481 * $otext and $ntext must be already segmented
483 function generateDiffBody( $otext, $ntext ) {
484 global $wgExternalDiffEngine, $wgContLang;
485 $fname = 'DifferenceEngine::generateDiffBody';
487 $otext = str_replace( "\r\n", "\n", $otext );
488 $ntext = str_replace( "\r\n", "\n", $ntext );
490 if ( $wgExternalDiffEngine == 'wikidiff' ) {
491 # For historical reasons, external diff engine expects
492 # input text to be HTML-escaped already
493 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
494 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
495 if( !function_exists( 'wikidiff_do_diff' ) ) {
496 dl('php_wikidiff.so');
498 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) );
501 if ( $wgExternalDiffEngine == 'wikidiff2' ) {
502 # Better external diff engine, the 2 may some day be dropped
503 # This one does the escaping and segmenting itself
504 if ( !function_exists( 'wikidiff2_do_diff' ) ) {
505 wfProfileIn( "$fname-dl" );
506 @dl
('php_wikidiff2.so');
507 wfProfileOut( "$fname-dl" );
509 if ( function_exists( 'wikidiff2_do_diff' ) ) {
510 wfProfileIn( 'wikidiff2_do_diff' );
511 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
512 wfProfileOut( 'wikidiff2_do_diff' );
516 if ( $wgExternalDiffEngine !== false ) {
518 global $wgTmpDirectory;
519 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
520 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
522 $tempFile1 = fopen( $tempName1, "w" );
524 wfProfileOut( $fname );
527 $tempFile2 = fopen( $tempName2, "w" );
529 wfProfileOut( $fname );
532 fwrite( $tempFile1, $otext );
533 fwrite( $tempFile2, $ntext );
534 fclose( $tempFile1 );
535 fclose( $tempFile2 );
536 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
537 wfProfileIn( "$fname-shellexec" );
538 $difftext = wfShellExec( $cmd );
539 wfProfileOut( "$fname-shellexec" );
540 unlink( $tempName1 );
541 unlink( $tempName2 );
546 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
547 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
548 $diffs = new Diff( $ota, $nta );
549 $formatter = new TableDiffFormatter();
550 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
555 * Replace line numbers with the text in the user's language
557 function localiseLineNumbers( $text ) {
558 return preg_replace_callback( '/<!--LINE (\d+)-->/',
559 array( &$this, 'localiseLineNumbersCb' ), $text );
562 function localiseLineNumbersCb( $matches ) {
564 return wfMsgExt( 'lineno', array('parseinline'), $wgLang->formatNum( $matches[1] ) );
569 * If there are revisions between the ones being compared, return a note saying so.
571 function getMultiNotice() {
572 if ( !is_object($this->mOldRev
) ||
!is_object($this->mNewRev
) )
575 if( !$this->mOldPage
->equals( $this->mNewPage
) ) {
576 // Comparing two different pages? Count would be meaningless.
580 $oldid = $this->mOldRev
->getId();
581 $newid = $this->mNewRev
->getId();
582 if ( $oldid > $newid ) {
583 $tmp = $oldid; $oldid = $newid; $newid = $tmp;
586 $n = $this->mTitle
->countRevisionsBetween( $oldid, $newid );
590 return wfMsgExt( 'diff-multi', array( 'parseinline' ), $n );
595 * Add the header to a diff body
597 static function addHeader( $diff, $otitle, $ntitle, $multi = '' ) {
602 <col class='diff-marker' />
603 <col class='diff-content' />
604 <col class='diff-marker' />
605 <col class='diff-content' />
607 <td colspan='2' class='diff-otitle'>{$otitle}</td>
608 <td colspan='2' class='diff-ntitle'>{$ntitle}</td>
613 $header .= "<tr><td colspan='4' align='center' class='diff-multi'>{$multi}</td></tr>";
615 return $header . $diff . "</table>";
619 * Use specified text instead of loading from the database
621 function setText( $oldText, $newText ) {
622 $this->mOldtext
= $oldText;
623 $this->mNewtext
= $newText;
624 $this->mTextLoaded
= 2;
628 * Load revision metadata for the specified articles. If newid is 0, then compare
629 * the old article in oldid to the current article; if oldid is 0, then
630 * compare the current article to the immediately previous one (ignoring the
633 * If oldid is false, leave the corresponding revision object set
634 * to false. This is impossible via ordinary user input, and is provided for
637 function loadRevisionData() {
639 if ( $this->mRevisionsLoaded
) {
642 // Whether it succeeds or fails, we don't want to try again
643 $this->mRevisionsLoaded
= true;
646 // Load the new revision object
647 $this->mNewRev
= $this->mNewid
648 ? Revision
::newFromId( $this->mNewid
)
649 : Revision
::newFromTitle( $this->mTitle
);
650 if( !$this->mNewRev
instanceof Revision
)
653 // Update the new revision ID in case it was 0 (makes life easier doing UI stuff)
654 $this->mNewid
= $this->mNewRev
->getId();
656 // Set assorted variables
657 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
658 $this->mNewPage
= $this->mNewRev
->getTitle();
659 if( $this->mNewRev
->isCurrent() ) {
660 $newLink = $this->mNewPage
->escapeLocalUrl();
661 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
662 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit' );
664 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a> ($timestamp)"
665 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
668 $newLink = $this->mNewPage
->escapeLocalUrl( 'oldid=' . $this->mNewid
);
669 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mNewid
);
670 $this->mPagetitle
= wfMsgHTML( 'revisionasof', $timestamp );
672 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>"
673 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
675 if ( !$this->mNewRev
->userCan(Revision
::DELETED_TEXT
) ) {
676 $this->mNewtitle
= "<span class='history-deleted'>{$this->mPagetitle}</span>";
677 } else if ( $this->mNewRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
678 $this->mNewtitle
= '<span class="history-deleted">'.$this->mNewtitle
.'</span>';
681 // Load the old revision object
682 $this->mOldRev
= false;
683 if( $this->mOldid
) {
684 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
685 } elseif ( $this->mOldid
=== 0 ) {
686 $rev = $this->mNewRev
->getPrevious();
688 $this->mOldid
= $rev->getId();
689 $this->mOldRev
= $rev;
691 // No previous revision; mark to show as first-version only.
692 $this->mOldid
= false;
693 $this->mOldRev
= false;
695 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
697 if( is_null( $this->mOldRev
) ) {
701 if ( $this->mOldRev
) {
702 $this->mOldPage
= $this->mOldRev
->getTitle();
704 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
705 $oldLink = $this->mOldPage
->escapeLocalUrl( 'oldid=' . $this->mOldid
);
706 $oldEdit = $this->mOldPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mOldid
);
707 $this->mOldPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t ) );
709 $this->mOldtitle
= "<a href='$oldLink'>{$this->mOldPagetitle}</a>"
710 . " (<a href='$oldEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
711 // Add an "undo" link
712 $newUndo = $this->mNewPage
->escapeLocalUrl( 'action=edit&undoafter=' . $this->mOldid
. '&undo=' . $this->mNewid
);
713 if ( $this->mNewRev
->userCan(Revision
::DELETED_TEXT
) )
714 $this->mNewtitle
.= " (<a href='$newUndo'>" . htmlspecialchars( wfMsg( 'editundo' ) ) . "</a>)";
716 if ( !$this->mOldRev
->userCan(Revision
::DELETED_TEXT
) ) {
717 $this->mOldtitle
= "<span class='history-deleted'>{$this->mOldPagetitle}</span>";
718 } else if ( $this->mOldRev
->isDeleted(Revision
::DELETED_TEXT
) ) {
719 $this->mOldtitle
= '<span class="history-deleted">'.$this->mOldtitle
.'</span>';
727 * Load the text of the revisions, as well as revision data.
729 function loadText() {
730 if ( $this->mTextLoaded
== 2 ) {
733 // Whether it succeeds or fails, we don't want to try again
734 $this->mTextLoaded
= 2;
737 if ( !$this->loadRevisionData() ) {
740 if ( $this->mOldRev
) {
741 $this->mOldtext
= $this->mOldRev
->revText();
742 if ( $this->mOldtext
=== false ) {
746 if ( $this->mNewRev
) {
747 $this->mNewtext
= $this->mNewRev
->revText();
748 if ( $this->mNewtext
=== false ) {
756 * Load the text of the new revision, not the old one
758 function loadNewText() {
759 if ( $this->mTextLoaded
>= 1 ) {
762 $this->mTextLoaded
= 1;
764 if ( !$this->loadRevisionData() ) {
767 $this->mNewtext
= $this->mNewRev
->getText();
774 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
776 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
777 // You may copy this code freely under the conditions of the GPL.
780 define('USE_ASSERTS', function_exists('assert'));
785 * @addtogroup DifferenceEngine
793 trigger_error('pure virtual', E_USER_ERROR
);
797 return $this->orig ?
sizeof($this->orig
) : 0;
800 function nclosing() {
801 return $this->closing ?
sizeof($this->closing
) : 0;
808 * @addtogroup DifferenceEngine
810 class _DiffOp_Copy
extends _DiffOp
{
813 function _DiffOp_Copy ($orig, $closing = false) {
814 if (!is_array($closing))
817 $this->closing
= $closing;
821 return new _DiffOp_Copy($this->closing
, $this->orig
);
828 * @addtogroup DifferenceEngine
830 class _DiffOp_Delete
extends _DiffOp
{
831 var $type = 'delete';
833 function _DiffOp_Delete ($lines) {
834 $this->orig
= $lines;
835 $this->closing
= false;
839 return new _DiffOp_Add($this->orig
);
846 * @addtogroup DifferenceEngine
848 class _DiffOp_Add
extends _DiffOp
{
851 function _DiffOp_Add ($lines) {
852 $this->closing
= $lines;
857 return new _DiffOp_Delete($this->closing
);
864 * @addtogroup DifferenceEngine
866 class _DiffOp_Change
extends _DiffOp
{
867 var $type = 'change';
869 function _DiffOp_Change ($orig, $closing) {
871 $this->closing
= $closing;
875 return new _DiffOp_Change($this->closing
, $this->orig
);
881 * Class used internally by Diff to actually compute the diffs.
883 * The algorithm used here is mostly lifted from the perl module
884 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
885 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
887 * More ideas are taken from:
888 * http://www.ics.uci.edu/~eppstein/161/960229.html
890 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
891 * diffutils-2.7, which can be found at:
892 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
894 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
897 * Line length limits for robustness added by Tim Starling, 2005-08-31
899 * @author Geoffrey T. Dairiki, Tim Starling
901 * @addtogroup DifferenceEngine
905 const MAX_XREF_LENGTH
= 10000;
907 function diff ($from_lines, $to_lines) {
908 $fname = '_DiffEngine::diff';
909 wfProfileIn( $fname );
911 $n_from = sizeof($from_lines);
912 $n_to = sizeof($to_lines);
914 $this->xchanged
= $this->ychanged
= array();
915 $this->xv
= $this->yv
= array();
916 $this->xind
= $this->yind
= array();
918 unset($this->in_seq
);
921 // Skip leading common lines.
922 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
923 if ($from_lines[$skip] !== $to_lines[$skip])
925 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
927 // Skip trailing common lines.
928 $xi = $n_from; $yi = $n_to;
929 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
930 if ($from_lines[$xi] !== $to_lines[$yi])
932 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
935 // Ignore lines which do not exist in both files.
936 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
937 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
940 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
941 $line = $to_lines[$yi];
942 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
944 $yhash[$this->_line_hash($line)] = 1;
948 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
949 $line = $from_lines[$xi];
950 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
957 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
959 // Merge edits when possible
960 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
961 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
963 // Compute the edit operations.
966 while ($xi < $n_from ||
$yi < $n_to) {
967 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
968 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
970 // Skip matching "snake".
972 while ( $xi < $n_from && $yi < $n_to
973 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
974 $copy[] = $from_lines[$xi++
];
978 $edits[] = new _DiffOp_Copy($copy);
980 // Find deletes & adds.
982 while ($xi < $n_from && $this->xchanged
[$xi])
983 $delete[] = $from_lines[$xi++
];
986 while ($yi < $n_to && $this->ychanged
[$yi])
987 $add[] = $to_lines[$yi++
];
990 $edits[] = new _DiffOp_Change($delete, $add);
992 $edits[] = new _DiffOp_Delete($delete);
994 $edits[] = new _DiffOp_Add($add);
996 wfProfileOut( $fname );
1001 * Returns the whole line if it's small enough, or the MD5 hash otherwise
1003 function _line_hash( $line ) {
1004 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
1005 return md5( $line );
1012 /* Divide the Largest Common Subsequence (LCS) of the sequences
1013 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
1016 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
1017 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
1018 * sub sequences. The first sub-sequence is contained in [X0, X1),
1019 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
1020 * that (X0, Y0) == (XOFF, YOFF) and
1021 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
1023 * This function assumes that the first lines of the specified portions
1024 * of the two files do not match, and likewise that the last lines do not
1025 * match. The caller must trim matching lines from the beginning and end
1026 * of the portions it is going to specify.
1028 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
1029 $fname = '_DiffEngine::_diag';
1030 wfProfileIn( $fname );
1033 if ($xlim - $xoff > $ylim - $yoff) {
1034 // Things seems faster (I'm not sure I understand why)
1035 // when the shortest sequence in X.
1037 list ($xoff, $xlim, $yoff, $ylim)
1038 = array( $yoff, $ylim, $xoff, $xlim);
1042 for ($i = $ylim - 1; $i >= $yoff; $i--)
1043 $ymatches[$this->xv
[$i]][] = $i;
1045 for ($i = $ylim - 1; $i >= $yoff; $i--)
1046 $ymatches[$this->yv
[$i]][] = $i;
1049 $this->seq
[0]= $yoff - 1;
1050 $this->in_seq
= array();
1051 $ymids[0] = array();
1053 $numer = $xlim - $xoff +
$nchunks - 1;
1055 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
1056 wfProfileIn( "$fname-chunk" );
1058 for ($i = 0; $i <= $this->lcs
; $i++
)
1059 $ymids[$i][$chunk-1] = $this->seq
[$i];
1061 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
1062 for ( ; $x < $x1; $x++
) {
1063 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
1064 if (empty($ymatches[$line]))
1066 $matches = $ymatches[$line];
1068 while (list ($junk, $y) = each($matches))
1069 if (empty($this->in_seq
[$y])) {
1070 $k = $this->_lcs_pos($y);
1071 USE_ASSERTS
&& assert($k > 0);
1072 $ymids[$k] = $ymids[$k-1];
1075 while (list ( /* $junk */, $y) = each($matches)) {
1076 if ($y > $this->seq
[$k-1]) {
1077 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
1078 // Optimization: this is a common case:
1079 // next match is just replacing previous match.
1080 $this->in_seq
[$this->seq
[$k]] = false;
1081 $this->seq
[$k] = $y;
1082 $this->in_seq
[$y] = 1;
1083 } else if (empty($this->in_seq
[$y])) {
1084 $k = $this->_lcs_pos($y);
1085 USE_ASSERTS
&& assert($k > 0);
1086 $ymids[$k] = $ymids[$k-1];
1090 wfProfileOut( "$fname-chunk" );
1093 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
1094 $ymid = $ymids[$this->lcs
];
1095 for ($n = 0; $n < $nchunks - 1; $n++
) {
1096 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
1097 $y1 = $ymid[$n] +
1;
1098 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
1100 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
1102 wfProfileOut( $fname );
1103 return array($this->lcs
, $seps);
1106 function _lcs_pos ($ypos) {
1107 $fname = '_DiffEngine::_lcs_pos';
1108 wfProfileIn( $fname );
1111 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
1112 $this->seq
[++
$this->lcs
] = $ypos;
1113 $this->in_seq
[$ypos] = 1;
1114 wfProfileOut( $fname );
1119 while ($beg < $end) {
1120 $mid = (int)(($beg +
$end) / 2);
1121 if ( $ypos > $this->seq
[$mid] )
1127 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
1129 $this->in_seq
[$this->seq
[$end]] = false;
1130 $this->seq
[$end] = $ypos;
1131 $this->in_seq
[$ypos] = 1;
1132 wfProfileOut( $fname );
1136 /* Find LCS of two sequences.
1138 * The results are recorded in the vectors $this->{x,y}changed[], by
1139 * storing a 1 in the element for each line that is an insertion
1140 * or deletion (ie. is not in the LCS).
1142 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
1144 * Note that XLIM, YLIM are exclusive bounds.
1145 * All line numbers are origin-0 and discarded lines are not counted.
1147 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
1148 $fname = '_DiffEngine::_compareseq';
1149 wfProfileIn( $fname );
1151 // Slide down the bottom initial diagonal.
1152 while ($xoff < $xlim && $yoff < $ylim
1153 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
1158 // Slide up the top initial diagonal.
1159 while ($xlim > $xoff && $ylim > $yoff
1160 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
1165 if ($xoff == $xlim ||
$yoff == $ylim)
1168 // This is ad hoc but seems to work well.
1169 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
1170 //$nchunks = max(2,min(8,(int)$nchunks));
1171 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
1173 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
1177 // X and Y sequences have no common subsequence:
1178 // mark all changed.
1179 while ($yoff < $ylim)
1180 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
1181 while ($xoff < $xlim)
1182 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
1184 // Use the partitions to split this problem into subproblems.
1187 while ($pt2 = next($seps)) {
1188 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1192 wfProfileOut( $fname );
1195 /* Adjust inserts/deletes of identical lines to join changes
1196 * as much as possible.
1198 * We do something when a run of changed lines include a
1199 * line at one end and has an excluded, identical line at the other.
1200 * We are free to choose which identical line is included.
1201 * `compareseq' usually chooses the one at the beginning,
1202 * but usually it is cleaner to consider the following identical line
1203 * to be the "change".
1205 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1207 function _shift_boundaries ($lines, &$changed, $other_changed) {
1208 $fname = '_DiffEngine::_shift_boundaries';
1209 wfProfileIn( $fname );
1213 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1214 $len = sizeof($lines);
1215 $other_len = sizeof($other_changed);
1219 * Scan forwards to find beginning of another run of changes.
1220 * Also keep track of the corresponding point in the other file.
1222 * Throughout this code, $i and $j are adjusted together so that
1223 * the first $i elements of $changed and the first $j elements
1224 * of $other_changed both contain the same number of zeros
1225 * (unchanged lines).
1226 * Furthermore, $j is always kept so that $j == $other_len or
1227 * $other_changed[$j] == false.
1229 while ($j < $other_len && $other_changed[$j])
1232 while ($i < $len && ! $changed[$i]) {
1233 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1235 while ($j < $other_len && $other_changed[$j])
1244 // Find the end of this run of changes.
1245 while (++
$i < $len && $changed[$i])
1250 * Record the length of this run of changes, so that
1251 * we can later determine whether the run has grown.
1253 $runlength = $i - $start;
1256 * Move the changed region back, so long as the
1257 * previous unchanged line matches the last changed one.
1258 * This merges with previous changed regions.
1260 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1261 $changed[--$start] = 1;
1262 $changed[--$i] = false;
1263 while ($start > 0 && $changed[$start - 1])
1265 USE_ASSERTS
&& assert('$j > 0');
1266 while ($other_changed[--$j])
1268 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1272 * Set CORRESPONDING to the end of the changed run, at the last
1273 * point where it corresponds to a changed run in the other file.
1274 * CORRESPONDING == LEN means no such point has been found.
1276 $corresponding = $j < $other_len ?
$i : $len;
1279 * Move the changed region forward, so long as the
1280 * first changed line matches the following unchanged one.
1281 * This merges with following changed regions.
1282 * Do this second, so that if there are no merges,
1283 * the changed region is moved forward as far as possible.
1285 while ($i < $len && $lines[$start] == $lines[$i]) {
1286 $changed[$start++
] = false;
1288 while ($i < $len && $changed[$i])
1291 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1293 if ($j < $other_len && $other_changed[$j]) {
1294 $corresponding = $i;
1295 while ($j < $other_len && $other_changed[$j])
1299 } while ($runlength != $i - $start);
1302 * If possible, move the fully-merged run of changes
1303 * back to a corresponding run in the other file.
1305 while ($corresponding < $i) {
1306 $changed[--$start] = 1;
1308 USE_ASSERTS
&& assert('$j > 0');
1309 while ($other_changed[--$j])
1311 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1314 wfProfileOut( $fname );
1319 * Class representing a 'diff' between two sequences of strings.
1322 * @addtogroup DifferenceEngine
1330 * Computes diff between sequences of strings.
1332 * @param $from_lines array An array of strings.
1333 * (Typically these are lines from a file.)
1334 * @param $to_lines array An array of strings.
1336 function Diff($from_lines, $to_lines) {
1337 $eng = new _DiffEngine
;
1338 $this->edits
= $eng->diff($from_lines, $to_lines);
1339 //$this->_check($from_lines, $to_lines);
1343 * Compute reversed Diff.
1347 * $diff = new Diff($lines1, $lines2);
1348 * $rev = $diff->reverse();
1349 * @return object A Diff object representing the inverse of the
1352 function reverse () {
1354 $rev->edits
= array();
1355 foreach ($this->edits
as $edit) {
1356 $rev->edits
[] = $edit->reverse();
1362 * Check for empty diff.
1364 * @return bool True iff two sequences were identical.
1366 function isEmpty () {
1367 foreach ($this->edits
as $edit) {
1368 if ($edit->type
!= 'copy')
1375 * Compute the length of the Longest Common Subsequence (LCS).
1377 * This is mostly for diagnostic purposed.
1379 * @return int The length of the LCS.
1383 foreach ($this->edits
as $edit) {
1384 if ($edit->type
== 'copy')
1385 $lcs +
= sizeof($edit->orig
);
1391 * Get the original set of lines.
1393 * This reconstructs the $from_lines parameter passed to the
1396 * @return array The original sequence of strings.
1401 foreach ($this->edits
as $edit) {
1403 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1409 * Get the closing set of lines.
1411 * This reconstructs the $to_lines parameter passed to the
1414 * @return array The sequence of strings.
1416 function closing() {
1419 foreach ($this->edits
as $edit) {
1421 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1427 * Check a Diff for validity.
1429 * This is here only for debugging purposes.
1431 function _check ($from_lines, $to_lines) {
1432 $fname = 'Diff::_check';
1433 wfProfileIn( $fname );
1434 if (serialize($from_lines) != serialize($this->orig()))
1435 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1436 if (serialize($to_lines) != serialize($this->closing()))
1437 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1439 $rev = $this->reverse();
1440 if (serialize($to_lines) != serialize($rev->orig()))
1441 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1442 if (serialize($from_lines) != serialize($rev->closing()))
1443 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1447 foreach ($this->edits
as $edit) {
1448 if ( $prevtype == $edit->type
)
1449 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1450 $prevtype = $edit->type
;
1453 $lcs = $this->lcs();
1454 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1455 wfProfileOut( $fname );
1460 * @todo document, bad name.
1462 * @addtogroup DifferenceEngine
1464 class MappedDiff
extends Diff
1469 * Computes diff between sequences of strings.
1471 * This can be used to compute things like
1472 * case-insensitve diffs, or diffs which ignore
1473 * changes in white-space.
1475 * @param $from_lines array An array of strings.
1476 * (Typically these are lines from a file.)
1478 * @param $to_lines array An array of strings.
1480 * @param $mapped_from_lines array This array should
1481 * have the same size number of elements as $from_lines.
1482 * The elements in $mapped_from_lines and
1483 * $mapped_to_lines are what is actually compared
1484 * when computing the diff.
1486 * @param $mapped_to_lines array This array should
1487 * have the same number of elements as $to_lines.
1489 function MappedDiff($from_lines, $to_lines,
1490 $mapped_from_lines, $mapped_to_lines) {
1491 $fname = 'MappedDiff::MappedDiff';
1492 wfProfileIn( $fname );
1494 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1495 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1497 $this->Diff($mapped_from_lines, $mapped_to_lines);
1500 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1501 $orig = &$this->edits
[$i]->orig
;
1502 if (is_array($orig)) {
1503 $orig = array_slice($from_lines, $xi, sizeof($orig));
1504 $xi +
= sizeof($orig);
1507 $closing = &$this->edits
[$i]->closing
;
1508 if (is_array($closing)) {
1509 $closing = array_slice($to_lines, $yi, sizeof($closing));
1510 $yi +
= sizeof($closing);
1513 wfProfileOut( $fname );
1518 * A class to format Diffs
1520 * This class formats the diff in classic diff format.
1521 * It is intended that this class be customized via inheritance,
1522 * to obtain fancier outputs.
1525 * @addtogroup DifferenceEngine
1530 * Number of leading context "lines" to preserve.
1532 * This should be left at zero for this class, but subclasses
1533 * may want to set this to other values.
1535 var $leading_context_lines = 0;
1538 * Number of trailing context "lines" to preserve.
1540 * This should be left at zero for this class, but subclasses
1541 * may want to set this to other values.
1543 var $trailing_context_lines = 0;
1548 * @param $diff object A Diff object.
1549 * @return string The formatted output.
1551 function format($diff) {
1552 $fname = 'DiffFormatter::format';
1553 wfProfileIn( $fname );
1559 $nlead = $this->leading_context_lines
;
1560 $ntrail = $this->trailing_context_lines
;
1562 $this->_start_diff();
1564 foreach ($diff->edits
as $edit) {
1565 if ($edit->type
== 'copy') {
1566 if (is_array($block)) {
1567 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1572 $context = array_slice($edit->orig
, 0, $ntrail);
1573 $block[] = new _DiffOp_Copy($context);
1575 $this->_block($x0, $ntrail +
$xi - $x0,
1576 $y0, $ntrail +
$yi - $y0,
1581 $context = $edit->orig
;
1584 if (! is_array($block)) {
1585 $context = array_slice($context, sizeof($context) - $nlead);
1586 $x0 = $xi - sizeof($context);
1587 $y0 = $yi - sizeof($context);
1590 $block[] = new _DiffOp_Copy($context);
1596 $xi +
= sizeof($edit->orig
);
1598 $yi +
= sizeof($edit->closing
);
1601 if (is_array($block))
1602 $this->_block($x0, $xi - $x0,
1606 $end = $this->_end_diff();
1607 wfProfileOut( $fname );
1611 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1612 $fname = 'DiffFormatter::_block';
1613 wfProfileIn( $fname );
1614 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1615 foreach ($edits as $edit) {
1616 if ($edit->type
== 'copy')
1617 $this->_context($edit->orig
);
1618 elseif ($edit->type
== 'add')
1619 $this->_added($edit->closing
);
1620 elseif ($edit->type
== 'delete')
1621 $this->_deleted($edit->orig
);
1622 elseif ($edit->type
== 'change')
1623 $this->_changed($edit->orig
, $edit->closing
);
1625 trigger_error('Unknown edit type', E_USER_ERROR
);
1627 $this->_end_block();
1628 wfProfileOut( $fname );
1631 function _start_diff() {
1635 function _end_diff() {
1636 $val = ob_get_contents();
1641 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1643 $xbeg .= "," . ($xbeg +
$xlen - 1);
1645 $ybeg .= "," . ($ybeg +
$ylen - 1);
1647 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1650 function _start_block($header) {
1651 echo $header . "\n";
1654 function _end_block() {
1657 function _lines($lines, $prefix = ' ') {
1658 foreach ($lines as $line)
1659 echo "$prefix $line\n";
1662 function _context($lines) {
1663 $this->_lines($lines);
1666 function _added($lines) {
1667 $this->_lines($lines, '>');
1669 function _deleted($lines) {
1670 $this->_lines($lines, '<');
1673 function _changed($orig, $closing) {
1674 $this->_deleted($orig);
1676 $this->_added($closing);
1681 * A formatter that outputs unified diffs
1682 * @addtogroup DifferenceEngine
1685 class UnifiedDiffFormatter
extends DiffFormatter
1687 var $leading_context_lines = 2;
1688 var $trailing_context_lines = 2;
1690 function _added($lines) {
1691 $this->_lines($lines, '+');
1693 function _deleted($lines) {
1694 $this->_lines($lines, '-');
1696 function _changed($orig, $closing) {
1697 $this->_deleted($orig);
1698 $this->_added($closing);
1700 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1701 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
1706 * A pseudo-formatter that just passes along the Diff::$edits array
1707 * @addtogroup DifferenceEngine
1709 class ArrayDiffFormatter
extends DiffFormatter
1711 function format($diff)
1716 foreach($diff->edits
as $edit)
1720 foreach($edit->closing
as $l)
1725 'newline' => $newline++
1730 foreach($edit->orig
as $l)
1733 'action' => 'delete',
1735 'oldline' => $oldline++
,
1740 foreach($edit->orig
as $i => $l)
1743 'action' => 'change',
1745 'new' => @$edit->closing
[$i],
1746 'oldline' => $oldline++
,
1747 'newline' => $newline++
,
1752 $oldline +
= count($edit->orig
);
1753 $newline +
= count($edit->orig
);
1760 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1764 define('NBSP', ' '); // iso-8859-x non-breaking space.
1769 * @addtogroup DifferenceEngine
1771 class _HWLDF_WordAccumulator
{
1772 function _HWLDF_WordAccumulator () {
1773 $this->_lines
= array();
1779 function _flushGroup ($new_tag) {
1780 if ($this->_group
!== '') {
1781 if ($this->_tag
== 'ins')
1782 $this->_line
.= '<ins class="diffchange">' .
1783 htmlspecialchars ( $this->_group
) . '</ins>';
1784 elseif ($this->_tag
== 'del')
1785 $this->_line
.= '<del class="diffchange">' .
1786 htmlspecialchars ( $this->_group
) . '</del>';
1788 $this->_line
.= htmlspecialchars ( $this->_group
);
1791 $this->_tag
= $new_tag;
1794 function _flushLine ($new_tag) {
1795 $this->_flushGroup($new_tag);
1796 if ($this->_line
!= '')
1797 array_push ( $this->_lines
, $this->_line
);
1799 # make empty lines visible by inserting an NBSP
1800 array_push ( $this->_lines
, NBSP
);
1804 function addWords ($words, $tag = '') {
1805 if ($tag != $this->_tag
)
1806 $this->_flushGroup($tag);
1808 foreach ($words as $word) {
1809 // new-line should only come as first char of word.
1812 if ($word[0] == "\n") {
1813 $this->_flushLine($tag);
1814 $word = substr($word, 1);
1816 assert(!strstr($word, "\n"));
1817 $this->_group
.= $word;
1821 function getLines() {
1822 $this->_flushLine('~done');
1823 return $this->_lines
;
1830 * @addtogroup DifferenceEngine
1832 class WordLevelDiff
extends MappedDiff
1834 const MAX_LINE_LENGTH
= 10000;
1836 function WordLevelDiff ($orig_lines, $closing_lines) {
1837 $fname = 'WordLevelDiff::WordLevelDiff';
1838 wfProfileIn( $fname );
1840 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1841 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1843 $this->MappedDiff($orig_words, $closing_words,
1844 $orig_stripped, $closing_stripped);
1845 wfProfileOut( $fname );
1848 function _split($lines) {
1849 $fname = 'WordLevelDiff::_split';
1850 wfProfileIn( $fname );
1853 $stripped = array();
1855 foreach ( $lines as $line ) {
1856 # If the line is too long, just pretend the entire line is one big word
1857 # This prevents resource exhaustion problems
1864 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1866 $stripped[] = $line;
1869 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1872 $words = array_merge( $words, $m[0] );
1873 $stripped = array_merge( $stripped, $m[1] );
1877 wfProfileOut( $fname );
1878 return array($words, $stripped);
1882 $fname = 'WordLevelDiff::orig';
1883 wfProfileIn( $fname );
1884 $orig = new _HWLDF_WordAccumulator
;
1886 foreach ($this->edits
as $edit) {
1887 if ($edit->type
== 'copy')
1888 $orig->addWords($edit->orig
);
1889 elseif ($edit->orig
)
1890 $orig->addWords($edit->orig
, 'del');
1892 $lines = $orig->getLines();
1893 wfProfileOut( $fname );
1897 function closing () {
1898 $fname = 'WordLevelDiff::closing';
1899 wfProfileIn( $fname );
1900 $closing = new _HWLDF_WordAccumulator
;
1902 foreach ($this->edits
as $edit) {
1903 if ($edit->type
== 'copy')
1904 $closing->addWords($edit->closing
);
1905 elseif ($edit->closing
)
1906 $closing->addWords($edit->closing
, 'ins');
1908 $lines = $closing->getLines();
1909 wfProfileOut( $fname );
1915 * Wikipedia Table style diff formatter.
1918 * @addtogroup DifferenceEngine
1920 class TableDiffFormatter
extends DiffFormatter
1922 function TableDiffFormatter() {
1923 $this->leading_context_lines
= 2;
1924 $this->trailing_context_lines
= 2;
1927 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1928 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE '.$xbeg."--></td>\n" .
1929 '<td colspan="2" class="diff-lineno"><!--LINE '.$ybeg."--></td></tr>\n";
1933 function _start_block( $header ) {
1937 function _end_block() {
1940 function _lines( $lines, $prefix=' ', $color='white' ) {
1943 # HTML-escape parameter before calling this
1944 function addedLine( $line ) {
1945 return $this->wrapLine( '+', 'diff-addedline', $line );
1948 # HTML-escape parameter before calling this
1949 function deletedLine( $line ) {
1950 return $this->wrapLine( '-', 'diff-deletedline', $line );
1953 # HTML-escape parameter before calling this
1954 function contextLine( $line ) {
1955 return $this->wrapLine( ' ', 'diff-context', $line );
1958 private function wrapLine( $marker, $class, $line ) {
1959 if( $line !== '' ) {
1960 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1961 $line = "<div>$line</div>";
1963 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1966 function emptyLine() {
1967 return '<td colspan="2"> </td>';
1970 function _added( $lines ) {
1971 foreach ($lines as $line) {
1972 echo '<tr>' . $this->emptyLine() .
1973 $this->addedLine( '<ins class="diffchange">' .
1974 htmlspecialchars ( $line ) . '</ins>' ) . "</tr>\n";
1978 function _deleted($lines) {
1979 foreach ($lines as $line) {
1980 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1981 htmlspecialchars ( $line ) . '</del>' ) .
1982 $this->emptyLine() . "</tr>\n";
1986 function _context( $lines ) {
1987 foreach ($lines as $line) {
1989 $this->contextLine( htmlspecialchars ( $line ) ) .
1990 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1994 function _changed( $orig, $closing ) {
1995 $fname = 'TableDiffFormatter::_changed';
1996 wfProfileIn( $fname );
1998 $diff = new WordLevelDiff( $orig, $closing );
1999 $del = $diff->orig();
2000 $add = $diff->closing();
2002 # Notice that WordLevelDiff returns HTML-escaped output.
2003 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
2005 while ( $line = array_shift( $del ) ) {
2006 $aline = array_shift( $add );
2007 echo '<tr>' . $this->deletedLine( $line ) .
2008 $this->addedLine( $aline ) . "</tr>\n";
2010 foreach ($add as $line) { # If any leftovers
2011 echo '<tr>' . $this->emptyLine() .
2012 $this->addedLine( $line ) . "</tr>\n";
2014 wfProfileOut( $fname );