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 $wgOut->setArticleFlag( false );
101 if ( ! $this->loadRevisionData() ) {
102 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, {$this->mNewid})";
103 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
104 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
105 $wgOut->addWikitext( $mtext );
106 wfProfileOut( $fname );
110 wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev
, $this->mNewRev
) );
112 if ( $this->mNewRev
->isCurrent() ) {
113 $wgOut->setArticleFlag( true );
116 # mOldid is false if the difference engine is called with a "vague" query for
117 # a diff between a version V and its previous version V' AND the version V
118 # is the first version of that article. In that case, V' does not exist.
119 if ( $this->mOldid
=== false ) {
120 $this->showFirstRevision();
121 wfProfileOut( $fname );
125 $wgOut->suppressQuickbar();
127 $oldTitle = $this->mOldPage
->getPrefixedText();
128 $newTitle = $this->mNewPage
->getPrefixedText();
129 if( $oldTitle == $newTitle ) {
130 $wgOut->setPageTitle( $newTitle );
132 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
134 $wgOut->setSubtitle( wfMsg( 'difference' ) );
135 $wgOut->setRobotpolicy( 'noindex,nofollow' );
137 if ( !( $this->mOldPage
->userCanRead() && $this->mNewPage
->userCanRead() ) ) {
138 $wgOut->loginToUse();
140 wfProfileOut( $fname );
144 $sk = $wgUser->getSkin();
146 if ( $this->mNewRev
->isCurrent() && $wgUser->isAllowed('rollback') ) {
147 $rollback = ' ' . $sk->generateRollback( $this->mNewRev
);
151 if( $wgUseRCPatrol && $this->mRcidMarkPatrolled
!= 0 && $wgUser->isAllowed( 'patrol' ) ) {
152 $patrol = ' [' . $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'markaspatrolleddiff' ), "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']';
157 $prevlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'previousdiff' ),
158 'diff=prev&oldid='.$this->mOldid
, '', '', 'id="differences-prevlink"' );
159 if ( $this->mNewRev
->isCurrent() ) {
160 $nextlink = ' ';
162 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ),
163 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
169 if ($this->mOldRev
->mMinorEdit
== 1) {
170 $oldminor = wfElement( 'span', array( 'class' => 'minor' ),
171 wfMsg( 'minoreditletter') ) . ' ';
174 if ($this->mNewRev
->mMinorEdit
== 1) {
175 $newminor = wfElement( 'span', array( 'class' => 'minor' ),
176 wfMsg( 'minoreditletter') ) . ' ';
179 $oldHeader = "<strong>{$this->mOldtitle}</strong><br />" .
180 $sk->revUserTools( $this->mOldRev
) . "<br />" .
181 $oldminor . $sk->revComment( $this->mOldRev
, true ) . "<br />" .
183 $newHeader = "<strong>{$this->mNewtitle}</strong><br />" .
184 $sk->revUserTools( $this->mNewRev
) . " $rollback<br />" .
185 $newminor . $sk->revComment( $this->mNewRev
, true ) . "<br />" .
188 $this->showDiff( $oldHeader, $newHeader );
189 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
191 if( !$this->mNewRev
->isCurrent() ) {
192 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
195 $this->loadNewText();
196 if( is_object( $this->mNewRev
) ) {
197 $wgOut->setRevisionId( $this->mNewRev
->getId() );
199 $wgOut->addSecondaryWikiText( $this->mNewtext
);
201 if( !$this->mNewRev
->isCurrent() ) {
202 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
205 wfProfileOut( $fname );
209 * Show the first revision of an article. Uses normal diff headers in
210 * contrast to normal "old revision" display style.
212 function showFirstRevision() {
213 global $wgOut, $wgUser;
215 $fname = 'DifferenceEngine::showFirstRevision';
216 wfProfileIn( $fname );
218 # Get article text from the DB
220 if ( ! $this->loadNewText() ) {
221 $t = $this->mTitle
->getPrefixedText() . " (Diff: {$this->mOldid}, " .
223 $mtext = wfMsg( 'missingarticle', "<nowiki>$t</nowiki>" );
224 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
225 $wgOut->addWikitext( $mtext );
226 wfProfileOut( $fname );
229 if ( $this->mNewRev
->isCurrent() ) {
230 $wgOut->setArticleFlag( true );
233 # Check if user is allowed to look at this page. If not, bail out.
235 if ( !( $this->mTitle
->userCanRead() ) ) {
236 $wgOut->loginToUse();
238 wfProfileOut( $fname );
242 # Prepare the header box
244 $sk = $wgUser->getSkin();
246 $nextlink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid
, '', '', 'id="differences-nextlink"' );
247 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />" .
248 $sk->revUserTools( $this->mNewRev
) . "<br />" .
249 $sk->revComment( $this->mNewRev
) . "<br />" .
250 $nextlink . "</div>\n";
252 $wgOut->addHTML( $header );
254 $wgOut->setSubtitle( wfMsg( 'difference' ) );
255 $wgOut->setRobotpolicy( 'noindex,nofollow' );
258 # Show current revision
260 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
261 if( is_object( $this->mNewRev
) ) {
262 $wgOut->setRevisionId( $this->mNewRev
->getId() );
264 $wgOut->addSecondaryWikiText( $this->mNewtext
);
266 wfProfileOut( $fname );
270 * Get the diff text, send it to $wgOut
271 * Returns false if the diff could not be generated, otherwise returns true
273 function showDiff( $otitle, $ntitle ) {
275 $diff = $this->getDiff( $otitle, $ntitle );
276 if ( $diff === false ) {
277 $wgOut->addWikitext( wfMsg( 'missingarticle', "<nowiki>(fixme, bug)</nowiki>" ) );
280 $wgOut->addHTML( $diff );
286 * Get diff table, including header
287 * Note that the interface has changed, it's no longer static.
288 * Returns false on error
290 function getDiff( $otitle, $ntitle ) {
291 $body = $this->getDiffBody();
292 if ( $body === false ) {
295 $multi = $this->getMultiNotice();
296 return $this->addHeader( $body, $otitle, $ntitle, $multi );
301 * Get the diff table body, without header
303 * Returns false on error
305 function getDiffBody() {
307 $fname = 'DifferenceEngine::getDiffBody';
308 wfProfileIn( $fname );
312 if ( $this->mOldid
&& $this->mNewid
) {
314 $key = wfMemcKey( 'diff', 'oldid', $this->mOldid
, 'newid', $this->mNewid
);
315 $difftext = $wgMemc->get( $key );
317 wfIncrStats( 'diff_cache_hit' );
318 $difftext = $this->localiseLineNumbers( $difftext );
319 $difftext .= "\n<!-- diff cache key $key -->\n";
320 wfProfileOut( $fname );
325 if ( !$this->loadText() ) {
326 wfProfileOut( $fname );
330 $difftext = $this->generateDiffBody( $this->mOldtext
, $this->mNewtext
);
332 // Save to cache for 7 days
333 if ( $key !== false && $difftext !== false ) {
334 wfIncrStats( 'diff_cache_miss' );
335 $wgMemc->set( $key, $difftext, 7*86400 );
337 wfIncrStats( 'diff_uncacheable' );
339 // Replace line numbers with the text in the user's language
340 if ( $difftext !== false ) {
341 $difftext = $this->localiseLineNumbers( $difftext );
343 wfProfileOut( $fname );
348 * Generate a diff, no caching
349 * $otext and $ntext must be already segmented
351 function generateDiffBody( $otext, $ntext ) {
352 global $wgExternalDiffEngine, $wgContLang;
353 $fname = 'DifferenceEngine::generateDiffBody';
355 $otext = str_replace( "\r\n", "\n", $otext );
356 $ntext = str_replace( "\r\n", "\n", $ntext );
358 if ( $wgExternalDiffEngine == 'wikidiff' ) {
359 # For historical reasons, external diff engine expects
360 # input text to be HTML-escaped already
361 $otext = htmlspecialchars ( $wgContLang->segmentForDiff( $otext ) );
362 $ntext = htmlspecialchars ( $wgContLang->segmentForDiff( $ntext ) );
363 if( !function_exists( 'wikidiff_do_diff' ) ) {
364 dl('php_wikidiff.so');
366 return $wgContLang->unsegementForDiff( wikidiff_do_diff( $otext, $ntext, 2 ) );
369 if ( $wgExternalDiffEngine == 'wikidiff2' ) {
370 # Better external diff engine, the 2 may some day be dropped
371 # This one does the escaping and segmenting itself
372 if ( !function_exists( 'wikidiff2_do_diff' ) ) {
373 wfProfileIn( "$fname-dl" );
374 @dl
('php_wikidiff2.so');
375 wfProfileOut( "$fname-dl" );
377 if ( function_exists( 'wikidiff2_do_diff' ) ) {
378 wfProfileIn( 'wikidiff2_do_diff' );
379 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
380 wfProfileOut( 'wikidiff2_do_diff' );
384 if ( $wgExternalDiffEngine !== false ) {
386 global $wgTmpDirectory;
387 $tempName1 = tempnam( $wgTmpDirectory, 'diff_' );
388 $tempName2 = tempnam( $wgTmpDirectory, 'diff_' );
390 $tempFile1 = fopen( $tempName1, "w" );
392 wfProfileOut( $fname );
395 $tempFile2 = fopen( $tempName2, "w" );
397 wfProfileOut( $fname );
400 fwrite( $tempFile1, $otext );
401 fwrite( $tempFile2, $ntext );
402 fclose( $tempFile1 );
403 fclose( $tempFile2 );
404 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
405 wfProfileIn( "$fname-shellexec" );
406 $difftext = wfShellExec( $cmd );
407 wfProfileOut( "$fname-shellexec" );
408 unlink( $tempName1 );
409 unlink( $tempName2 );
414 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
415 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
416 $diffs = new Diff( $ota, $nta );
417 $formatter = new TableDiffFormatter();
418 return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
423 * Replace line numbers with the text in the user's language
425 function localiseLineNumbers( $text ) {
426 return preg_replace_callback( '/<!--LINE (\d+)-->/',
427 array( &$this, 'localiseLineNumbersCb' ), $text );
430 function localiseLineNumbersCb( $matches ) {
432 return wfMsgExt( 'lineno', array('parseinline'), $wgLang->formatNum( $matches[1] ) );
437 * If there are revisions between the ones being compared, return a note saying so.
439 function getMultiNotice() {
440 if ( !is_object($this->mOldRev
) ||
!is_object($this->mNewRev
) )
443 $oldid = $this->mOldRev
->getId();
444 $newid = $this->mNewRev
->getId();
445 if ( $oldid > $newid ) {
446 $tmp = $oldid; $oldid = $newid; $newid = $tmp;
449 $n = $this->mTitle
->countRevisionsBetween( $oldid, $newid );
453 $msg = wfMsgExt( 'diff-multi-revs', array( 'parseinline' ), $n );
454 return wfMsgExt( 'diff-multi', array('parseinline'), $msg );
459 * Add the header to a diff body
461 function addHeader( $diff, $otitle, $ntitle, $multi = '' ) {
463 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
465 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
466 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
471 $header .= "<tr><td colspan='4' align='center' class='diff-multi'>{$multi}</td></tr>";
473 return $header . $diff . "</table>";
477 * Use specified text instead of loading from the database
479 function setText( $oldText, $newText ) {
480 $this->mOldtext
= $oldText;
481 $this->mNewtext
= $newText;
482 $this->mTextLoaded
= 2;
486 * Load revision metadata for the specified articles. If newid is 0, then compare
487 * the old article in oldid to the current article; if oldid is 0, then
488 * compare the current article to the immediately previous one (ignoring the
491 * If oldid is false, leave the corresponding revision object set
492 * to false. This is impossible via ordinary user input, and is provided for
495 function loadRevisionData() {
497 if ( $this->mRevisionsLoaded
) {
500 // Whether it succeeds or fails, we don't want to try again
501 $this->mRevisionsLoaded
= true;
504 // Load the new revision object
505 if( $this->mNewid
) {
506 $this->mNewRev
= Revision
::newFromId( $this->mNewid
);
508 $this->mNewRev
= Revision
::newFromTitle( $this->mTitle
);
511 if( is_null( $this->mNewRev
) ) {
515 // Set assorted variables
516 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
517 $this->mNewPage
= $this->mNewRev
->getTitle();
518 if( $this->mNewRev
->isCurrent() ) {
519 $newLink = $this->mNewPage
->escapeLocalUrl();
520 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
521 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit' );
523 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a> ($timestamp)</strong>"
524 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
527 $newLink = $this->mNewPage
->escapeLocalUrl( 'oldid=' . $this->mNewid
);
528 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mNewid
);
529 $newUndo = $this->mNewPage
->escapeLocalUrl( 'action=edit&undo=' . $this->mNewid
);
530 $this->mPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $timestamp ) );
532 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a></strong>"
533 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)"
534 . " (<a href='$newUndo'>" . htmlspecialchars( wfMsg( 'editundo' ) ) . "</a>)";
537 // Load the old revision object
538 $this->mOldRev
= false;
539 if( $this->mOldid
) {
540 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
541 } elseif ( $this->mOldid
=== 0 ) {
542 $rev = $this->mNewRev
->getPrevious();
544 $this->mOldid
= $rev->getId();
545 $this->mOldRev
= $rev;
547 // No previous revision; mark to show as first-version only.
548 $this->mOldid
= false;
549 $this->mOldRev
= false;
551 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
553 if( is_null( $this->mOldRev
) ) {
557 if ( $this->mOldRev
) {
558 $this->mOldPage
= $this->mOldRev
->getTitle();
560 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
561 $oldLink = $this->mOldPage
->escapeLocalUrl( 'oldid=' . $this->mOldid
);
562 $oldEdit = $this->mOldPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mOldid
);
563 $this->mOldtitle
= "<strong><a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) )
564 . "</a></strong> (<a href='$oldEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
571 * Load the text of the revisions, as well as revision data.
573 function loadText() {
574 if ( $this->mTextLoaded
== 2 ) {
577 // Whether it succeeds or fails, we don't want to try again
578 $this->mTextLoaded
= 2;
581 if ( !$this->loadRevisionData() ) {
584 if ( $this->mOldRev
) {
585 // FIXME: permission tests
586 $this->mOldtext
= $this->mOldRev
->getText();
587 if ( $this->mOldtext
=== false ) {
591 if ( $this->mNewRev
) {
592 $this->mNewtext
= $this->mNewRev
->getText();
593 if ( $this->mNewtext
=== false ) {
601 * Load the text of the new revision, not the old one
603 function loadNewText() {
604 if ( $this->mTextLoaded
>= 1 ) {
607 $this->mTextLoaded
= 1;
609 if ( !$this->loadRevisionData() ) {
612 $this->mNewtext
= $this->mNewRev
->getText();
619 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
621 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
622 // You may copy this code freely under the conditions of the GPL.
625 define('USE_ASSERTS', function_exists('assert'));
631 * @subpackage DifferenceEngine
639 trigger_error('pure virtual', E_USER_ERROR
);
643 return $this->orig ?
sizeof($this->orig
) : 0;
646 function nclosing() {
647 return $this->closing ?
sizeof($this->closing
) : 0;
655 * @subpackage DifferenceEngine
657 class _DiffOp_Copy
extends _DiffOp
{
660 function _DiffOp_Copy ($orig, $closing = false) {
661 if (!is_array($closing))
664 $this->closing
= $closing;
668 return new _DiffOp_Copy($this->closing
, $this->orig
);
676 * @subpackage DifferenceEngine
678 class _DiffOp_Delete
extends _DiffOp
{
679 var $type = 'delete';
681 function _DiffOp_Delete ($lines) {
682 $this->orig
= $lines;
683 $this->closing
= false;
687 return new _DiffOp_Add($this->orig
);
695 * @subpackage DifferenceEngine
697 class _DiffOp_Add
extends _DiffOp
{
700 function _DiffOp_Add ($lines) {
701 $this->closing
= $lines;
706 return new _DiffOp_Delete($this->closing
);
714 * @subpackage DifferenceEngine
716 class _DiffOp_Change
extends _DiffOp
{
717 var $type = 'change';
719 function _DiffOp_Change ($orig, $closing) {
721 $this->closing
= $closing;
725 return new _DiffOp_Change($this->closing
, $this->orig
);
731 * Class used internally by Diff to actually compute the diffs.
733 * The algorithm used here is mostly lifted from the perl module
734 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
735 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
737 * More ideas are taken from:
738 * http://www.ics.uci.edu/~eppstein/161/960229.html
740 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
741 * diffutils-2.7, which can be found at:
742 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
744 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
747 * Line length limits for robustness added by Tim Starling, 2005-08-31
749 * @author Geoffrey T. Dairiki, Tim Starling
752 * @subpackage DifferenceEngine
756 const MAX_XREF_LENGTH
= 10000;
758 function diff ($from_lines, $to_lines) {
759 $fname = '_DiffEngine::diff';
760 wfProfileIn( $fname );
762 $n_from = sizeof($from_lines);
763 $n_to = sizeof($to_lines);
765 $this->xchanged
= $this->ychanged
= array();
766 $this->xv
= $this->yv
= array();
767 $this->xind
= $this->yind
= array();
769 unset($this->in_seq
);
772 // Skip leading common lines.
773 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
774 if ($from_lines[$skip] !== $to_lines[$skip])
776 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
778 // Skip trailing common lines.
779 $xi = $n_from; $yi = $n_to;
780 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
781 if ($from_lines[$xi] !== $to_lines[$yi])
783 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
786 // Ignore lines which do not exist in both files.
787 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
788 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
791 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
792 $line = $to_lines[$yi];
793 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
795 $yhash[$this->_line_hash($line)] = 1;
799 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
800 $line = $from_lines[$xi];
801 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
808 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
810 // Merge edits when possible
811 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
812 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
814 // Compute the edit operations.
817 while ($xi < $n_from ||
$yi < $n_to) {
818 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
819 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
821 // Skip matching "snake".
823 while ( $xi < $n_from && $yi < $n_to
824 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
825 $copy[] = $from_lines[$xi++
];
829 $edits[] = new _DiffOp_Copy($copy);
831 // Find deletes & adds.
833 while ($xi < $n_from && $this->xchanged
[$xi])
834 $delete[] = $from_lines[$xi++
];
837 while ($yi < $n_to && $this->ychanged
[$yi])
838 $add[] = $to_lines[$yi++
];
841 $edits[] = new _DiffOp_Change($delete, $add);
843 $edits[] = new _DiffOp_Delete($delete);
845 $edits[] = new _DiffOp_Add($add);
847 wfProfileOut( $fname );
852 * Returns the whole line if it's small enough, or the MD5 hash otherwise
854 function _line_hash( $line ) {
855 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
863 /* Divide the Largest Common Subsequence (LCS) of the sequences
864 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
867 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
868 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
869 * sub sequences. The first sub-sequence is contained in [X0, X1),
870 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
871 * that (X0, Y0) == (XOFF, YOFF) and
872 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
874 * This function assumes that the first lines of the specified portions
875 * of the two files do not match, and likewise that the last lines do not
876 * match. The caller must trim matching lines from the beginning and end
877 * of the portions it is going to specify.
879 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
880 $fname = '_DiffEngine::_diag';
881 wfProfileIn( $fname );
884 if ($xlim - $xoff > $ylim - $yoff) {
885 // Things seems faster (I'm not sure I understand why)
886 // when the shortest sequence in X.
888 list ($xoff, $xlim, $yoff, $ylim)
889 = array( $yoff, $ylim, $xoff, $xlim);
893 for ($i = $ylim - 1; $i >= $yoff; $i--)
894 $ymatches[$this->xv
[$i]][] = $i;
896 for ($i = $ylim - 1; $i >= $yoff; $i--)
897 $ymatches[$this->yv
[$i]][] = $i;
900 $this->seq
[0]= $yoff - 1;
901 $this->in_seq
= array();
904 $numer = $xlim - $xoff +
$nchunks - 1;
906 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
907 wfProfileIn( "$fname-chunk" );
909 for ($i = 0; $i <= $this->lcs
; $i++
)
910 $ymids[$i][$chunk-1] = $this->seq
[$i];
912 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
913 for ( ; $x < $x1; $x++
) {
914 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
915 if (empty($ymatches[$line]))
917 $matches = $ymatches[$line];
919 while (list ($junk, $y) = each($matches))
920 if (empty($this->in_seq
[$y])) {
921 $k = $this->_lcs_pos($y);
922 USE_ASSERTS
&& assert($k > 0);
923 $ymids[$k] = $ymids[$k-1];
926 while (list ( /* $junk */, $y) = each($matches)) {
927 if ($y > $this->seq
[$k-1]) {
928 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
929 // Optimization: this is a common case:
930 // next match is just replacing previous match.
931 $this->in_seq
[$this->seq
[$k]] = false;
933 $this->in_seq
[$y] = 1;
934 } else if (empty($this->in_seq
[$y])) {
935 $k = $this->_lcs_pos($y);
936 USE_ASSERTS
&& assert($k > 0);
937 $ymids[$k] = $ymids[$k-1];
941 wfProfileOut( "$fname-chunk" );
944 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
945 $ymid = $ymids[$this->lcs
];
946 for ($n = 0; $n < $nchunks - 1; $n++
) {
947 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
949 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
951 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
953 wfProfileOut( $fname );
954 return array($this->lcs
, $seps);
957 function _lcs_pos ($ypos) {
958 $fname = '_DiffEngine::_lcs_pos';
959 wfProfileIn( $fname );
962 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
963 $this->seq
[++
$this->lcs
] = $ypos;
964 $this->in_seq
[$ypos] = 1;
965 wfProfileOut( $fname );
970 while ($beg < $end) {
971 $mid = (int)(($beg +
$end) / 2);
972 if ( $ypos > $this->seq
[$mid] )
978 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
980 $this->in_seq
[$this->seq
[$end]] = false;
981 $this->seq
[$end] = $ypos;
982 $this->in_seq
[$ypos] = 1;
983 wfProfileOut( $fname );
987 /* Find LCS of two sequences.
989 * The results are recorded in the vectors $this->{x,y}changed[], by
990 * storing a 1 in the element for each line that is an insertion
991 * or deletion (ie. is not in the LCS).
993 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
995 * Note that XLIM, YLIM are exclusive bounds.
996 * All line numbers are origin-0 and discarded lines are not counted.
998 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
999 $fname = '_DiffEngine::_compareseq';
1000 wfProfileIn( $fname );
1002 // Slide down the bottom initial diagonal.
1003 while ($xoff < $xlim && $yoff < $ylim
1004 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
1009 // Slide up the top initial diagonal.
1010 while ($xlim > $xoff && $ylim > $yoff
1011 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
1016 if ($xoff == $xlim ||
$yoff == $ylim)
1019 // This is ad hoc but seems to work well.
1020 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
1021 //$nchunks = max(2,min(8,(int)$nchunks));
1022 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
1024 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
1028 // X and Y sequences have no common subsequence:
1029 // mark all changed.
1030 while ($yoff < $ylim)
1031 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
1032 while ($xoff < $xlim)
1033 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
1035 // Use the partitions to split this problem into subproblems.
1038 while ($pt2 = next($seps)) {
1039 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1043 wfProfileOut( $fname );
1046 /* Adjust inserts/deletes of identical lines to join changes
1047 * as much as possible.
1049 * We do something when a run of changed lines include a
1050 * line at one end and has an excluded, identical line at the other.
1051 * We are free to choose which identical line is included.
1052 * `compareseq' usually chooses the one at the beginning,
1053 * but usually it is cleaner to consider the following identical line
1054 * to be the "change".
1056 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1058 function _shift_boundaries ($lines, &$changed, $other_changed) {
1059 $fname = '_DiffEngine::_shift_boundaries';
1060 wfProfileIn( $fname );
1064 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1065 $len = sizeof($lines);
1066 $other_len = sizeof($other_changed);
1070 * Scan forwards to find beginning of another run of changes.
1071 * Also keep track of the corresponding point in the other file.
1073 * Throughout this code, $i and $j are adjusted together so that
1074 * the first $i elements of $changed and the first $j elements
1075 * of $other_changed both contain the same number of zeros
1076 * (unchanged lines).
1077 * Furthermore, $j is always kept so that $j == $other_len or
1078 * $other_changed[$j] == false.
1080 while ($j < $other_len && $other_changed[$j])
1083 while ($i < $len && ! $changed[$i]) {
1084 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1086 while ($j < $other_len && $other_changed[$j])
1095 // Find the end of this run of changes.
1096 while (++
$i < $len && $changed[$i])
1101 * Record the length of this run of changes, so that
1102 * we can later determine whether the run has grown.
1104 $runlength = $i - $start;
1107 * Move the changed region back, so long as the
1108 * previous unchanged line matches the last changed one.
1109 * This merges with previous changed regions.
1111 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1112 $changed[--$start] = 1;
1113 $changed[--$i] = false;
1114 while ($start > 0 && $changed[$start - 1])
1116 USE_ASSERTS
&& assert('$j > 0');
1117 while ($other_changed[--$j])
1119 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1123 * Set CORRESPONDING to the end of the changed run, at the last
1124 * point where it corresponds to a changed run in the other file.
1125 * CORRESPONDING == LEN means no such point has been found.
1127 $corresponding = $j < $other_len ?
$i : $len;
1130 * Move the changed region forward, so long as the
1131 * first changed line matches the following unchanged one.
1132 * This merges with following changed regions.
1133 * Do this second, so that if there are no merges,
1134 * the changed region is moved forward as far as possible.
1136 while ($i < $len && $lines[$start] == $lines[$i]) {
1137 $changed[$start++
] = false;
1139 while ($i < $len && $changed[$i])
1142 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1144 if ($j < $other_len && $other_changed[$j]) {
1145 $corresponding = $i;
1146 while ($j < $other_len && $other_changed[$j])
1150 } while ($runlength != $i - $start);
1153 * If possible, move the fully-merged run of changes
1154 * back to a corresponding run in the other file.
1156 while ($corresponding < $i) {
1157 $changed[--$start] = 1;
1159 USE_ASSERTS
&& assert('$j > 0');
1160 while ($other_changed[--$j])
1162 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1165 wfProfileOut( $fname );
1170 * Class representing a 'diff' between two sequences of strings.
1173 * @package MediaWiki
1174 * @subpackage DifferenceEngine
1182 * Computes diff between sequences of strings.
1184 * @param $from_lines array An array of strings.
1185 * (Typically these are lines from a file.)
1186 * @param $to_lines array An array of strings.
1188 function Diff($from_lines, $to_lines) {
1189 $eng = new _DiffEngine
;
1190 $this->edits
= $eng->diff($from_lines, $to_lines);
1191 //$this->_check($from_lines, $to_lines);
1195 * Compute reversed Diff.
1199 * $diff = new Diff($lines1, $lines2);
1200 * $rev = $diff->reverse();
1201 * @return object A Diff object representing the inverse of the
1204 function reverse () {
1206 $rev->edits
= array();
1207 foreach ($this->edits
as $edit) {
1208 $rev->edits
[] = $edit->reverse();
1214 * Check for empty diff.
1216 * @return bool True iff two sequences were identical.
1218 function isEmpty () {
1219 foreach ($this->edits
as $edit) {
1220 if ($edit->type
!= 'copy')
1227 * Compute the length of the Longest Common Subsequence (LCS).
1229 * This is mostly for diagnostic purposed.
1231 * @return int The length of the LCS.
1235 foreach ($this->edits
as $edit) {
1236 if ($edit->type
== 'copy')
1237 $lcs +
= sizeof($edit->orig
);
1243 * Get the original set of lines.
1245 * This reconstructs the $from_lines parameter passed to the
1248 * @return array The original sequence of strings.
1253 foreach ($this->edits
as $edit) {
1255 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1261 * Get the closing set of lines.
1263 * This reconstructs the $to_lines parameter passed to the
1266 * @return array The sequence of strings.
1268 function closing() {
1271 foreach ($this->edits
as $edit) {
1273 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1279 * Check a Diff for validity.
1281 * This is here only for debugging purposes.
1283 function _check ($from_lines, $to_lines) {
1284 $fname = 'Diff::_check';
1285 wfProfileIn( $fname );
1286 if (serialize($from_lines) != serialize($this->orig()))
1287 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1288 if (serialize($to_lines) != serialize($this->closing()))
1289 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1291 $rev = $this->reverse();
1292 if (serialize($to_lines) != serialize($rev->orig()))
1293 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1294 if (serialize($from_lines) != serialize($rev->closing()))
1295 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1299 foreach ($this->edits
as $edit) {
1300 if ( $prevtype == $edit->type
)
1301 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1302 $prevtype = $edit->type
;
1305 $lcs = $this->lcs();
1306 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1307 wfProfileOut( $fname );
1315 * @package MediaWiki
1316 * @subpackage DifferenceEngine
1318 class MappedDiff
extends Diff
1323 * Computes diff between sequences of strings.
1325 * This can be used to compute things like
1326 * case-insensitve diffs, or diffs which ignore
1327 * changes in white-space.
1329 * @param $from_lines array An array of strings.
1330 * (Typically these are lines from a file.)
1332 * @param $to_lines array An array of strings.
1334 * @param $mapped_from_lines array This array should
1335 * have the same size number of elements as $from_lines.
1336 * The elements in $mapped_from_lines and
1337 * $mapped_to_lines are what is actually compared
1338 * when computing the diff.
1340 * @param $mapped_to_lines array This array should
1341 * have the same number of elements as $to_lines.
1343 function MappedDiff($from_lines, $to_lines,
1344 $mapped_from_lines, $mapped_to_lines) {
1345 $fname = 'MappedDiff::MappedDiff';
1346 wfProfileIn( $fname );
1348 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1349 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1351 $this->Diff($mapped_from_lines, $mapped_to_lines);
1354 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1355 $orig = &$this->edits
[$i]->orig
;
1356 if (is_array($orig)) {
1357 $orig = array_slice($from_lines, $xi, sizeof($orig));
1358 $xi +
= sizeof($orig);
1361 $closing = &$this->edits
[$i]->closing
;
1362 if (is_array($closing)) {
1363 $closing = array_slice($to_lines, $yi, sizeof($closing));
1364 $yi +
= sizeof($closing);
1367 wfProfileOut( $fname );
1372 * A class to format Diffs
1374 * This class formats the diff in classic diff format.
1375 * It is intended that this class be customized via inheritance,
1376 * to obtain fancier outputs.
1379 * @package MediaWiki
1380 * @subpackage DifferenceEngine
1385 * Number of leading context "lines" to preserve.
1387 * This should be left at zero for this class, but subclasses
1388 * may want to set this to other values.
1390 var $leading_context_lines = 0;
1393 * Number of trailing context "lines" to preserve.
1395 * This should be left at zero for this class, but subclasses
1396 * may want to set this to other values.
1398 var $trailing_context_lines = 0;
1403 * @param $diff object A Diff object.
1404 * @return string The formatted output.
1406 function format($diff) {
1407 $fname = 'DiffFormatter::format';
1408 wfProfileIn( $fname );
1414 $nlead = $this->leading_context_lines
;
1415 $ntrail = $this->trailing_context_lines
;
1417 $this->_start_diff();
1419 foreach ($diff->edits
as $edit) {
1420 if ($edit->type
== 'copy') {
1421 if (is_array($block)) {
1422 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1427 $context = array_slice($edit->orig
, 0, $ntrail);
1428 $block[] = new _DiffOp_Copy($context);
1430 $this->_block($x0, $ntrail +
$xi - $x0,
1431 $y0, $ntrail +
$yi - $y0,
1436 $context = $edit->orig
;
1439 if (! is_array($block)) {
1440 $context = array_slice($context, sizeof($context) - $nlead);
1441 $x0 = $xi - sizeof($context);
1442 $y0 = $yi - sizeof($context);
1445 $block[] = new _DiffOp_Copy($context);
1451 $xi +
= sizeof($edit->orig
);
1453 $yi +
= sizeof($edit->closing
);
1456 if (is_array($block))
1457 $this->_block($x0, $xi - $x0,
1461 $end = $this->_end_diff();
1462 wfProfileOut( $fname );
1466 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1467 $fname = 'DiffFormatter::_block';
1468 wfProfileIn( $fname );
1469 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1470 foreach ($edits as $edit) {
1471 if ($edit->type
== 'copy')
1472 $this->_context($edit->orig
);
1473 elseif ($edit->type
== 'add')
1474 $this->_added($edit->closing
);
1475 elseif ($edit->type
== 'delete')
1476 $this->_deleted($edit->orig
);
1477 elseif ($edit->type
== 'change')
1478 $this->_changed($edit->orig
, $edit->closing
);
1480 trigger_error('Unknown edit type', E_USER_ERROR
);
1482 $this->_end_block();
1483 wfProfileOut( $fname );
1486 function _start_diff() {
1490 function _end_diff() {
1491 $val = ob_get_contents();
1496 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1498 $xbeg .= "," . ($xbeg +
$xlen - 1);
1500 $ybeg .= "," . ($ybeg +
$ylen - 1);
1502 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1505 function _start_block($header) {
1509 function _end_block() {
1512 function _lines($lines, $prefix = ' ') {
1513 foreach ($lines as $line)
1514 echo "$prefix $line\n";
1517 function _context($lines) {
1518 $this->_lines($lines);
1521 function _added($lines) {
1522 $this->_lines($lines, '>');
1524 function _deleted($lines) {
1525 $this->_lines($lines, '<');
1528 function _changed($orig, $closing) {
1529 $this->_deleted($orig);
1531 $this->_added($closing);
1537 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1541 define('NBSP', ' '); // iso-8859-x non-breaking space.
1546 * @package MediaWiki
1547 * @subpackage DifferenceEngine
1549 class _HWLDF_WordAccumulator
{
1550 function _HWLDF_WordAccumulator () {
1551 $this->_lines
= array();
1557 function _flushGroup ($new_tag) {
1558 if ($this->_group
!== '') {
1559 if ($this->_tag
== 'mark')
1560 $this->_line
.= '<span class="diffchange">' .
1561 htmlspecialchars ( $this->_group
) . '</span>';
1563 $this->_line
.= htmlspecialchars ( $this->_group
);
1566 $this->_tag
= $new_tag;
1569 function _flushLine ($new_tag) {
1570 $this->_flushGroup($new_tag);
1571 if ($this->_line
!= '')
1572 array_push ( $this->_lines
, $this->_line
);
1574 # make empty lines visible by inserting an NBSP
1575 array_push ( $this->_lines
, NBSP
);
1579 function addWords ($words, $tag = '') {
1580 if ($tag != $this->_tag
)
1581 $this->_flushGroup($tag);
1583 foreach ($words as $word) {
1584 // new-line should only come as first char of word.
1587 if ($word[0] == "\n") {
1588 $this->_flushLine($tag);
1589 $word = substr($word, 1);
1591 assert(!strstr($word, "\n"));
1592 $this->_group
.= $word;
1596 function getLines() {
1597 $this->_flushLine('~done');
1598 return $this->_lines
;
1605 * @package MediaWiki
1606 * @subpackage DifferenceEngine
1608 class WordLevelDiff
extends MappedDiff
1610 const MAX_LINE_LENGTH
= 10000;
1612 function WordLevelDiff ($orig_lines, $closing_lines) {
1613 $fname = 'WordLevelDiff::WordLevelDiff';
1614 wfProfileIn( $fname );
1616 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1617 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1619 $this->MappedDiff($orig_words, $closing_words,
1620 $orig_stripped, $closing_stripped);
1621 wfProfileOut( $fname );
1624 function _split($lines) {
1625 $fname = 'WordLevelDiff::_split';
1626 wfProfileIn( $fname );
1629 $stripped = array();
1631 foreach ( $lines as $line ) {
1632 # If the line is too long, just pretend the entire line is one big word
1633 # This prevents resource exhaustion problems
1640 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1642 $stripped[] = $line;
1645 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1648 $words = array_merge( $words, $m[0] );
1649 $stripped = array_merge( $stripped, $m[1] );
1653 wfProfileOut( $fname );
1654 return array($words, $stripped);
1658 $fname = 'WordLevelDiff::orig';
1659 wfProfileIn( $fname );
1660 $orig = new _HWLDF_WordAccumulator
;
1662 foreach ($this->edits
as $edit) {
1663 if ($edit->type
== 'copy')
1664 $orig->addWords($edit->orig
);
1665 elseif ($edit->orig
)
1666 $orig->addWords($edit->orig
, 'mark');
1668 $lines = $orig->getLines();
1669 wfProfileOut( $fname );
1673 function closing () {
1674 $fname = 'WordLevelDiff::closing';
1675 wfProfileIn( $fname );
1676 $closing = new _HWLDF_WordAccumulator
;
1678 foreach ($this->edits
as $edit) {
1679 if ($edit->type
== 'copy')
1680 $closing->addWords($edit->closing
);
1681 elseif ($edit->closing
)
1682 $closing->addWords($edit->closing
, 'mark');
1684 $lines = $closing->getLines();
1685 wfProfileOut( $fname );
1691 * Wikipedia Table style diff formatter.
1694 * @package MediaWiki
1695 * @subpackage DifferenceEngine
1697 class TableDiffFormatter
extends DiffFormatter
1699 function TableDiffFormatter() {
1700 $this->leading_context_lines
= 2;
1701 $this->trailing_context_lines
= 2;
1704 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1705 $r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" .
1706 '<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n";
1710 function _start_block( $header ) {
1714 function _end_block() {
1717 function _lines( $lines, $prefix=' ', $color='white' ) {
1720 # HTML-escape parameter before calling this
1721 function addedLine( $line ) {
1722 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1725 # HTML-escape parameter before calling this
1726 function deletedLine( $line ) {
1727 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1730 # HTML-escape parameter before calling this
1731 function contextLine( $line ) {
1732 return "<td> </td><td class='diff-context'>{$line}</td>";
1735 function emptyLine() {
1736 return '<td colspan="2"> </td>';
1739 function _added( $lines ) {
1740 foreach ($lines as $line) {
1741 echo '<tr>' . $this->emptyLine() .
1742 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1746 function _deleted($lines) {
1747 foreach ($lines as $line) {
1748 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1749 $this->emptyLine() . "</tr>\n";
1753 function _context( $lines ) {
1754 foreach ($lines as $line) {
1756 $this->contextLine( htmlspecialchars ( $line ) ) .
1757 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1761 function _changed( $orig, $closing ) {
1762 $fname = 'TableDiffFormatter::_changed';
1763 wfProfileIn( $fname );
1765 $diff = new WordLevelDiff( $orig, $closing );
1766 $del = $diff->orig();
1767 $add = $diff->closing();
1769 # Notice that WordLevelDiff returns HTML-escaped output.
1770 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1772 while ( $line = array_shift( $del ) ) {
1773 $aline = array_shift( $add );
1774 echo '<tr>' . $this->deletedLine( $line ) .
1775 $this->addedLine( $aline ) . "</tr>\n";
1777 foreach ($add as $line) { # If any leftovers
1778 echo '<tr>' . $this->emptyLine() .
1779 $this->addedLine( $line ) . "</tr>\n";
1781 wfProfileOut( $fname );