5 * @subpackage DifferenceEngine
9 require_once( 'Revision.php' );
11 define( 'MAX_DIFF_LINE', 10000 );
12 define( 'MAX_DIFF_XREF_LENGTH', 10000 );
18 * @subpackage DifferenceEngine
20 class DifferenceEngine
{
39 $mRevisionsLoaded = false, // Have the revisions been loaded
40 $mTextLoaded = 0, // How many text blobs have been loaded, 0, 1 or 2?
46 * @param $titleObj Title object that the diff is associated with
47 * @param $old Integer: old ID we want to show and diff with.
48 * @param $new String: either 'prev' or 'next'.
49 * @param $rcid Integer: ??? FIXME (default 0)
51 function DifferenceEngine( $titleObj = null, $old = 0, $new = 0, $rcid = 0 ) {
52 $this->mTitle
= $titleObj;
53 wfDebug("DifferenceEngine old '$old' new '$new' rcid '$rcid'\n");
55 if ( 'prev' === $new ) {
56 # Show diff between revision $old and the previous one.
57 # Get previous one from DB.
59 $this->mNewid
= intval($old);
61 $this->mOldid
= $this->mTitle
->getPreviousRevisionID( $this->mNewid
);
63 } elseif ( 'next' === $new ) {
64 # Show diff between revision $old and the previous one.
65 # Get previous one from DB.
67 $this->mOldid
= intval($old);
68 $this->mNewid
= $this->mTitle
->getNextRevisionID( $this->mOldid
);
69 if ( false === $this->mNewid
) {
70 # if no result, NewId points to the newest old revision. The only newer
71 # revision is cur, which is "0".
76 $this->mOldid
= intval($old);
77 $this->mNewid
= intval($new);
79 $this->mRcidMarkPatrolled
= intval($rcid); # force it to be an integer
82 function showDiffPage() {
83 global $wgUser, $wgOut, $wgContLang, $wgUseExternalEditor, $wgUseRCPatrol;
84 $fname = 'DifferenceEngine::showDiffPage';
85 wfProfileIn( $fname );
87 # If external diffs are enabled both globally and for the user,
88 # we'll use the application/x-external-editor interface to call
89 # an external diff tool like kompare, kdiff3, etc.
90 if($wgUseExternalEditor && $wgUser->getOption('externaldiff')) {
91 global $wgInputEncoding,$wgServer,$wgScript,$wgLang;
93 header ( "Content-type: application/x-external-editor; charset=".$wgInputEncoding );
94 $url1=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mOldid
);
95 $url2=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mNewid
);
96 $special=$wgLang->getNsText(NS_SPECIAL
);
101 Script={$wgServer}{$wgScript}
102 Special namespace={$special}
116 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
118 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
120 $wgOut->setArticleFlag( false );
121 if ( ! $this->loadRevisionData() ) {
122 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
123 $wgOut->addWikitext( $mtext );
124 wfProfileOut( $fname );
127 if ( $this->mNewRev
->isCurrent() ) {
128 $wgOut->setArticleFlag( true );
131 # mOldid is false if the difference engine is called with a "vague" query for
132 # a diff between a version V and its previous version V' AND the version V
133 # is the first version of that article. In that case, V' does not exist.
134 if ( $this->mOldid
=== false ) {
135 $this->showFirstRevision();
136 wfProfileOut( $fname );
140 $wgOut->suppressQuickbar();
142 $oldTitle = $this->mOldPage
->getPrefixedText();
143 $newTitle = $this->mNewPage
->getPrefixedText();
144 if( $oldTitle == $newTitle ) {
145 $wgOut->setPageTitle( $newTitle );
147 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
149 $wgOut->setSubtitle( wfMsg( 'difference' ) );
150 $wgOut->setRobotpolicy( 'noindex,follow' );
152 if ( !( $this->mOldPage
->userCanRead() && $this->mNewPage
->userCanRead() ) ) {
153 $wgOut->loginToUse();
155 wfProfileOut( $fname );
159 $sk = $wgUser->getSkin();
160 $talk = $wgContLang->getNsText( NS_TALK
);
161 $contribs = wfMsg( 'contribslink' );
163 if ( $this->mNewRev
->isCurrent() && $wgUser->isAllowed('rollback') ) {
164 $username = $this->mNewRev
->getUserText();
165 $rollback = ' <strong>[' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'rollbacklink' ),
166 'action=rollback&from=' . urlencode( $username ) .
167 '&token=' . urlencode( $wgUser->editToken( array( $this->mTitle
->getPrefixedText(), $username ) ) ) ) .
172 if( $wgUseRCPatrol && $this->mRcidMarkPatrolled
!= 0 && $wgUser->isAllowed( 'patrol' ) ) {
173 $patrol = ' [' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'markaspatrolleddiff' ), "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']';
178 $prevlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'previousdiff' ),
179 'diff=prev&oldid='.$this->mOldid
, '', '', 'id="differences-prevlink"' );
180 if ( $this->mNewRev
->isCurrent() ) {
183 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ),
184 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
187 $oldHeader = "<strong>{$this->mOldtitle}</strong><br />" .
188 $sk->revUserTools( $this->mOldRev
) . "<br />" .
189 $sk->revComment( $this->mOldRev
) . "<br />" .
191 $newHeader = "<strong>{$this->mNewtitle}</strong><br />" .
192 $sk->revUserTools( $this->mNewRev
) . " $rollback<br />" .
193 $sk->revComment( $this->mNewRev
) . "<br />" .
196 $this->showDiff( $oldHeader, $newHeader );
197 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
199 if( !$this->mNewRev
->isCurrent() ) {
200 $oldEditSectionSetting = $wgOut->mParserOptions
->setEditSection( false );
203 $this->loadNewText();
204 if( is_object( $this->mNewRev
) ) {
205 $wgOut->setRevisionId( $this->mNewRev
->getId() );
207 $wgOut->addSecondaryWikiText( $this->mNewtext
);
209 if( !$this->mNewRev
->isCurrent() ) {
210 $wgOut->mParserOptions
->setEditSection( $oldEditSectionSetting );
213 wfProfileOut( $fname );
217 * Show the first revision of an article. Uses normal diff headers in
218 * contrast to normal "old revision" display style.
220 function showFirstRevision() {
221 global $wgOut, $wgUser;
223 $fname = 'DifferenceEngine::showFirstRevision';
224 wfProfileIn( $fname );
226 # Get article text from the DB
228 if ( ! $this->loadNewText() ) {
229 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
231 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
232 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
233 $wgOut->addWikitext( $mtext );
234 wfProfileOut( $fname );
237 if ( $this->mNewRev
->isCurrent() ) {
238 $wgOut->setArticleFlag( true );
241 # Check if user is allowed to look at this page. If not, bail out.
243 if ( !( $this->mTitle
->userCanRead() ) ) {
244 $wgOut->loginToUse();
246 wfProfileOut( $fname );
250 # Prepare the header box
252 $sk = $wgUser->getSkin();
254 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
255 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />" .
256 $sk->revUserTools( $this->mNewRev
) . "<br />" .
257 $sk->revComment( $this->mNewRev
) . "<br />" .
258 $nextlink . "</div>\n";
260 $wgOut->addHTML( $header );
262 $wgOut->setSubtitle( wfMsg( 'difference' ) );
263 $wgOut->setRobotpolicy( 'noindex,nofollow' );
266 # Show current revision
268 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
269 if( is_object( $this->mNewRev
) ) {
270 $wgOut->setRevisionId( $this->mNewRev
->getId() );
272 $wgOut->addSecondaryWikiText( $this->mNewtext
);
274 wfProfileOut( $fname );
278 * Get the diff text, send it to $wgOut
279 * Returns false if the diff could not be generated, otherwise returns true
281 function showDiff( $otitle, $ntitle ) {
283 $diff = $this->getDiff( $otitle, $ntitle );
284 if ( $diff === false ) {
285 $wgOut->addWikitext( wfMsg( 'missingarticle', "<nowiki>(fixme, bug)</nowiki>" ) );
288 $wgOut->addHTML( $diff );
294 * Get diff table, including header
295 * Note that the interface has changed, it's no longer static.
296 * Returns false on error
298 function getDiff( $otitle, $ntitle ) {
299 $body = $this->getDiffBody();
300 if ( $body === false ) {
303 return $this->addHeader( $body, $otitle, $ntitle );
308 * Get the diff table body, without header
310 * Returns false on error
312 function getDiffBody() {
313 global $wgMemc, $wgDBname;
314 $fname = 'DifferenceEngine::getDiffBody';
315 wfProfileIn( $fname );
319 if ( $this->mOldid
&& $this->mNewid
) {
321 $key = "$wgDBname:diff:oldid:{$this->mOldid}:newid:{$this->mNewid}";
322 $difftext = $wgMemc->get( $key );
324 wfIncrStats( 'diff_cache_hit' );
325 $difftext = $this->localiseLineNumbers( $difftext );
326 $difftext .= "\n<!-- diff cache key $key -->\n";
327 wfProfileOut( $fname );
332 if ( !$this->loadText() ) {
333 wfProfileOut( $fname );
337 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
339 // Save to cache for 7 days
340 if ( $key !== false && $difftext !== false ) {
341 wfIncrStats( 'diff_cache_miss' );
342 $wgMemc->set( $key, $difftext, 7*86400 );
344 wfIncrStats( 'diff_uncacheable' );
346 // Replace line numbers with the text in the user's language
347 if ( $difftext !== false ) {
348 $difftext = $this->localiseLineNumbers( $difftext );
350 wfProfileOut( $fname );
355 * Generate a diff, no caching
356 * $otext and $ntext must be already segmented
358 function generateDiffBody( $otext, $ntext ) {
359 global $wgExternalDiffEngine, $wgContLang;
360 $fname = 'DifferenceEngine::generateDiffBody';
362 $otext = str_replace( "\r\n", "\n", $otext );
363 $ntext = str_replace( "\r\n", "\n", $ntext );
365 if ( $wgExternalDiffEngine == 'wikidiff' ) {
366 # For historical reasons, external diff engine expects
367 # input text to be HTML-escaped already
368 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
369 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
370 if( !function_exists( 'wikidiff_do_diff' ) ) {
371 dl('php_wikidiff.so');
373 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) );
376 if ( $wgExternalDiffEngine == 'wikidiff2' ) {
377 # Better external diff engine, the 2 may some day be dropped
378 # This one does the escaping and segmenting itself
379 if ( !function_exists( 'wikidiff2_do_diff' ) ) {
380 wfProfileIn( "$fname-dl" );
381 @dl
('php_wikidiff2.so');
382 wfProfileOut( "$fname-dl" );
384 if ( function_exists( 'wikidiff2_do_diff' ) ) {
385 wfProfileIn( 'wikidiff2_do_diff' );
386 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
387 wfProfileOut( 'wikidiff2_do_diff' );
391 if ( $wgExternalDiffEngine !== false ) {
393 global $wgTmpDirectory;
394 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
395 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
397 $tempFile1 = fopen( $tempName1, "w" );
399 wfProfileOut( $fname );
402 $tempFile2 = fopen( $tempName2, "w" );
404 wfProfileOut( $fname );
407 fwrite( $tempFile1, $otext );
408 fwrite( $tempFile2, $ntext );
409 fclose( $tempFile1 );
410 fclose( $tempFile2 );
411 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
412 wfProfileIn( "$fname-shellexec" );
413 $difftext = wfShellExec( $cmd );
414 wfProfileOut( "$fname-shellexec" );
415 unlink( $tempName1 );
416 unlink( $tempName2 );
421 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
422 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
423 $diffs =& new Diff( $ota, $nta );
424 $formatter =& new TableDiffFormatter();
425 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
430 * Replace line numbers with the text in the user's language
432 function localiseLineNumbers( $text ) {
433 return preg_replace_callback( '/<!--LINE (\d+)-->/',
434 array( &$this, 'localiseLineNumbersCb' ), $text );
437 function localiseLineNumbersCb( $matches ) {
439 return wfMsg( 'lineno', $wgLang->formatNum( $matches[1] ) );
443 * Add the header to a diff body
445 function addHeader( $diff, $otitle, $ntitle ) {
447 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
449 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
450 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
459 * Use specified text instead of loading from the database
461 function setText( $oldText, $newText ) {
462 $this->mOldtext
= $oldText;
463 $this->mNewtext
= $newText;
464 $this->mTextLoaded
= 2;
468 * Load revision metadata for the specified articles. If newid is 0, then compare
469 * the old article in oldid to the current article; if oldid is 0, then
470 * compare the current article to the immediately previous one (ignoring the
473 * If oldid is false, leave the corresponding revision object set
474 * to false. This is impossible via ordinary user input, and is provided for
477 function loadRevisionData() {
479 if ( $this->mRevisionsLoaded
) {
482 // Whether it succeeds or fails, we don't want to try again
483 $this->mRevisionsLoaded
= true;
486 // Load the new revision object
487 if( $this->mNewid
) {
488 $this->mNewRev
= Revision
::newFromId( $this->mNewid
);
490 $this->mNewRev
= Revision
::newFromTitle( $this->mTitle
);
493 if( is_null( $this->mNewRev
) ) {
497 // Set assorted variables
498 if( $this->mNewRev
->isCurrent() ) {
499 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
500 $this->mNewPage
= $this->mTitle
;
501 $newLink = $this->mNewPage
->escapeLocalUrl();
502 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
504 $this->mNewPage
= $this->mNewRev
->getTitle();
505 $newLink = $this->mNewPage
->escapeLocalUrl ('oldid=' . $this->mNewid
);
506 $t = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
507 $this->mPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t ) );
508 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
511 // Load the old revision object
512 $this->mOldRev
= false;
513 if( $this->mOldid
) {
514 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
515 } elseif ( $this->mOldid
=== 0 ) {
516 $rev = $this->mNewRev
->getPrevious();
518 $this->mOldid
= $rev->getId();
519 $this->mOldRev
= $rev;
521 // No previous revision; mark to show as first-version only.
522 $this->mOldid
= false;
523 $this->mOldRev
= false;
525 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
527 if( is_null( $this->mOldRev
) ) {
531 if ( $this->mOldRev
) {
532 $this->mOldPage
= $this->mOldRev
->getTitle();
534 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
535 $oldLink = $this->mOldPage
->escapeLocalUrl( 'oldid=' . $this->mOldid
);
536 $this->mOldtitle
= "<a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) ) . '</a>';
543 * Load the text of the revisions, as well as revision data.
545 function loadText() {
546 if ( $this->mTextLoaded
== 2 ) {
549 // Whether it succeeds or fails, we don't want to try again
550 $this->mTextLoaded
= 2;
553 if ( !$this->loadRevisionData() ) {
556 if ( $this->mOldRev
) {
557 // FIXME: permission tests
558 $this->mOldtext
= $this->mOldRev
->getText();
559 if ( $this->mOldtext
=== false ) {
563 if ( $this->mNewRev
) {
564 $this->mNewtext
= $this->mNewRev
->getText();
565 if ( $this->mNewtext
=== false ) {
573 * Load the text of the new revision, not the old one
575 function loadNewText() {
576 if ( $this->mTextLoaded
>= 1 ) {
579 $this->mTextLoaded
= 1;
581 if ( !$this->loadRevisionData() ) {
584 $this->mNewtext
= $this->mNewRev
->getText();
591 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
593 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
594 // You may copy this code freely under the conditions of the GPL.
597 define('USE_ASSERTS', function_exists('assert'));
603 * @subpackage DifferenceEngine
612 trigger_error('pure virtual', E_USER_ERROR
);
616 return $this->orig ?
sizeof($this->orig
) : 0;
619 function nclosing() {
620 return $this->closing ?
sizeof($this->closing
) : 0;
628 * @subpackage DifferenceEngine
630 class _DiffOp_Copy
extends _DiffOp
{
634 function _DiffOp_Copy ($orig, $closing = false) {
635 if (!is_array($closing))
638 $this->closing
= $closing;
642 return new _DiffOp_Copy($this->closing
, $this->orig
);
650 * @subpackage DifferenceEngine
652 class _DiffOp_Delete
extends _DiffOp
{
653 public $type = 'delete';
655 function _DiffOp_Delete ($lines) {
656 $this->orig
= $lines;
657 $this->closing
= false;
661 return new _DiffOp_Add($this->orig
);
669 * @subpackage DifferenceEngine
671 class _DiffOp_Add
extends _DiffOp
{
672 public $type = 'add';
674 function _DiffOp_Add ($lines) {
675 $this->closing
= $lines;
680 return new _DiffOp_Delete($this->closing
);
688 * @subpackage DifferenceEngine
690 class _DiffOp_Change
extends _DiffOp
{
694 function _DiffOp_Change ($orig, $closing) {
696 $this->closing
= $closing;
700 return new _DiffOp_Change($this->closing
, $this->orig
);
706 * Class used internally by Diff to actually compute the diffs.
708 * The algorithm used here is mostly lifted from the perl module
709 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
710 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
712 * More ideas are taken from:
713 * http://www.ics.uci.edu/~eppstein/161/960229.html
715 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
716 * diffutils-2.7, which can be found at:
717 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
719 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
722 * Line length limits for robustness added by Tim Starling, 2005-08-31
724 * @author Geoffrey T. Dairiki, Tim Starling
727 * @subpackage DifferenceEngine
731 function diff ($from_lines, $to_lines) {
732 $fname = '_DiffEngine::diff';
733 wfProfileIn( $fname );
735 $n_from = sizeof($from_lines);
736 $n_to = sizeof($to_lines);
738 $this->xchanged
= $this->ychanged
= array();
739 $this->xv
= $this->yv
= array();
740 $this->xind
= $this->yind
= array();
742 unset($this->in_seq
);
745 // Skip leading common lines.
746 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
747 if ($from_lines[$skip] !== $to_lines[$skip])
749 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
751 // Skip trailing common lines.
752 $xi = $n_from; $yi = $n_to;
753 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
754 if ($from_lines[$xi] !== $to_lines[$yi])
756 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
759 // Ignore lines which do not exist in both files.
760 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
761 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
764 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
765 $line = $to_lines[$yi];
766 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
768 $yhash[$this->_line_hash($line)] = 1;
772 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
773 $line = $from_lines[$xi];
774 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
781 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
783 // Merge edits when possible
784 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
785 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
787 // Compute the edit operations.
790 while ($xi < $n_from ||
$yi < $n_to) {
791 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
792 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
794 // Skip matching "snake".
796 while ( $xi < $n_from && $yi < $n_to
797 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
798 $copy[] = $from_lines[$xi++
];
802 $edits[] = new _DiffOp_Copy($copy);
804 // Find deletes & adds.
806 while ($xi < $n_from && $this->xchanged
[$xi])
807 $delete[] = $from_lines[$xi++
];
810 while ($yi < $n_to && $this->ychanged
[$yi])
811 $add[] = $to_lines[$yi++
];
814 $edits[] = new _DiffOp_Change($delete, $add);
816 $edits[] = new _DiffOp_Delete($delete);
818 $edits[] = new _DiffOp_Add($add);
820 wfProfileOut( $fname );
825 * Returns the whole line if it's small enough, or the MD5 hash otherwise
827 function _line_hash( $line ) {
828 if ( strlen( $line ) > MAX_DIFF_XREF_LENGTH
) {
836 /* Divide the Largest Common Subsequence (LCS) of the sequences
837 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
840 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
841 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
842 * sub sequences. The first sub-sequence is contained in [X0, X1),
843 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
844 * that (X0, Y0) == (XOFF, YOFF) and
845 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
847 * This function assumes that the first lines of the specified portions
848 * of the two files do not match, and likewise that the last lines do not
849 * match. The caller must trim matching lines from the beginning and end
850 * of the portions it is going to specify.
852 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
853 $fname = '_DiffEngine::_diag';
854 wfProfileIn( $fname );
857 if ($xlim - $xoff > $ylim - $yoff) {
858 // Things seems faster (I'm not sure I understand why)
859 // when the shortest sequence in X.
861 list ($xoff, $xlim, $yoff, $ylim)
862 = array( $yoff, $ylim, $xoff, $xlim);
866 for ($i = $ylim - 1; $i >= $yoff; $i--)
867 $ymatches[$this->xv
[$i]][] = $i;
869 for ($i = $ylim - 1; $i >= $yoff; $i--)
870 $ymatches[$this->yv
[$i]][] = $i;
873 $this->seq
[0]= $yoff - 1;
874 $this->in_seq
= array();
877 $numer = $xlim - $xoff +
$nchunks - 1;
879 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
880 wfProfileIn( "$fname-chunk" );
882 for ($i = 0; $i <= $this->lcs
; $i++
)
883 $ymids[$i][$chunk-1] = $this->seq
[$i];
885 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
886 for ( ; $x < $x1; $x++
) {
887 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
888 if (empty($ymatches[$line]))
890 $matches = $ymatches[$line];
892 while (list ($junk, $y) = each($matches))
893 if (empty($this->in_seq
[$y])) {
894 $k = $this->_lcs_pos($y);
895 USE_ASSERTS
&& assert($k > 0);
896 $ymids[$k] = $ymids[$k-1];
899 while (list ($junk, $y) = each($matches)) {
900 if ($y > $this->seq
[$k-1]) {
901 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
902 // Optimization: this is a common case:
903 // next match is just replacing previous match.
904 $this->in_seq
[$this->seq
[$k]] = false;
906 $this->in_seq
[$y] = 1;
907 } else if (empty($this->in_seq
[$y])) {
908 $k = $this->_lcs_pos($y);
909 USE_ASSERTS
&& assert($k > 0);
910 $ymids[$k] = $ymids[$k-1];
914 wfProfileOut( "$fname-chunk" );
917 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
918 $ymid = $ymids[$this->lcs
];
919 for ($n = 0; $n < $nchunks - 1; $n++
) {
920 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
922 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
924 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
926 wfProfileOut( $fname );
927 return array($this->lcs
, $seps);
930 function _lcs_pos ($ypos) {
931 $fname = '_DiffEngine::_lcs_pos';
932 wfProfileIn( $fname );
935 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
936 $this->seq
[++
$this->lcs
] = $ypos;
937 $this->in_seq
[$ypos] = 1;
938 wfProfileOut( $fname );
943 while ($beg < $end) {
944 $mid = (int)(($beg +
$end) / 2);
945 if ( $ypos > $this->seq
[$mid] )
951 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
953 $this->in_seq
[$this->seq
[$end]] = false;
954 $this->seq
[$end] = $ypos;
955 $this->in_seq
[$ypos] = 1;
956 wfProfileOut( $fname );
960 /* Find LCS of two sequences.
962 * The results are recorded in the vectors $this->{x,y}changed[], by
963 * storing a 1 in the element for each line that is an insertion
964 * or deletion (ie. is not in the LCS).
966 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
968 * Note that XLIM, YLIM are exclusive bounds.
969 * All line numbers are origin-0 and discarded lines are not counted.
971 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
972 $fname = '_DiffEngine::_compareseq';
973 wfProfileIn( $fname );
975 // Slide down the bottom initial diagonal.
976 while ($xoff < $xlim && $yoff < $ylim
977 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
982 // Slide up the top initial diagonal.
983 while ($xlim > $xoff && $ylim > $yoff
984 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
989 if ($xoff == $xlim ||
$yoff == $ylim)
992 // This is ad hoc but seems to work well.
993 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
994 //$nchunks = max(2,min(8,(int)$nchunks));
995 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
997 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
1001 // X and Y sequences have no common subsequence:
1002 // mark all changed.
1003 while ($yoff < $ylim)
1004 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
1005 while ($xoff < $xlim)
1006 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
1008 // Use the partitions to split this problem into subproblems.
1011 while ($pt2 = next($seps)) {
1012 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1016 wfProfileOut( $fname );
1019 /* Adjust inserts/deletes of identical lines to join changes
1020 * as much as possible.
1022 * We do something when a run of changed lines include a
1023 * line at one end and has an excluded, identical line at the other.
1024 * We are free to choose which identical line is included.
1025 * `compareseq' usually chooses the one at the beginning,
1026 * but usually it is cleaner to consider the following identical line
1027 * to be the "change".
1029 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1031 function _shift_boundaries ($lines, &$changed, $other_changed) {
1032 $fname = '_DiffEngine::_shift_boundaries';
1033 wfProfileIn( $fname );
1037 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1038 $len = sizeof($lines);
1039 $other_len = sizeof($other_changed);
1043 * Scan forwards to find beginning of another run of changes.
1044 * Also keep track of the corresponding point in the other file.
1046 * Throughout this code, $i and $j are adjusted together so that
1047 * the first $i elements of $changed and the first $j elements
1048 * of $other_changed both contain the same number of zeros
1049 * (unchanged lines).
1050 * Furthermore, $j is always kept so that $j == $other_len or
1051 * $other_changed[$j] == false.
1053 while ($j < $other_len && $other_changed[$j])
1056 while ($i < $len && ! $changed[$i]) {
1057 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1059 while ($j < $other_len && $other_changed[$j])
1068 // Find the end of this run of changes.
1069 while (++
$i < $len && $changed[$i])
1074 * Record the length of this run of changes, so that
1075 * we can later determine whether the run has grown.
1077 $runlength = $i - $start;
1080 * Move the changed region back, so long as the
1081 * previous unchanged line matches the last changed one.
1082 * This merges with previous changed regions.
1084 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1085 $changed[--$start] = 1;
1086 $changed[--$i] = false;
1087 while ($start > 0 && $changed[$start - 1])
1089 USE_ASSERTS
&& assert('$j > 0');
1090 while ($other_changed[--$j])
1092 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1096 * Set CORRESPONDING to the end of the changed run, at the last
1097 * point where it corresponds to a changed run in the other file.
1098 * CORRESPONDING == LEN means no such point has been found.
1100 $corresponding = $j < $other_len ?
$i : $len;
1103 * Move the changed region forward, so long as the
1104 * first changed line matches the following unchanged one.
1105 * This merges with following changed regions.
1106 * Do this second, so that if there are no merges,
1107 * the changed region is moved forward as far as possible.
1109 while ($i < $len && $lines[$start] == $lines[$i]) {
1110 $changed[$start++
] = false;
1112 while ($i < $len && $changed[$i])
1115 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1117 if ($j < $other_len && $other_changed[$j]) {
1118 $corresponding = $i;
1119 while ($j < $other_len && $other_changed[$j])
1123 } while ($runlength != $i - $start);
1126 * If possible, move the fully-merged run of changes
1127 * back to a corresponding run in the other file.
1129 while ($corresponding < $i) {
1130 $changed[--$start] = 1;
1132 USE_ASSERTS
&& assert('$j > 0');
1133 while ($other_changed[--$j])
1135 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1138 wfProfileOut( $fname );
1143 * Class representing a 'diff' between two sequences of strings.
1146 * @package MediaWiki
1147 * @subpackage DifferenceEngine
1155 * Computes diff between sequences of strings.
1157 * @param $from_lines array An array of strings.
1158 * (Typically these are lines from a file.)
1159 * @param $to_lines array An array of strings.
1161 function Diff($from_lines, $to_lines) {
1162 $eng = new _DiffEngine
;
1163 $this->edits
= $eng->diff($from_lines, $to_lines);
1164 //$this->_check($from_lines, $to_lines);
1168 * Compute reversed Diff.
1172 * $diff = new Diff($lines1, $lines2);
1173 * $rev = $diff->reverse();
1174 * @return object A Diff object representing the inverse of the
1177 function reverse () {
1179 $rev->edits
= array();
1180 foreach ($this->edits
as $edit) {
1181 $rev->edits
[] = $edit->reverse();
1187 * Check for empty diff.
1189 * @return bool True iff two sequences were identical.
1191 function isEmpty () {
1192 foreach ($this->edits
as $edit) {
1193 if ($edit->type
!= 'copy')
1200 * Compute the length of the Longest Common Subsequence (LCS).
1202 * This is mostly for diagnostic purposed.
1204 * @return int The length of the LCS.
1208 foreach ($this->edits
as $edit) {
1209 if ($edit->type
== 'copy')
1210 $lcs +
= sizeof($edit->orig
);
1216 * Get the original set of lines.
1218 * This reconstructs the $from_lines parameter passed to the
1221 * @return array The original sequence of strings.
1226 foreach ($this->edits
as $edit) {
1228 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1234 * Get the closing set of lines.
1236 * This reconstructs the $to_lines parameter passed to the
1239 * @return array The sequence of strings.
1241 function closing() {
1244 foreach ($this->edits
as $edit) {
1246 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1252 * Check a Diff for validity.
1254 * This is here only for debugging purposes.
1256 function _check ($from_lines, $to_lines) {
1257 $fname = 'Diff::_check';
1258 wfProfileIn( $fname );
1259 if (serialize($from_lines) != serialize($this->orig()))
1260 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1261 if (serialize($to_lines) != serialize($this->closing()))
1262 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1264 $rev = $this->reverse();
1265 if (serialize($to_lines) != serialize($rev->orig()))
1266 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1267 if (serialize($from_lines) != serialize($rev->closing()))
1268 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1272 foreach ($this->edits
as $edit) {
1273 if ( $prevtype == $edit->type
)
1274 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1275 $prevtype = $edit->type
;
1278 $lcs = $this->lcs();
1279 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1280 wfProfileOut( $fname );
1288 * @package MediaWiki
1289 * @subpackage DifferenceEngine
1291 class MappedDiff
extends Diff
1296 * Computes diff between sequences of strings.
1298 * This can be used to compute things like
1299 * case-insensitve diffs, or diffs which ignore
1300 * changes in white-space.
1302 * @param $from_lines array An array of strings.
1303 * (Typically these are lines from a file.)
1305 * @param $to_lines array An array of strings.
1307 * @param $mapped_from_lines array This array should
1308 * have the same size number of elements as $from_lines.
1309 * The elements in $mapped_from_lines and
1310 * $mapped_to_lines are what is actually compared
1311 * when computing the diff.
1313 * @param $mapped_to_lines array This array should
1314 * have the same number of elements as $to_lines.
1316 function MappedDiff($from_lines, $to_lines,
1317 $mapped_from_lines, $mapped_to_lines) {
1318 $fname = 'MappedDiff::MappedDiff';
1319 wfProfileIn( $fname );
1321 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1322 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1324 $this->Diff($mapped_from_lines, $mapped_to_lines);
1327 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1328 $orig = &$this->edits
[$i]->orig
;
1329 if (is_array($orig)) {
1330 $orig = array_slice($from_lines, $xi, sizeof($orig));
1331 $xi +
= sizeof($orig);
1334 $closing = &$this->edits
[$i]->closing
;
1335 if (is_array($closing)) {
1336 $closing = array_slice($to_lines, $yi, sizeof($closing));
1337 $yi +
= sizeof($closing);
1340 wfProfileOut( $fname );
1345 * A class to format Diffs
1347 * This class formats the diff in classic diff format.
1348 * It is intended that this class be customized via inheritance,
1349 * to obtain fancier outputs.
1352 * @package MediaWiki
1353 * @subpackage DifferenceEngine
1358 * Number of leading context "lines" to preserve.
1360 * This should be left at zero for this class, but subclasses
1361 * may want to set this to other values.
1363 public $leading_context_lines = 0;
1366 * Number of trailing context "lines" to preserve.
1368 * This should be left at zero for this class, but subclasses
1369 * may want to set this to other values.
1371 public $trailing_context_lines = 0;
1376 * @param $diff object A Diff object.
1377 * @return string The formatted output.
1379 function format($diff) {
1380 $fname = 'DiffFormatter::format';
1381 wfProfileIn( $fname );
1387 $nlead = $this->leading_context_lines
;
1388 $ntrail = $this->trailing_context_lines
;
1390 $this->_start_diff();
1392 foreach ($diff->edits
as $edit) {
1393 if ($edit->type
== 'copy') {
1394 if (is_array($block)) {
1395 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1400 $context = array_slice($edit->orig
, 0, $ntrail);
1401 $block[] = new _DiffOp_Copy($context);
1403 $this->_block($x0, $ntrail +
$xi - $x0,
1404 $y0, $ntrail +
$yi - $y0,
1409 $context = $edit->orig
;
1412 if (! is_array($block)) {
1413 $context = array_slice($context, sizeof($context) - $nlead);
1414 $x0 = $xi - sizeof($context);
1415 $y0 = $yi - sizeof($context);
1418 $block[] = new _DiffOp_Copy($context);
1424 $xi +
= sizeof($edit->orig
);
1426 $yi +
= sizeof($edit->closing
);
1429 if (is_array($block))
1430 $this->_block($x0, $xi - $x0,
1434 $end = $this->_end_diff();
1435 wfProfileOut( $fname );
1439 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1440 $fname = 'DiffFormatter::_block';
1441 wfProfileIn( $fname );
1442 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1443 foreach ($edits as $edit) {
1444 if ($edit->type
== 'copy')
1445 $this->_context($edit->orig
);
1446 elseif ($edit->type
== 'add')
1447 $this->_added($edit->closing
);
1448 elseif ($edit->type
== 'delete')
1449 $this->_deleted($edit->orig
);
1450 elseif ($edit->type
== 'change')
1451 $this->_changed($edit->orig
, $edit->closing
);
1453 trigger_error('Unknown edit type', E_USER_ERROR
);
1455 $this->_end_block();
1456 wfProfileOut( $fname );
1459 function _start_diff() {
1463 function _end_diff() {
1464 $val = ob_get_contents();
1469 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1471 $xbeg .= "," . ($xbeg +
$xlen - 1);
1473 $ybeg .= "," . ($ybeg +
$ylen - 1);
1475 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1478 function _start_block($header) {
1482 function _end_block() {
1485 function _lines($lines, $prefix = ' ') {
1486 foreach ($lines as $line)
1487 echo "$prefix $line\n";
1490 function _context($lines) {
1491 $this->_lines($lines);
1494 function _added($lines) {
1495 $this->_lines($lines, '>');
1497 function _deleted($lines) {
1498 $this->_lines($lines, '<');
1501 function _changed($orig, $closing) {
1502 $this->_deleted($orig);
1504 $this->_added($closing);
1510 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1514 define('NBSP', ' '); // iso-8859-x non-breaking space.
1519 * @package MediaWiki
1520 * @subpackage DifferenceEngine
1522 class _HWLDF_WordAccumulator
{
1523 function _HWLDF_WordAccumulator () {
1524 $this->_lines
= array();
1530 function _flushGroup ($new_tag) {
1531 if ($this->_group
!== '') {
1532 if ($this->_tag
== 'mark')
1533 $this->_line
.= '<span class="diffchange">' .
1534 htmlspecialchars ( $this->_group
) . '</span>';
1536 $this->_line
.= htmlspecialchars ( $this->_group
);
1539 $this->_tag
= $new_tag;
1542 function _flushLine ($new_tag) {
1543 $this->_flushGroup($new_tag);
1544 if ($this->_line
!= '')
1545 array_push ( $this->_lines
, $this->_line
);
1547 # make empty lines visible by inserting an NBSP
1548 array_push ( $this->_lines
, NBSP
);
1552 function addWords ($words, $tag = '') {
1553 if ($tag != $this->_tag
)
1554 $this->_flushGroup($tag);
1556 foreach ($words as $word) {
1557 // new-line should only come as first char of word.
1560 if ($word[0] == "\n") {
1561 $this->_flushLine($tag);
1562 $word = substr($word, 1);
1564 assert(!strstr($word, "\n"));
1565 $this->_group
.= $word;
1569 function getLines() {
1570 $this->_flushLine('~done');
1571 return $this->_lines
;
1578 * @package MediaWiki
1579 * @subpackage DifferenceEngine
1581 class WordLevelDiff
extends MappedDiff
1583 function WordLevelDiff ($orig_lines, $closing_lines) {
1584 $fname = 'WordLevelDiff::WordLevelDiff';
1585 wfProfileIn( $fname );
1587 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1588 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1590 $this->MappedDiff($orig_words, $closing_words,
1591 $orig_stripped, $closing_stripped);
1592 wfProfileOut( $fname );
1595 function _split($lines) {
1596 $fname = 'WordLevelDiff::_split';
1597 wfProfileIn( $fname );
1600 $stripped = array();
1602 foreach ( $lines as $line ) {
1603 # If the line is too long, just pretend the entire line is one big word
1604 # This prevents resource exhaustion problems
1611 if ( strlen( $line ) > MAX_DIFF_LINE
) {
1613 $stripped[] = $line;
1615 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1618 $words = array_merge( $words, $m[0] );
1619 $stripped = array_merge( $stripped, $m[1] );
1623 wfProfileOut( $fname );
1624 return array($words, $stripped);
1628 $fname = 'WordLevelDiff::orig';
1629 wfProfileIn( $fname );
1630 $orig = new _HWLDF_WordAccumulator
;
1632 foreach ($this->edits
as $edit) {
1633 if ($edit->type
== 'copy')
1634 $orig->addWords($edit->orig
);
1635 elseif ($edit->orig
)
1636 $orig->addWords($edit->orig
, 'mark');
1638 $lines = $orig->getLines();
1639 wfProfileOut( $fname );
1643 function closing () {
1644 $fname = 'WordLevelDiff::closing';
1645 wfProfileIn( $fname );
1646 $closing = new _HWLDF_WordAccumulator
;
1648 foreach ($this->edits
as $edit) {
1649 if ($edit->type
== 'copy')
1650 $closing->addWords($edit->closing
);
1651 elseif ($edit->closing
)
1652 $closing->addWords($edit->closing
, 'mark');
1654 $lines = $closing->getLines();
1655 wfProfileOut( $fname );
1661 * Wikipedia Table style diff formatter.
1664 * @package MediaWiki
1665 * @subpackage DifferenceEngine
1667 class TableDiffFormatter
extends DiffFormatter
1669 function TableDiffFormatter() {
1670 $this->leading_context_lines
= 2;
1671 $this->trailing_context_lines
= 2;
1674 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1675 $r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" .
1676 '<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n";
1680 function _start_block( $header ) {
1684 function _end_block() {
1687 function _lines( $lines, $prefix=' ', $color='white' ) {
1690 # HTML-escape parameter before calling this
1691 function addedLine( $line ) {
1692 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1695 # HTML-escape parameter before calling this
1696 function deletedLine( $line ) {
1697 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1700 # HTML-escape parameter before calling this
1701 function contextLine( $line ) {
1702 return "<td> </td><td class='diff-context'>{$line}</td>";
1705 function emptyLine() {
1706 return '<td colspan="2"> </td>';
1709 function _added( $lines ) {
1710 foreach ($lines as $line) {
1711 echo '<tr>' . $this->emptyLine() .
1712 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1716 function _deleted($lines) {
1717 foreach ($lines as $line) {
1718 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1719 $this->emptyLine() . "</tr>\n";
1723 function _context( $lines ) {
1724 foreach ($lines as $line) {
1726 $this->contextLine( htmlspecialchars ( $line ) ) .
1727 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1731 function _changed( $orig, $closing ) {
1732 $fname = 'TableDiffFormatter::_changed';
1733 wfProfileIn( $fname );
1735 $diff = new WordLevelDiff( $orig, $closing );
1736 $del = $diff->orig();
1737 $add = $diff->closing();
1739 # Notice that WordLevelDiff returns HTML-escaped output.
1740 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1742 while ( $line = array_shift( $del ) ) {
1743 $aline = array_shift( $add );
1744 echo '<tr>' . $this->deletedLine( $line ) .
1745 $this->addedLine( $aline ) . "</tr>\n";
1747 foreach ($add as $line) { # If any leftovers
1748 echo '<tr>' . $this->emptyLine() .
1749 $this->addedLine( $line ) . "</tr>\n";
1751 wfProfileOut( $fname );