682b42513083120fddc4ec0c3f0ff94c73c66afc
5 * @subpackage DifferenceEngine
12 * @subpackage DifferenceEngine
14 class DifferenceEngine
{
18 var $mOldid, $mNewid, $mTitle;
19 var $mOldtitle, $mNewtitle, $mPagetitle;
20 var $mOldtext, $mNewtext;
21 var $mOldPage, $mNewPage;
22 var $mRcidMarkPatrolled;
23 var $mOldRev, $mNewRev;
24 var $mRevisionsLoaded = false; // Have the revisions been loaded
25 var $mTextLoaded = 0; // How many text blobs have been loaded, 0, 1 or 2?
30 * @param $titleObj Title object that the diff is associated with
31 * @param $old Integer: old ID we want to show and diff with.
32 * @param $new String: either 'prev' or 'next'.
33 * @param $rcid Integer: ??? FIXME (default 0)
35 function DifferenceEngine( $titleObj = null, $old = 0, $new = 0, $rcid = 0 ) {
36 $this->mTitle
= $titleObj;
37 wfDebug("DifferenceEngine old '$old' new '$new' rcid '$rcid'\n");
39 if ( 'prev' === $new ) {
40 # Show diff between revision $old and the previous one.
41 # Get previous one from DB.
43 $this->mNewid
= intval($old);
45 $this->mOldid
= $this->mTitle
->getPreviousRevisionID( $this->mNewid
);
47 } elseif ( 'next' === $new ) {
48 # Show diff between revision $old and the previous one.
49 # Get previous one from DB.
51 $this->mOldid
= intval($old);
52 $this->mNewid
= $this->mTitle
->getNextRevisionID( $this->mOldid
);
53 if ( false === $this->mNewid
) {
54 # if no result, NewId points to the newest old revision. The only newer
55 # revision is cur, which is "0".
60 $this->mOldid
= intval($old);
61 $this->mNewid
= intval($new);
63 $this->mRcidMarkPatrolled
= intval($rcid); # force it to be an integer
66 function showDiffPage() {
67 global $wgUser, $wgOut, $wgContLang, $wgUseExternalEditor, $wgUseRCPatrol;
68 $fname = 'DifferenceEngine::showDiffPage';
69 wfProfileIn( $fname );
71 # If external diffs are enabled both globally and for the user,
72 # we'll use the application/x-external-editor interface to call
73 # an external diff tool like kompare, kdiff3, etc.
74 if($wgUseExternalEditor && $wgUser->getOption('externaldiff')) {
75 global $wgInputEncoding,$wgServer,$wgScript,$wgLang;
77 header ( "Content-type: application/x-external-editor; charset=".$wgInputEncoding );
78 $url1=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mOldid
);
79 $url2=$this->mTitle
->getFullURL("action=raw&oldid=".$this->mNewid
);
80 $special=$wgLang->getNsText(NS_SPECIAL
);
85 Script={$wgServer}{$wgScript}
86 Special namespace={$special}
100 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
102 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
104 $wgOut->setArticleFlag( false );
105 if ( ! $this->loadRevisionData() ) {
106 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
107 $wgOut->addWikitext( $mtext );
108 wfProfileOut( $fname );
112 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
114 if ( $this->mNewRev
->isCurrent() ) {
115 $wgOut->setArticleFlag( true );
118 # mOldid is false if the difference engine is called with a "vague" query for
119 # a diff between a version V and its previous version V' AND the version V
120 # is the first version of that article. In that case, V' does not exist.
121 if ( $this->mOldid
=== false ) {
122 $this->showFirstRevision();
123 wfProfileOut( $fname );
127 $wgOut->suppressQuickbar();
129 $oldTitle = $this->mOldPage
->getPrefixedText();
130 $newTitle = $this->mNewPage
->getPrefixedText();
131 if( $oldTitle == $newTitle ) {
132 $wgOut->setPageTitle( $newTitle );
134 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
136 $wgOut->setSubtitle( wfMsg( 'difference' ) );
137 $wgOut->setRobotpolicy( 'noindex,nofollow' );
139 if ( !( $this->mOldPage
->userCanRead() && $this->mNewPage
->userCanRead() ) ) {
140 $wgOut->loginToUse();
142 wfProfileOut( $fname );
146 $sk = $wgUser->getSkin();
148 if ( $this->mNewRev
->isCurrent() && $wgUser->isAllowed('rollback') ) {
149 $rollback = ' ' . $sk->generateRollback( $this->mNewRev
);
153 if( $wgUseRCPatrol && $this->mRcidMarkPatrolled
!= 0 && $wgUser->isAllowed( 'patrol' ) ) {
154 $patrol = ' [' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'markaspatrolleddiff' ), "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']';
159 $prevlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'previousdiff' ),
160 'diff=prev&oldid='.$this->mOldid
, '', '', 'id="differences-prevlink"' );
161 if ( $this->mNewRev
->isCurrent() ) {
162 $nextlink = ' ';
164 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ),
165 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
171 if ($this->mOldRev
->mMinorEdit
== 1) {
172 $oldminor = wfElement( 'span', array( 'class' => 'minor' ),
173 wfMsg( 'minoreditletter') ) . ' ';
176 if ($this->mNewRev
->mMinorEdit
== 1) {
177 $newminor = wfElement( 'span', array( 'class' => 'minor' ),
178 wfMsg( 'minoreditletter') ) . ' ';
181 $oldHeader = "<strong>{$this->mOldtitle}</strong><br />" .
182 $sk->revUserTools( $this->mOldRev
) . "<br />" .
183 $oldminor . $sk->revComment( $this->mOldRev
, true ) . "<br />" .
185 $newHeader = "<strong>{$this->mNewtitle}</strong><br />" .
186 $sk->revUserTools( $this->mNewRev
) . " $rollback<br />" .
187 $newminor . $sk->revComment( $this->mNewRev
, true ) . "<br />" .
190 $this->showDiff( $oldHeader, $newHeader );
191 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
193 if( !$this->mNewRev
->isCurrent() ) {
194 $oldEditSectionSetting = $wgOut->parserOptions()->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->parserOptions()->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() {
308 $fname = 'DifferenceEngine::getDiffBody';
309 wfProfileIn( $fname );
313 if ( $this->mOldid
&& $this->mNewid
) {
315 $key = wfMemcKey( '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 wfProfileIn( "$fname-dl" );
375 @dl
('php_wikidiff2.so');
376 wfProfileOut( "$fname-dl" );
378 if ( function_exists( 'wikidiff2_do_diff' ) ) {
379 wfProfileIn( 'wikidiff2_do_diff' );
380 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
381 wfProfileOut( 'wikidiff2_do_diff' );
385 if ( $wgExternalDiffEngine !== false ) {
387 global $wgTmpDirectory;
388 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
389 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
391 $tempFile1 = fopen( $tempName1, "w" );
393 wfProfileOut( $fname );
396 $tempFile2 = fopen( $tempName2, "w" );
398 wfProfileOut( $fname );
401 fwrite( $tempFile1, $otext );
402 fwrite( $tempFile2, $ntext );
403 fclose( $tempFile1 );
404 fclose( $tempFile2 );
405 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
406 wfProfileIn( "$fname-shellexec" );
407 $difftext = wfShellExec( $cmd );
408 wfProfileOut( "$fname-shellexec" );
409 unlink( $tempName1 );
410 unlink( $tempName2 );
415 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
416 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
417 $diffs = new Diff( $ota, $nta );
418 $formatter = new TableDiffFormatter();
419 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
424 * Replace line numbers with the text in the user's language
426 function localiseLineNumbers( $text ) {
427 return preg_replace_callback( '/<!--LINE (\d+)-->/',
428 array( &$this, 'localiseLineNumbersCb' ), $text );
431 function localiseLineNumbersCb( $matches ) {
433 return wfMsgExt( 'lineno', array('parseinline'), $wgLang->formatNum( $matches[1] ) );
437 * Add the header to a diff body
439 function addHeader( $diff, $otitle, $ntitle ) {
441 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
443 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
444 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
453 * Use specified text instead of loading from the database
455 function setText( $oldText, $newText ) {
456 $this->mOldtext
= $oldText;
457 $this->mNewtext
= $newText;
458 $this->mTextLoaded
= 2;
462 * Load revision metadata for the specified articles. If newid is 0, then compare
463 * the old article in oldid to the current article; if oldid is 0, then
464 * compare the current article to the immediately previous one (ignoring the
467 * If oldid is false, leave the corresponding revision object set
468 * to false. This is impossible via ordinary user input, and is provided for
471 function loadRevisionData() {
473 if ( $this->mRevisionsLoaded
) {
476 // Whether it succeeds or fails, we don't want to try again
477 $this->mRevisionsLoaded
= true;
480 // Load the new revision object
481 if( $this->mNewid
) {
482 $this->mNewRev
= Revision
::newFromId( $this->mNewid
);
484 $this->mNewRev
= Revision
::newFromTitle( $this->mTitle
);
487 if( is_null( $this->mNewRev
) ) {
491 // Set assorted variables
492 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
493 $this->mNewPage
= $this->mNewRev
->getTitle();
494 if( $this->mNewRev
->isCurrent() ) {
495 $newLink = $this->mNewPage
->escapeLocalUrl();
496 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
497 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit' );
499 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a> ($timestamp)</strong>"
500 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
503 $newLink = $this->mNewPage
->escapeLocalUrl( 'oldid=' . $this->mNewid
);
504 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mNewid
);
505 $newUndo = $this->mNewPage
->escapeLocalUrl( 'action=edit&undo=' . $this->mNewid
);
506 $this->mPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $timestamp ) );
508 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a></strong>"
509 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)"
510 . " (<a href='$newUndo'>" . htmlspecialchars( wfMsg( 'editundo' ) ) . "</a>)";
513 // Load the old revision object
514 $this->mOldRev
= false;
515 if( $this->mOldid
) {
516 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
517 } elseif ( $this->mOldid
=== 0 ) {
518 $rev = $this->mNewRev
->getPrevious();
520 $this->mOldid
= $rev->getId();
521 $this->mOldRev
= $rev;
523 // No previous revision; mark to show as first-version only.
524 $this->mOldid
= false;
525 $this->mOldRev
= false;
527 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
529 if( is_null( $this->mOldRev
) ) {
533 if ( $this->mOldRev
) {
534 $this->mOldPage
= $this->mOldRev
->getTitle();
536 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
537 $oldLink = $this->mOldPage
->escapeLocalUrl( 'oldid=' . $this->mOldid
);
538 $oldEdit = $this->mOldPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mOldid
);
539 $this->mOldtitle
= "<strong><a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) )
540 . "</a></strong> (<a href='$oldEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
547 * Load the text of the revisions, as well as revision data.
549 function loadText() {
550 if ( $this->mTextLoaded
== 2 ) {
553 // Whether it succeeds or fails, we don't want to try again
554 $this->mTextLoaded
= 2;
557 if ( !$this->loadRevisionData() ) {
560 if ( $this->mOldRev
) {
561 // FIXME: permission tests
562 $this->mOldtext
= $this->mOldRev
->getText();
563 if ( $this->mOldtext
=== false ) {
567 if ( $this->mNewRev
) {
568 $this->mNewtext
= $this->mNewRev
->getText();
569 if ( $this->mNewtext
=== false ) {
577 * Load the text of the new revision, not the old one
579 function loadNewText() {
580 if ( $this->mTextLoaded
>= 1 ) {
583 $this->mTextLoaded
= 1;
585 if ( !$this->loadRevisionData() ) {
588 $this->mNewtext
= $this->mNewRev
->getText();
595 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
597 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
598 // You may copy this code freely under the conditions of the GPL.
601 define('USE_ASSERTS', function_exists('assert'));
607 * @subpackage DifferenceEngine
615 trigger_error('pure virtual', E_USER_ERROR
);
619 return $this->orig ?
sizeof($this->orig
) : 0;
622 function nclosing() {
623 return $this->closing ?
sizeof($this->closing
) : 0;
631 * @subpackage DifferenceEngine
633 class _DiffOp_Copy
extends _DiffOp
{
636 function _DiffOp_Copy ($orig, $closing = false) {
637 if (!is_array($closing))
640 $this->closing
= $closing;
644 return new _DiffOp_Copy($this->closing
, $this->orig
);
652 * @subpackage DifferenceEngine
654 class _DiffOp_Delete
extends _DiffOp
{
655 var $type = 'delete';
657 function _DiffOp_Delete ($lines) {
658 $this->orig
= $lines;
659 $this->closing
= false;
663 return new _DiffOp_Add($this->orig
);
671 * @subpackage DifferenceEngine
673 class _DiffOp_Add
extends _DiffOp
{
676 function _DiffOp_Add ($lines) {
677 $this->closing
= $lines;
682 return new _DiffOp_Delete($this->closing
);
690 * @subpackage DifferenceEngine
692 class _DiffOp_Change
extends _DiffOp
{
693 var $type = 'change';
695 function _DiffOp_Change ($orig, $closing) {
697 $this->closing
= $closing;
701 return new _DiffOp_Change($this->closing
, $this->orig
);
707 * Class used internally by Diff to actually compute the diffs.
709 * The algorithm used here is mostly lifted from the perl module
710 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
711 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
713 * More ideas are taken from:
714 * http://www.ics.uci.edu/~eppstein/161/960229.html
716 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
717 * diffutils-2.7, which can be found at:
718 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
720 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
723 * Line length limits for robustness added by Tim Starling, 2005-08-31
725 * @author Geoffrey T. Dairiki, Tim Starling
728 * @subpackage DifferenceEngine
732 const MAX_XREF_LENGTH
= 10000;
734 function diff ($from_lines, $to_lines) {
735 $fname = '_DiffEngine::diff';
736 wfProfileIn( $fname );
738 $n_from = sizeof($from_lines);
739 $n_to = sizeof($to_lines);
741 $this->xchanged
= $this->ychanged
= array();
742 $this->xv
= $this->yv
= array();
743 $this->xind
= $this->yind
= array();
745 unset($this->in_seq
);
748 // Skip leading common lines.
749 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
750 if ($from_lines[$skip] !== $to_lines[$skip])
752 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
754 // Skip trailing common lines.
755 $xi = $n_from; $yi = $n_to;
756 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
757 if ($from_lines[$xi] !== $to_lines[$yi])
759 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
762 // Ignore lines which do not exist in both files.
763 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
764 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
767 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
768 $line = $to_lines[$yi];
769 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
771 $yhash[$this->_line_hash($line)] = 1;
775 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
776 $line = $from_lines[$xi];
777 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
784 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
786 // Merge edits when possible
787 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
788 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
790 // Compute the edit operations.
793 while ($xi < $n_from ||
$yi < $n_to) {
794 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
795 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
797 // Skip matching "snake".
799 while ( $xi < $n_from && $yi < $n_to
800 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
801 $copy[] = $from_lines[$xi++
];
805 $edits[] = new _DiffOp_Copy($copy);
807 // Find deletes & adds.
809 while ($xi < $n_from && $this->xchanged
[$xi])
810 $delete[] = $from_lines[$xi++
];
813 while ($yi < $n_to && $this->ychanged
[$yi])
814 $add[] = $to_lines[$yi++
];
817 $edits[] = new _DiffOp_Change($delete, $add);
819 $edits[] = new _DiffOp_Delete($delete);
821 $edits[] = new _DiffOp_Add($add);
823 wfProfileOut( $fname );
828 * Returns the whole line if it's small enough, or the MD5 hash otherwise
830 function _line_hash( $line ) {
831 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
839 /* Divide the Largest Common Subsequence (LCS) of the sequences
840 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
843 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
844 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
845 * sub sequences. The first sub-sequence is contained in [X0, X1),
846 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
847 * that (X0, Y0) == (XOFF, YOFF) and
848 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
850 * This function assumes that the first lines of the specified portions
851 * of the two files do not match, and likewise that the last lines do not
852 * match. The caller must trim matching lines from the beginning and end
853 * of the portions it is going to specify.
855 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
856 $fname = '_DiffEngine::_diag';
857 wfProfileIn( $fname );
860 if ($xlim - $xoff > $ylim - $yoff) {
861 // Things seems faster (I'm not sure I understand why)
862 // when the shortest sequence in X.
864 list ($xoff, $xlim, $yoff, $ylim)
865 = array( $yoff, $ylim, $xoff, $xlim);
869 for ($i = $ylim - 1; $i >= $yoff; $i--)
870 $ymatches[$this->xv
[$i]][] = $i;
872 for ($i = $ylim - 1; $i >= $yoff; $i--)
873 $ymatches[$this->yv
[$i]][] = $i;
876 $this->seq
[0]= $yoff - 1;
877 $this->in_seq
= array();
880 $numer = $xlim - $xoff +
$nchunks - 1;
882 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
883 wfProfileIn( "$fname-chunk" );
885 for ($i = 0; $i <= $this->lcs
; $i++
)
886 $ymids[$i][$chunk-1] = $this->seq
[$i];
888 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
889 for ( ; $x < $x1; $x++
) {
890 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
891 if (empty($ymatches[$line]))
893 $matches = $ymatches[$line];
895 while (list ($junk, $y) = each($matches))
896 if (empty($this->in_seq
[$y])) {
897 $k = $this->_lcs_pos($y);
898 USE_ASSERTS
&& assert($k > 0);
899 $ymids[$k] = $ymids[$k-1];
902 while (list ( /* $junk */, $y) = each($matches)) {
903 if ($y > $this->seq
[$k-1]) {
904 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
905 // Optimization: this is a common case:
906 // next match is just replacing previous match.
907 $this->in_seq
[$this->seq
[$k]] = false;
909 $this->in_seq
[$y] = 1;
910 } else if (empty($this->in_seq
[$y])) {
911 $k = $this->_lcs_pos($y);
912 USE_ASSERTS
&& assert($k > 0);
913 $ymids[$k] = $ymids[$k-1];
917 wfProfileOut( "$fname-chunk" );
920 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
921 $ymid = $ymids[$this->lcs
];
922 for ($n = 0; $n < $nchunks - 1; $n++
) {
923 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
925 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
927 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
929 wfProfileOut( $fname );
930 return array($this->lcs
, $seps);
933 function _lcs_pos ($ypos) {
934 $fname = '_DiffEngine::_lcs_pos';
935 wfProfileIn( $fname );
938 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
939 $this->seq
[++
$this->lcs
] = $ypos;
940 $this->in_seq
[$ypos] = 1;
941 wfProfileOut( $fname );
946 while ($beg < $end) {
947 $mid = (int)(($beg +
$end) / 2);
948 if ( $ypos > $this->seq
[$mid] )
954 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
956 $this->in_seq
[$this->seq
[$end]] = false;
957 $this->seq
[$end] = $ypos;
958 $this->in_seq
[$ypos] = 1;
959 wfProfileOut( $fname );
963 /* Find LCS of two sequences.
965 * The results are recorded in the vectors $this->{x,y}changed[], by
966 * storing a 1 in the element for each line that is an insertion
967 * or deletion (ie. is not in the LCS).
969 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
971 * Note that XLIM, YLIM are exclusive bounds.
972 * All line numbers are origin-0 and discarded lines are not counted.
974 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
975 $fname = '_DiffEngine::_compareseq';
976 wfProfileIn( $fname );
978 // Slide down the bottom initial diagonal.
979 while ($xoff < $xlim && $yoff < $ylim
980 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
985 // Slide up the top initial diagonal.
986 while ($xlim > $xoff && $ylim > $yoff
987 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
992 if ($xoff == $xlim ||
$yoff == $ylim)
995 // This is ad hoc but seems to work well.
996 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
997 //$nchunks = max(2,min(8,(int)$nchunks));
998 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
1000 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
1004 // X and Y sequences have no common subsequence:
1005 // mark all changed.
1006 while ($yoff < $ylim)
1007 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
1008 while ($xoff < $xlim)
1009 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
1011 // Use the partitions to split this problem into subproblems.
1014 while ($pt2 = next($seps)) {
1015 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1019 wfProfileOut( $fname );
1022 /* Adjust inserts/deletes of identical lines to join changes
1023 * as much as possible.
1025 * We do something when a run of changed lines include a
1026 * line at one end and has an excluded, identical line at the other.
1027 * We are free to choose which identical line is included.
1028 * `compareseq' usually chooses the one at the beginning,
1029 * but usually it is cleaner to consider the following identical line
1030 * to be the "change".
1032 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1034 function _shift_boundaries ($lines, &$changed, $other_changed) {
1035 $fname = '_DiffEngine::_shift_boundaries';
1036 wfProfileIn( $fname );
1040 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1041 $len = sizeof($lines);
1042 $other_len = sizeof($other_changed);
1046 * Scan forwards to find beginning of another run of changes.
1047 * Also keep track of the corresponding point in the other file.
1049 * Throughout this code, $i and $j are adjusted together so that
1050 * the first $i elements of $changed and the first $j elements
1051 * of $other_changed both contain the same number of zeros
1052 * (unchanged lines).
1053 * Furthermore, $j is always kept so that $j == $other_len or
1054 * $other_changed[$j] == false.
1056 while ($j < $other_len && $other_changed[$j])
1059 while ($i < $len && ! $changed[$i]) {
1060 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1062 while ($j < $other_len && $other_changed[$j])
1071 // Find the end of this run of changes.
1072 while (++
$i < $len && $changed[$i])
1077 * Record the length of this run of changes, so that
1078 * we can later determine whether the run has grown.
1080 $runlength = $i - $start;
1083 * Move the changed region back, so long as the
1084 * previous unchanged line matches the last changed one.
1085 * This merges with previous changed regions.
1087 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1088 $changed[--$start] = 1;
1089 $changed[--$i] = false;
1090 while ($start > 0 && $changed[$start - 1])
1092 USE_ASSERTS
&& assert('$j > 0');
1093 while ($other_changed[--$j])
1095 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1099 * Set CORRESPONDING to the end of the changed run, at the last
1100 * point where it corresponds to a changed run in the other file.
1101 * CORRESPONDING == LEN means no such point has been found.
1103 $corresponding = $j < $other_len ?
$i : $len;
1106 * Move the changed region forward, so long as the
1107 * first changed line matches the following unchanged one.
1108 * This merges with following changed regions.
1109 * Do this second, so that if there are no merges,
1110 * the changed region is moved forward as far as possible.
1112 while ($i < $len && $lines[$start] == $lines[$i]) {
1113 $changed[$start++
] = false;
1115 while ($i < $len && $changed[$i])
1118 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1120 if ($j < $other_len && $other_changed[$j]) {
1121 $corresponding = $i;
1122 while ($j < $other_len && $other_changed[$j])
1126 } while ($runlength != $i - $start);
1129 * If possible, move the fully-merged run of changes
1130 * back to a corresponding run in the other file.
1132 while ($corresponding < $i) {
1133 $changed[--$start] = 1;
1135 USE_ASSERTS
&& assert('$j > 0');
1136 while ($other_changed[--$j])
1138 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1141 wfProfileOut( $fname );
1146 * Class representing a 'diff' between two sequences of strings.
1149 * @package MediaWiki
1150 * @subpackage DifferenceEngine
1158 * Computes diff between sequences of strings.
1160 * @param $from_lines array An array of strings.
1161 * (Typically these are lines from a file.)
1162 * @param $to_lines array An array of strings.
1164 function Diff($from_lines, $to_lines) {
1165 $eng = new _DiffEngine
;
1166 $this->edits
= $eng->diff($from_lines, $to_lines);
1167 //$this->_check($from_lines, $to_lines);
1171 * Compute reversed Diff.
1175 * $diff = new Diff($lines1, $lines2);
1176 * $rev = $diff->reverse();
1177 * @return object A Diff object representing the inverse of the
1180 function reverse () {
1182 $rev->edits
= array();
1183 foreach ($this->edits
as $edit) {
1184 $rev->edits
[] = $edit->reverse();
1190 * Check for empty diff.
1192 * @return bool True iff two sequences were identical.
1194 function isEmpty () {
1195 foreach ($this->edits
as $edit) {
1196 if ($edit->type
!= 'copy')
1203 * Compute the length of the Longest Common Subsequence (LCS).
1205 * This is mostly for diagnostic purposed.
1207 * @return int The length of the LCS.
1211 foreach ($this->edits
as $edit) {
1212 if ($edit->type
== 'copy')
1213 $lcs +
= sizeof($edit->orig
);
1219 * Get the original set of lines.
1221 * This reconstructs the $from_lines parameter passed to the
1224 * @return array The original sequence of strings.
1229 foreach ($this->edits
as $edit) {
1231 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1237 * Get the closing set of lines.
1239 * This reconstructs the $to_lines parameter passed to the
1242 * @return array The sequence of strings.
1244 function closing() {
1247 foreach ($this->edits
as $edit) {
1249 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1255 * Check a Diff for validity.
1257 * This is here only for debugging purposes.
1259 function _check ($from_lines, $to_lines) {
1260 $fname = 'Diff::_check';
1261 wfProfileIn( $fname );
1262 if (serialize($from_lines) != serialize($this->orig()))
1263 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1264 if (serialize($to_lines) != serialize($this->closing()))
1265 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1267 $rev = $this->reverse();
1268 if (serialize($to_lines) != serialize($rev->orig()))
1269 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1270 if (serialize($from_lines) != serialize($rev->closing()))
1271 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1275 foreach ($this->edits
as $edit) {
1276 if ( $prevtype == $edit->type
)
1277 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1278 $prevtype = $edit->type
;
1281 $lcs = $this->lcs();
1282 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1283 wfProfileOut( $fname );
1291 * @package MediaWiki
1292 * @subpackage DifferenceEngine
1294 class MappedDiff
extends Diff
1299 * Computes diff between sequences of strings.
1301 * This can be used to compute things like
1302 * case-insensitve diffs, or diffs which ignore
1303 * changes in white-space.
1305 * @param $from_lines array An array of strings.
1306 * (Typically these are lines from a file.)
1308 * @param $to_lines array An array of strings.
1310 * @param $mapped_from_lines array This array should
1311 * have the same size number of elements as $from_lines.
1312 * The elements in $mapped_from_lines and
1313 * $mapped_to_lines are what is actually compared
1314 * when computing the diff.
1316 * @param $mapped_to_lines array This array should
1317 * have the same number of elements as $to_lines.
1319 function MappedDiff($from_lines, $to_lines,
1320 $mapped_from_lines, $mapped_to_lines) {
1321 $fname = 'MappedDiff::MappedDiff';
1322 wfProfileIn( $fname );
1324 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1325 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1327 $this->Diff($mapped_from_lines, $mapped_to_lines);
1330 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1331 $orig = &$this->edits
[$i]->orig
;
1332 if (is_array($orig)) {
1333 $orig = array_slice($from_lines, $xi, sizeof($orig));
1334 $xi +
= sizeof($orig);
1337 $closing = &$this->edits
[$i]->closing
;
1338 if (is_array($closing)) {
1339 $closing = array_slice($to_lines, $yi, sizeof($closing));
1340 $yi +
= sizeof($closing);
1343 wfProfileOut( $fname );
1348 * A class to format Diffs
1350 * This class formats the diff in classic diff format.
1351 * It is intended that this class be customized via inheritance,
1352 * to obtain fancier outputs.
1355 * @package MediaWiki
1356 * @subpackage DifferenceEngine
1361 * Number of leading context "lines" to preserve.
1363 * This should be left at zero for this class, but subclasses
1364 * may want to set this to other values.
1366 var $leading_context_lines = 0;
1369 * Number of trailing context "lines" to preserve.
1371 * This should be left at zero for this class, but subclasses
1372 * may want to set this to other values.
1374 var $trailing_context_lines = 0;
1379 * @param $diff object A Diff object.
1380 * @return string The formatted output.
1382 function format($diff) {
1383 $fname = 'DiffFormatter::format';
1384 wfProfileIn( $fname );
1390 $nlead = $this->leading_context_lines
;
1391 $ntrail = $this->trailing_context_lines
;
1393 $this->_start_diff();
1395 foreach ($diff->edits
as $edit) {
1396 if ($edit->type
== 'copy') {
1397 if (is_array($block)) {
1398 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1403 $context = array_slice($edit->orig
, 0, $ntrail);
1404 $block[] = new _DiffOp_Copy($context);
1406 $this->_block($x0, $ntrail +
$xi - $x0,
1407 $y0, $ntrail +
$yi - $y0,
1412 $context = $edit->orig
;
1415 if (! is_array($block)) {
1416 $context = array_slice($context, sizeof($context) - $nlead);
1417 $x0 = $xi - sizeof($context);
1418 $y0 = $yi - sizeof($context);
1421 $block[] = new _DiffOp_Copy($context);
1427 $xi +
= sizeof($edit->orig
);
1429 $yi +
= sizeof($edit->closing
);
1432 if (is_array($block))
1433 $this->_block($x0, $xi - $x0,
1437 $end = $this->_end_diff();
1438 wfProfileOut( $fname );
1442 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1443 $fname = 'DiffFormatter::_block';
1444 wfProfileIn( $fname );
1445 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1446 foreach ($edits as $edit) {
1447 if ($edit->type
== 'copy')
1448 $this->_context($edit->orig
);
1449 elseif ($edit->type
== 'add')
1450 $this->_added($edit->closing
);
1451 elseif ($edit->type
== 'delete')
1452 $this->_deleted($edit->orig
);
1453 elseif ($edit->type
== 'change')
1454 $this->_changed($edit->orig
, $edit->closing
);
1456 trigger_error('Unknown edit type', E_USER_ERROR
);
1458 $this->_end_block();
1459 wfProfileOut( $fname );
1462 function _start_diff() {
1466 function _end_diff() {
1467 $val = ob_get_contents();
1472 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1474 $xbeg .= "," . ($xbeg +
$xlen - 1);
1476 $ybeg .= "," . ($ybeg +
$ylen - 1);
1478 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1481 function _start_block($header) {
1485 function _end_block() {
1488 function _lines($lines, $prefix = ' ') {
1489 foreach ($lines as $line)
1490 echo "$prefix $line\n";
1493 function _context($lines) {
1494 $this->_lines($lines);
1497 function _added($lines) {
1498 $this->_lines($lines, '>');
1500 function _deleted($lines) {
1501 $this->_lines($lines, '<');
1504 function _changed($orig, $closing) {
1505 $this->_deleted($orig);
1507 $this->_added($closing);
1513 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1517 define('NBSP', ' '); // iso-8859-x non-breaking space.
1522 * @package MediaWiki
1523 * @subpackage DifferenceEngine
1525 class _HWLDF_WordAccumulator
{
1526 function _HWLDF_WordAccumulator () {
1527 $this->_lines
= array();
1533 function _flushGroup ($new_tag) {
1534 if ($this->_group
!== '') {
1535 if ($this->_tag
== 'mark')
1536 $this->_line
.= '<span class="diffchange">' .
1537 htmlspecialchars ( $this->_group
) . '</span>';
1539 $this->_line
.= htmlspecialchars ( $this->_group
);
1542 $this->_tag
= $new_tag;
1545 function _flushLine ($new_tag) {
1546 $this->_flushGroup($new_tag);
1547 if ($this->_line
!= '')
1548 array_push ( $this->_lines
, $this->_line
);
1550 # make empty lines visible by inserting an NBSP
1551 array_push ( $this->_lines
, NBSP
);
1555 function addWords ($words, $tag = '') {
1556 if ($tag != $this->_tag
)
1557 $this->_flushGroup($tag);
1559 foreach ($words as $word) {
1560 // new-line should only come as first char of word.
1563 if ($word[0] == "\n") {
1564 $this->_flushLine($tag);
1565 $word = substr($word, 1);
1567 assert(!strstr($word, "\n"));
1568 $this->_group
.= $word;
1572 function getLines() {
1573 $this->_flushLine('~done');
1574 return $this->_lines
;
1581 * @package MediaWiki
1582 * @subpackage DifferenceEngine
1584 class WordLevelDiff
extends MappedDiff
1586 const MAX_LINE_LENGTH
= 10000;
1588 function WordLevelDiff ($orig_lines, $closing_lines) {
1589 $fname = 'WordLevelDiff::WordLevelDiff';
1590 wfProfileIn( $fname );
1592 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1593 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1595 $this->MappedDiff($orig_words, $closing_words,
1596 $orig_stripped, $closing_stripped);
1597 wfProfileOut( $fname );
1600 function _split($lines) {
1601 $fname = 'WordLevelDiff::_split';
1602 wfProfileIn( $fname );
1605 $stripped = array();
1607 foreach ( $lines as $line ) {
1608 # If the line is too long, just pretend the entire line is one big word
1609 # This prevents resource exhaustion problems
1616 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1618 $stripped[] = $line;
1621 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1624 $words = array_merge( $words, $m[0] );
1625 $stripped = array_merge( $stripped, $m[1] );
1629 wfProfileOut( $fname );
1630 return array($words, $stripped);
1634 $fname = 'WordLevelDiff::orig';
1635 wfProfileIn( $fname );
1636 $orig = new _HWLDF_WordAccumulator
;
1638 foreach ($this->edits
as $edit) {
1639 if ($edit->type
== 'copy')
1640 $orig->addWords($edit->orig
);
1641 elseif ($edit->orig
)
1642 $orig->addWords($edit->orig
, 'mark');
1644 $lines = $orig->getLines();
1645 wfProfileOut( $fname );
1649 function closing () {
1650 $fname = 'WordLevelDiff::closing';
1651 wfProfileIn( $fname );
1652 $closing = new _HWLDF_WordAccumulator
;
1654 foreach ($this->edits
as $edit) {
1655 if ($edit->type
== 'copy')
1656 $closing->addWords($edit->closing
);
1657 elseif ($edit->closing
)
1658 $closing->addWords($edit->closing
, 'mark');
1660 $lines = $closing->getLines();
1661 wfProfileOut( $fname );
1667 * Wikipedia Table style diff formatter.
1670 * @package MediaWiki
1671 * @subpackage DifferenceEngine
1673 class TableDiffFormatter
extends DiffFormatter
1675 function TableDiffFormatter() {
1676 $this->leading_context_lines
= 2;
1677 $this->trailing_context_lines
= 2;
1680 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1681 $r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" .
1682 '<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n";
1686 function _start_block( $header ) {
1690 function _end_block() {
1693 function _lines( $lines, $prefix=' ', $color='white' ) {
1696 # HTML-escape parameter before calling this
1697 function addedLine( $line ) {
1698 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1701 # HTML-escape parameter before calling this
1702 function deletedLine( $line ) {
1703 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1706 # HTML-escape parameter before calling this
1707 function contextLine( $line ) {
1708 return "<td> </td><td class='diff-context'>{$line}</td>";
1711 function emptyLine() {
1712 return '<td colspan="2"> </td>';
1715 function _added( $lines ) {
1716 foreach ($lines as $line) {
1717 echo '<tr>' . $this->emptyLine() .
1718 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1722 function _deleted($lines) {
1723 foreach ($lines as $line) {
1724 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1725 $this->emptyLine() . "</tr>\n";
1729 function _context( $lines ) {
1730 foreach ($lines as $line) {
1732 $this->contextLine( htmlspecialchars ( $line ) ) .
1733 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1737 function _changed( $orig, $closing ) {
1738 $fname = 'TableDiffFormatter::_changed';
1739 wfProfileIn( $fname );
1741 $diff = new WordLevelDiff( $orig, $closing );
1742 $del = $diff->orig();
1743 $add = $diff->closing();
1745 # Notice that WordLevelDiff returns HTML-escaped output.
1746 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1748 while ( $line = array_shift( $del ) ) {
1749 $aline = array_shift( $add );
1750 echo '<tr>' . $this->deletedLine( $line ) .
1751 $this->addedLine( $aline ) . "</tr>\n";
1753 foreach ($add as $line) { # If any leftovers
1754 echo '<tr>' . $this->emptyLine() .
1755 $this->addedLine( $line ) . "</tr>\n";
1757 wfProfileOut( $fname );