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
{
24 var $mOldid, $mNewid, $mTitle;
25 var $mOldtitle, $mNewtitle, $mPagetitle;
26 var $mOldtext, $mNewtext;
27 var $mOldPage, $mNewPage;
28 var $mRcidMarkPatrolled;
29 var $mOldRev, $mNewRev;
30 var $mRevisionsLoaded = false; // Have the revisions been loaded
31 var $mTextLoaded = 0; // How many text blobs have been loaded, 0, 1 or 2?
36 * @param Title $titleObj Title object that the diff is associated with
37 * @param integer $old Old ID we want to show and diff with.
38 * @param string $new Either 'prev' or 'next'.
39 * @param integer $rcid ??? (default 0)
41 function DifferenceEngine( $titleObj = null, $old = 0, $new = 0, $rcid = 0 ) {
42 $this->mTitle
= $titleObj;
43 wfDebug("DifferenceEngine old '$old' new '$new' rcid '$rcid'\n");
45 if ( 'prev' === $new ) {
46 # Show diff between revision $old and the previous one.
47 # Get previous one from DB.
49 $this->mNewid
= intval($old);
51 $this->mOldid
= $this->mTitle
->getPreviousRevisionID( $this->mNewid
);
53 } elseif ( 'next' === $new ) {
54 # Show diff between revision $old and the previous one.
55 # Get previous one from DB.
57 $this->mOldid
= intval($old);
58 $this->mNewid
= $this->mTitle
->getNextRevisionID( $this->mOldid
);
59 if ( false === $this->mNewid
) {
60 # if no result, NewId points to the newest old revision. The only newer
61 # revision is cur, which is "0".
66 $this->mOldid
= intval($old);
67 $this->mNewid
= intval($new);
69 $this->mRcidMarkPatrolled
= intval($rcid); # force it to be an integer
72 function showDiffPage() {
73 global $wgUser, $wgOut, $wgContLang, $wgOnlySysopsCanPatrol;
74 global $wgUseExternalEditor, $wgUseRCPatrol;
75 $fname = 'DifferenceEngine::showDiffPage';
76 wfProfileIn( $fname );
78 # If external diffs are enabled both globally and for the user,
79 # we'll use the application/x-external-editor interface to call
80 # an external diff tool like kompare, kdiff3, etc.
81 if($wgUseExternalEditor && $wgUser->getOption('externaldiff')) {
82 global $wgInputEncoding,$wgServer,$wgScript,$wgLang;
84 header ( "Content-type: application/x-external-editor; charset=".$wgInputEncoding );
85 $url1=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mOldid
);
86 $url2=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mNewid
);
87 $special=$wgLang->getNsText(NS_SPECIAL
);
92 Script={$wgServer}{$wgScript}
93 Special namespace={$special}
107 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
109 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
111 $wgOut->setArticleFlag( false );
112 if ( ! $this->loadRevisionData() ) {
113 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
114 $wgOut->addWikitext( $mtext );
115 wfProfileOut( $fname );
118 if ( $this->mNewRev
->isCurrent() ) {
119 $wgOut->setArticleFlag( true );
122 # mOldid is false if the difference engine is called with a "vague" query for
123 # a diff between a version V and its previous version V' AND the version V
124 # is the first version of that article. In that case, V' does not exist.
125 if ( $this->mOldid
=== false ) {
126 $this->showFirstRevision();
127 wfProfileOut( $fname );
131 $wgOut->suppressQuickbar();
133 $oldTitle = $this->mOldPage
->getPrefixedText();
134 $newTitle = $this->mNewPage
->getPrefixedText();
135 if( $oldTitle == $newTitle ) {
136 $wgOut->setPageTitle( $newTitle );
138 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
140 $wgOut->setSubtitle( wfMsg( 'difference' ) );
141 $wgOut->setRobotpolicy( 'noindex,follow' );
143 if ( !( $this->mOldPage
->userCanRead() && $this->mNewPage
->userCanRead() ) ) {
144 $wgOut->loginToUse();
146 wfProfileOut( $fname );
150 $sk = $wgUser->getSkin();
151 $talk = $wgContLang->getNsText( NS_TALK
);
152 $contribs = wfMsg( 'contribslink' );
154 if ( $this->mNewRev
->isCurrent() && $wgUser->isAllowed('rollback') ) {
155 $username = $this->mNewRev
->getUserText();
156 $rollback = ' <strong>[' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'rollbacklink' ),
157 'action=rollback&from=' . urlencode( $username ) .
158 '&token=' . urlencode( $wgUser->editToken( array( $this->mTitle
->getPrefixedText(), $username ) ) ) ) .
163 if ( $wgUseRCPatrol && $this->mRcidMarkPatrolled
!= 0 && $wgUser->isLoggedIn() &&
164 ( $wgUser->isAllowed('rollback') ||
!$wgOnlySysopsCanPatrol ) )
166 $patrol = ' [' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'markaspatrolleddiff' ),
167 "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']';
172 $prevlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'previousdiff' ),
173 'diff=prev&oldid='.$this->mOldid
, '', '', 'id="differences-prevlink"' );
174 if ( $this->mNewRev
->isCurrent() ) {
177 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ),
178 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
181 $oldHeader = "<strong>{$this->mOldtitle}</strong><br />" .
182 $sk->revUserTools( $this->mOldRev
) . "<br />" .
183 $sk->revComment( $this->mOldRev
) . "<br />" .
185 $newHeader = "<strong>{$this->mNewtitle}</strong><br />" .
186 $sk->revUserTools( $this->mNewRev
) . " $rollback<br />" .
187 $sk->revComment( $this->mNewRev
) . "<br />" .
190 $this->showDiff( $oldHeader, $newHeader );
191 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
193 if( !$this->mNewRev
->isCurrent() ) {
194 $oldEditSectionSetting = $wgOut->mParserOptions
->setEditSection( false );
197 $this->loadNewText();
198 if( is_object( $this->mNewRev
) ) {
199 $wgOut->setRevisionId( $this->mNewRev
->getId() );
201 $wgOut->addSecondaryWikiText( $this->mNewtext
);
203 if( !$this->mNewRev
->isCurrent() ) {
204 $wgOut->mParserOptions
->setEditSection( $oldEditSectionSetting );
207 wfProfileOut( $fname );
211 * Show the first revision of an article. Uses normal diff headers in
212 * contrast to normal "old revision" display style.
214 function showFirstRevision() {
215 global $wgOut, $wgUser;
217 $fname = 'DifferenceEngine::showFirstRevision';
218 wfProfileIn( $fname );
220 # Get article text from the DB
222 if ( ! $this->loadNewText() ) {
223 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
225 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
226 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
227 $wgOut->addWikitext( $mtext );
228 wfProfileOut( $fname );
231 if ( $this->mNewRev
->isCurrent() ) {
232 $wgOut->setArticleFlag( true );
235 # Check if user is allowed to look at this page. If not, bail out.
237 if ( !( $this->mTitle
->userCanRead() ) ) {
238 $wgOut->loginToUse();
240 wfProfileOut( $fname );
244 # Prepare the header box
246 $sk = $wgUser->getSkin();
248 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
249 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />" .
250 $sk->revUserTools( $this->mNewRev
) . "<br />" .
251 $sk->revComment( $this->mNewRev
) . "<br />" .
252 $nextlink . "</div>\n";
254 $wgOut->addHTML( $header );
256 $wgOut->setSubtitle( wfMsg( 'difference' ) );
257 $wgOut->setRobotpolicy( 'noindex,nofollow' );
260 # Show current revision
262 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
263 if( is_object( $this->mNewRev
) ) {
264 $wgOut->setRevisionId( $this->mNewRev
->getId() );
266 $wgOut->addSecondaryWikiText( $this->mNewtext
);
268 wfProfileOut( $fname );
272 * Get the diff text, send it to $wgOut
273 * Returns false if the diff could not be generated, otherwise returns true
275 function showDiff( $otitle, $ntitle ) {
277 $diff = $this->getDiff( $otitle, $ntitle );
278 if ( $diff === false ) {
279 $wgOut->addWikitext( wfMsg( 'missingarticle', "<nowiki>(fixme, bug)</nowiki>" ) );
282 $wgOut->addHTML( $diff );
288 * Get diff table, including header
289 * Note that the interface has changed, it's no longer static.
290 * Returns false on error
292 function getDiff( $otitle, $ntitle ) {
293 $body = $this->getDiffBody();
294 if ( $body === false ) {
297 return $this->addHeader( $body, $otitle, $ntitle );
302 * Get the diff table body, without header
304 * Returns false on error
306 function getDiffBody() {
307 global $wgMemc, $wgDBname;
308 $fname = 'DifferenceEngine::getDiffBody';
309 wfProfileIn( $fname );
313 if ( $this->mOldid
&& $this->mNewid
) {
315 $key = "$wgDBname:diff:oldid:{$this->mOldid}:newid:{$this->mNewid}";
316 $difftext = $wgMemc->get( $key );
318 wfIncrStats( 'diff_cache_hit' );
319 $difftext = $this->localiseLineNumbers( $difftext );
320 $difftext .= "\n<!-- diff cache key $key -->\n";
321 wfProfileOut( $fname );
326 if ( !$this->loadText() ) {
327 wfProfileOut( $fname );
331 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
333 // Save to cache for 7 days
334 if ( $key !== false && $difftext !== false ) {
335 wfIncrStats( 'diff_cache_miss' );
336 $wgMemc->set( $key, $difftext, 7*86400 );
338 wfIncrStats( 'diff_uncacheable' );
340 // Replace line numbers with the text in the user's language
341 if ( $difftext !== false ) {
342 $difftext = $this->localiseLineNumbers( $difftext );
344 wfProfileOut( $fname );
349 * Generate a diff, no caching
350 * $otext and $ntext must be already segmented
352 function generateDiffBody( $otext, $ntext ) {
353 global $wgExternalDiffEngine, $wgContLang;
354 $fname = 'DifferenceEngine::generateDiffBody';
356 $otext = str_replace( "\r\n", "\n", $otext );
357 $ntext = str_replace( "\r\n", "\n", $ntext );
359 if ( $wgExternalDiffEngine == 'wikidiff' ) {
360 # For historical reasons, external diff engine expects
361 # input text to be HTML-escaped already
362 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
363 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
364 if( !function_exists( 'wikidiff_do_diff' ) ) {
365 dl('php_wikidiff.so');
367 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) );
370 if ( $wgExternalDiffEngine == 'wikidiff2' ) {
371 # Better external diff engine, the 2 may some day be dropped
372 # This one does the escaping and segmenting itself
373 if ( !function_exists( 'wikidiff2_do_diff' ) ) {
374 @dl
('php_wikidiff2.so');
376 if ( function_exists( 'wikidiff2_do_diff' ) ) {
377 return wikidiff2_do_diff( $otext, $ntext, 2 );
380 if ( $wgExternalDiffEngine !== false ) {
382 global $wgTmpDirectory;
383 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
384 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
386 $tempFile1 = fopen( $tempName1, "w" );
388 wfProfileOut( $fname );
391 $tempFile2 = fopen( $tempName2, "w" );
393 wfProfileOut( $fname );
396 fwrite( $tempFile1, $otext );
397 fwrite( $tempFile2, $ntext );
398 fclose( $tempFile1 );
399 fclose( $tempFile2 );
400 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
401 wfProfileIn( "$fname-shellexec" );
402 $difftext = wfShellExec( $cmd );
403 wfProfileOut( "$fname-shellexec" );
404 unlink( $tempName1 );
405 unlink( $tempName2 );
410 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
411 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
412 $diffs =& new Diff( $ota, $nta );
413 $formatter =& new TableDiffFormatter();
414 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
419 * Replace line numbers with the text in the user's language
421 function localiseLineNumbers( $text ) {
422 return preg_replace_callback( '/<!--LINE (\d+)-->/',
423 array( &$this, 'localiseLineNumbersCb' ), $text );
426 function localiseLineNumbersCb( $matches ) {
428 return wfMsg( 'lineno', $wgLang->formatNum( $matches[1] ) );
432 * Add the header to a diff body
434 function addHeader( $diff, $otitle, $ntitle ) {
436 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
438 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
439 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
448 * Use specified text instead of loading from the database
450 function setText( $oldText, $newText ) {
451 $this->mOldtext
= $oldText;
452 $this->mNewtext
= $newText;
453 $this->mTextLoaded
= 2;
457 * Load revision metadata for the specified articles. If newid is 0, then compare
458 * the old article in oldid to the current article; if oldid is 0, then
459 * compare the current article to the immediately previous one (ignoring the
462 * If oldid is false, leave the corresponding revision object set
463 * to false. This is impossible via ordinary user input, and is provided for
466 function loadRevisionData() {
468 if ( $this->mRevisionsLoaded
) {
471 // Whether it succeeds or fails, we don't want to try again
472 $this->mRevisionsLoaded
= true;
475 // Load the new revision object
476 if( $this->mNewid
) {
477 $this->mNewRev
= Revision
::newFromId( $this->mNewid
);
479 $this->mNewRev
= Revision
::newFromTitle( $this->mTitle
);
482 if( is_null( $this->mNewRev
) ) {
486 // Set assorted variables
487 if( $this->mNewRev
->isCurrent() ) {
488 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
489 $this->mNewPage
= $this->mTitle
;
490 $newLink = $this->mNewPage
->escapeLocalUrl();
491 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
493 $this->mNewPage
= $this->mNewRev
->getTitle();
494 $newLink = $this->mNewPage
->escapeLocalUrl ('oldid=' . $this->mNewid
);
495 $t = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
496 $this->mPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t ) );
497 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
500 // Load the old revision object
501 $this->mOldRev
= false;
502 if( $this->mOldid
) {
503 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
504 } elseif ( $this->mOldid
=== 0 ) {
505 $rev = $this->mNewRev
->getPrevious();
507 $this->mOldid
= $rev->getId();
508 $this->mOldRev
= $rev;
510 // No previous revision; mark to show as first-version only.
511 $this->mOldid
= false;
512 $this->mOldRev
= false;
514 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
516 if( is_null( $this->mOldRev
) ) {
520 if ( $this->mOldRev
) {
521 $this->mOldPage
= $this->mOldRev
->getTitle();
523 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
524 $oldLink = $this->mOldPage
->escapeLocalUrl( 'oldid=' . $this->mOldid
);
525 $this->mOldtitle
= "<a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) ) . '</a>';
532 * Load the text of the revisions, as well as revision data.
534 function loadText() {
535 if ( $this->mTextLoaded
== 2 ) {
538 // Whether it succeeds or fails, we don't want to try again
539 $this->mTextLoaded
= 2;
542 if ( !$this->loadRevisionData() ) {
545 if ( $this->mOldRev
) {
546 // FIXME: permission tests
547 $this->mOldtext
= $this->mOldRev
->getText();
548 if ( $this->mOldtext
=== false ) {
552 if ( $this->mNewRev
) {
553 $this->mNewtext
= $this->mNewRev
->getText();
554 if ( $this->mNewtext
=== false ) {
562 * Load the text of the new revision, not the old one
564 function loadNewText() {
565 if ( $this->mTextLoaded
>= 1 ) {
568 $this->mTextLoaded
= 1;
570 if ( !$this->loadRevisionData() ) {
573 $this->mNewtext
= $this->mNewRev
->getText();
580 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
582 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
583 // You may copy this code freely under the conditions of the GPL.
586 define('USE_ASSERTS', function_exists('assert'));
592 * @subpackage DifferenceEngine
600 trigger_error('pure virtual', E_USER_ERROR
);
604 return $this->orig ?
sizeof($this->orig
) : 0;
607 function nclosing() {
608 return $this->closing ?
sizeof($this->closing
) : 0;
616 * @subpackage DifferenceEngine
618 class _DiffOp_Copy
extends _DiffOp
{
621 function _DiffOp_Copy ($orig, $closing = false) {
622 if (!is_array($closing))
625 $this->closing
= $closing;
629 return new _DiffOp_Copy($this->closing
, $this->orig
);
637 * @subpackage DifferenceEngine
639 class _DiffOp_Delete
extends _DiffOp
{
640 var $type = 'delete';
642 function _DiffOp_Delete ($lines) {
643 $this->orig
= $lines;
644 $this->closing
= false;
648 return new _DiffOp_Add($this->orig
);
656 * @subpackage DifferenceEngine
658 class _DiffOp_Add
extends _DiffOp
{
661 function _DiffOp_Add ($lines) {
662 $this->closing
= $lines;
667 return new _DiffOp_Delete($this->closing
);
675 * @subpackage DifferenceEngine
677 class _DiffOp_Change
extends _DiffOp
{
678 var $type = 'change';
680 function _DiffOp_Change ($orig, $closing) {
682 $this->closing
= $closing;
686 return new _DiffOp_Change($this->closing
, $this->orig
);
692 * Class used internally by Diff to actually compute the diffs.
694 * The algorithm used here is mostly lifted from the perl module
695 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
696 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
698 * More ideas are taken from:
699 * http://www.ics.uci.edu/~eppstein/161/960229.html
701 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
702 * diffutils-2.7, which can be found at:
703 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
705 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
708 * Line length limits for robustness added by Tim Starling, 2005-08-31
710 * @author Geoffrey T. Dairiki, Tim Starling
713 * @subpackage DifferenceEngine
717 function diff ($from_lines, $to_lines) {
718 $fname = '_DiffEngine::diff';
719 wfProfileIn( $fname );
721 $n_from = sizeof($from_lines);
722 $n_to = sizeof($to_lines);
724 $this->xchanged
= $this->ychanged
= array();
725 $this->xv
= $this->yv
= array();
726 $this->xind
= $this->yind
= array();
728 unset($this->in_seq
);
731 // Skip leading common lines.
732 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
733 if ($from_lines[$skip] !== $to_lines[$skip])
735 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
737 // Skip trailing common lines.
738 $xi = $n_from; $yi = $n_to;
739 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
740 if ($from_lines[$xi] !== $to_lines[$yi])
742 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
745 // Ignore lines which do not exist in both files.
746 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
747 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
750 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
751 $line = $to_lines[$yi];
752 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
754 $yhash[$this->_line_hash($line)] = 1;
758 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
759 $line = $from_lines[$xi];
760 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
767 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
769 // Merge edits when possible
770 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
771 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
773 // Compute the edit operations.
776 while ($xi < $n_from ||
$yi < $n_to) {
777 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
778 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
780 // Skip matching "snake".
782 while ( $xi < $n_from && $yi < $n_to
783 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
784 $copy[] = $from_lines[$xi++
];
788 $edits[] = new _DiffOp_Copy($copy);
790 // Find deletes & adds.
792 while ($xi < $n_from && $this->xchanged
[$xi])
793 $delete[] = $from_lines[$xi++
];
796 while ($yi < $n_to && $this->ychanged
[$yi])
797 $add[] = $to_lines[$yi++
];
800 $edits[] = new _DiffOp_Change($delete, $add);
802 $edits[] = new _DiffOp_Delete($delete);
804 $edits[] = new _DiffOp_Add($add);
806 wfProfileOut( $fname );
811 * Returns the whole line if it's small enough, or the MD5 hash otherwise
813 function _line_hash( $line ) {
814 if ( strlen( $line ) > MAX_DIFF_XREF_LENGTH
) {
822 /* Divide the Largest Common Subsequence (LCS) of the sequences
823 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
826 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
827 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
828 * sub sequences. The first sub-sequence is contained in [X0, X1),
829 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
830 * that (X0, Y0) == (XOFF, YOFF) and
831 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
833 * This function assumes that the first lines of the specified portions
834 * of the two files do not match, and likewise that the last lines do not
835 * match. The caller must trim matching lines from the beginning and end
836 * of the portions it is going to specify.
838 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
839 $fname = '_DiffEngine::_diag';
840 wfProfileIn( $fname );
843 if ($xlim - $xoff > $ylim - $yoff) {
844 // Things seems faster (I'm not sure I understand why)
845 // when the shortest sequence in X.
847 list ($xoff, $xlim, $yoff, $ylim)
848 = array( $yoff, $ylim, $xoff, $xlim);
852 for ($i = $ylim - 1; $i >= $yoff; $i--)
853 $ymatches[$this->xv
[$i]][] = $i;
855 for ($i = $ylim - 1; $i >= $yoff; $i--)
856 $ymatches[$this->yv
[$i]][] = $i;
859 $this->seq
[0]= $yoff - 1;
860 $this->in_seq
= array();
863 $numer = $xlim - $xoff +
$nchunks - 1;
865 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
866 wfProfileIn( "$fname-chunk" );
868 for ($i = 0; $i <= $this->lcs
; $i++
)
869 $ymids[$i][$chunk-1] = $this->seq
[$i];
871 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
872 for ( ; $x < $x1; $x++
) {
873 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
874 if (empty($ymatches[$line]))
876 $matches = $ymatches[$line];
878 while (list ($junk, $y) = each($matches))
879 if (empty($this->in_seq
[$y])) {
880 $k = $this->_lcs_pos($y);
881 USE_ASSERTS
&& assert($k > 0);
882 $ymids[$k] = $ymids[$k-1];
885 while (list ($junk, $y) = each($matches)) {
886 if ($y > $this->seq
[$k-1]) {
887 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
888 // Optimization: this is a common case:
889 // next match is just replacing previous match.
890 $this->in_seq
[$this->seq
[$k]] = false;
892 $this->in_seq
[$y] = 1;
893 } else if (empty($this->in_seq
[$y])) {
894 $k = $this->_lcs_pos($y);
895 USE_ASSERTS
&& assert($k > 0);
896 $ymids[$k] = $ymids[$k-1];
900 wfProfileOut( "$fname-chunk" );
903 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
904 $ymid = $ymids[$this->lcs
];
905 for ($n = 0; $n < $nchunks - 1; $n++
) {
906 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
908 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
910 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
912 wfProfileOut( $fname );
913 return array($this->lcs
, $seps);
916 function _lcs_pos ($ypos) {
917 $fname = '_DiffEngine::_lcs_pos';
918 wfProfileIn( $fname );
921 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
922 $this->seq
[++
$this->lcs
] = $ypos;
923 $this->in_seq
[$ypos] = 1;
924 wfProfileOut( $fname );
929 while ($beg < $end) {
930 $mid = (int)(($beg +
$end) / 2);
931 if ( $ypos > $this->seq
[$mid] )
937 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
939 $this->in_seq
[$this->seq
[$end]] = false;
940 $this->seq
[$end] = $ypos;
941 $this->in_seq
[$ypos] = 1;
942 wfProfileOut( $fname );
946 /* Find LCS of two sequences.
948 * The results are recorded in the vectors $this->{x,y}changed[], by
949 * storing a 1 in the element for each line that is an insertion
950 * or deletion (ie. is not in the LCS).
952 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
954 * Note that XLIM, YLIM are exclusive bounds.
955 * All line numbers are origin-0 and discarded lines are not counted.
957 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
958 $fname = '_DiffEngine::_compareseq';
959 wfProfileIn( $fname );
961 // Slide down the bottom initial diagonal.
962 while ($xoff < $xlim && $yoff < $ylim
963 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
968 // Slide up the top initial diagonal.
969 while ($xlim > $xoff && $ylim > $yoff
970 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
975 if ($xoff == $xlim ||
$yoff == $ylim)
978 // This is ad hoc but seems to work well.
979 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
980 //$nchunks = max(2,min(8,(int)$nchunks));
981 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
983 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
987 // X and Y sequences have no common subsequence:
989 while ($yoff < $ylim)
990 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
991 while ($xoff < $xlim)
992 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
994 // Use the partitions to split this problem into subproblems.
997 while ($pt2 = next($seps)) {
998 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1002 wfProfileOut( $fname );
1005 /* Adjust inserts/deletes of identical lines to join changes
1006 * as much as possible.
1008 * We do something when a run of changed lines include a
1009 * line at one end and has an excluded, identical line at the other.
1010 * We are free to choose which identical line is included.
1011 * `compareseq' usually chooses the one at the beginning,
1012 * but usually it is cleaner to consider the following identical line
1013 * to be the "change".
1015 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1017 function _shift_boundaries ($lines, &$changed, $other_changed) {
1018 $fname = '_DiffEngine::_shift_boundaries';
1019 wfProfileIn( $fname );
1023 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1024 $len = sizeof($lines);
1025 $other_len = sizeof($other_changed);
1029 * Scan forwards to find beginning of another run of changes.
1030 * Also keep track of the corresponding point in the other file.
1032 * Throughout this code, $i and $j are adjusted together so that
1033 * the first $i elements of $changed and the first $j elements
1034 * of $other_changed both contain the same number of zeros
1035 * (unchanged lines).
1036 * Furthermore, $j is always kept so that $j == $other_len or
1037 * $other_changed[$j] == false.
1039 while ($j < $other_len && $other_changed[$j])
1042 while ($i < $len && ! $changed[$i]) {
1043 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1045 while ($j < $other_len && $other_changed[$j])
1054 // Find the end of this run of changes.
1055 while (++
$i < $len && $changed[$i])
1060 * Record the length of this run of changes, so that
1061 * we can later determine whether the run has grown.
1063 $runlength = $i - $start;
1066 * Move the changed region back, so long as the
1067 * previous unchanged line matches the last changed one.
1068 * This merges with previous changed regions.
1070 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1071 $changed[--$start] = 1;
1072 $changed[--$i] = false;
1073 while ($start > 0 && $changed[$start - 1])
1075 USE_ASSERTS
&& assert('$j > 0');
1076 while ($other_changed[--$j])
1078 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1082 * Set CORRESPONDING to the end of the changed run, at the last
1083 * point where it corresponds to a changed run in the other file.
1084 * CORRESPONDING == LEN means no such point has been found.
1086 $corresponding = $j < $other_len ?
$i : $len;
1089 * Move the changed region forward, so long as the
1090 * first changed line matches the following unchanged one.
1091 * This merges with following changed regions.
1092 * Do this second, so that if there are no merges,
1093 * the changed region is moved forward as far as possible.
1095 while ($i < $len && $lines[$start] == $lines[$i]) {
1096 $changed[$start++
] = false;
1098 while ($i < $len && $changed[$i])
1101 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1103 if ($j < $other_len && $other_changed[$j]) {
1104 $corresponding = $i;
1105 while ($j < $other_len && $other_changed[$j])
1109 } while ($runlength != $i - $start);
1112 * If possible, move the fully-merged run of changes
1113 * back to a corresponding run in the other file.
1115 while ($corresponding < $i) {
1116 $changed[--$start] = 1;
1118 USE_ASSERTS
&& assert('$j > 0');
1119 while ($other_changed[--$j])
1121 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1124 wfProfileOut( $fname );
1129 * Class representing a 'diff' between two sequences of strings.
1132 * @package MediaWiki
1133 * @subpackage DifferenceEngine
1141 * Computes diff between sequences of strings.
1143 * @param $from_lines array An array of strings.
1144 * (Typically these are lines from a file.)
1145 * @param $to_lines array An array of strings.
1147 function Diff($from_lines, $to_lines) {
1148 $eng = new _DiffEngine
;
1149 $this->edits
= $eng->diff($from_lines, $to_lines);
1150 //$this->_check($from_lines, $to_lines);
1154 * Compute reversed Diff.
1158 * $diff = new Diff($lines1, $lines2);
1159 * $rev = $diff->reverse();
1160 * @return object A Diff object representing the inverse of the
1163 function reverse () {
1165 $rev->edits
= array();
1166 foreach ($this->edits
as $edit) {
1167 $rev->edits
[] = $edit->reverse();
1173 * Check for empty diff.
1175 * @return bool True iff two sequences were identical.
1177 function isEmpty () {
1178 foreach ($this->edits
as $edit) {
1179 if ($edit->type
!= 'copy')
1186 * Compute the length of the Longest Common Subsequence (LCS).
1188 * This is mostly for diagnostic purposed.
1190 * @return int The length of the LCS.
1194 foreach ($this->edits
as $edit) {
1195 if ($edit->type
== 'copy')
1196 $lcs +
= sizeof($edit->orig
);
1202 * Get the original set of lines.
1204 * This reconstructs the $from_lines parameter passed to the
1207 * @return array The original sequence of strings.
1212 foreach ($this->edits
as $edit) {
1214 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1220 * Get the closing set of lines.
1222 * This reconstructs the $to_lines parameter passed to the
1225 * @return array The sequence of strings.
1227 function closing() {
1230 foreach ($this->edits
as $edit) {
1232 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1238 * Check a Diff for validity.
1240 * This is here only for debugging purposes.
1242 function _check ($from_lines, $to_lines) {
1243 $fname = 'Diff::_check';
1244 wfProfileIn( $fname );
1245 if (serialize($from_lines) != serialize($this->orig()))
1246 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1247 if (serialize($to_lines) != serialize($this->closing()))
1248 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1250 $rev = $this->reverse();
1251 if (serialize($to_lines) != serialize($rev->orig()))
1252 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1253 if (serialize($from_lines) != serialize($rev->closing()))
1254 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1258 foreach ($this->edits
as $edit) {
1259 if ( $prevtype == $edit->type
)
1260 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1261 $prevtype = $edit->type
;
1264 $lcs = $this->lcs();
1265 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1266 wfProfileOut( $fname );
1274 * @package MediaWiki
1275 * @subpackage DifferenceEngine
1277 class MappedDiff
extends Diff
1282 * Computes diff between sequences of strings.
1284 * This can be used to compute things like
1285 * case-insensitve diffs, or diffs which ignore
1286 * changes in white-space.
1288 * @param $from_lines array An array of strings.
1289 * (Typically these are lines from a file.)
1291 * @param $to_lines array An array of strings.
1293 * @param $mapped_from_lines array This array should
1294 * have the same size number of elements as $from_lines.
1295 * The elements in $mapped_from_lines and
1296 * $mapped_to_lines are what is actually compared
1297 * when computing the diff.
1299 * @param $mapped_to_lines array This array should
1300 * have the same number of elements as $to_lines.
1302 function MappedDiff($from_lines, $to_lines,
1303 $mapped_from_lines, $mapped_to_lines) {
1304 $fname = 'MappedDiff::MappedDiff';
1305 wfProfileIn( $fname );
1307 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1308 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1310 $this->Diff($mapped_from_lines, $mapped_to_lines);
1313 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1314 $orig = &$this->edits
[$i]->orig
;
1315 if (is_array($orig)) {
1316 $orig = array_slice($from_lines, $xi, sizeof($orig));
1317 $xi +
= sizeof($orig);
1320 $closing = &$this->edits
[$i]->closing
;
1321 if (is_array($closing)) {
1322 $closing = array_slice($to_lines, $yi, sizeof($closing));
1323 $yi +
= sizeof($closing);
1326 wfProfileOut( $fname );
1331 * A class to format Diffs
1333 * This class formats the diff in classic diff format.
1334 * It is intended that this class be customized via inheritance,
1335 * to obtain fancier outputs.
1338 * @package MediaWiki
1339 * @subpackage DifferenceEngine
1344 * Number of leading context "lines" to preserve.
1346 * This should be left at zero for this class, but subclasses
1347 * may want to set this to other values.
1349 var $leading_context_lines = 0;
1352 * Number of trailing context "lines" to preserve.
1354 * This should be left at zero for this class, but subclasses
1355 * may want to set this to other values.
1357 var $trailing_context_lines = 0;
1362 * @param $diff object A Diff object.
1363 * @return string The formatted output.
1365 function format($diff) {
1366 $fname = 'DiffFormatter::format';
1367 wfProfileIn( $fname );
1373 $nlead = $this->leading_context_lines
;
1374 $ntrail = $this->trailing_context_lines
;
1376 $this->_start_diff();
1378 foreach ($diff->edits
as $edit) {
1379 if ($edit->type
== 'copy') {
1380 if (is_array($block)) {
1381 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1386 $context = array_slice($edit->orig
, 0, $ntrail);
1387 $block[] = new _DiffOp_Copy($context);
1389 $this->_block($x0, $ntrail +
$xi - $x0,
1390 $y0, $ntrail +
$yi - $y0,
1395 $context = $edit->orig
;
1398 if (! is_array($block)) {
1399 $context = array_slice($context, sizeof($context) - $nlead);
1400 $x0 = $xi - sizeof($context);
1401 $y0 = $yi - sizeof($context);
1404 $block[] = new _DiffOp_Copy($context);
1410 $xi +
= sizeof($edit->orig
);
1412 $yi +
= sizeof($edit->closing
);
1415 if (is_array($block))
1416 $this->_block($x0, $xi - $x0,
1420 $end = $this->_end_diff();
1421 wfProfileOut( $fname );
1425 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1426 $fname = 'DiffFormatter::_block';
1427 wfProfileIn( $fname );
1428 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1429 foreach ($edits as $edit) {
1430 if ($edit->type
== 'copy')
1431 $this->_context($edit->orig
);
1432 elseif ($edit->type
== 'add')
1433 $this->_added($edit->closing
);
1434 elseif ($edit->type
== 'delete')
1435 $this->_deleted($edit->orig
);
1436 elseif ($edit->type
== 'change')
1437 $this->_changed($edit->orig
, $edit->closing
);
1439 trigger_error('Unknown edit type', E_USER_ERROR
);
1441 $this->_end_block();
1442 wfProfileOut( $fname );
1445 function _start_diff() {
1449 function _end_diff() {
1450 $val = ob_get_contents();
1455 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1457 $xbeg .= "," . ($xbeg +
$xlen - 1);
1459 $ybeg .= "," . ($ybeg +
$ylen - 1);
1461 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1464 function _start_block($header) {
1468 function _end_block() {
1471 function _lines($lines, $prefix = ' ') {
1472 foreach ($lines as $line)
1473 echo "$prefix $line\n";
1476 function _context($lines) {
1477 $this->_lines($lines);
1480 function _added($lines) {
1481 $this->_lines($lines, '>');
1483 function _deleted($lines) {
1484 $this->_lines($lines, '<');
1487 function _changed($orig, $closing) {
1488 $this->_deleted($orig);
1490 $this->_added($closing);
1496 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1500 define('NBSP', ' '); // iso-8859-x non-breaking space.
1505 * @package MediaWiki
1506 * @subpackage DifferenceEngine
1508 class _HWLDF_WordAccumulator
{
1509 function _HWLDF_WordAccumulator () {
1510 $this->_lines
= array();
1516 function _flushGroup ($new_tag) {
1517 if ($this->_group
!== '') {
1518 if ($this->_tag
== 'mark')
1519 $this->_line
.= '<span class="diffchange">' .
1520 htmlspecialchars ( $this->_group
) . '</span>';
1522 $this->_line
.= htmlspecialchars ( $this->_group
);
1525 $this->_tag
= $new_tag;
1528 function _flushLine ($new_tag) {
1529 $this->_flushGroup($new_tag);
1530 if ($this->_line
!= '')
1531 array_push ( $this->_lines
, $this->_line
);
1533 # make empty lines visible by inserting an NBSP
1534 array_push ( $this->_lines
, NBSP
);
1538 function addWords ($words, $tag = '') {
1539 if ($tag != $this->_tag
)
1540 $this->_flushGroup($tag);
1542 foreach ($words as $word) {
1543 // new-line should only come as first char of word.
1546 if ($word[0] == "\n") {
1547 $this->_flushLine($tag);
1548 $word = substr($word, 1);
1550 assert(!strstr($word, "\n"));
1551 $this->_group
.= $word;
1555 function getLines() {
1556 $this->_flushLine('~done');
1557 return $this->_lines
;
1564 * @package MediaWiki
1565 * @subpackage DifferenceEngine
1567 class WordLevelDiff
extends MappedDiff
1569 function WordLevelDiff ($orig_lines, $closing_lines) {
1570 $fname = 'WordLevelDiff::WordLevelDiff';
1571 wfProfileIn( $fname );
1573 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1574 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1576 $this->MappedDiff($orig_words, $closing_words,
1577 $orig_stripped, $closing_stripped);
1578 wfProfileOut( $fname );
1581 function _split($lines) {
1582 $fname = 'WordLevelDiff::_split';
1583 wfProfileIn( $fname );
1586 $stripped = array();
1588 foreach ( $lines as $line ) {
1589 # If the line is too long, just pretend the entire line is one big word
1590 # This prevents resource exhaustion problems
1597 if ( strlen( $line ) > MAX_DIFF_LINE
) {
1599 $stripped[] = $line;
1601 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1604 $words = array_merge( $words, $m[0] );
1605 $stripped = array_merge( $stripped, $m[1] );
1609 wfProfileOut( $fname );
1610 return array($words, $stripped);
1614 $fname = 'WordLevelDiff::orig';
1615 wfProfileIn( $fname );
1616 $orig = new _HWLDF_WordAccumulator
;
1618 foreach ($this->edits
as $edit) {
1619 if ($edit->type
== 'copy')
1620 $orig->addWords($edit->orig
);
1621 elseif ($edit->orig
)
1622 $orig->addWords($edit->orig
, 'mark');
1624 $lines = $orig->getLines();
1625 wfProfileOut( $fname );
1629 function closing () {
1630 $fname = 'WordLevelDiff::closing';
1631 wfProfileIn( $fname );
1632 $closing = new _HWLDF_WordAccumulator
;
1634 foreach ($this->edits
as $edit) {
1635 if ($edit->type
== 'copy')
1636 $closing->addWords($edit->closing
);
1637 elseif ($edit->closing
)
1638 $closing->addWords($edit->closing
, 'mark');
1640 $lines = $closing->getLines();
1641 wfProfileOut( $fname );
1647 * Wikipedia Table style diff formatter.
1650 * @package MediaWiki
1651 * @subpackage DifferenceEngine
1653 class TableDiffFormatter
extends DiffFormatter
1655 function TableDiffFormatter() {
1656 $this->leading_context_lines
= 2;
1657 $this->trailing_context_lines
= 2;
1660 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1661 $r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" .
1662 '<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n";
1666 function _start_block( $header ) {
1670 function _end_block() {
1673 function _lines( $lines, $prefix=' ', $color='white' ) {
1676 # HTML-escape parameter before calling this
1677 function addedLine( $line ) {
1678 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1681 # HTML-escape parameter before calling this
1682 function deletedLine( $line ) {
1683 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1686 # HTML-escape parameter before calling this
1687 function contextLine( $line ) {
1688 return "<td> </td><td class='diff-context'>{$line}</td>";
1691 function emptyLine() {
1692 return '<td colspan="2"> </td>';
1695 function _added( $lines ) {
1696 foreach ($lines as $line) {
1697 echo '<tr>' . $this->emptyLine() .
1698 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1702 function _deleted($lines) {
1703 foreach ($lines as $line) {
1704 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1705 $this->emptyLine() . "</tr>\n";
1709 function _context( $lines ) {
1710 foreach ($lines as $line) {
1712 $this->contextLine( htmlspecialchars ( $line ) ) .
1713 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1717 function _changed( $orig, $closing ) {
1718 $fname = 'TableDiffFormatter::_changed';
1719 wfProfileIn( $fname );
1721 $diff = new WordLevelDiff( $orig, $closing );
1722 $del = $diff->orig();
1723 $add = $diff->closing();
1725 # Notice that WordLevelDiff returns HTML-escaped output.
1726 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1728 while ( $line = array_shift( $del ) ) {
1729 $aline = array_shift( $add );
1730 echo '<tr>' . $this->deletedLine( $line ) .
1731 $this->addedLine( $aline ) . "</tr>\n";
1733 foreach ($add as $line) { # If any leftovers
1734 echo '<tr>' . $this->emptyLine() .
1735 $this->addedLine( $line ) . "</tr>\n";
1737 wfProfileOut( $fname );