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, $wgUseExternalEditor, $wgUseRCPatrol;
74 $fname = 'DifferenceEngine::showDiffPage';
75 wfProfileIn( $fname );
77 # If external diffs are enabled both globally and for the user,
78 # we'll use the application/x-external-editor interface to call
79 # an external diff tool like kompare, kdiff3, etc.
80 if($wgUseExternalEditor && $wgUser->getOption('externaldiff')) {
81 global $wgInputEncoding,$wgServer,$wgScript,$wgLang;
83 header ( "Content-type: application/x-external-editor; charset=".$wgInputEncoding );
84 $url1=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mOldid
);
85 $url2=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mNewid
);
86 $special=$wgLang->getNsText(NS_SPECIAL
);
91 Script={$wgServer}{$wgScript}
92 Special namespace={$special}
106 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
108 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
110 $wgOut->setArticleFlag( false );
111 if ( ! $this->loadRevisionData() ) {
112 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
113 $wgOut->addWikitext( $mtext );
114 wfProfileOut( $fname );
117 if ( $this->mNewRev
->isCurrent() ) {
118 $wgOut->setArticleFlag( true );
121 # mOldid is false if the difference engine is called with a "vague" query for
122 # a diff between a version V and its previous version V' AND the version V
123 # is the first version of that article. In that case, V' does not exist.
124 if ( $this->mOldid
=== false ) {
125 $this->showFirstRevision();
126 wfProfileOut( $fname );
130 $wgOut->suppressQuickbar();
132 $oldTitle = $this->mOldPage
->getPrefixedText();
133 $newTitle = $this->mNewPage
->getPrefixedText();
134 if( $oldTitle == $newTitle ) {
135 $wgOut->setPageTitle( $newTitle );
137 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
139 $wgOut->setSubtitle( wfMsg( 'difference' ) );
140 $wgOut->setRobotpolicy( 'noindex,follow' );
142 if ( !( $this->mOldPage
->userCanRead() && $this->mNewPage
->userCanRead() ) ) {
143 $wgOut->loginToUse();
145 wfProfileOut( $fname );
149 $sk = $wgUser->getSkin();
150 $talk = $wgContLang->getNsText( NS_TALK
);
151 $contribs = wfMsg( 'contribslink' );
153 if ( $this->mNewRev
->isCurrent() && $wgUser->isAllowed('rollback') ) {
154 $username = $this->mNewRev
->getUserText();
155 $rollback = ' <strong>[' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'rollbacklink' ),
156 'action=rollback&from=' . urlencode( $username ) .
157 '&token=' . urlencode( $wgUser->editToken( array( $this->mTitle
->getPrefixedText(), $username ) ) ) ) .
162 if( $wgUseRCPatrol && $this->mRcidMarkPatrolled
!= 0 && $wgUser->isAllowed( 'patrol' ) ) {
163 $patrol = ' [' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'markaspatrolleddiff' ), "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']';
168 $prevlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'previousdiff' ),
169 'diff=prev&oldid='.$this->mOldid
, '', '', 'id="differences-prevlink"' );
170 if ( $this->mNewRev
->isCurrent() ) {
173 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ),
174 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
177 $oldHeader = "<strong>{$this->mOldtitle}</strong><br />" .
178 $sk->revUserTools( $this->mOldRev
) . "<br />" .
179 $sk->revComment( $this->mOldRev
) . "<br />" .
181 $newHeader = "<strong>{$this->mNewtitle}</strong><br />" .
182 $sk->revUserTools( $this->mNewRev
) . " $rollback<br />" .
183 $sk->revComment( $this->mNewRev
) . "<br />" .
186 $this->showDiff( $oldHeader, $newHeader );
187 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
189 if( !$this->mNewRev
->isCurrent() ) {
190 $oldEditSectionSetting = $wgOut->mParserOptions
->setEditSection( false );
193 $this->loadNewText();
194 if( is_object( $this->mNewRev
) ) {
195 $wgOut->setRevisionId( $this->mNewRev
->getId() );
197 $wgOut->addSecondaryWikiText( $this->mNewtext
);
199 if( !$this->mNewRev
->isCurrent() ) {
200 $wgOut->mParserOptions
->setEditSection( $oldEditSectionSetting );
203 wfProfileOut( $fname );
207 * Show the first revision of an article. Uses normal diff headers in
208 * contrast to normal "old revision" display style.
210 function showFirstRevision() {
211 global $wgOut, $wgUser;
213 $fname = 'DifferenceEngine::showFirstRevision';
214 wfProfileIn( $fname );
216 # Get article text from the DB
218 if ( ! $this->loadNewText() ) {
219 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
221 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
222 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
223 $wgOut->addWikitext( $mtext );
224 wfProfileOut( $fname );
227 if ( $this->mNewRev
->isCurrent() ) {
228 $wgOut->setArticleFlag( true );
231 # Check if user is allowed to look at this page. If not, bail out.
233 if ( !( $this->mTitle
->userCanRead() ) ) {
234 $wgOut->loginToUse();
236 wfProfileOut( $fname );
240 # Prepare the header box
242 $sk = $wgUser->getSkin();
244 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
245 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />" .
246 $sk->revUserTools( $this->mNewRev
) . "<br />" .
247 $sk->revComment( $this->mNewRev
) . "<br />" .
248 $nextlink . "</div>\n";
250 $wgOut->addHTML( $header );
252 $wgOut->setSubtitle( wfMsg( 'difference' ) );
253 $wgOut->setRobotpolicy( 'noindex,nofollow' );
256 # Show current revision
258 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
259 if( is_object( $this->mNewRev
) ) {
260 $wgOut->setRevisionId( $this->mNewRev
->getId() );
262 $wgOut->addSecondaryWikiText( $this->mNewtext
);
264 wfProfileOut( $fname );
268 * Get the diff text, send it to $wgOut
269 * Returns false if the diff could not be generated, otherwise returns true
271 function showDiff( $otitle, $ntitle ) {
273 $diff = $this->getDiff( $otitle, $ntitle );
274 if ( $diff === false ) {
275 $wgOut->addWikitext( wfMsg( 'missingarticle', "<nowiki>(fixme, bug)</nowiki>" ) );
278 $wgOut->addHTML( $diff );
284 * Get diff table, including header
285 * Note that the interface has changed, it's no longer static.
286 * Returns false on error
288 function getDiff( $otitle, $ntitle ) {
289 $body = $this->getDiffBody();
290 if ( $body === false ) {
293 return $this->addHeader( $body, $otitle, $ntitle );
298 * Get the diff table body, without header
300 * Returns false on error
302 function getDiffBody() {
303 global $wgMemc, $wgDBname;
304 $fname = 'DifferenceEngine::getDiffBody';
305 wfProfileIn( $fname );
309 if ( $this->mOldid
&& $this->mNewid
) {
311 $key = "$wgDBname:diff:oldid:{$this->mOldid}:newid:{$this->mNewid}";
312 $difftext = $wgMemc->get( $key );
314 wfIncrStats( 'diff_cache_hit' );
315 $difftext = $this->localiseLineNumbers( $difftext );
316 $difftext .= "\n<!-- diff cache key $key -->\n";
317 wfProfileOut( $fname );
322 if ( !$this->loadText() ) {
323 wfProfileOut( $fname );
327 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
329 // Save to cache for 7 days
330 if ( $key !== false && $difftext !== false ) {
331 wfIncrStats( 'diff_cache_miss' );
332 $wgMemc->set( $key, $difftext, 7*86400 );
334 wfIncrStats( 'diff_uncacheable' );
336 // Replace line numbers with the text in the user's language
337 if ( $difftext !== false ) {
338 $difftext = $this->localiseLineNumbers( $difftext );
340 wfProfileOut( $fname );
345 * Generate a diff, no caching
346 * $otext and $ntext must be already segmented
348 function generateDiffBody( $otext, $ntext ) {
349 global $wgExternalDiffEngine, $wgContLang;
350 $fname = 'DifferenceEngine::generateDiffBody';
352 $otext = str_replace( "\r\n", "\n", $otext );
353 $ntext = str_replace( "\r\n", "\n", $ntext );
355 if ( $wgExternalDiffEngine == 'wikidiff' ) {
356 # For historical reasons, external diff engine expects
357 # input text to be HTML-escaped already
358 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
359 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
360 if( !function_exists( 'wikidiff_do_diff' ) ) {
361 dl('php_wikidiff.so');
363 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) );
366 if ( $wgExternalDiffEngine == 'wikidiff2' ) {
367 # Better external diff engine, the 2 may some day be dropped
368 # This one does the escaping and segmenting itself
369 if ( !function_exists( 'wikidiff2_do_diff' ) ) {
370 wfProfileIn( "$fname-dl" );
371 @dl
('php_wikidiff2.so');
372 wfProfileOut( "$fname-dl" );
374 if ( function_exists( 'wikidiff2_do_diff' ) ) {
375 wfProfileIn( 'wikidiff2_do_diff' );
376 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
377 wfProfileOut( 'wikidiff2_do_diff' );
381 if ( $wgExternalDiffEngine !== false ) {
383 global $wgTmpDirectory;
384 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
385 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
387 $tempFile1 = fopen( $tempName1, "w" );
389 wfProfileOut( $fname );
392 $tempFile2 = fopen( $tempName2, "w" );
394 wfProfileOut( $fname );
397 fwrite( $tempFile1, $otext );
398 fwrite( $tempFile2, $ntext );
399 fclose( $tempFile1 );
400 fclose( $tempFile2 );
401 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
402 wfProfileIn( "$fname-shellexec" );
403 $difftext = wfShellExec( $cmd );
404 wfProfileOut( "$fname-shellexec" );
405 unlink( $tempName1 );
406 unlink( $tempName2 );
411 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
412 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
413 $diffs =& new Diff( $ota, $nta );
414 $formatter =& new TableDiffFormatter();
415 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
420 * Replace line numbers with the text in the user's language
422 function localiseLineNumbers( $text ) {
423 return preg_replace_callback( '/<!--LINE (\d+)-->/',
424 array( &$this, 'localiseLineNumbersCb' ), $text );
427 function localiseLineNumbersCb( $matches ) {
429 return wfMsg( 'lineno', $wgLang->formatNum( $matches[1] ) );
433 * Add the header to a diff body
435 function addHeader( $diff, $otitle, $ntitle ) {
437 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
439 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
440 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
449 * Use specified text instead of loading from the database
451 function setText( $oldText, $newText ) {
452 $this->mOldtext
= $oldText;
453 $this->mNewtext
= $newText;
454 $this->mTextLoaded
= 2;
458 * Load revision metadata for the specified articles. If newid is 0, then compare
459 * the old article in oldid to the current article; if oldid is 0, then
460 * compare the current article to the immediately previous one (ignoring the
463 * If oldid is false, leave the corresponding revision object set
464 * to false. This is impossible via ordinary user input, and is provided for
467 function loadRevisionData() {
469 if ( $this->mRevisionsLoaded
) {
472 // Whether it succeeds or fails, we don't want to try again
473 $this->mRevisionsLoaded
= true;
476 // Load the new revision object
477 if( $this->mNewid
) {
478 $this->mNewRev
= Revision
::newFromId( $this->mNewid
);
480 $this->mNewRev
= Revision
::newFromTitle( $this->mTitle
);
483 if( is_null( $this->mNewRev
) ) {
487 // Set assorted variables
488 if( $this->mNewRev
->isCurrent() ) {
489 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
490 $this->mNewPage
= $this->mTitle
;
491 $newLink = $this->mNewPage
->escapeLocalUrl();
492 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
494 $this->mNewPage
= $this->mNewRev
->getTitle();
495 $newLink = $this->mNewPage
->escapeLocalUrl ('oldid=' . $this->mNewid
);
496 $t = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
497 $this->mPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t ) );
498 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
501 // Load the old revision object
502 $this->mOldRev
= false;
503 if( $this->mOldid
) {
504 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
505 } elseif ( $this->mOldid
=== 0 ) {
506 $rev = $this->mNewRev
->getPrevious();
508 $this->mOldid
= $rev->getId();
509 $this->mOldRev
= $rev;
511 // No previous revision; mark to show as first-version only.
512 $this->mOldid
= false;
513 $this->mOldRev
= false;
515 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
517 if( is_null( $this->mOldRev
) ) {
521 if ( $this->mOldRev
) {
522 $this->mOldPage
= $this->mOldRev
->getTitle();
524 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
525 $oldLink = $this->mOldPage
->escapeLocalUrl( 'oldid=' . $this->mOldid
);
526 $this->mOldtitle
= "<a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) ) . '</a>';
533 * Load the text of the revisions, as well as revision data.
535 function loadText() {
536 if ( $this->mTextLoaded
== 2 ) {
539 // Whether it succeeds or fails, we don't want to try again
540 $this->mTextLoaded
= 2;
543 if ( !$this->loadRevisionData() ) {
546 if ( $this->mOldRev
) {
547 // FIXME: permission tests
548 $this->mOldtext
= $this->mOldRev
->getText();
549 if ( $this->mOldtext
=== false ) {
553 if ( $this->mNewRev
) {
554 $this->mNewtext
= $this->mNewRev
->getText();
555 if ( $this->mNewtext
=== false ) {
563 * Load the text of the new revision, not the old one
565 function loadNewText() {
566 if ( $this->mTextLoaded
>= 1 ) {
569 $this->mTextLoaded
= 1;
571 if ( !$this->loadRevisionData() ) {
574 $this->mNewtext
= $this->mNewRev
->getText();
581 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
583 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
584 // You may copy this code freely under the conditions of the GPL.
587 define('USE_ASSERTS', function_exists('assert'));
593 * @subpackage DifferenceEngine
601 trigger_error('pure virtual', E_USER_ERROR
);
605 return $this->orig ?
sizeof($this->orig
) : 0;
608 function nclosing() {
609 return $this->closing ?
sizeof($this->closing
) : 0;
617 * @subpackage DifferenceEngine
619 class _DiffOp_Copy
extends _DiffOp
{
622 function _DiffOp_Copy ($orig, $closing = false) {
623 if (!is_array($closing))
626 $this->closing
= $closing;
630 return new _DiffOp_Copy($this->closing
, $this->orig
);
638 * @subpackage DifferenceEngine
640 class _DiffOp_Delete
extends _DiffOp
{
641 var $type = 'delete';
643 function _DiffOp_Delete ($lines) {
644 $this->orig
= $lines;
645 $this->closing
= false;
649 return new _DiffOp_Add($this->orig
);
657 * @subpackage DifferenceEngine
659 class _DiffOp_Add
extends _DiffOp
{
662 function _DiffOp_Add ($lines) {
663 $this->closing
= $lines;
668 return new _DiffOp_Delete($this->closing
);
676 * @subpackage DifferenceEngine
678 class _DiffOp_Change
extends _DiffOp
{
679 var $type = 'change';
681 function _DiffOp_Change ($orig, $closing) {
683 $this->closing
= $closing;
687 return new _DiffOp_Change($this->closing
, $this->orig
);
693 * Class used internally by Diff to actually compute the diffs.
695 * The algorithm used here is mostly lifted from the perl module
696 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
697 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
699 * More ideas are taken from:
700 * http://www.ics.uci.edu/~eppstein/161/960229.html
702 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
703 * diffutils-2.7, which can be found at:
704 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
706 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
709 * Line length limits for robustness added by Tim Starling, 2005-08-31
711 * @author Geoffrey T. Dairiki, Tim Starling
714 * @subpackage DifferenceEngine
718 function diff ($from_lines, $to_lines) {
719 $fname = '_DiffEngine::diff';
720 wfProfileIn( $fname );
722 $n_from = sizeof($from_lines);
723 $n_to = sizeof($to_lines);
725 $this->xchanged
= $this->ychanged
= array();
726 $this->xv
= $this->yv
= array();
727 $this->xind
= $this->yind
= array();
729 unset($this->in_seq
);
732 // Skip leading common lines.
733 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
734 if ($from_lines[$skip] !== $to_lines[$skip])
736 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
738 // Skip trailing common lines.
739 $xi = $n_from; $yi = $n_to;
740 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
741 if ($from_lines[$xi] !== $to_lines[$yi])
743 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
746 // Ignore lines which do not exist in both files.
747 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
748 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
751 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
752 $line = $to_lines[$yi];
753 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
755 $yhash[$this->_line_hash($line)] = 1;
759 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
760 $line = $from_lines[$xi];
761 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
768 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
770 // Merge edits when possible
771 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
772 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
774 // Compute the edit operations.
777 while ($xi < $n_from ||
$yi < $n_to) {
778 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
779 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
781 // Skip matching "snake".
783 while ( $xi < $n_from && $yi < $n_to
784 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
785 $copy[] = $from_lines[$xi++
];
789 $edits[] = new _DiffOp_Copy($copy);
791 // Find deletes & adds.
793 while ($xi < $n_from && $this->xchanged
[$xi])
794 $delete[] = $from_lines[$xi++
];
797 while ($yi < $n_to && $this->ychanged
[$yi])
798 $add[] = $to_lines[$yi++
];
801 $edits[] = new _DiffOp_Change($delete, $add);
803 $edits[] = new _DiffOp_Delete($delete);
805 $edits[] = new _DiffOp_Add($add);
807 wfProfileOut( $fname );
812 * Returns the whole line if it's small enough, or the MD5 hash otherwise
814 function _line_hash( $line ) {
815 if ( strlen( $line ) > MAX_DIFF_XREF_LENGTH
) {
823 /* Divide the Largest Common Subsequence (LCS) of the sequences
824 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
827 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
828 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
829 * sub sequences. The first sub-sequence is contained in [X0, X1),
830 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
831 * that (X0, Y0) == (XOFF, YOFF) and
832 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
834 * This function assumes that the first lines of the specified portions
835 * of the two files do not match, and likewise that the last lines do not
836 * match. The caller must trim matching lines from the beginning and end
837 * of the portions it is going to specify.
839 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
840 $fname = '_DiffEngine::_diag';
841 wfProfileIn( $fname );
844 if ($xlim - $xoff > $ylim - $yoff) {
845 // Things seems faster (I'm not sure I understand why)
846 // when the shortest sequence in X.
848 list ($xoff, $xlim, $yoff, $ylim)
849 = array( $yoff, $ylim, $xoff, $xlim);
853 for ($i = $ylim - 1; $i >= $yoff; $i--)
854 $ymatches[$this->xv
[$i]][] = $i;
856 for ($i = $ylim - 1; $i >= $yoff; $i--)
857 $ymatches[$this->yv
[$i]][] = $i;
860 $this->seq
[0]= $yoff - 1;
861 $this->in_seq
= array();
864 $numer = $xlim - $xoff +
$nchunks - 1;
866 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
867 wfProfileIn( "$fname-chunk" );
869 for ($i = 0; $i <= $this->lcs
; $i++
)
870 $ymids[$i][$chunk-1] = $this->seq
[$i];
872 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
873 for ( ; $x < $x1; $x++
) {
874 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
875 if (empty($ymatches[$line]))
877 $matches = $ymatches[$line];
879 while (list ($junk, $y) = each($matches))
880 if (empty($this->in_seq
[$y])) {
881 $k = $this->_lcs_pos($y);
882 USE_ASSERTS
&& assert($k > 0);
883 $ymids[$k] = $ymids[$k-1];
886 while (list ($junk, $y) = each($matches)) {
887 if ($y > $this->seq
[$k-1]) {
888 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
889 // Optimization: this is a common case:
890 // next match is just replacing previous match.
891 $this->in_seq
[$this->seq
[$k]] = false;
893 $this->in_seq
[$y] = 1;
894 } else if (empty($this->in_seq
[$y])) {
895 $k = $this->_lcs_pos($y);
896 USE_ASSERTS
&& assert($k > 0);
897 $ymids[$k] = $ymids[$k-1];
901 wfProfileOut( "$fname-chunk" );
904 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
905 $ymid = $ymids[$this->lcs
];
906 for ($n = 0; $n < $nchunks - 1; $n++
) {
907 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
909 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
911 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
913 wfProfileOut( $fname );
914 return array($this->lcs
, $seps);
917 function _lcs_pos ($ypos) {
918 $fname = '_DiffEngine::_lcs_pos';
919 wfProfileIn( $fname );
922 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
923 $this->seq
[++
$this->lcs
] = $ypos;
924 $this->in_seq
[$ypos] = 1;
925 wfProfileOut( $fname );
930 while ($beg < $end) {
931 $mid = (int)(($beg +
$end) / 2);
932 if ( $ypos > $this->seq
[$mid] )
938 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
940 $this->in_seq
[$this->seq
[$end]] = false;
941 $this->seq
[$end] = $ypos;
942 $this->in_seq
[$ypos] = 1;
943 wfProfileOut( $fname );
947 /* Find LCS of two sequences.
949 * The results are recorded in the vectors $this->{x,y}changed[], by
950 * storing a 1 in the element for each line that is an insertion
951 * or deletion (ie. is not in the LCS).
953 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
955 * Note that XLIM, YLIM are exclusive bounds.
956 * All line numbers are origin-0 and discarded lines are not counted.
958 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
959 $fname = '_DiffEngine::_compareseq';
960 wfProfileIn( $fname );
962 // Slide down the bottom initial diagonal.
963 while ($xoff < $xlim && $yoff < $ylim
964 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
969 // Slide up the top initial diagonal.
970 while ($xlim > $xoff && $ylim > $yoff
971 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
976 if ($xoff == $xlim ||
$yoff == $ylim)
979 // This is ad hoc but seems to work well.
980 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
981 //$nchunks = max(2,min(8,(int)$nchunks));
982 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
984 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
988 // X and Y sequences have no common subsequence:
990 while ($yoff < $ylim)
991 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
992 while ($xoff < $xlim)
993 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
995 // Use the partitions to split this problem into subproblems.
998 while ($pt2 = next($seps)) {
999 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1003 wfProfileOut( $fname );
1006 /* Adjust inserts/deletes of identical lines to join changes
1007 * as much as possible.
1009 * We do something when a run of changed lines include a
1010 * line at one end and has an excluded, identical line at the other.
1011 * We are free to choose which identical line is included.
1012 * `compareseq' usually chooses the one at the beginning,
1013 * but usually it is cleaner to consider the following identical line
1014 * to be the "change".
1016 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1018 function _shift_boundaries ($lines, &$changed, $other_changed) {
1019 $fname = '_DiffEngine::_shift_boundaries';
1020 wfProfileIn( $fname );
1024 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1025 $len = sizeof($lines);
1026 $other_len = sizeof($other_changed);
1030 * Scan forwards to find beginning of another run of changes.
1031 * Also keep track of the corresponding point in the other file.
1033 * Throughout this code, $i and $j are adjusted together so that
1034 * the first $i elements of $changed and the first $j elements
1035 * of $other_changed both contain the same number of zeros
1036 * (unchanged lines).
1037 * Furthermore, $j is always kept so that $j == $other_len or
1038 * $other_changed[$j] == false.
1040 while ($j < $other_len && $other_changed[$j])
1043 while ($i < $len && ! $changed[$i]) {
1044 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1046 while ($j < $other_len && $other_changed[$j])
1055 // Find the end of this run of changes.
1056 while (++
$i < $len && $changed[$i])
1061 * Record the length of this run of changes, so that
1062 * we can later determine whether the run has grown.
1064 $runlength = $i - $start;
1067 * Move the changed region back, so long as the
1068 * previous unchanged line matches the last changed one.
1069 * This merges with previous changed regions.
1071 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1072 $changed[--$start] = 1;
1073 $changed[--$i] = false;
1074 while ($start > 0 && $changed[$start - 1])
1076 USE_ASSERTS
&& assert('$j > 0');
1077 while ($other_changed[--$j])
1079 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1083 * Set CORRESPONDING to the end of the changed run, at the last
1084 * point where it corresponds to a changed run in the other file.
1085 * CORRESPONDING == LEN means no such point has been found.
1087 $corresponding = $j < $other_len ?
$i : $len;
1090 * Move the changed region forward, so long as the
1091 * first changed line matches the following unchanged one.
1092 * This merges with following changed regions.
1093 * Do this second, so that if there are no merges,
1094 * the changed region is moved forward as far as possible.
1096 while ($i < $len && $lines[$start] == $lines[$i]) {
1097 $changed[$start++
] = false;
1099 while ($i < $len && $changed[$i])
1102 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1104 if ($j < $other_len && $other_changed[$j]) {
1105 $corresponding = $i;
1106 while ($j < $other_len && $other_changed[$j])
1110 } while ($runlength != $i - $start);
1113 * If possible, move the fully-merged run of changes
1114 * back to a corresponding run in the other file.
1116 while ($corresponding < $i) {
1117 $changed[--$start] = 1;
1119 USE_ASSERTS
&& assert('$j > 0');
1120 while ($other_changed[--$j])
1122 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1125 wfProfileOut( $fname );
1130 * Class representing a 'diff' between two sequences of strings.
1133 * @package MediaWiki
1134 * @subpackage DifferenceEngine
1142 * Computes diff between sequences of strings.
1144 * @param $from_lines array An array of strings.
1145 * (Typically these are lines from a file.)
1146 * @param $to_lines array An array of strings.
1148 function Diff($from_lines, $to_lines) {
1149 $eng = new _DiffEngine
;
1150 $this->edits
= $eng->diff($from_lines, $to_lines);
1151 //$this->_check($from_lines, $to_lines);
1155 * Compute reversed Diff.
1159 * $diff = new Diff($lines1, $lines2);
1160 * $rev = $diff->reverse();
1161 * @return object A Diff object representing the inverse of the
1164 function reverse () {
1166 $rev->edits
= array();
1167 foreach ($this->edits
as $edit) {
1168 $rev->edits
[] = $edit->reverse();
1174 * Check for empty diff.
1176 * @return bool True iff two sequences were identical.
1178 function isEmpty () {
1179 foreach ($this->edits
as $edit) {
1180 if ($edit->type
!= 'copy')
1187 * Compute the length of the Longest Common Subsequence (LCS).
1189 * This is mostly for diagnostic purposed.
1191 * @return int The length of the LCS.
1195 foreach ($this->edits
as $edit) {
1196 if ($edit->type
== 'copy')
1197 $lcs +
= sizeof($edit->orig
);
1203 * Get the original set of lines.
1205 * This reconstructs the $from_lines parameter passed to the
1208 * @return array The original sequence of strings.
1213 foreach ($this->edits
as $edit) {
1215 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1221 * Get the closing set of lines.
1223 * This reconstructs the $to_lines parameter passed to the
1226 * @return array The sequence of strings.
1228 function closing() {
1231 foreach ($this->edits
as $edit) {
1233 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1239 * Check a Diff for validity.
1241 * This is here only for debugging purposes.
1243 function _check ($from_lines, $to_lines) {
1244 $fname = 'Diff::_check';
1245 wfProfileIn( $fname );
1246 if (serialize($from_lines) != serialize($this->orig()))
1247 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1248 if (serialize($to_lines) != serialize($this->closing()))
1249 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1251 $rev = $this->reverse();
1252 if (serialize($to_lines) != serialize($rev->orig()))
1253 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1254 if (serialize($from_lines) != serialize($rev->closing()))
1255 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1259 foreach ($this->edits
as $edit) {
1260 if ( $prevtype == $edit->type
)
1261 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1262 $prevtype = $edit->type
;
1265 $lcs = $this->lcs();
1266 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1267 wfProfileOut( $fname );
1275 * @package MediaWiki
1276 * @subpackage DifferenceEngine
1278 class MappedDiff
extends Diff
1283 * Computes diff between sequences of strings.
1285 * This can be used to compute things like
1286 * case-insensitve diffs, or diffs which ignore
1287 * changes in white-space.
1289 * @param $from_lines array An array of strings.
1290 * (Typically these are lines from a file.)
1292 * @param $to_lines array An array of strings.
1294 * @param $mapped_from_lines array This array should
1295 * have the same size number of elements as $from_lines.
1296 * The elements in $mapped_from_lines and
1297 * $mapped_to_lines are what is actually compared
1298 * when computing the diff.
1300 * @param $mapped_to_lines array This array should
1301 * have the same number of elements as $to_lines.
1303 function MappedDiff($from_lines, $to_lines,
1304 $mapped_from_lines, $mapped_to_lines) {
1305 $fname = 'MappedDiff::MappedDiff';
1306 wfProfileIn( $fname );
1308 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1309 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1311 $this->Diff($mapped_from_lines, $mapped_to_lines);
1314 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1315 $orig = &$this->edits
[$i]->orig
;
1316 if (is_array($orig)) {
1317 $orig = array_slice($from_lines, $xi, sizeof($orig));
1318 $xi +
= sizeof($orig);
1321 $closing = &$this->edits
[$i]->closing
;
1322 if (is_array($closing)) {
1323 $closing = array_slice($to_lines, $yi, sizeof($closing));
1324 $yi +
= sizeof($closing);
1327 wfProfileOut( $fname );
1332 * A class to format Diffs
1334 * This class formats the diff in classic diff format.
1335 * It is intended that this class be customized via inheritance,
1336 * to obtain fancier outputs.
1339 * @package MediaWiki
1340 * @subpackage DifferenceEngine
1345 * Number of leading context "lines" to preserve.
1347 * This should be left at zero for this class, but subclasses
1348 * may want to set this to other values.
1350 var $leading_context_lines = 0;
1353 * Number of trailing context "lines" to preserve.
1355 * This should be left at zero for this class, but subclasses
1356 * may want to set this to other values.
1358 var $trailing_context_lines = 0;
1363 * @param $diff object A Diff object.
1364 * @return string The formatted output.
1366 function format($diff) {
1367 $fname = 'DiffFormatter::format';
1368 wfProfileIn( $fname );
1374 $nlead = $this->leading_context_lines
;
1375 $ntrail = $this->trailing_context_lines
;
1377 $this->_start_diff();
1379 foreach ($diff->edits
as $edit) {
1380 if ($edit->type
== 'copy') {
1381 if (is_array($block)) {
1382 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1387 $context = array_slice($edit->orig
, 0, $ntrail);
1388 $block[] = new _DiffOp_Copy($context);
1390 $this->_block($x0, $ntrail +
$xi - $x0,
1391 $y0, $ntrail +
$yi - $y0,
1396 $context = $edit->orig
;
1399 if (! is_array($block)) {
1400 $context = array_slice($context, sizeof($context) - $nlead);
1401 $x0 = $xi - sizeof($context);
1402 $y0 = $yi - sizeof($context);
1405 $block[] = new _DiffOp_Copy($context);
1411 $xi +
= sizeof($edit->orig
);
1413 $yi +
= sizeof($edit->closing
);
1416 if (is_array($block))
1417 $this->_block($x0, $xi - $x0,
1421 $end = $this->_end_diff();
1422 wfProfileOut( $fname );
1426 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1427 $fname = 'DiffFormatter::_block';
1428 wfProfileIn( $fname );
1429 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1430 foreach ($edits as $edit) {
1431 if ($edit->type
== 'copy')
1432 $this->_context($edit->orig
);
1433 elseif ($edit->type
== 'add')
1434 $this->_added($edit->closing
);
1435 elseif ($edit->type
== 'delete')
1436 $this->_deleted($edit->orig
);
1437 elseif ($edit->type
== 'change')
1438 $this->_changed($edit->orig
, $edit->closing
);
1440 trigger_error('Unknown edit type', E_USER_ERROR
);
1442 $this->_end_block();
1443 wfProfileOut( $fname );
1446 function _start_diff() {
1450 function _end_diff() {
1451 $val = ob_get_contents();
1456 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1458 $xbeg .= "," . ($xbeg +
$xlen - 1);
1460 $ybeg .= "," . ($ybeg +
$ylen - 1);
1462 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1465 function _start_block($header) {
1469 function _end_block() {
1472 function _lines($lines, $prefix = ' ') {
1473 foreach ($lines as $line)
1474 echo "$prefix $line\n";
1477 function _context($lines) {
1478 $this->_lines($lines);
1481 function _added($lines) {
1482 $this->_lines($lines, '>');
1484 function _deleted($lines) {
1485 $this->_lines($lines, '<');
1488 function _changed($orig, $closing) {
1489 $this->_deleted($orig);
1491 $this->_added($closing);
1497 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1501 define('NBSP', ' '); // iso-8859-x non-breaking space.
1506 * @package MediaWiki
1507 * @subpackage DifferenceEngine
1509 class _HWLDF_WordAccumulator
{
1510 function _HWLDF_WordAccumulator () {
1511 $this->_lines
= array();
1517 function _flushGroup ($new_tag) {
1518 if ($this->_group
!== '') {
1519 if ($this->_tag
== 'mark')
1520 $this->_line
.= '<span class="diffchange">' .
1521 htmlspecialchars ( $this->_group
) . '</span>';
1523 $this->_line
.= htmlspecialchars ( $this->_group
);
1526 $this->_tag
= $new_tag;
1529 function _flushLine ($new_tag) {
1530 $this->_flushGroup($new_tag);
1531 if ($this->_line
!= '')
1532 array_push ( $this->_lines
, $this->_line
);
1534 # make empty lines visible by inserting an NBSP
1535 array_push ( $this->_lines
, NBSP
);
1539 function addWords ($words, $tag = '') {
1540 if ($tag != $this->_tag
)
1541 $this->_flushGroup($tag);
1543 foreach ($words as $word) {
1544 // new-line should only come as first char of word.
1547 if ($word[0] == "\n") {
1548 $this->_flushLine($tag);
1549 $word = substr($word, 1);
1551 assert(!strstr($word, "\n"));
1552 $this->_group
.= $word;
1556 function getLines() {
1557 $this->_flushLine('~done');
1558 return $this->_lines
;
1565 * @package MediaWiki
1566 * @subpackage DifferenceEngine
1568 class WordLevelDiff
extends MappedDiff
1570 function WordLevelDiff ($orig_lines, $closing_lines) {
1571 $fname = 'WordLevelDiff::WordLevelDiff';
1572 wfProfileIn( $fname );
1574 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1575 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1577 $this->MappedDiff($orig_words, $closing_words,
1578 $orig_stripped, $closing_stripped);
1579 wfProfileOut( $fname );
1582 function _split($lines) {
1583 $fname = 'WordLevelDiff::_split';
1584 wfProfileIn( $fname );
1587 $stripped = array();
1589 foreach ( $lines as $line ) {
1590 # If the line is too long, just pretend the entire line is one big word
1591 # This prevents resource exhaustion problems
1598 if ( strlen( $line ) > MAX_DIFF_LINE
) {
1600 $stripped[] = $line;
1602 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1605 $words = array_merge( $words, $m[0] );
1606 $stripped = array_merge( $stripped, $m[1] );
1610 wfProfileOut( $fname );
1611 return array($words, $stripped);
1615 $fname = 'WordLevelDiff::orig';
1616 wfProfileIn( $fname );
1617 $orig = new _HWLDF_WordAccumulator
;
1619 foreach ($this->edits
as $edit) {
1620 if ($edit->type
== 'copy')
1621 $orig->addWords($edit->orig
);
1622 elseif ($edit->orig
)
1623 $orig->addWords($edit->orig
, 'mark');
1625 $lines = $orig->getLines();
1626 wfProfileOut( $fname );
1630 function closing () {
1631 $fname = 'WordLevelDiff::closing';
1632 wfProfileIn( $fname );
1633 $closing = new _HWLDF_WordAccumulator
;
1635 foreach ($this->edits
as $edit) {
1636 if ($edit->type
== 'copy')
1637 $closing->addWords($edit->closing
);
1638 elseif ($edit->closing
)
1639 $closing->addWords($edit->closing
, 'mark');
1641 $lines = $closing->getLines();
1642 wfProfileOut( $fname );
1648 * Wikipedia Table style diff formatter.
1651 * @package MediaWiki
1652 * @subpackage DifferenceEngine
1654 class TableDiffFormatter
extends DiffFormatter
1656 function TableDiffFormatter() {
1657 $this->leading_context_lines
= 2;
1658 $this->trailing_context_lines
= 2;
1661 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1662 $r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" .
1663 '<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n";
1667 function _start_block( $header ) {
1671 function _end_block() {
1674 function _lines( $lines, $prefix=' ', $color='white' ) {
1677 # HTML-escape parameter before calling this
1678 function addedLine( $line ) {
1679 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1682 # HTML-escape parameter before calling this
1683 function deletedLine( $line ) {
1684 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1687 # HTML-escape parameter before calling this
1688 function contextLine( $line ) {
1689 return "<td> </td><td class='diff-context'>{$line}</td>";
1692 function emptyLine() {
1693 return '<td colspan="2"> </td>';
1696 function _added( $lines ) {
1697 foreach ($lines as $line) {
1698 echo '<tr>' . $this->emptyLine() .
1699 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1703 function _deleted($lines) {
1704 foreach ($lines as $line) {
1705 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1706 $this->emptyLine() . "</tr>\n";
1710 function _context( $lines ) {
1711 foreach ($lines as $line) {
1713 $this->contextLine( htmlspecialchars ( $line ) ) .
1714 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1718 function _changed( $orig, $closing ) {
1719 $fname = 'TableDiffFormatter::_changed';
1720 wfProfileIn( $fname );
1722 $diff = new WordLevelDiff( $orig, $closing );
1723 $del = $diff->orig();
1724 $add = $diff->closing();
1726 # Notice that WordLevelDiff returns HTML-escaped output.
1727 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1729 while ( $line = array_shift( $del ) ) {
1730 $aline = array_shift( $add );
1731 echo '<tr>' . $this->deletedLine( $line ) .
1732 $this->addedLine( $aline ) . "</tr>\n";
1734 foreach ($add as $line) { # If any leftovers
1735 echo '<tr>' . $this->emptyLine() .
1736 $this->addedLine( $line ) . "</tr>\n";
1738 wfProfileOut( $fname );