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 $titleObj Title object that the diff is associated with
37 * @param $old Integer: old ID we want to show and diff with.
38 * @param $new String: either 'prev' or 'next'.
39 * @param $rcid Integer: ??? FIXME (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 );
118 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
120 if ( $this->mNewRev
->isCurrent() ) {
121 $wgOut->setArticleFlag( true );
124 # mOldid is false if the difference engine is called with a "vague" query for
125 # a diff between a version V and its previous version V' AND the version V
126 # is the first version of that article. In that case, V' does not exist.
127 if ( $this->mOldid
=== false ) {
128 $this->showFirstRevision();
129 wfProfileOut( $fname );
133 $wgOut->suppressQuickbar();
135 $oldTitle = $this->mOldPage
->getPrefixedText();
136 $newTitle = $this->mNewPage
->getPrefixedText();
137 if( $oldTitle == $newTitle ) {
138 $wgOut->setPageTitle( $newTitle );
140 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
142 $wgOut->setSubtitle( wfMsg( 'difference' ) );
143 $wgOut->setRobotpolicy( 'noindex,follow' );
145 if ( !( $this->mOldPage
->userCanRead() && $this->mNewPage
->userCanRead() ) ) {
146 $wgOut->loginToUse();
148 wfProfileOut( $fname );
152 $sk = $wgUser->getSkin();
153 $talk = $wgContLang->getNsText( NS_TALK
);
154 $contribs = wfMsg( 'contribslink' );
156 if ( $this->mNewRev
->isCurrent() && $wgUser->isAllowed('rollback') ) {
157 $username = $this->mNewRev
->getUserText();
158 $rollback = ' <strong>[' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'rollbacklink' ),
159 'action=rollback&from=' . urlencode( $username ) .
160 '&token=' . urlencode( $wgUser->editToken( array( $this->mTitle
->getPrefixedText(), $username ) ) ) ) .
165 if( $wgUseRCPatrol && $this->mRcidMarkPatrolled
!= 0 && $wgUser->isAllowed( 'patrol' ) ) {
166 $patrol = ' [' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'markaspatrolleddiff' ), "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']';
171 $prevlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'previousdiff' ),
172 'diff=prev&oldid='.$this->mOldid
, '', '', 'id="differences-prevlink"' );
173 if ( $this->mNewRev
->isCurrent() ) {
176 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ),
177 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
180 $oldHeader = "<strong>{$this->mOldtitle}</strong><br />" .
181 $sk->revUserTools( $this->mOldRev
) . "<br />" .
182 $sk->revComment( $this->mOldRev
) . "<br />" .
184 $newHeader = "<strong>{$this->mNewtitle}</strong><br />" .
185 $sk->revUserTools( $this->mNewRev
) . " $rollback<br />" .
186 $sk->revComment( $this->mNewRev
) . "<br />" .
189 $this->showDiff( $oldHeader, $newHeader );
190 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
192 if( !$this->mNewRev
->isCurrent() ) {
193 $oldEditSectionSetting = $wgOut->mParserOptions
->setEditSection( false );
196 $this->loadNewText();
197 if( is_object( $this->mNewRev
) ) {
198 $wgOut->setRevisionId( $this->mNewRev
->getId() );
200 $wgOut->addSecondaryWikiText( $this->mNewtext
);
202 if( !$this->mNewRev
->isCurrent() ) {
203 $wgOut->mParserOptions
->setEditSection( $oldEditSectionSetting );
206 wfProfileOut( $fname );
210 * Show the first revision of an article. Uses normal diff headers in
211 * contrast to normal "old revision" display style.
213 function showFirstRevision() {
214 global $wgOut, $wgUser;
216 $fname = 'DifferenceEngine::showFirstRevision';
217 wfProfileIn( $fname );
219 # Get article text from the DB
221 if ( ! $this->loadNewText() ) {
222 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
224 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
225 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
226 $wgOut->addWikitext( $mtext );
227 wfProfileOut( $fname );
230 if ( $this->mNewRev
->isCurrent() ) {
231 $wgOut->setArticleFlag( true );
234 # Check if user is allowed to look at this page. If not, bail out.
236 if ( !( $this->mTitle
->userCanRead() ) ) {
237 $wgOut->loginToUse();
239 wfProfileOut( $fname );
243 # Prepare the header box
245 $sk = $wgUser->getSkin();
247 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
248 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />" .
249 $sk->revUserTools( $this->mNewRev
) . "<br />" .
250 $sk->revComment( $this->mNewRev
) . "<br />" .
251 $nextlink . "</div>\n";
253 $wgOut->addHTML( $header );
255 $wgOut->setSubtitle( wfMsg( 'difference' ) );
256 $wgOut->setRobotpolicy( 'noindex,nofollow' );
259 # Show current revision
261 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
262 if( is_object( $this->mNewRev
) ) {
263 $wgOut->setRevisionId( $this->mNewRev
->getId() );
265 $wgOut->addSecondaryWikiText( $this->mNewtext
);
267 wfProfileOut( $fname );
271 * Get the diff text, send it to $wgOut
272 * Returns false if the diff could not be generated, otherwise returns true
274 function showDiff( $otitle, $ntitle ) {
276 $diff = $this->getDiff( $otitle, $ntitle );
277 if ( $diff === false ) {
278 $wgOut->addWikitext( wfMsg( 'missingarticle', "<nowiki>(fixme, bug)</nowiki>" ) );
281 $wgOut->addHTML( $diff );
287 * Get diff table, including header
288 * Note that the interface has changed, it's no longer static.
289 * Returns false on error
291 function getDiff( $otitle, $ntitle ) {
292 $body = $this->getDiffBody();
293 if ( $body === false ) {
296 return $this->addHeader( $body, $otitle, $ntitle );
301 * Get the diff table body, without header
303 * Returns false on error
305 function getDiffBody() {
306 global $wgMemc, $wgDBname;
307 $fname = 'DifferenceEngine::getDiffBody';
308 wfProfileIn( $fname );
312 if ( $this->mOldid
&& $this->mNewid
) {
314 $key = "$wgDBname:diff:oldid:{$this->mOldid}:newid:{$this->mNewid}";
315 $difftext = $wgMemc->get( $key );
317 wfIncrStats( 'diff_cache_hit' );
318 $difftext = $this->localiseLineNumbers( $difftext );
319 $difftext .= "\n<!-- diff cache key $key -->\n";
320 wfProfileOut( $fname );
325 if ( !$this->loadText() ) {
326 wfProfileOut( $fname );
330 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
332 // Save to cache for 7 days
333 if ( $key !== false && $difftext !== false ) {
334 wfIncrStats( 'diff_cache_miss' );
335 $wgMemc->set( $key, $difftext, 7*86400 );
337 wfIncrStats( 'diff_uncacheable' );
339 // Replace line numbers with the text in the user's language
340 if ( $difftext !== false ) {
341 $difftext = $this->localiseLineNumbers( $difftext );
343 wfProfileOut( $fname );
348 * Generate a diff, no caching
349 * $otext and $ntext must be already segmented
351 function generateDiffBody( $otext, $ntext ) {
352 global $wgExternalDiffEngine, $wgContLang;
353 $fname = 'DifferenceEngine::generateDiffBody';
355 $otext = str_replace( "\r\n", "\n", $otext );
356 $ntext = str_replace( "\r\n", "\n", $ntext );
358 if ( $wgExternalDiffEngine == 'wikidiff' ) {
359 # For historical reasons, external diff engine expects
360 # input text to be HTML-escaped already
361 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
362 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
363 if( !function_exists( 'wikidiff_do_diff' ) ) {
364 dl('php_wikidiff.so');
366 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) );
369 if ( $wgExternalDiffEngine == 'wikidiff2' ) {
370 # Better external diff engine, the 2 may some day be dropped
371 # This one does the escaping and segmenting itself
372 if ( !function_exists( 'wikidiff2_do_diff' ) ) {
373 wfProfileIn( "$fname-dl" );
374 @dl
('php_wikidiff2.so');
375 wfProfileOut( "$fname-dl" );
377 if ( function_exists( 'wikidiff2_do_diff' ) ) {
378 wfProfileIn( 'wikidiff2_do_diff' );
379 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
380 wfProfileOut( 'wikidiff2_do_diff' );
384 if ( $wgExternalDiffEngine !== false ) {
386 global $wgTmpDirectory;
387 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
388 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
390 $tempFile1 = fopen( $tempName1, "w" );
392 wfProfileOut( $fname );
395 $tempFile2 = fopen( $tempName2, "w" );
397 wfProfileOut( $fname );
400 fwrite( $tempFile1, $otext );
401 fwrite( $tempFile2, $ntext );
402 fclose( $tempFile1 );
403 fclose( $tempFile2 );
404 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
405 wfProfileIn( "$fname-shellexec" );
406 $difftext = wfShellExec( $cmd );
407 wfProfileOut( "$fname-shellexec" );
408 unlink( $tempName1 );
409 unlink( $tempName2 );
414 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
415 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
416 $diffs =& new Diff( $ota, $nta );
417 $formatter =& new TableDiffFormatter();
418 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
423 * Replace line numbers with the text in the user's language
425 function localiseLineNumbers( $text ) {
426 return preg_replace_callback( '/<!--LINE (\d+)-->/',
427 array( &$this, 'localiseLineNumbersCb' ), $text );
430 function localiseLineNumbersCb( $matches ) {
432 return wfMsgExt( 'lineno', array('parseinline'), $wgLang->formatNum( $matches[1] ) );
436 * Add the header to a diff body
438 function addHeader( $diff, $otitle, $ntitle ) {
440 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
442 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
443 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
452 * Use specified text instead of loading from the database
454 function setText( $oldText, $newText ) {
455 $this->mOldtext
= $oldText;
456 $this->mNewtext
= $newText;
457 $this->mTextLoaded
= 2;
461 * Load revision metadata for the specified articles. If newid is 0, then compare
462 * the old article in oldid to the current article; if oldid is 0, then
463 * compare the current article to the immediately previous one (ignoring the
466 * If oldid is false, leave the corresponding revision object set
467 * to false. This is impossible via ordinary user input, and is provided for
470 function loadRevisionData() {
472 if ( $this->mRevisionsLoaded
) {
475 // Whether it succeeds or fails, we don't want to try again
476 $this->mRevisionsLoaded
= true;
479 // Load the new revision object
480 if( $this->mNewid
) {
481 $this->mNewRev
= Revision
::newFromId( $this->mNewid
);
483 $this->mNewRev
= Revision
::newFromTitle( $this->mTitle
);
486 if( is_null( $this->mNewRev
) ) {
490 // Set assorted variables
491 if( $this->mNewRev
->isCurrent() ) {
492 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
493 $this->mNewPage
= $this->mTitle
;
494 $newLink = $this->mNewPage
->escapeLocalUrl();
495 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
497 $this->mNewPage
= $this->mNewRev
->getTitle();
498 $newLink = $this->mNewPage
->escapeLocalUrl ('oldid=' . $this->mNewid
);
499 $t = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
500 $this->mPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $t ) );
501 $this->mNewtitle
= "<a href='$newLink'>{$this->mPagetitle}</a>";
504 // Load the old revision object
505 $this->mOldRev
= false;
506 if( $this->mOldid
) {
507 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
508 } elseif ( $this->mOldid
=== 0 ) {
509 $rev = $this->mNewRev
->getPrevious();
511 $this->mOldid
= $rev->getId();
512 $this->mOldRev
= $rev;
514 // No previous revision; mark to show as first-version only.
515 $this->mOldid
= false;
516 $this->mOldRev
= false;
518 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
520 if( is_null( $this->mOldRev
) ) {
524 if ( $this->mOldRev
) {
525 $this->mOldPage
= $this->mOldRev
->getTitle();
527 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
528 $oldLink = $this->mOldPage
->escapeLocalUrl( 'oldid=' . $this->mOldid
);
529 $this->mOldtitle
= "<a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) ) . '</a>';
536 * Load the text of the revisions, as well as revision data.
538 function loadText() {
539 if ( $this->mTextLoaded
== 2 ) {
542 // Whether it succeeds or fails, we don't want to try again
543 $this->mTextLoaded
= 2;
546 if ( !$this->loadRevisionData() ) {
549 if ( $this->mOldRev
) {
550 // FIXME: permission tests
551 $this->mOldtext
= $this->mOldRev
->getText();
552 if ( $this->mOldtext
=== false ) {
556 if ( $this->mNewRev
) {
557 $this->mNewtext
= $this->mNewRev
->getText();
558 if ( $this->mNewtext
=== false ) {
566 * Load the text of the new revision, not the old one
568 function loadNewText() {
569 if ( $this->mTextLoaded
>= 1 ) {
572 $this->mTextLoaded
= 1;
574 if ( !$this->loadRevisionData() ) {
577 $this->mNewtext
= $this->mNewRev
->getText();
584 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
586 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
587 // You may copy this code freely under the conditions of the GPL.
590 define('USE_ASSERTS', function_exists('assert'));
596 * @subpackage DifferenceEngine
604 trigger_error('pure virtual', E_USER_ERROR
);
608 return $this->orig ?
sizeof($this->orig
) : 0;
611 function nclosing() {
612 return $this->closing ?
sizeof($this->closing
) : 0;
620 * @subpackage DifferenceEngine
622 class _DiffOp_Copy
extends _DiffOp
{
625 function _DiffOp_Copy ($orig, $closing = false) {
626 if (!is_array($closing))
629 $this->closing
= $closing;
633 return new _DiffOp_Copy($this->closing
, $this->orig
);
641 * @subpackage DifferenceEngine
643 class _DiffOp_Delete
extends _DiffOp
{
644 var $type = 'delete';
646 function _DiffOp_Delete ($lines) {
647 $this->orig
= $lines;
648 $this->closing
= false;
652 return new _DiffOp_Add($this->orig
);
660 * @subpackage DifferenceEngine
662 class _DiffOp_Add
extends _DiffOp
{
665 function _DiffOp_Add ($lines) {
666 $this->closing
= $lines;
671 return new _DiffOp_Delete($this->closing
);
679 * @subpackage DifferenceEngine
681 class _DiffOp_Change
extends _DiffOp
{
682 var $type = 'change';
684 function _DiffOp_Change ($orig, $closing) {
686 $this->closing
= $closing;
690 return new _DiffOp_Change($this->closing
, $this->orig
);
696 * Class used internally by Diff to actually compute the diffs.
698 * The algorithm used here is mostly lifted from the perl module
699 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
700 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
702 * More ideas are taken from:
703 * http://www.ics.uci.edu/~eppstein/161/960229.html
705 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
706 * diffutils-2.7, which can be found at:
707 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
709 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
712 * Line length limits for robustness added by Tim Starling, 2005-08-31
714 * @author Geoffrey T. Dairiki, Tim Starling
717 * @subpackage DifferenceEngine
721 function diff ($from_lines, $to_lines) {
722 $fname = '_DiffEngine::diff';
723 wfProfileIn( $fname );
725 $n_from = sizeof($from_lines);
726 $n_to = sizeof($to_lines);
728 $this->xchanged
= $this->ychanged
= array();
729 $this->xv
= $this->yv
= array();
730 $this->xind
= $this->yind
= array();
732 unset($this->in_seq
);
735 // Skip leading common lines.
736 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
737 if ($from_lines[$skip] !== $to_lines[$skip])
739 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
741 // Skip trailing common lines.
742 $xi = $n_from; $yi = $n_to;
743 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
744 if ($from_lines[$xi] !== $to_lines[$yi])
746 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
749 // Ignore lines which do not exist in both files.
750 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
751 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
754 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
755 $line = $to_lines[$yi];
756 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
758 $yhash[$this->_line_hash($line)] = 1;
762 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
763 $line = $from_lines[$xi];
764 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
771 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
773 // Merge edits when possible
774 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
775 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
777 // Compute the edit operations.
780 while ($xi < $n_from ||
$yi < $n_to) {
781 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
782 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
784 // Skip matching "snake".
786 while ( $xi < $n_from && $yi < $n_to
787 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
788 $copy[] = $from_lines[$xi++
];
792 $edits[] = new _DiffOp_Copy($copy);
794 // Find deletes & adds.
796 while ($xi < $n_from && $this->xchanged
[$xi])
797 $delete[] = $from_lines[$xi++
];
800 while ($yi < $n_to && $this->ychanged
[$yi])
801 $add[] = $to_lines[$yi++
];
804 $edits[] = new _DiffOp_Change($delete, $add);
806 $edits[] = new _DiffOp_Delete($delete);
808 $edits[] = new _DiffOp_Add($add);
810 wfProfileOut( $fname );
815 * Returns the whole line if it's small enough, or the MD5 hash otherwise
817 function _line_hash( $line ) {
818 if ( strlen( $line ) > MAX_DIFF_XREF_LENGTH
) {
826 /* Divide the Largest Common Subsequence (LCS) of the sequences
827 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
830 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
831 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
832 * sub sequences. The first sub-sequence is contained in [X0, X1),
833 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
834 * that (X0, Y0) == (XOFF, YOFF) and
835 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
837 * This function assumes that the first lines of the specified portions
838 * of the two files do not match, and likewise that the last lines do not
839 * match. The caller must trim matching lines from the beginning and end
840 * of the portions it is going to specify.
842 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
843 $fname = '_DiffEngine::_diag';
844 wfProfileIn( $fname );
847 if ($xlim - $xoff > $ylim - $yoff) {
848 // Things seems faster (I'm not sure I understand why)
849 // when the shortest sequence in X.
851 list ($xoff, $xlim, $yoff, $ylim)
852 = array( $yoff, $ylim, $xoff, $xlim);
856 for ($i = $ylim - 1; $i >= $yoff; $i--)
857 $ymatches[$this->xv
[$i]][] = $i;
859 for ($i = $ylim - 1; $i >= $yoff; $i--)
860 $ymatches[$this->yv
[$i]][] = $i;
863 $this->seq
[0]= $yoff - 1;
864 $this->in_seq
= array();
867 $numer = $xlim - $xoff +
$nchunks - 1;
869 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
870 wfProfileIn( "$fname-chunk" );
872 for ($i = 0; $i <= $this->lcs
; $i++
)
873 $ymids[$i][$chunk-1] = $this->seq
[$i];
875 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
876 for ( ; $x < $x1; $x++
) {
877 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
878 if (empty($ymatches[$line]))
880 $matches = $ymatches[$line];
882 while (list ($junk, $y) = each($matches))
883 if (empty($this->in_seq
[$y])) {
884 $k = $this->_lcs_pos($y);
885 USE_ASSERTS
&& assert($k > 0);
886 $ymids[$k] = $ymids[$k-1];
889 while (list ($junk, $y) = each($matches)) {
890 if ($y > $this->seq
[$k-1]) {
891 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
892 // Optimization: this is a common case:
893 // next match is just replacing previous match.
894 $this->in_seq
[$this->seq
[$k]] = false;
896 $this->in_seq
[$y] = 1;
897 } else if (empty($this->in_seq
[$y])) {
898 $k = $this->_lcs_pos($y);
899 USE_ASSERTS
&& assert($k > 0);
900 $ymids[$k] = $ymids[$k-1];
904 wfProfileOut( "$fname-chunk" );
907 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
908 $ymid = $ymids[$this->lcs
];
909 for ($n = 0; $n < $nchunks - 1; $n++
) {
910 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
912 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
914 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
916 wfProfileOut( $fname );
917 return array($this->lcs
, $seps);
920 function _lcs_pos ($ypos) {
921 $fname = '_DiffEngine::_lcs_pos';
922 wfProfileIn( $fname );
925 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
926 $this->seq
[++
$this->lcs
] = $ypos;
927 $this->in_seq
[$ypos] = 1;
928 wfProfileOut( $fname );
933 while ($beg < $end) {
934 $mid = (int)(($beg +
$end) / 2);
935 if ( $ypos > $this->seq
[$mid] )
941 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
943 $this->in_seq
[$this->seq
[$end]] = false;
944 $this->seq
[$end] = $ypos;
945 $this->in_seq
[$ypos] = 1;
946 wfProfileOut( $fname );
950 /* Find LCS of two sequences.
952 * The results are recorded in the vectors $this->{x,y}changed[], by
953 * storing a 1 in the element for each line that is an insertion
954 * or deletion (ie. is not in the LCS).
956 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
958 * Note that XLIM, YLIM are exclusive bounds.
959 * All line numbers are origin-0 and discarded lines are not counted.
961 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
962 $fname = '_DiffEngine::_compareseq';
963 wfProfileIn( $fname );
965 // Slide down the bottom initial diagonal.
966 while ($xoff < $xlim && $yoff < $ylim
967 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
972 // Slide up the top initial diagonal.
973 while ($xlim > $xoff && $ylim > $yoff
974 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
979 if ($xoff == $xlim ||
$yoff == $ylim)
982 // This is ad hoc but seems to work well.
983 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
984 //$nchunks = max(2,min(8,(int)$nchunks));
985 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
987 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
991 // X and Y sequences have no common subsequence:
993 while ($yoff < $ylim)
994 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
995 while ($xoff < $xlim)
996 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
998 // Use the partitions to split this problem into subproblems.
1001 while ($pt2 = next($seps)) {
1002 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1006 wfProfileOut( $fname );
1009 /* Adjust inserts/deletes of identical lines to join changes
1010 * as much as possible.
1012 * We do something when a run of changed lines include a
1013 * line at one end and has an excluded, identical line at the other.
1014 * We are free to choose which identical line is included.
1015 * `compareseq' usually chooses the one at the beginning,
1016 * but usually it is cleaner to consider the following identical line
1017 * to be the "change".
1019 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1021 function _shift_boundaries ($lines, &$changed, $other_changed) {
1022 $fname = '_DiffEngine::_shift_boundaries';
1023 wfProfileIn( $fname );
1027 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1028 $len = sizeof($lines);
1029 $other_len = sizeof($other_changed);
1033 * Scan forwards to find beginning of another run of changes.
1034 * Also keep track of the corresponding point in the other file.
1036 * Throughout this code, $i and $j are adjusted together so that
1037 * the first $i elements of $changed and the first $j elements
1038 * of $other_changed both contain the same number of zeros
1039 * (unchanged lines).
1040 * Furthermore, $j is always kept so that $j == $other_len or
1041 * $other_changed[$j] == false.
1043 while ($j < $other_len && $other_changed[$j])
1046 while ($i < $len && ! $changed[$i]) {
1047 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1049 while ($j < $other_len && $other_changed[$j])
1058 // Find the end of this run of changes.
1059 while (++
$i < $len && $changed[$i])
1064 * Record the length of this run of changes, so that
1065 * we can later determine whether the run has grown.
1067 $runlength = $i - $start;
1070 * Move the changed region back, so long as the
1071 * previous unchanged line matches the last changed one.
1072 * This merges with previous changed regions.
1074 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1075 $changed[--$start] = 1;
1076 $changed[--$i] = false;
1077 while ($start > 0 && $changed[$start - 1])
1079 USE_ASSERTS
&& assert('$j > 0');
1080 while ($other_changed[--$j])
1082 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1086 * Set CORRESPONDING to the end of the changed run, at the last
1087 * point where it corresponds to a changed run in the other file.
1088 * CORRESPONDING == LEN means no such point has been found.
1090 $corresponding = $j < $other_len ?
$i : $len;
1093 * Move the changed region forward, so long as the
1094 * first changed line matches the following unchanged one.
1095 * This merges with following changed regions.
1096 * Do this second, so that if there are no merges,
1097 * the changed region is moved forward as far as possible.
1099 while ($i < $len && $lines[$start] == $lines[$i]) {
1100 $changed[$start++
] = false;
1102 while ($i < $len && $changed[$i])
1105 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1107 if ($j < $other_len && $other_changed[$j]) {
1108 $corresponding = $i;
1109 while ($j < $other_len && $other_changed[$j])
1113 } while ($runlength != $i - $start);
1116 * If possible, move the fully-merged run of changes
1117 * back to a corresponding run in the other file.
1119 while ($corresponding < $i) {
1120 $changed[--$start] = 1;
1122 USE_ASSERTS
&& assert('$j > 0');
1123 while ($other_changed[--$j])
1125 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1128 wfProfileOut( $fname );
1133 * Class representing a 'diff' between two sequences of strings.
1136 * @package MediaWiki
1137 * @subpackage DifferenceEngine
1145 * Computes diff between sequences of strings.
1147 * @param $from_lines array An array of strings.
1148 * (Typically these are lines from a file.)
1149 * @param $to_lines array An array of strings.
1151 function Diff($from_lines, $to_lines) {
1152 $eng = new _DiffEngine
;
1153 $this->edits
= $eng->diff($from_lines, $to_lines);
1154 //$this->_check($from_lines, $to_lines);
1158 * Compute reversed Diff.
1162 * $diff = new Diff($lines1, $lines2);
1163 * $rev = $diff->reverse();
1164 * @return object A Diff object representing the inverse of the
1167 function reverse () {
1169 $rev->edits
= array();
1170 foreach ($this->edits
as $edit) {
1171 $rev->edits
[] = $edit->reverse();
1177 * Check for empty diff.
1179 * @return bool True iff two sequences were identical.
1181 function isEmpty () {
1182 foreach ($this->edits
as $edit) {
1183 if ($edit->type
!= 'copy')
1190 * Compute the length of the Longest Common Subsequence (LCS).
1192 * This is mostly for diagnostic purposed.
1194 * @return int The length of the LCS.
1198 foreach ($this->edits
as $edit) {
1199 if ($edit->type
== 'copy')
1200 $lcs +
= sizeof($edit->orig
);
1206 * Get the original set of lines.
1208 * This reconstructs the $from_lines parameter passed to the
1211 * @return array The original sequence of strings.
1216 foreach ($this->edits
as $edit) {
1218 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1224 * Get the closing set of lines.
1226 * This reconstructs the $to_lines parameter passed to the
1229 * @return array The sequence of strings.
1231 function closing() {
1234 foreach ($this->edits
as $edit) {
1236 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1242 * Check a Diff for validity.
1244 * This is here only for debugging purposes.
1246 function _check ($from_lines, $to_lines) {
1247 $fname = 'Diff::_check';
1248 wfProfileIn( $fname );
1249 if (serialize($from_lines) != serialize($this->orig()))
1250 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1251 if (serialize($to_lines) != serialize($this->closing()))
1252 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1254 $rev = $this->reverse();
1255 if (serialize($to_lines) != serialize($rev->orig()))
1256 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1257 if (serialize($from_lines) != serialize($rev->closing()))
1258 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1262 foreach ($this->edits
as $edit) {
1263 if ( $prevtype == $edit->type
)
1264 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1265 $prevtype = $edit->type
;
1268 $lcs = $this->lcs();
1269 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1270 wfProfileOut( $fname );
1278 * @package MediaWiki
1279 * @subpackage DifferenceEngine
1281 class MappedDiff
extends Diff
1286 * Computes diff between sequences of strings.
1288 * This can be used to compute things like
1289 * case-insensitve diffs, or diffs which ignore
1290 * changes in white-space.
1292 * @param $from_lines array An array of strings.
1293 * (Typically these are lines from a file.)
1295 * @param $to_lines array An array of strings.
1297 * @param $mapped_from_lines array This array should
1298 * have the same size number of elements as $from_lines.
1299 * The elements in $mapped_from_lines and
1300 * $mapped_to_lines are what is actually compared
1301 * when computing the diff.
1303 * @param $mapped_to_lines array This array should
1304 * have the same number of elements as $to_lines.
1306 function MappedDiff($from_lines, $to_lines,
1307 $mapped_from_lines, $mapped_to_lines) {
1308 $fname = 'MappedDiff::MappedDiff';
1309 wfProfileIn( $fname );
1311 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1312 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1314 $this->Diff($mapped_from_lines, $mapped_to_lines);
1317 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1318 $orig = &$this->edits
[$i]->orig
;
1319 if (is_array($orig)) {
1320 $orig = array_slice($from_lines, $xi, sizeof($orig));
1321 $xi +
= sizeof($orig);
1324 $closing = &$this->edits
[$i]->closing
;
1325 if (is_array($closing)) {
1326 $closing = array_slice($to_lines, $yi, sizeof($closing));
1327 $yi +
= sizeof($closing);
1330 wfProfileOut( $fname );
1335 * A class to format Diffs
1337 * This class formats the diff in classic diff format.
1338 * It is intended that this class be customized via inheritance,
1339 * to obtain fancier outputs.
1342 * @package MediaWiki
1343 * @subpackage DifferenceEngine
1348 * Number of leading context "lines" to preserve.
1350 * This should be left at zero for this class, but subclasses
1351 * may want to set this to other values.
1353 var $leading_context_lines = 0;
1356 * Number of trailing context "lines" to preserve.
1358 * This should be left at zero for this class, but subclasses
1359 * may want to set this to other values.
1361 var $trailing_context_lines = 0;
1366 * @param $diff object A Diff object.
1367 * @return string The formatted output.
1369 function format($diff) {
1370 $fname = 'DiffFormatter::format';
1371 wfProfileIn( $fname );
1377 $nlead = $this->leading_context_lines
;
1378 $ntrail = $this->trailing_context_lines
;
1380 $this->_start_diff();
1382 foreach ($diff->edits
as $edit) {
1383 if ($edit->type
== 'copy') {
1384 if (is_array($block)) {
1385 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1390 $context = array_slice($edit->orig
, 0, $ntrail);
1391 $block[] = new _DiffOp_Copy($context);
1393 $this->_block($x0, $ntrail +
$xi - $x0,
1394 $y0, $ntrail +
$yi - $y0,
1399 $context = $edit->orig
;
1402 if (! is_array($block)) {
1403 $context = array_slice($context, sizeof($context) - $nlead);
1404 $x0 = $xi - sizeof($context);
1405 $y0 = $yi - sizeof($context);
1408 $block[] = new _DiffOp_Copy($context);
1414 $xi +
= sizeof($edit->orig
);
1416 $yi +
= sizeof($edit->closing
);
1419 if (is_array($block))
1420 $this->_block($x0, $xi - $x0,
1424 $end = $this->_end_diff();
1425 wfProfileOut( $fname );
1429 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1430 $fname = 'DiffFormatter::_block';
1431 wfProfileIn( $fname );
1432 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1433 foreach ($edits as $edit) {
1434 if ($edit->type
== 'copy')
1435 $this->_context($edit->orig
);
1436 elseif ($edit->type
== 'add')
1437 $this->_added($edit->closing
);
1438 elseif ($edit->type
== 'delete')
1439 $this->_deleted($edit->orig
);
1440 elseif ($edit->type
== 'change')
1441 $this->_changed($edit->orig
, $edit->closing
);
1443 trigger_error('Unknown edit type', E_USER_ERROR
);
1445 $this->_end_block();
1446 wfProfileOut( $fname );
1449 function _start_diff() {
1453 function _end_diff() {
1454 $val = ob_get_contents();
1459 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1461 $xbeg .= "," . ($xbeg +
$xlen - 1);
1463 $ybeg .= "," . ($ybeg +
$ylen - 1);
1465 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1468 function _start_block($header) {
1472 function _end_block() {
1475 function _lines($lines, $prefix = ' ') {
1476 foreach ($lines as $line)
1477 echo "$prefix $line\n";
1480 function _context($lines) {
1481 $this->_lines($lines);
1484 function _added($lines) {
1485 $this->_lines($lines, '>');
1487 function _deleted($lines) {
1488 $this->_lines($lines, '<');
1491 function _changed($orig, $closing) {
1492 $this->_deleted($orig);
1494 $this->_added($closing);
1500 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1504 define('NBSP', ' '); // iso-8859-x non-breaking space.
1509 * @package MediaWiki
1510 * @subpackage DifferenceEngine
1512 class _HWLDF_WordAccumulator
{
1513 function _HWLDF_WordAccumulator () {
1514 $this->_lines
= array();
1520 function _flushGroup ($new_tag) {
1521 if ($this->_group
!== '') {
1522 if ($this->_tag
== 'mark')
1523 $this->_line
.= '<span class="diffchange">' .
1524 htmlspecialchars ( $this->_group
) . '</span>';
1526 $this->_line
.= htmlspecialchars ( $this->_group
);
1529 $this->_tag
= $new_tag;
1532 function _flushLine ($new_tag) {
1533 $this->_flushGroup($new_tag);
1534 if ($this->_line
!= '')
1535 array_push ( $this->_lines
, $this->_line
);
1537 # make empty lines visible by inserting an NBSP
1538 array_push ( $this->_lines
, NBSP
);
1542 function addWords ($words, $tag = '') {
1543 if ($tag != $this->_tag
)
1544 $this->_flushGroup($tag);
1546 foreach ($words as $word) {
1547 // new-line should only come as first char of word.
1550 if ($word[0] == "\n") {
1551 $this->_flushLine($tag);
1552 $word = substr($word, 1);
1554 assert(!strstr($word, "\n"));
1555 $this->_group
.= $word;
1559 function getLines() {
1560 $this->_flushLine('~done');
1561 return $this->_lines
;
1568 * @package MediaWiki
1569 * @subpackage DifferenceEngine
1571 class WordLevelDiff
extends MappedDiff
1573 function WordLevelDiff ($orig_lines, $closing_lines) {
1574 $fname = 'WordLevelDiff::WordLevelDiff';
1575 wfProfileIn( $fname );
1577 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1578 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1580 $this->MappedDiff($orig_words, $closing_words,
1581 $orig_stripped, $closing_stripped);
1582 wfProfileOut( $fname );
1585 function _split($lines) {
1586 $fname = 'WordLevelDiff::_split';
1587 wfProfileIn( $fname );
1590 $stripped = array();
1592 foreach ( $lines as $line ) {
1593 # If the line is too long, just pretend the entire line is one big word
1594 # This prevents resource exhaustion problems
1601 if ( strlen( $line ) > MAX_DIFF_LINE
) {
1603 $stripped[] = $line;
1605 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1608 $words = array_merge( $words, $m[0] );
1609 $stripped = array_merge( $stripped, $m[1] );
1613 wfProfileOut( $fname );
1614 return array($words, $stripped);
1618 $fname = 'WordLevelDiff::orig';
1619 wfProfileIn( $fname );
1620 $orig = new _HWLDF_WordAccumulator
;
1622 foreach ($this->edits
as $edit) {
1623 if ($edit->type
== 'copy')
1624 $orig->addWords($edit->orig
);
1625 elseif ($edit->orig
)
1626 $orig->addWords($edit->orig
, 'mark');
1628 $lines = $orig->getLines();
1629 wfProfileOut( $fname );
1633 function closing () {
1634 $fname = 'WordLevelDiff::closing';
1635 wfProfileIn( $fname );
1636 $closing = new _HWLDF_WordAccumulator
;
1638 foreach ($this->edits
as $edit) {
1639 if ($edit->type
== 'copy')
1640 $closing->addWords($edit->closing
);
1641 elseif ($edit->closing
)
1642 $closing->addWords($edit->closing
, 'mark');
1644 $lines = $closing->getLines();
1645 wfProfileOut( $fname );
1651 * Wikipedia Table style diff formatter.
1654 * @package MediaWiki
1655 * @subpackage DifferenceEngine
1657 class TableDiffFormatter
extends DiffFormatter
1659 function TableDiffFormatter() {
1660 $this->leading_context_lines
= 2;
1661 $this->trailing_context_lines
= 2;
1664 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1665 $r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" .
1666 '<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n";
1670 function _start_block( $header ) {
1674 function _end_block() {
1677 function _lines( $lines, $prefix=' ', $color='white' ) {
1680 # HTML-escape parameter before calling this
1681 function addedLine( $line ) {
1682 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1685 # HTML-escape parameter before calling this
1686 function deletedLine( $line ) {
1687 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1690 # HTML-escape parameter before calling this
1691 function contextLine( $line ) {
1692 return "<td> </td><td class='diff-context'>{$line}</td>";
1695 function emptyLine() {
1696 return '<td colspan="2"> </td>';
1699 function _added( $lines ) {
1700 foreach ($lines as $line) {
1701 echo '<tr>' . $this->emptyLine() .
1702 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1706 function _deleted($lines) {
1707 foreach ($lines as $line) {
1708 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1709 $this->emptyLine() . "</tr>\n";
1713 function _context( $lines ) {
1714 foreach ($lines as $line) {
1716 $this->contextLine( htmlspecialchars ( $line ) ) .
1717 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1721 function _changed( $orig, $closing ) {
1722 $fname = 'TableDiffFormatter::_changed';
1723 wfProfileIn( $fname );
1725 $diff = new WordLevelDiff( $orig, $closing );
1726 $del = $diff->orig();
1727 $add = $diff->closing();
1729 # Notice that WordLevelDiff returns HTML-escaped output.
1730 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1732 while ( $line = array_shift( $del ) ) {
1733 $aline = array_shift( $add );
1734 echo '<tr>' . $this->deletedLine( $line ) .
1735 $this->addedLine( $aline ) . "</tr>\n";
1737 foreach ($add as $line) { # If any leftovers
1738 echo '<tr>' . $this->emptyLine() .
1739 $this->addedLine( $line ) . "</tr>\n";
1741 wfProfileOut( $fname );