21ac558b3cf611266fb1c7ac170b533b8ae06572
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 $username = $this->mNewRev
->getUserText();
152 $rollback = ' <strong>[' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'rollbacklink' ),
153 'action=rollback&from=' . urlencode( $username ) .
154 '&token=' . urlencode( $wgUser->editToken( array( $this->mTitle
->getPrefixedText(), $username ) ) ) ) .
159 if( $wgUseRCPatrol && $this->mRcidMarkPatrolled
!= 0 && $wgUser->isAllowed( 'patrol' ) ) {
160 $patrol = ' [' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'markaspatrolleddiff' ), "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']';
165 $prevlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'previousdiff' ),
166 'diff=prev&oldid='.$this->mOldid
, '', '', 'id="differences-prevlink"' );
167 if ( $this->mNewRev
->isCurrent() ) {
168 $nextlink = ' ';
170 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ),
171 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
174 $oldHeader = "<strong>{$this->mOldtitle}</strong><br />" .
175 $sk->revUserTools( $this->mOldRev
) . "<br />" .
176 $sk->revComment( $this->mOldRev
) . "<br />" .
178 $newHeader = "<strong>{$this->mNewtitle}</strong><br />" .
179 $sk->revUserTools( $this->mNewRev
) . " $rollback<br />" .
180 $sk->revComment( $this->mNewRev
) . "<br />" .
183 $this->showDiff( $oldHeader, $newHeader );
184 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
186 if( !$this->mNewRev
->isCurrent() ) {
187 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
190 $this->loadNewText();
191 if( is_object( $this->mNewRev
) ) {
192 $wgOut->setRevisionId( $this->mNewRev
->getId() );
194 $wgOut->addSecondaryWikiText( $this->mNewtext
);
196 if( !$this->mNewRev
->isCurrent() ) {
197 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
200 wfProfileOut( $fname );
204 * Show the first revision of an article. Uses normal diff headers in
205 * contrast to normal "old revision" display style.
207 function showFirstRevision() {
208 global $wgOut, $wgUser;
210 $fname = 'DifferenceEngine::showFirstRevision';
211 wfProfileIn( $fname );
213 # Get article text from the DB
215 if ( ! $this->loadNewText() ) {
216 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
218 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
219 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
220 $wgOut->addWikitext( $mtext );
221 wfProfileOut( $fname );
224 if ( $this->mNewRev
->isCurrent() ) {
225 $wgOut->setArticleFlag( true );
228 # Check if user is allowed to look at this page. If not, bail out.
230 if ( !( $this->mTitle
->userCanRead() ) ) {
231 $wgOut->loginToUse();
233 wfProfileOut( $fname );
237 # Prepare the header box
239 $sk = $wgUser->getSkin();
241 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
242 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />" .
243 $sk->revUserTools( $this->mNewRev
) . "<br />" .
244 $sk->revComment( $this->mNewRev
) . "<br />" .
245 $nextlink . "</div>\n";
247 $wgOut->addHTML( $header );
249 $wgOut->setSubtitle( wfMsg( 'difference' ) );
250 $wgOut->setRobotpolicy( 'noindex,nofollow' );
253 # Show current revision
255 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
256 if( is_object( $this->mNewRev
) ) {
257 $wgOut->setRevisionId( $this->mNewRev
->getId() );
259 $wgOut->addSecondaryWikiText( $this->mNewtext
);
261 wfProfileOut( $fname );
265 * Get the diff text, send it to $wgOut
266 * Returns false if the diff could not be generated, otherwise returns true
268 function showDiff( $otitle, $ntitle ) {
270 $diff = $this->getDiff( $otitle, $ntitle );
271 if ( $diff === false ) {
272 $wgOut->addWikitext( wfMsg( 'missingarticle', "<nowiki>(fixme, bug)</nowiki>" ) );
275 $wgOut->addHTML( $diff );
281 * Get diff table, including header
282 * Note that the interface has changed, it's no longer static.
283 * Returns false on error
285 function getDiff( $otitle, $ntitle ) {
286 $body = $this->getDiffBody();
287 if ( $body === false ) {
290 return $this->addHeader( $body, $otitle, $ntitle );
295 * Get the diff table body, without header
297 * Returns false on error
299 function getDiffBody() {
300 global $wgMemc, $wgDBname;
301 $fname = 'DifferenceEngine::getDiffBody';
302 wfProfileIn( $fname );
306 if ( $this->mOldid
&& $this->mNewid
) {
308 $key = "$wgDBname:diff:oldid:{$this->mOldid}:newid:{$this->mNewid}";
309 $difftext = $wgMemc->get( $key );
311 wfIncrStats( 'diff_cache_hit' );
312 $difftext = $this->localiseLineNumbers( $difftext );
313 $difftext .= "\n<!-- diff cache key $key -->\n";
314 wfProfileOut( $fname );
319 if ( !$this->loadText() ) {
320 wfProfileOut( $fname );
324 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
326 // Save to cache for 7 days
327 if ( $key !== false && $difftext !== false ) {
328 wfIncrStats( 'diff_cache_miss' );
329 $wgMemc->set( $key, $difftext, 7*86400 );
331 wfIncrStats( 'diff_uncacheable' );
333 // Replace line numbers with the text in the user's language
334 if ( $difftext !== false ) {
335 $difftext = $this->localiseLineNumbers( $difftext );
337 wfProfileOut( $fname );
342 * Generate a diff, no caching
343 * $otext and $ntext must be already segmented
345 function generateDiffBody( $otext, $ntext ) {
346 global $wgExternalDiffEngine, $wgContLang;
347 $fname = 'DifferenceEngine::generateDiffBody';
349 $otext = str_replace( "\r\n", "\n", $otext );
350 $ntext = str_replace( "\r\n", "\n", $ntext );
352 if ( $wgExternalDiffEngine == 'wikidiff' ) {
353 # For historical reasons, external diff engine expects
354 # input text to be HTML-escaped already
355 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
356 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
357 if( !function_exists( 'wikidiff_do_diff' ) ) {
358 dl('php_wikidiff.so');
360 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) );
363 if ( $wgExternalDiffEngine == 'wikidiff2' ) {
364 # Better external diff engine, the 2 may some day be dropped
365 # This one does the escaping and segmenting itself
366 if ( !function_exists( 'wikidiff2_do_diff' ) ) {
367 wfProfileIn( "$fname-dl" );
368 @dl
('php_wikidiff2.so');
369 wfProfileOut( "$fname-dl" );
371 if ( function_exists( 'wikidiff2_do_diff' ) ) {
372 wfProfileIn( 'wikidiff2_do_diff' );
373 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
374 wfProfileOut( 'wikidiff2_do_diff' );
378 if ( $wgExternalDiffEngine !== false ) {
380 global $wgTmpDirectory;
381 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
382 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
384 $tempFile1 = fopen( $tempName1, "w" );
386 wfProfileOut( $fname );
389 $tempFile2 = fopen( $tempName2, "w" );
391 wfProfileOut( $fname );
394 fwrite( $tempFile1, $otext );
395 fwrite( $tempFile2, $ntext );
396 fclose( $tempFile1 );
397 fclose( $tempFile2 );
398 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
399 wfProfileIn( "$fname-shellexec" );
400 $difftext = wfShellExec( $cmd );
401 wfProfileOut( "$fname-shellexec" );
402 unlink( $tempName1 );
403 unlink( $tempName2 );
408 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
409 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
410 $diffs = new Diff( $ota, $nta );
411 $formatter = new TableDiffFormatter();
412 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
417 * Replace line numbers with the text in the user's language
419 function localiseLineNumbers( $text ) {
420 return preg_replace_callback( '/<!--LINE (\d+)-->/',
421 array( &$this, 'localiseLineNumbersCb' ), $text );
424 function localiseLineNumbersCb( $matches ) {
426 return wfMsgExt( 'lineno', array('parseinline'), $wgLang->formatNum( $matches[1] ) );
430 * Add the header to a diff body
432 function addHeader( $diff, $otitle, $ntitle ) {
434 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
436 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
437 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
446 * Use specified text instead of loading from the database
448 function setText( $oldText, $newText ) {
449 $this->mOldtext
= $oldText;
450 $this->mNewtext
= $newText;
451 $this->mTextLoaded
= 2;
455 * Load revision metadata for the specified articles. If newid is 0, then compare
456 * the old article in oldid to the current article; if oldid is 0, then
457 * compare the current article to the immediately previous one (ignoring the
460 * If oldid is false, leave the corresponding revision object set
461 * to false. This is impossible via ordinary user input, and is provided for
464 function loadRevisionData() {
466 if ( $this->mRevisionsLoaded
) {
469 // Whether it succeeds or fails, we don't want to try again
470 $this->mRevisionsLoaded
= true;
473 // Load the new revision object
474 if( $this->mNewid
) {
475 $this->mNewRev
= Revision
::newFromId( $this->mNewid
);
477 $this->mNewRev
= Revision
::newFromTitle( $this->mTitle
);
480 if( is_null( $this->mNewRev
) ) {
484 // Set assorted variables
485 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
486 $this->mNewPage
= $this->mNewRev
->getTitle();
487 if( $this->mNewRev
->isCurrent() ) {
488 $newLink = $this->mNewPage
->escapeLocalUrl();
489 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
490 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit' );
492 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a> ($timestamp)</strong>"
493 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
496 $newLink = $this->mNewPage
->escapeLocalUrl( 'oldid=' . $this->mNewid
);
497 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mNewid
);
498 $this->mPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $timestamp ) );
500 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a></strong>"
501 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</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 $oldEdit = $this->mOldPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mOldid
);
530 $this->mOldtitle
= "<strong><a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) )
531 . "</a></strong> (<a href='$oldEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
538 * Load the text of the revisions, as well as revision data.
540 function loadText() {
541 if ( $this->mTextLoaded
== 2 ) {
544 // Whether it succeeds or fails, we don't want to try again
545 $this->mTextLoaded
= 2;
548 if ( !$this->loadRevisionData() ) {
551 if ( $this->mOldRev
) {
552 // FIXME: permission tests
553 $this->mOldtext
= $this->mOldRev
->getText();
554 if ( $this->mOldtext
=== false ) {
558 if ( $this->mNewRev
) {
559 $this->mNewtext
= $this->mNewRev
->getText();
560 if ( $this->mNewtext
=== false ) {
568 * Load the text of the new revision, not the old one
570 function loadNewText() {
571 if ( $this->mTextLoaded
>= 1 ) {
574 $this->mTextLoaded
= 1;
576 if ( !$this->loadRevisionData() ) {
579 $this->mNewtext
= $this->mNewRev
->getText();
586 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
588 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
589 // You may copy this code freely under the conditions of the GPL.
592 define('USE_ASSERTS', function_exists('assert'));
598 * @subpackage DifferenceEngine
606 trigger_error('pure virtual', E_USER_ERROR
);
610 return $this->orig ?
sizeof($this->orig
) : 0;
613 function nclosing() {
614 return $this->closing ?
sizeof($this->closing
) : 0;
622 * @subpackage DifferenceEngine
624 class _DiffOp_Copy
extends _DiffOp
{
627 function _DiffOp_Copy ($orig, $closing = false) {
628 if (!is_array($closing))
631 $this->closing
= $closing;
635 return new _DiffOp_Copy($this->closing
, $this->orig
);
643 * @subpackage DifferenceEngine
645 class _DiffOp_Delete
extends _DiffOp
{
646 var $type = 'delete';
648 function _DiffOp_Delete ($lines) {
649 $this->orig
= $lines;
650 $this->closing
= false;
654 return new _DiffOp_Add($this->orig
);
662 * @subpackage DifferenceEngine
664 class _DiffOp_Add
extends _DiffOp
{
667 function _DiffOp_Add ($lines) {
668 $this->closing
= $lines;
673 return new _DiffOp_Delete($this->closing
);
681 * @subpackage DifferenceEngine
683 class _DiffOp_Change
extends _DiffOp
{
684 var $type = 'change';
686 function _DiffOp_Change ($orig, $closing) {
688 $this->closing
= $closing;
692 return new _DiffOp_Change($this->closing
, $this->orig
);
698 * Class used internally by Diff to actually compute the diffs.
700 * The algorithm used here is mostly lifted from the perl module
701 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
702 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
704 * More ideas are taken from:
705 * http://www.ics.uci.edu/~eppstein/161/960229.html
707 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
708 * diffutils-2.7, which can be found at:
709 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
711 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
714 * Line length limits for robustness added by Tim Starling, 2005-08-31
716 * @author Geoffrey T. Dairiki, Tim Starling
719 * @subpackage DifferenceEngine
723 const MAX_XREF_LENGTH
= 10000;
725 function diff ($from_lines, $to_lines) {
726 $fname = '_DiffEngine::diff';
727 wfProfileIn( $fname );
729 $n_from = sizeof($from_lines);
730 $n_to = sizeof($to_lines);
732 $this->xchanged
= $this->ychanged
= array();
733 $this->xv
= $this->yv
= array();
734 $this->xind
= $this->yind
= array();
736 unset($this->in_seq
);
739 // Skip leading common lines.
740 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
741 if ($from_lines[$skip] !== $to_lines[$skip])
743 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
745 // Skip trailing common lines.
746 $xi = $n_from; $yi = $n_to;
747 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
748 if ($from_lines[$xi] !== $to_lines[$yi])
750 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
753 // Ignore lines which do not exist in both files.
754 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
755 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
758 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
759 $line = $to_lines[$yi];
760 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
762 $yhash[$this->_line_hash($line)] = 1;
766 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
767 $line = $from_lines[$xi];
768 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
775 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
777 // Merge edits when possible
778 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
779 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
781 // Compute the edit operations.
784 while ($xi < $n_from ||
$yi < $n_to) {
785 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
786 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
788 // Skip matching "snake".
790 while ( $xi < $n_from && $yi < $n_to
791 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
792 $copy[] = $from_lines[$xi++
];
796 $edits[] = new _DiffOp_Copy($copy);
798 // Find deletes & adds.
800 while ($xi < $n_from && $this->xchanged
[$xi])
801 $delete[] = $from_lines[$xi++
];
804 while ($yi < $n_to && $this->ychanged
[$yi])
805 $add[] = $to_lines[$yi++
];
808 $edits[] = new _DiffOp_Change($delete, $add);
810 $edits[] = new _DiffOp_Delete($delete);
812 $edits[] = new _DiffOp_Add($add);
814 wfProfileOut( $fname );
819 * Returns the whole line if it's small enough, or the MD5 hash otherwise
821 function _line_hash( $line ) {
822 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
830 /* Divide the Largest Common Subsequence (LCS) of the sequences
831 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
834 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
835 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
836 * sub sequences. The first sub-sequence is contained in [X0, X1),
837 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
838 * that (X0, Y0) == (XOFF, YOFF) and
839 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
841 * This function assumes that the first lines of the specified portions
842 * of the two files do not match, and likewise that the last lines do not
843 * match. The caller must trim matching lines from the beginning and end
844 * of the portions it is going to specify.
846 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
847 $fname = '_DiffEngine::_diag';
848 wfProfileIn( $fname );
851 if ($xlim - $xoff > $ylim - $yoff) {
852 // Things seems faster (I'm not sure I understand why)
853 // when the shortest sequence in X.
855 list ($xoff, $xlim, $yoff, $ylim)
856 = array( $yoff, $ylim, $xoff, $xlim);
860 for ($i = $ylim - 1; $i >= $yoff; $i--)
861 $ymatches[$this->xv
[$i]][] = $i;
863 for ($i = $ylim - 1; $i >= $yoff; $i--)
864 $ymatches[$this->yv
[$i]][] = $i;
867 $this->seq
[0]= $yoff - 1;
868 $this->in_seq
= array();
871 $numer = $xlim - $xoff +
$nchunks - 1;
873 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
874 wfProfileIn( "$fname-chunk" );
876 for ($i = 0; $i <= $this->lcs
; $i++
)
877 $ymids[$i][$chunk-1] = $this->seq
[$i];
879 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
880 for ( ; $x < $x1; $x++
) {
881 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
882 if (empty($ymatches[$line]))
884 $matches = $ymatches[$line];
886 while (list ($junk, $y) = each($matches))
887 if (empty($this->in_seq
[$y])) {
888 $k = $this->_lcs_pos($y);
889 USE_ASSERTS
&& assert($k > 0);
890 $ymids[$k] = $ymids[$k-1];
893 while (list ($junk, $y) = each($matches)) {
894 if ($y > $this->seq
[$k-1]) {
895 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
896 // Optimization: this is a common case:
897 // next match is just replacing previous match.
898 $this->in_seq
[$this->seq
[$k]] = false;
900 $this->in_seq
[$y] = 1;
901 } else if (empty($this->in_seq
[$y])) {
902 $k = $this->_lcs_pos($y);
903 USE_ASSERTS
&& assert($k > 0);
904 $ymids[$k] = $ymids[$k-1];
908 wfProfileOut( "$fname-chunk" );
911 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
912 $ymid = $ymids[$this->lcs
];
913 for ($n = 0; $n < $nchunks - 1; $n++
) {
914 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
916 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
918 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
920 wfProfileOut( $fname );
921 return array($this->lcs
, $seps);
924 function _lcs_pos ($ypos) {
925 $fname = '_DiffEngine::_lcs_pos';
926 wfProfileIn( $fname );
929 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
930 $this->seq
[++
$this->lcs
] = $ypos;
931 $this->in_seq
[$ypos] = 1;
932 wfProfileOut( $fname );
937 while ($beg < $end) {
938 $mid = (int)(($beg +
$end) / 2);
939 if ( $ypos > $this->seq
[$mid] )
945 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
947 $this->in_seq
[$this->seq
[$end]] = false;
948 $this->seq
[$end] = $ypos;
949 $this->in_seq
[$ypos] = 1;
950 wfProfileOut( $fname );
954 /* Find LCS of two sequences.
956 * The results are recorded in the vectors $this->{x,y}changed[], by
957 * storing a 1 in the element for each line that is an insertion
958 * or deletion (ie. is not in the LCS).
960 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
962 * Note that XLIM, YLIM are exclusive bounds.
963 * All line numbers are origin-0 and discarded lines are not counted.
965 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
966 $fname = '_DiffEngine::_compareseq';
967 wfProfileIn( $fname );
969 // Slide down the bottom initial diagonal.
970 while ($xoff < $xlim && $yoff < $ylim
971 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
976 // Slide up the top initial diagonal.
977 while ($xlim > $xoff && $ylim > $yoff
978 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
983 if ($xoff == $xlim ||
$yoff == $ylim)
986 // This is ad hoc but seems to work well.
987 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
988 //$nchunks = max(2,min(8,(int)$nchunks));
989 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
991 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
995 // X and Y sequences have no common subsequence:
997 while ($yoff < $ylim)
998 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
999 while ($xoff < $xlim)
1000 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
1002 // Use the partitions to split this problem into subproblems.
1005 while ($pt2 = next($seps)) {
1006 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1010 wfProfileOut( $fname );
1013 /* Adjust inserts/deletes of identical lines to join changes
1014 * as much as possible.
1016 * We do something when a run of changed lines include a
1017 * line at one end and has an excluded, identical line at the other.
1018 * We are free to choose which identical line is included.
1019 * `compareseq' usually chooses the one at the beginning,
1020 * but usually it is cleaner to consider the following identical line
1021 * to be the "change".
1023 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1025 function _shift_boundaries ($lines, &$changed, $other_changed) {
1026 $fname = '_DiffEngine::_shift_boundaries';
1027 wfProfileIn( $fname );
1031 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1032 $len = sizeof($lines);
1033 $other_len = sizeof($other_changed);
1037 * Scan forwards to find beginning of another run of changes.
1038 * Also keep track of the corresponding point in the other file.
1040 * Throughout this code, $i and $j are adjusted together so that
1041 * the first $i elements of $changed and the first $j elements
1042 * of $other_changed both contain the same number of zeros
1043 * (unchanged lines).
1044 * Furthermore, $j is always kept so that $j == $other_len or
1045 * $other_changed[$j] == false.
1047 while ($j < $other_len && $other_changed[$j])
1050 while ($i < $len && ! $changed[$i]) {
1051 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1053 while ($j < $other_len && $other_changed[$j])
1062 // Find the end of this run of changes.
1063 while (++
$i < $len && $changed[$i])
1068 * Record the length of this run of changes, so that
1069 * we can later determine whether the run has grown.
1071 $runlength = $i - $start;
1074 * Move the changed region back, so long as the
1075 * previous unchanged line matches the last changed one.
1076 * This merges with previous changed regions.
1078 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1079 $changed[--$start] = 1;
1080 $changed[--$i] = false;
1081 while ($start > 0 && $changed[$start - 1])
1083 USE_ASSERTS
&& assert('$j > 0');
1084 while ($other_changed[--$j])
1086 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1090 * Set CORRESPONDING to the end of the changed run, at the last
1091 * point where it corresponds to a changed run in the other file.
1092 * CORRESPONDING == LEN means no such point has been found.
1094 $corresponding = $j < $other_len ?
$i : $len;
1097 * Move the changed region forward, so long as the
1098 * first changed line matches the following unchanged one.
1099 * This merges with following changed regions.
1100 * Do this second, so that if there are no merges,
1101 * the changed region is moved forward as far as possible.
1103 while ($i < $len && $lines[$start] == $lines[$i]) {
1104 $changed[$start++
] = false;
1106 while ($i < $len && $changed[$i])
1109 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1111 if ($j < $other_len && $other_changed[$j]) {
1112 $corresponding = $i;
1113 while ($j < $other_len && $other_changed[$j])
1117 } while ($runlength != $i - $start);
1120 * If possible, move the fully-merged run of changes
1121 * back to a corresponding run in the other file.
1123 while ($corresponding < $i) {
1124 $changed[--$start] = 1;
1126 USE_ASSERTS
&& assert('$j > 0');
1127 while ($other_changed[--$j])
1129 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1132 wfProfileOut( $fname );
1137 * Class representing a 'diff' between two sequences of strings.
1140 * @package MediaWiki
1141 * @subpackage DifferenceEngine
1149 * Computes diff between sequences of strings.
1151 * @param $from_lines array An array of strings.
1152 * (Typically these are lines from a file.)
1153 * @param $to_lines array An array of strings.
1155 function Diff($from_lines, $to_lines) {
1156 $eng = new _DiffEngine
;
1157 $this->edits
= $eng->diff($from_lines, $to_lines);
1158 //$this->_check($from_lines, $to_lines);
1162 * Compute reversed Diff.
1166 * $diff = new Diff($lines1, $lines2);
1167 * $rev = $diff->reverse();
1168 * @return object A Diff object representing the inverse of the
1171 function reverse () {
1173 $rev->edits
= array();
1174 foreach ($this->edits
as $edit) {
1175 $rev->edits
[] = $edit->reverse();
1181 * Check for empty diff.
1183 * @return bool True iff two sequences were identical.
1185 function isEmpty () {
1186 foreach ($this->edits
as $edit) {
1187 if ($edit->type
!= 'copy')
1194 * Compute the length of the Longest Common Subsequence (LCS).
1196 * This is mostly for diagnostic purposed.
1198 * @return int The length of the LCS.
1202 foreach ($this->edits
as $edit) {
1203 if ($edit->type
== 'copy')
1204 $lcs +
= sizeof($edit->orig
);
1210 * Get the original set of lines.
1212 * This reconstructs the $from_lines parameter passed to the
1215 * @return array The original sequence of strings.
1220 foreach ($this->edits
as $edit) {
1222 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1228 * Get the closing set of lines.
1230 * This reconstructs the $to_lines parameter passed to the
1233 * @return array The sequence of strings.
1235 function closing() {
1238 foreach ($this->edits
as $edit) {
1240 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1246 * Check a Diff for validity.
1248 * This is here only for debugging purposes.
1250 function _check ($from_lines, $to_lines) {
1251 $fname = 'Diff::_check';
1252 wfProfileIn( $fname );
1253 if (serialize($from_lines) != serialize($this->orig()))
1254 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1255 if (serialize($to_lines) != serialize($this->closing()))
1256 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1258 $rev = $this->reverse();
1259 if (serialize($to_lines) != serialize($rev->orig()))
1260 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1261 if (serialize($from_lines) != serialize($rev->closing()))
1262 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1266 foreach ($this->edits
as $edit) {
1267 if ( $prevtype == $edit->type
)
1268 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1269 $prevtype = $edit->type
;
1272 $lcs = $this->lcs();
1273 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1274 wfProfileOut( $fname );
1282 * @package MediaWiki
1283 * @subpackage DifferenceEngine
1285 class MappedDiff
extends Diff
1290 * Computes diff between sequences of strings.
1292 * This can be used to compute things like
1293 * case-insensitve diffs, or diffs which ignore
1294 * changes in white-space.
1296 * @param $from_lines array An array of strings.
1297 * (Typically these are lines from a file.)
1299 * @param $to_lines array An array of strings.
1301 * @param $mapped_from_lines array This array should
1302 * have the same size number of elements as $from_lines.
1303 * The elements in $mapped_from_lines and
1304 * $mapped_to_lines are what is actually compared
1305 * when computing the diff.
1307 * @param $mapped_to_lines array This array should
1308 * have the same number of elements as $to_lines.
1310 function MappedDiff($from_lines, $to_lines,
1311 $mapped_from_lines, $mapped_to_lines) {
1312 $fname = 'MappedDiff::MappedDiff';
1313 wfProfileIn( $fname );
1315 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1316 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1318 $this->Diff($mapped_from_lines, $mapped_to_lines);
1321 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1322 $orig = &$this->edits
[$i]->orig
;
1323 if (is_array($orig)) {
1324 $orig = array_slice($from_lines, $xi, sizeof($orig));
1325 $xi +
= sizeof($orig);
1328 $closing = &$this->edits
[$i]->closing
;
1329 if (is_array($closing)) {
1330 $closing = array_slice($to_lines, $yi, sizeof($closing));
1331 $yi +
= sizeof($closing);
1334 wfProfileOut( $fname );
1339 * A class to format Diffs
1341 * This class formats the diff in classic diff format.
1342 * It is intended that this class be customized via inheritance,
1343 * to obtain fancier outputs.
1346 * @package MediaWiki
1347 * @subpackage DifferenceEngine
1352 * Number of leading context "lines" to preserve.
1354 * This should be left at zero for this class, but subclasses
1355 * may want to set this to other values.
1357 var $leading_context_lines = 0;
1360 * Number of trailing context "lines" to preserve.
1362 * This should be left at zero for this class, but subclasses
1363 * may want to set this to other values.
1365 var $trailing_context_lines = 0;
1370 * @param $diff object A Diff object.
1371 * @return string The formatted output.
1373 function format($diff) {
1374 $fname = 'DiffFormatter::format';
1375 wfProfileIn( $fname );
1381 $nlead = $this->leading_context_lines
;
1382 $ntrail = $this->trailing_context_lines
;
1384 $this->_start_diff();
1386 foreach ($diff->edits
as $edit) {
1387 if ($edit->type
== 'copy') {
1388 if (is_array($block)) {
1389 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1394 $context = array_slice($edit->orig
, 0, $ntrail);
1395 $block[] = new _DiffOp_Copy($context);
1397 $this->_block($x0, $ntrail +
$xi - $x0,
1398 $y0, $ntrail +
$yi - $y0,
1403 $context = $edit->orig
;
1406 if (! is_array($block)) {
1407 $context = array_slice($context, sizeof($context) - $nlead);
1408 $x0 = $xi - sizeof($context);
1409 $y0 = $yi - sizeof($context);
1412 $block[] = new _DiffOp_Copy($context);
1418 $xi +
= sizeof($edit->orig
);
1420 $yi +
= sizeof($edit->closing
);
1423 if (is_array($block))
1424 $this->_block($x0, $xi - $x0,
1428 $end = $this->_end_diff();
1429 wfProfileOut( $fname );
1433 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1434 $fname = 'DiffFormatter::_block';
1435 wfProfileIn( $fname );
1436 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1437 foreach ($edits as $edit) {
1438 if ($edit->type
== 'copy')
1439 $this->_context($edit->orig
);
1440 elseif ($edit->type
== 'add')
1441 $this->_added($edit->closing
);
1442 elseif ($edit->type
== 'delete')
1443 $this->_deleted($edit->orig
);
1444 elseif ($edit->type
== 'change')
1445 $this->_changed($edit->orig
, $edit->closing
);
1447 trigger_error('Unknown edit type', E_USER_ERROR
);
1449 $this->_end_block();
1450 wfProfileOut( $fname );
1453 function _start_diff() {
1457 function _end_diff() {
1458 $val = ob_get_contents();
1463 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1465 $xbeg .= "," . ($xbeg +
$xlen - 1);
1467 $ybeg .= "," . ($ybeg +
$ylen - 1);
1469 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1472 function _start_block($header) {
1476 function _end_block() {
1479 function _lines($lines, $prefix = ' ') {
1480 foreach ($lines as $line)
1481 echo "$prefix $line\n";
1484 function _context($lines) {
1485 $this->_lines($lines);
1488 function _added($lines) {
1489 $this->_lines($lines, '>');
1491 function _deleted($lines) {
1492 $this->_lines($lines, '<');
1495 function _changed($orig, $closing) {
1496 $this->_deleted($orig);
1498 $this->_added($closing);
1504 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1508 define('NBSP', ' '); // iso-8859-x non-breaking space.
1513 * @package MediaWiki
1514 * @subpackage DifferenceEngine
1516 class _HWLDF_WordAccumulator
{
1517 function _HWLDF_WordAccumulator () {
1518 $this->_lines
= array();
1524 function _flushGroup ($new_tag) {
1525 if ($this->_group
!== '') {
1526 if ($this->_tag
== 'mark')
1527 $this->_line
.= '<span class="diffchange">' .
1528 htmlspecialchars ( $this->_group
) . '</span>';
1530 $this->_line
.= htmlspecialchars ( $this->_group
);
1533 $this->_tag
= $new_tag;
1536 function _flushLine ($new_tag) {
1537 $this->_flushGroup($new_tag);
1538 if ($this->_line
!= '')
1539 array_push ( $this->_lines
, $this->_line
);
1541 # make empty lines visible by inserting an NBSP
1542 array_push ( $this->_lines
, NBSP
);
1546 function addWords ($words, $tag = '') {
1547 if ($tag != $this->_tag
)
1548 $this->_flushGroup($tag);
1550 foreach ($words as $word) {
1551 // new-line should only come as first char of word.
1554 if ($word[0] == "\n") {
1555 $this->_flushLine($tag);
1556 $word = substr($word, 1);
1558 assert(!strstr($word, "\n"));
1559 $this->_group
.= $word;
1563 function getLines() {
1564 $this->_flushLine('~done');
1565 return $this->_lines
;
1572 * @package MediaWiki
1573 * @subpackage DifferenceEngine
1575 class WordLevelDiff
extends MappedDiff
1577 const MAX_LINE_LENGTH
= 10000;
1579 function WordLevelDiff ($orig_lines, $closing_lines) {
1580 $fname = 'WordLevelDiff::WordLevelDiff';
1581 wfProfileIn( $fname );
1583 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1584 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1586 $this->MappedDiff($orig_words, $closing_words,
1587 $orig_stripped, $closing_stripped);
1588 wfProfileOut( $fname );
1591 function _split($lines) {
1592 $fname = 'WordLevelDiff::_split';
1593 wfProfileIn( $fname );
1596 $stripped = array();
1598 foreach ( $lines as $line ) {
1599 # If the line is too long, just pretend the entire line is one big word
1600 # This prevents resource exhaustion problems
1607 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1609 $stripped[] = $line;
1611 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1614 $words = array_merge( $words, $m[0] );
1615 $stripped = array_merge( $stripped, $m[1] );
1619 wfProfileOut( $fname );
1620 return array($words, $stripped);
1624 $fname = 'WordLevelDiff::orig';
1625 wfProfileIn( $fname );
1626 $orig = new _HWLDF_WordAccumulator
;
1628 foreach ($this->edits
as $edit) {
1629 if ($edit->type
== 'copy')
1630 $orig->addWords($edit->orig
);
1631 elseif ($edit->orig
)
1632 $orig->addWords($edit->orig
, 'mark');
1634 $lines = $orig->getLines();
1635 wfProfileOut( $fname );
1639 function closing () {
1640 $fname = 'WordLevelDiff::closing';
1641 wfProfileIn( $fname );
1642 $closing = new _HWLDF_WordAccumulator
;
1644 foreach ($this->edits
as $edit) {
1645 if ($edit->type
== 'copy')
1646 $closing->addWords($edit->closing
);
1647 elseif ($edit->closing
)
1648 $closing->addWords($edit->closing
, 'mark');
1650 $lines = $closing->getLines();
1651 wfProfileOut( $fname );
1657 * Wikipedia Table style diff formatter.
1660 * @package MediaWiki
1661 * @subpackage DifferenceEngine
1663 class TableDiffFormatter
extends DiffFormatter
1665 function TableDiffFormatter() {
1666 $this->leading_context_lines
= 2;
1667 $this->trailing_context_lines
= 2;
1670 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1671 $r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" .
1672 '<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n";
1676 function _start_block( $header ) {
1680 function _end_block() {
1683 function _lines( $lines, $prefix=' ', $color='white' ) {
1686 # HTML-escape parameter before calling this
1687 function addedLine( $line ) {
1688 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1691 # HTML-escape parameter before calling this
1692 function deletedLine( $line ) {
1693 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1696 # HTML-escape parameter before calling this
1697 function contextLine( $line ) {
1698 return "<td> </td><td class='diff-context'>{$line}</td>";
1701 function emptyLine() {
1702 return '<td colspan="2"> </td>';
1705 function _added( $lines ) {
1706 foreach ($lines as $line) {
1707 echo '<tr>' . $this->emptyLine() .
1708 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1712 function _deleted($lines) {
1713 foreach ($lines as $line) {
1714 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1715 $this->emptyLine() . "</tr>\n";
1719 function _context( $lines ) {
1720 foreach ($lines as $line) {
1722 $this->contextLine( htmlspecialchars ( $line ) ) .
1723 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1727 function _changed( $orig, $closing ) {
1728 $fname = 'TableDiffFormatter::_changed';
1729 wfProfileIn( $fname );
1731 $diff = new WordLevelDiff( $orig, $closing );
1732 $del = $diff->orig();
1733 $add = $diff->closing();
1735 # Notice that WordLevelDiff returns HTML-escaped output.
1736 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1738 while ( $line = array_shift( $del ) ) {
1739 $aline = array_shift( $add );
1740 echo '<tr>' . $this->deletedLine( $line ) .
1741 $this->addedLine( $aline ) . "</tr>\n";
1743 foreach ($add as $line) { # If any leftovers
1744 echo '<tr>' . $this->emptyLine() .
1745 $this->addedLine( $line ) . "</tr>\n";
1747 wfProfileOut( $fname );