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();
147 $talk = $wgContLang->getNsText( NS_TALK
);
148 $contribs = wfMsg( 'contribslink' );
150 if ( $this->mNewRev
->isCurrent() && $wgUser->isAllowed('rollback') ) {
151 $rollback = ' ' . $sk->generateRollback( $this->mNewRev
);
155 if( $wgUseRCPatrol && $this->mRcidMarkPatrolled
!= 0 && $wgUser->isAllowed( 'patrol' ) ) {
156 $patrol = ' [' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'markaspatrolleddiff' ), "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']';
161 $prevlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'previousdiff' ),
162 'diff=prev&oldid='.$this->mOldid
, '', '', 'id="differences-prevlink"' );
163 if ( $this->mNewRev
->isCurrent() ) {
164 $nextlink = ' ';
166 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ),
167 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
173 if ($this->mOldRev
->mMinorEdit
== 1) {
174 $oldminor = wfElement( 'span', array( 'class' => 'minor' ),
175 wfMsg( 'minoreditletter') ) . ' ';
178 if ($this->mNewRev
->mMinorEdit
== 1) {
179 $newminor = wfElement( 'span', array( 'class' => 'minor' ),
180 wfMsg( 'minoreditletter') ) . ' ';
183 $oldHeader = "<strong>{$this->mOldtitle}</strong><br />" .
184 $sk->revUserTools( $this->mOldRev
) . "<br />" .
185 $oldminor . $sk->revComment( $this->mOldRev
, true ) . "<br />" .
187 $newHeader = "<strong>{$this->mNewtitle}</strong><br />" .
188 $sk->revUserTools( $this->mNewRev
) . " $rollback<br />" .
189 $newminor . $sk->revComment( $this->mNewRev
, true ) . "<br />" .
192 $this->showDiff( $oldHeader, $newHeader );
193 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
195 if( !$this->mNewRev
->isCurrent() ) {
196 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
199 $this->loadNewText();
200 if( is_object( $this->mNewRev
) ) {
201 $wgOut->setRevisionId( $this->mNewRev
->getId() );
203 $wgOut->addSecondaryWikiText( $this->mNewtext
);
205 if( !$this->mNewRev
->isCurrent() ) {
206 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
209 wfProfileOut( $fname );
213 * Show the first revision of an article. Uses normal diff headers in
214 * contrast to normal "old revision" display style.
216 function showFirstRevision() {
217 global $wgOut, $wgUser;
219 $fname = 'DifferenceEngine::showFirstRevision';
220 wfProfileIn( $fname );
222 # Get article text from the DB
224 if ( ! $this->loadNewText() ) {
225 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
227 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
228 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
229 $wgOut->addWikitext( $mtext );
230 wfProfileOut( $fname );
233 if ( $this->mNewRev
->isCurrent() ) {
234 $wgOut->setArticleFlag( true );
237 # Check if user is allowed to look at this page. If not, bail out.
239 if ( !( $this->mTitle
->userCanRead() ) ) {
240 $wgOut->loginToUse();
242 wfProfileOut( $fname );
246 # Prepare the header box
248 $sk = $wgUser->getSkin();
250 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
251 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />" .
252 $sk->revUserTools( $this->mNewRev
) . "<br />" .
253 $sk->revComment( $this->mNewRev
) . "<br />" .
254 $nextlink . "</div>\n";
256 $wgOut->addHTML( $header );
258 $wgOut->setSubtitle( wfMsg( 'difference' ) );
259 $wgOut->setRobotpolicy( 'noindex,nofollow' );
262 # Show current revision
264 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
265 if( is_object( $this->mNewRev
) ) {
266 $wgOut->setRevisionId( $this->mNewRev
->getId() );
268 $wgOut->addSecondaryWikiText( $this->mNewtext
);
270 wfProfileOut( $fname );
274 * Get the diff text, send it to $wgOut
275 * Returns false if the diff could not be generated, otherwise returns true
277 function showDiff( $otitle, $ntitle ) {
279 $diff = $this->getDiff( $otitle, $ntitle );
280 if ( $diff === false ) {
281 $wgOut->addWikitext( wfMsg( 'missingarticle', "<nowiki>(fixme, bug)</nowiki>" ) );
284 $wgOut->addHTML( $diff );
290 * Get diff table, including header
291 * Note that the interface has changed, it's no longer static.
292 * Returns false on error
294 function getDiff( $otitle, $ntitle ) {
295 $body = $this->getDiffBody();
296 if ( $body === false ) {
299 return $this->addHeader( $body, $otitle, $ntitle );
304 * Get the diff table body, without header
306 * Returns false on error
308 function getDiffBody() {
310 $fname = 'DifferenceEngine::getDiffBody';
311 wfProfileIn( $fname );
315 if ( $this->mOldid
&& $this->mNewid
) {
317 $key = wfMemcKey( 'diff', 'oldid', $this->mOldid
, 'newid', $this->mNewid
);
318 $difftext = $wgMemc->get( $key );
320 wfIncrStats( 'diff_cache_hit' );
321 $difftext = $this->localiseLineNumbers( $difftext );
322 $difftext .= "\n<!-- diff cache key $key -->\n";
323 wfProfileOut( $fname );
328 if ( !$this->loadText() ) {
329 wfProfileOut( $fname );
333 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
335 // Save to cache for 7 days
336 if ( $key !== false && $difftext !== false ) {
337 wfIncrStats( 'diff_cache_miss' );
338 $wgMemc->set( $key, $difftext, 7*86400 );
340 wfIncrStats( 'diff_uncacheable' );
342 // Replace line numbers with the text in the user's language
343 if ( $difftext !== false ) {
344 $difftext = $this->localiseLineNumbers( $difftext );
346 wfProfileOut( $fname );
351 * Generate a diff, no caching
352 * $otext and $ntext must be already segmented
354 function generateDiffBody( $otext, $ntext ) {
355 global $wgExternalDiffEngine, $wgContLang;
356 $fname = 'DifferenceEngine::generateDiffBody';
358 $otext = str_replace( "\r\n", "\n", $otext );
359 $ntext = str_replace( "\r\n", "\n", $ntext );
361 if ( $wgExternalDiffEngine == 'wikidiff' ) {
362 # For historical reasons, external diff engine expects
363 # input text to be HTML-escaped already
364 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
365 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
366 if( !function_exists( 'wikidiff_do_diff' ) ) {
367 dl('php_wikidiff.so');
369 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) );
372 if ( $wgExternalDiffEngine == 'wikidiff2' ) {
373 # Better external diff engine, the 2 may some day be dropped
374 # This one does the escaping and segmenting itself
375 if ( !function_exists( 'wikidiff2_do_diff' ) ) {
376 wfProfileIn( "$fname-dl" );
377 @dl
('php_wikidiff2.so');
378 wfProfileOut( "$fname-dl" );
380 if ( function_exists( 'wikidiff2_do_diff' ) ) {
381 wfProfileIn( 'wikidiff2_do_diff' );
382 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
383 wfProfileOut( 'wikidiff2_do_diff' );
387 if ( $wgExternalDiffEngine !== false ) {
389 global $wgTmpDirectory;
390 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
391 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
393 $tempFile1 = fopen( $tempName1, "w" );
395 wfProfileOut( $fname );
398 $tempFile2 = fopen( $tempName2, "w" );
400 wfProfileOut( $fname );
403 fwrite( $tempFile1, $otext );
404 fwrite( $tempFile2, $ntext );
405 fclose( $tempFile1 );
406 fclose( $tempFile2 );
407 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
408 wfProfileIn( "$fname-shellexec" );
409 $difftext = wfShellExec( $cmd );
410 wfProfileOut( "$fname-shellexec" );
411 unlink( $tempName1 );
412 unlink( $tempName2 );
417 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
418 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
419 $diffs = new Diff( $ota, $nta );
420 $formatter = new TableDiffFormatter();
421 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
426 * Replace line numbers with the text in the user's language
428 function localiseLineNumbers( $text ) {
429 return preg_replace_callback( '/<!--LINE (\d+)-->/',
430 array( &$this, 'localiseLineNumbersCb' ), $text );
433 function localiseLineNumbersCb( $matches ) {
435 return wfMsgExt( 'lineno', array('parseinline'), $wgLang->formatNum( $matches[1] ) );
439 * Add the header to a diff body
441 function addHeader( $diff, $otitle, $ntitle ) {
443 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
445 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
446 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
455 * Use specified text instead of loading from the database
457 function setText( $oldText, $newText ) {
458 $this->mOldtext
= $oldText;
459 $this->mNewtext
= $newText;
460 $this->mTextLoaded
= 2;
464 * Load revision metadata for the specified articles. If newid is 0, then compare
465 * the old article in oldid to the current article; if oldid is 0, then
466 * compare the current article to the immediately previous one (ignoring the
469 * If oldid is false, leave the corresponding revision object set
470 * to false. This is impossible via ordinary user input, and is provided for
473 function loadRevisionData() {
475 if ( $this->mRevisionsLoaded
) {
478 // Whether it succeeds or fails, we don't want to try again
479 $this->mRevisionsLoaded
= true;
482 // Load the new revision object
483 if( $this->mNewid
) {
484 $this->mNewRev
= Revision
::newFromId( $this->mNewid
);
486 $this->mNewRev
= Revision
::newFromTitle( $this->mTitle
);
489 if( is_null( $this->mNewRev
) ) {
493 // Set assorted variables
494 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
495 $this->mNewPage
= $this->mNewRev
->getTitle();
496 if( $this->mNewRev
->isCurrent() ) {
497 $newLink = $this->mNewPage
->escapeLocalUrl();
498 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
499 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit' );
501 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a> ($timestamp)</strong>"
502 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
505 $newLink = $this->mNewPage
->escapeLocalUrl( 'oldid=' . $this->mNewid
);
506 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mNewid
);
507 $newUndo = $this->mNewPage
->escapeLocalUrl( 'action=edit&undo=' . $this->mNewid
);
508 $this->mPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $timestamp ) );
510 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a></strong>"
511 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)"
512 . " (<a href='$newUndo'>" . htmlspecialchars( wfMsg( 'editundo' ) ) . "</a>)";
515 // Load the old revision object
516 $this->mOldRev
= false;
517 if( $this->mOldid
) {
518 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
519 } elseif ( $this->mOldid
=== 0 ) {
520 $rev = $this->mNewRev
->getPrevious();
522 $this->mOldid
= $rev->getId();
523 $this->mOldRev
= $rev;
525 // No previous revision; mark to show as first-version only.
526 $this->mOldid
= false;
527 $this->mOldRev
= false;
529 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
531 if( is_null( $this->mOldRev
) ) {
535 if ( $this->mOldRev
) {
536 $this->mOldPage
= $this->mOldRev
->getTitle();
538 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
539 $oldLink = $this->mOldPage
->escapeLocalUrl( 'oldid=' . $this->mOldid
);
540 $oldEdit = $this->mOldPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mOldid
);
541 $this->mOldtitle
= "<strong><a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) )
542 . "</a></strong> (<a href='$oldEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
549 * Load the text of the revisions, as well as revision data.
551 function loadText() {
552 if ( $this->mTextLoaded
== 2 ) {
555 // Whether it succeeds or fails, we don't want to try again
556 $this->mTextLoaded
= 2;
559 if ( !$this->loadRevisionData() ) {
562 if ( $this->mOldRev
) {
563 // FIXME: permission tests
564 $this->mOldtext
= $this->mOldRev
->getText();
565 if ( $this->mOldtext
=== false ) {
569 if ( $this->mNewRev
) {
570 $this->mNewtext
= $this->mNewRev
->getText();
571 if ( $this->mNewtext
=== false ) {
579 * Load the text of the new revision, not the old one
581 function loadNewText() {
582 if ( $this->mTextLoaded
>= 1 ) {
585 $this->mTextLoaded
= 1;
587 if ( !$this->loadRevisionData() ) {
590 $this->mNewtext
= $this->mNewRev
->getText();
597 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
599 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
600 // You may copy this code freely under the conditions of the GPL.
603 define('USE_ASSERTS', function_exists('assert'));
609 * @subpackage DifferenceEngine
617 trigger_error('pure virtual', E_USER_ERROR
);
621 return $this->orig ?
sizeof($this->orig
) : 0;
624 function nclosing() {
625 return $this->closing ?
sizeof($this->closing
) : 0;
633 * @subpackage DifferenceEngine
635 class _DiffOp_Copy
extends _DiffOp
{
638 function _DiffOp_Copy ($orig, $closing = false) {
639 if (!is_array($closing))
642 $this->closing
= $closing;
646 return new _DiffOp_Copy($this->closing
, $this->orig
);
654 * @subpackage DifferenceEngine
656 class _DiffOp_Delete
extends _DiffOp
{
657 var $type = 'delete';
659 function _DiffOp_Delete ($lines) {
660 $this->orig
= $lines;
661 $this->closing
= false;
665 return new _DiffOp_Add($this->orig
);
673 * @subpackage DifferenceEngine
675 class _DiffOp_Add
extends _DiffOp
{
678 function _DiffOp_Add ($lines) {
679 $this->closing
= $lines;
684 return new _DiffOp_Delete($this->closing
);
692 * @subpackage DifferenceEngine
694 class _DiffOp_Change
extends _DiffOp
{
695 var $type = 'change';
697 function _DiffOp_Change ($orig, $closing) {
699 $this->closing
= $closing;
703 return new _DiffOp_Change($this->closing
, $this->orig
);
709 * Class used internally by Diff to actually compute the diffs.
711 * The algorithm used here is mostly lifted from the perl module
712 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
713 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
715 * More ideas are taken from:
716 * http://www.ics.uci.edu/~eppstein/161/960229.html
718 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
719 * diffutils-2.7, which can be found at:
720 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
722 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
725 * Line length limits for robustness added by Tim Starling, 2005-08-31
727 * @author Geoffrey T. Dairiki, Tim Starling
730 * @subpackage DifferenceEngine
734 const MAX_XREF_LENGTH
= 10000;
736 function diff ($from_lines, $to_lines) {
737 $fname = '_DiffEngine::diff';
738 wfProfileIn( $fname );
740 $n_from = sizeof($from_lines);
741 $n_to = sizeof($to_lines);
743 $this->xchanged
= $this->ychanged
= array();
744 $this->xv
= $this->yv
= array();
745 $this->xind
= $this->yind
= array();
747 unset($this->in_seq
);
750 // Skip leading common lines.
751 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
752 if ($from_lines[$skip] !== $to_lines[$skip])
754 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
756 // Skip trailing common lines.
757 $xi = $n_from; $yi = $n_to;
758 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
759 if ($from_lines[$xi] !== $to_lines[$yi])
761 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
764 // Ignore lines which do not exist in both files.
765 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
766 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
769 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
770 $line = $to_lines[$yi];
771 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
773 $yhash[$this->_line_hash($line)] = 1;
777 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
778 $line = $from_lines[$xi];
779 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
786 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
788 // Merge edits when possible
789 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
790 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
792 // Compute the edit operations.
795 while ($xi < $n_from ||
$yi < $n_to) {
796 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
797 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
799 // Skip matching "snake".
801 while ( $xi < $n_from && $yi < $n_to
802 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
803 $copy[] = $from_lines[$xi++
];
807 $edits[] = new _DiffOp_Copy($copy);
809 // Find deletes & adds.
811 while ($xi < $n_from && $this->xchanged
[$xi])
812 $delete[] = $from_lines[$xi++
];
815 while ($yi < $n_to && $this->ychanged
[$yi])
816 $add[] = $to_lines[$yi++
];
819 $edits[] = new _DiffOp_Change($delete, $add);
821 $edits[] = new _DiffOp_Delete($delete);
823 $edits[] = new _DiffOp_Add($add);
825 wfProfileOut( $fname );
830 * Returns the whole line if it's small enough, or the MD5 hash otherwise
832 function _line_hash( $line ) {
833 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
841 /* Divide the Largest Common Subsequence (LCS) of the sequences
842 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
845 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
846 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
847 * sub sequences. The first sub-sequence is contained in [X0, X1),
848 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
849 * that (X0, Y0) == (XOFF, YOFF) and
850 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
852 * This function assumes that the first lines of the specified portions
853 * of the two files do not match, and likewise that the last lines do not
854 * match. The caller must trim matching lines from the beginning and end
855 * of the portions it is going to specify.
857 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
858 $fname = '_DiffEngine::_diag';
859 wfProfileIn( $fname );
862 if ($xlim - $xoff > $ylim - $yoff) {
863 // Things seems faster (I'm not sure I understand why)
864 // when the shortest sequence in X.
866 list ($xoff, $xlim, $yoff, $ylim)
867 = array( $yoff, $ylim, $xoff, $xlim);
871 for ($i = $ylim - 1; $i >= $yoff; $i--)
872 $ymatches[$this->xv
[$i]][] = $i;
874 for ($i = $ylim - 1; $i >= $yoff; $i--)
875 $ymatches[$this->yv
[$i]][] = $i;
878 $this->seq
[0]= $yoff - 1;
879 $this->in_seq
= array();
882 $numer = $xlim - $xoff +
$nchunks - 1;
884 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
885 wfProfileIn( "$fname-chunk" );
887 for ($i = 0; $i <= $this->lcs
; $i++
)
888 $ymids[$i][$chunk-1] = $this->seq
[$i];
890 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
891 for ( ; $x < $x1; $x++
) {
892 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
893 if (empty($ymatches[$line]))
895 $matches = $ymatches[$line];
897 while (list ($junk, $y) = each($matches))
898 if (empty($this->in_seq
[$y])) {
899 $k = $this->_lcs_pos($y);
900 USE_ASSERTS
&& assert($k > 0);
901 $ymids[$k] = $ymids[$k-1];
904 while (list ($junk, $y) = each($matches)) {
905 if ($y > $this->seq
[$k-1]) {
906 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
907 // Optimization: this is a common case:
908 // next match is just replacing previous match.
909 $this->in_seq
[$this->seq
[$k]] = false;
911 $this->in_seq
[$y] = 1;
912 } else if (empty($this->in_seq
[$y])) {
913 $k = $this->_lcs_pos($y);
914 USE_ASSERTS
&& assert($k > 0);
915 $ymids[$k] = $ymids[$k-1];
919 wfProfileOut( "$fname-chunk" );
922 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
923 $ymid = $ymids[$this->lcs
];
924 for ($n = 0; $n < $nchunks - 1; $n++
) {
925 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
927 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
929 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
931 wfProfileOut( $fname );
932 return array($this->lcs
, $seps);
935 function _lcs_pos ($ypos) {
936 $fname = '_DiffEngine::_lcs_pos';
937 wfProfileIn( $fname );
940 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
941 $this->seq
[++
$this->lcs
] = $ypos;
942 $this->in_seq
[$ypos] = 1;
943 wfProfileOut( $fname );
948 while ($beg < $end) {
949 $mid = (int)(($beg +
$end) / 2);
950 if ( $ypos > $this->seq
[$mid] )
956 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
958 $this->in_seq
[$this->seq
[$end]] = false;
959 $this->seq
[$end] = $ypos;
960 $this->in_seq
[$ypos] = 1;
961 wfProfileOut( $fname );
965 /* Find LCS of two sequences.
967 * The results are recorded in the vectors $this->{x,y}changed[], by
968 * storing a 1 in the element for each line that is an insertion
969 * or deletion (ie. is not in the LCS).
971 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
973 * Note that XLIM, YLIM are exclusive bounds.
974 * All line numbers are origin-0 and discarded lines are not counted.
976 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
977 $fname = '_DiffEngine::_compareseq';
978 wfProfileIn( $fname );
980 // Slide down the bottom initial diagonal.
981 while ($xoff < $xlim && $yoff < $ylim
982 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
987 // Slide up the top initial diagonal.
988 while ($xlim > $xoff && $ylim > $yoff
989 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
994 if ($xoff == $xlim ||
$yoff == $ylim)
997 // This is ad hoc but seems to work well.
998 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
999 //$nchunks = max(2,min(8,(int)$nchunks));
1000 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
1002 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
1006 // X and Y sequences have no common subsequence:
1007 // mark all changed.
1008 while ($yoff < $ylim)
1009 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
1010 while ($xoff < $xlim)
1011 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
1013 // Use the partitions to split this problem into subproblems.
1016 while ($pt2 = next($seps)) {
1017 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1021 wfProfileOut( $fname );
1024 /* Adjust inserts/deletes of identical lines to join changes
1025 * as much as possible.
1027 * We do something when a run of changed lines include a
1028 * line at one end and has an excluded, identical line at the other.
1029 * We are free to choose which identical line is included.
1030 * `compareseq' usually chooses the one at the beginning,
1031 * but usually it is cleaner to consider the following identical line
1032 * to be the "change".
1034 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1036 function _shift_boundaries ($lines, &$changed, $other_changed) {
1037 $fname = '_DiffEngine::_shift_boundaries';
1038 wfProfileIn( $fname );
1042 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1043 $len = sizeof($lines);
1044 $other_len = sizeof($other_changed);
1048 * Scan forwards to find beginning of another run of changes.
1049 * Also keep track of the corresponding point in the other file.
1051 * Throughout this code, $i and $j are adjusted together so that
1052 * the first $i elements of $changed and the first $j elements
1053 * of $other_changed both contain the same number of zeros
1054 * (unchanged lines).
1055 * Furthermore, $j is always kept so that $j == $other_len or
1056 * $other_changed[$j] == false.
1058 while ($j < $other_len && $other_changed[$j])
1061 while ($i < $len && ! $changed[$i]) {
1062 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1064 while ($j < $other_len && $other_changed[$j])
1073 // Find the end of this run of changes.
1074 while (++
$i < $len && $changed[$i])
1079 * Record the length of this run of changes, so that
1080 * we can later determine whether the run has grown.
1082 $runlength = $i - $start;
1085 * Move the changed region back, so long as the
1086 * previous unchanged line matches the last changed one.
1087 * This merges with previous changed regions.
1089 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1090 $changed[--$start] = 1;
1091 $changed[--$i] = false;
1092 while ($start > 0 && $changed[$start - 1])
1094 USE_ASSERTS
&& assert('$j > 0');
1095 while ($other_changed[--$j])
1097 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1101 * Set CORRESPONDING to the end of the changed run, at the last
1102 * point where it corresponds to a changed run in the other file.
1103 * CORRESPONDING == LEN means no such point has been found.
1105 $corresponding = $j < $other_len ?
$i : $len;
1108 * Move the changed region forward, so long as the
1109 * first changed line matches the following unchanged one.
1110 * This merges with following changed regions.
1111 * Do this second, so that if there are no merges,
1112 * the changed region is moved forward as far as possible.
1114 while ($i < $len && $lines[$start] == $lines[$i]) {
1115 $changed[$start++
] = false;
1117 while ($i < $len && $changed[$i])
1120 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1122 if ($j < $other_len && $other_changed[$j]) {
1123 $corresponding = $i;
1124 while ($j < $other_len && $other_changed[$j])
1128 } while ($runlength != $i - $start);
1131 * If possible, move the fully-merged run of changes
1132 * back to a corresponding run in the other file.
1134 while ($corresponding < $i) {
1135 $changed[--$start] = 1;
1137 USE_ASSERTS
&& assert('$j > 0');
1138 while ($other_changed[--$j])
1140 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1143 wfProfileOut( $fname );
1148 * Class representing a 'diff' between two sequences of strings.
1151 * @package MediaWiki
1152 * @subpackage DifferenceEngine
1160 * Computes diff between sequences of strings.
1162 * @param $from_lines array An array of strings.
1163 * (Typically these are lines from a file.)
1164 * @param $to_lines array An array of strings.
1166 function Diff($from_lines, $to_lines) {
1167 $eng = new _DiffEngine
;
1168 $this->edits
= $eng->diff($from_lines, $to_lines);
1169 //$this->_check($from_lines, $to_lines);
1173 * Compute reversed Diff.
1177 * $diff = new Diff($lines1, $lines2);
1178 * $rev = $diff->reverse();
1179 * @return object A Diff object representing the inverse of the
1182 function reverse () {
1184 $rev->edits
= array();
1185 foreach ($this->edits
as $edit) {
1186 $rev->edits
[] = $edit->reverse();
1192 * Check for empty diff.
1194 * @return bool True iff two sequences were identical.
1196 function isEmpty () {
1197 foreach ($this->edits
as $edit) {
1198 if ($edit->type
!= 'copy')
1205 * Compute the length of the Longest Common Subsequence (LCS).
1207 * This is mostly for diagnostic purposed.
1209 * @return int The length of the LCS.
1213 foreach ($this->edits
as $edit) {
1214 if ($edit->type
== 'copy')
1215 $lcs +
= sizeof($edit->orig
);
1221 * Get the original set of lines.
1223 * This reconstructs the $from_lines parameter passed to the
1226 * @return array The original sequence of strings.
1231 foreach ($this->edits
as $edit) {
1233 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1239 * Get the closing set of lines.
1241 * This reconstructs the $to_lines parameter passed to the
1244 * @return array The sequence of strings.
1246 function closing() {
1249 foreach ($this->edits
as $edit) {
1251 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1257 * Check a Diff for validity.
1259 * This is here only for debugging purposes.
1261 function _check ($from_lines, $to_lines) {
1262 $fname = 'Diff::_check';
1263 wfProfileIn( $fname );
1264 if (serialize($from_lines) != serialize($this->orig()))
1265 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1266 if (serialize($to_lines) != serialize($this->closing()))
1267 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1269 $rev = $this->reverse();
1270 if (serialize($to_lines) != serialize($rev->orig()))
1271 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1272 if (serialize($from_lines) != serialize($rev->closing()))
1273 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1277 foreach ($this->edits
as $edit) {
1278 if ( $prevtype == $edit->type
)
1279 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1280 $prevtype = $edit->type
;
1283 $lcs = $this->lcs();
1284 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1285 wfProfileOut( $fname );
1293 * @package MediaWiki
1294 * @subpackage DifferenceEngine
1296 class MappedDiff
extends Diff
1301 * Computes diff between sequences of strings.
1303 * This can be used to compute things like
1304 * case-insensitve diffs, or diffs which ignore
1305 * changes in white-space.
1307 * @param $from_lines array An array of strings.
1308 * (Typically these are lines from a file.)
1310 * @param $to_lines array An array of strings.
1312 * @param $mapped_from_lines array This array should
1313 * have the same size number of elements as $from_lines.
1314 * The elements in $mapped_from_lines and
1315 * $mapped_to_lines are what is actually compared
1316 * when computing the diff.
1318 * @param $mapped_to_lines array This array should
1319 * have the same number of elements as $to_lines.
1321 function MappedDiff($from_lines, $to_lines,
1322 $mapped_from_lines, $mapped_to_lines) {
1323 $fname = 'MappedDiff::MappedDiff';
1324 wfProfileIn( $fname );
1326 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1327 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1329 $this->Diff($mapped_from_lines, $mapped_to_lines);
1332 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1333 $orig = &$this->edits
[$i]->orig
;
1334 if (is_array($orig)) {
1335 $orig = array_slice($from_lines, $xi, sizeof($orig));
1336 $xi +
= sizeof($orig);
1339 $closing = &$this->edits
[$i]->closing
;
1340 if (is_array($closing)) {
1341 $closing = array_slice($to_lines, $yi, sizeof($closing));
1342 $yi +
= sizeof($closing);
1345 wfProfileOut( $fname );
1350 * A class to format Diffs
1352 * This class formats the diff in classic diff format.
1353 * It is intended that this class be customized via inheritance,
1354 * to obtain fancier outputs.
1357 * @package MediaWiki
1358 * @subpackage DifferenceEngine
1363 * Number of leading context "lines" to preserve.
1365 * This should be left at zero for this class, but subclasses
1366 * may want to set this to other values.
1368 var $leading_context_lines = 0;
1371 * Number of trailing context "lines" to preserve.
1373 * This should be left at zero for this class, but subclasses
1374 * may want to set this to other values.
1376 var $trailing_context_lines = 0;
1381 * @param $diff object A Diff object.
1382 * @return string The formatted output.
1384 function format($diff) {
1385 $fname = 'DiffFormatter::format';
1386 wfProfileIn( $fname );
1392 $nlead = $this->leading_context_lines
;
1393 $ntrail = $this->trailing_context_lines
;
1395 $this->_start_diff();
1397 foreach ($diff->edits
as $edit) {
1398 if ($edit->type
== 'copy') {
1399 if (is_array($block)) {
1400 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1405 $context = array_slice($edit->orig
, 0, $ntrail);
1406 $block[] = new _DiffOp_Copy($context);
1408 $this->_block($x0, $ntrail +
$xi - $x0,
1409 $y0, $ntrail +
$yi - $y0,
1414 $context = $edit->orig
;
1417 if (! is_array($block)) {
1418 $context = array_slice($context, sizeof($context) - $nlead);
1419 $x0 = $xi - sizeof($context);
1420 $y0 = $yi - sizeof($context);
1423 $block[] = new _DiffOp_Copy($context);
1429 $xi +
= sizeof($edit->orig
);
1431 $yi +
= sizeof($edit->closing
);
1434 if (is_array($block))
1435 $this->_block($x0, $xi - $x0,
1439 $end = $this->_end_diff();
1440 wfProfileOut( $fname );
1444 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1445 $fname = 'DiffFormatter::_block';
1446 wfProfileIn( $fname );
1447 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1448 foreach ($edits as $edit) {
1449 if ($edit->type
== 'copy')
1450 $this->_context($edit->orig
);
1451 elseif ($edit->type
== 'add')
1452 $this->_added($edit->closing
);
1453 elseif ($edit->type
== 'delete')
1454 $this->_deleted($edit->orig
);
1455 elseif ($edit->type
== 'change')
1456 $this->_changed($edit->orig
, $edit->closing
);
1458 trigger_error('Unknown edit type', E_USER_ERROR
);
1460 $this->_end_block();
1461 wfProfileOut( $fname );
1464 function _start_diff() {
1468 function _end_diff() {
1469 $val = ob_get_contents();
1474 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1476 $xbeg .= "," . ($xbeg +
$xlen - 1);
1478 $ybeg .= "," . ($ybeg +
$ylen - 1);
1480 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1483 function _start_block($header) {
1487 function _end_block() {
1490 function _lines($lines, $prefix = ' ') {
1491 foreach ($lines as $line)
1492 echo "$prefix $line\n";
1495 function _context($lines) {
1496 $this->_lines($lines);
1499 function _added($lines) {
1500 $this->_lines($lines, '>');
1502 function _deleted($lines) {
1503 $this->_lines($lines, '<');
1506 function _changed($orig, $closing) {
1507 $this->_deleted($orig);
1509 $this->_added($closing);
1515 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1519 define('NBSP', ' '); // iso-8859-x non-breaking space.
1524 * @package MediaWiki
1525 * @subpackage DifferenceEngine
1527 class _HWLDF_WordAccumulator
{
1528 function _HWLDF_WordAccumulator () {
1529 $this->_lines
= array();
1535 function _flushGroup ($new_tag) {
1536 if ($this->_group
!== '') {
1537 if ($this->_tag
== 'mark')
1538 $this->_line
.= '<span class="diffchange">' .
1539 htmlspecialchars ( $this->_group
) . '</span>';
1541 $this->_line
.= htmlspecialchars ( $this->_group
);
1544 $this->_tag
= $new_tag;
1547 function _flushLine ($new_tag) {
1548 $this->_flushGroup($new_tag);
1549 if ($this->_line
!= '')
1550 array_push ( $this->_lines
, $this->_line
);
1552 # make empty lines visible by inserting an NBSP
1553 array_push ( $this->_lines
, NBSP
);
1557 function addWords ($words, $tag = '') {
1558 if ($tag != $this->_tag
)
1559 $this->_flushGroup($tag);
1561 foreach ($words as $word) {
1562 // new-line should only come as first char of word.
1565 if ($word[0] == "\n") {
1566 $this->_flushLine($tag);
1567 $word = substr($word, 1);
1569 assert(!strstr($word, "\n"));
1570 $this->_group
.= $word;
1574 function getLines() {
1575 $this->_flushLine('~done');
1576 return $this->_lines
;
1583 * @package MediaWiki
1584 * @subpackage DifferenceEngine
1586 class WordLevelDiff
extends MappedDiff
1588 const MAX_LINE_LENGTH
= 10000;
1590 function WordLevelDiff ($orig_lines, $closing_lines) {
1591 $fname = 'WordLevelDiff::WordLevelDiff';
1592 wfProfileIn( $fname );
1594 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1595 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1597 $this->MappedDiff($orig_words, $closing_words,
1598 $orig_stripped, $closing_stripped);
1599 wfProfileOut( $fname );
1602 function _split($lines) {
1603 $fname = 'WordLevelDiff::_split';
1604 wfProfileIn( $fname );
1607 $stripped = array();
1609 foreach ( $lines as $line ) {
1610 # If the line is too long, just pretend the entire line is one big word
1611 # This prevents resource exhaustion problems
1618 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1620 $stripped[] = $line;
1622 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1625 $words = array_merge( $words, $m[0] );
1626 $stripped = array_merge( $stripped, $m[1] );
1630 wfProfileOut( $fname );
1631 return array($words, $stripped);
1635 $fname = 'WordLevelDiff::orig';
1636 wfProfileIn( $fname );
1637 $orig = new _HWLDF_WordAccumulator
;
1639 foreach ($this->edits
as $edit) {
1640 if ($edit->type
== 'copy')
1641 $orig->addWords($edit->orig
);
1642 elseif ($edit->orig
)
1643 $orig->addWords($edit->orig
, 'mark');
1645 $lines = $orig->getLines();
1646 wfProfileOut( $fname );
1650 function closing () {
1651 $fname = 'WordLevelDiff::closing';
1652 wfProfileIn( $fname );
1653 $closing = new _HWLDF_WordAccumulator
;
1655 foreach ($this->edits
as $edit) {
1656 if ($edit->type
== 'copy')
1657 $closing->addWords($edit->closing
);
1658 elseif ($edit->closing
)
1659 $closing->addWords($edit->closing
, 'mark');
1661 $lines = $closing->getLines();
1662 wfProfileOut( $fname );
1668 * Wikipedia Table style diff formatter.
1671 * @package MediaWiki
1672 * @subpackage DifferenceEngine
1674 class TableDiffFormatter
extends DiffFormatter
1676 function TableDiffFormatter() {
1677 $this->leading_context_lines
= 2;
1678 $this->trailing_context_lines
= 2;
1681 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1682 $r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" .
1683 '<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n";
1687 function _start_block( $header ) {
1691 function _end_block() {
1694 function _lines( $lines, $prefix=' ', $color='white' ) {
1697 # HTML-escape parameter before calling this
1698 function addedLine( $line ) {
1699 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1702 # HTML-escape parameter before calling this
1703 function deletedLine( $line ) {
1704 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1707 # HTML-escape parameter before calling this
1708 function contextLine( $line ) {
1709 return "<td> </td><td class='diff-context'>{$line}</td>";
1712 function emptyLine() {
1713 return '<td colspan="2"> </td>';
1716 function _added( $lines ) {
1717 foreach ($lines as $line) {
1718 echo '<tr>' . $this->emptyLine() .
1719 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1723 function _deleted($lines) {
1724 foreach ($lines as $line) {
1725 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1726 $this->emptyLine() . "</tr>\n";
1730 function _context( $lines ) {
1731 foreach ($lines as $line) {
1733 $this->contextLine( htmlspecialchars ( $line ) ) .
1734 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1738 function _changed( $orig, $closing ) {
1739 $fname = 'TableDiffFormatter::_changed';
1740 wfProfileIn( $fname );
1742 $diff = new WordLevelDiff( $orig, $closing );
1743 $del = $diff->orig();
1744 $add = $diff->closing();
1746 # Notice that WordLevelDiff returns HTML-escaped output.
1747 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1749 while ( $line = array_shift( $del ) ) {
1750 $aline = array_shift( $add );
1751 echo '<tr>' . $this->deletedLine( $line ) .
1752 $this->addedLine( $aline ) . "</tr>\n";
1754 foreach ($add as $line) { # If any leftovers
1755 echo '<tr>' . $this->emptyLine() .
1756 $this->addedLine( $line ) . "</tr>\n";
1758 wfProfileOut( $fname );