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 if( !$this->mOldPage
->equals( $this->mNewPage
) ) {
444 // Comparing two different pages? Count would be meaningless.
448 $oldid = $this->mOldRev
->getId();
449 $newid = $this->mNewRev
->getId();
450 if ( $oldid > $newid ) {
451 $tmp = $oldid; $oldid = $newid; $newid = $tmp;
454 $n = $this->mTitle
->countRevisionsBetween( $oldid, $newid );
458 return wfMsgExt( 'diff-multi', array( 'parseinline' ), $n );
463 * Add the header to a diff body
465 function addHeader( $diff, $otitle, $ntitle, $multi = '' ) {
467 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
469 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
470 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
475 $header .= "<tr><td colspan='4' align='center' class='diff-multi'>{$multi}</td></tr>";
477 return $header . $diff . "</table>";
481 * Use specified text instead of loading from the database
483 function setText( $oldText, $newText ) {
484 $this->mOldtext
= $oldText;
485 $this->mNewtext
= $newText;
486 $this->mTextLoaded
= 2;
490 * Load revision metadata for the specified articles. If newid is 0, then compare
491 * the old article in oldid to the current article; if oldid is 0, then
492 * compare the current article to the immediately previous one (ignoring the
495 * If oldid is false, leave the corresponding revision object set
496 * to false. This is impossible via ordinary user input, and is provided for
499 function loadRevisionData() {
501 if ( $this->mRevisionsLoaded
) {
504 // Whether it succeeds or fails, we don't want to try again
505 $this->mRevisionsLoaded
= true;
508 // Load the new revision object
509 if( $this->mNewid
) {
510 $this->mNewRev
= Revision
::newFromId( $this->mNewid
);
512 $this->mNewRev
= Revision
::newFromTitle( $this->mTitle
);
515 if( is_null( $this->mNewRev
) ) {
519 // Set assorted variables
520 $timestamp = $wgLang->timeanddate( $this->mNewRev
->getTimestamp(), true );
521 $this->mNewPage
= $this->mNewRev
->getTitle();
522 if( $this->mNewRev
->isCurrent() ) {
523 $newLink = $this->mNewPage
->escapeLocalUrl();
524 $this->mPagetitle
= htmlspecialchars( wfMsg( 'currentrev' ) );
525 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit' );
527 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a> ($timestamp)</strong>"
528 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
531 $newLink = $this->mNewPage
->escapeLocalUrl( 'oldid=' . $this->mNewid
);
532 $newEdit = $this->mNewPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mNewid
);
533 $newUndo = $this->mNewPage
->escapeLocalUrl( 'action=edit&undo=' . $this->mNewid
);
534 $this->mPagetitle
= htmlspecialchars( wfMsg( 'revisionasof', $timestamp ) );
536 $this->mNewtitle
= "<strong><a href='$newLink'>{$this->mPagetitle}</a></strong>"
537 . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)"
538 . " (<a href='$newUndo'>" . htmlspecialchars( wfMsg( 'editundo' ) ) . "</a>)";
541 // Load the old revision object
542 $this->mOldRev
= false;
543 if( $this->mOldid
) {
544 $this->mOldRev
= Revision
::newFromId( $this->mOldid
);
545 } elseif ( $this->mOldid
=== 0 ) {
546 $rev = $this->mNewRev
->getPrevious();
548 $this->mOldid
= $rev->getId();
549 $this->mOldRev
= $rev;
551 // No previous revision; mark to show as first-version only.
552 $this->mOldid
= false;
553 $this->mOldRev
= false;
555 }/* elseif ( $this->mOldid === false ) leave mOldRev false; */
557 if( is_null( $this->mOldRev
) ) {
561 if ( $this->mOldRev
) {
562 $this->mOldPage
= $this->mOldRev
->getTitle();
564 $t = $wgLang->timeanddate( $this->mOldRev
->getTimestamp(), true );
565 $oldLink = $this->mOldPage
->escapeLocalUrl( 'oldid=' . $this->mOldid
);
566 $oldEdit = $this->mOldPage
->escapeLocalUrl( 'action=edit&oldid=' . $this->mOldid
);
567 $this->mOldtitle
= "<strong><a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) )
568 . "</a></strong> (<a href='$oldEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
575 * Load the text of the revisions, as well as revision data.
577 function loadText() {
578 if ( $this->mTextLoaded
== 2 ) {
581 // Whether it succeeds or fails, we don't want to try again
582 $this->mTextLoaded
= 2;
585 if ( !$this->loadRevisionData() ) {
588 if ( $this->mOldRev
) {
589 // FIXME: permission tests
590 $this->mOldtext
= $this->mOldRev
->getText();
591 if ( $this->mOldtext
=== false ) {
595 if ( $this->mNewRev
) {
596 $this->mNewtext
= $this->mNewRev
->getText();
597 if ( $this->mNewtext
=== false ) {
605 * Load the text of the new revision, not the old one
607 function loadNewText() {
608 if ( $this->mTextLoaded
>= 1 ) {
611 $this->mTextLoaded
= 1;
613 if ( !$this->loadRevisionData() ) {
616 $this->mNewtext
= $this->mNewRev
->getText();
623 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
625 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
626 // You may copy this code freely under the conditions of the GPL.
629 define('USE_ASSERTS', function_exists('assert'));
635 * @subpackage DifferenceEngine
643 trigger_error('pure virtual', E_USER_ERROR
);
647 return $this->orig ?
sizeof($this->orig
) : 0;
650 function nclosing() {
651 return $this->closing ?
sizeof($this->closing
) : 0;
659 * @subpackage DifferenceEngine
661 class _DiffOp_Copy
extends _DiffOp
{
664 function _DiffOp_Copy ($orig, $closing = false) {
665 if (!is_array($closing))
668 $this->closing
= $closing;
672 return new _DiffOp_Copy($this->closing
, $this->orig
);
680 * @subpackage DifferenceEngine
682 class _DiffOp_Delete
extends _DiffOp
{
683 var $type = 'delete';
685 function _DiffOp_Delete ($lines) {
686 $this->orig
= $lines;
687 $this->closing
= false;
691 return new _DiffOp_Add($this->orig
);
699 * @subpackage DifferenceEngine
701 class _DiffOp_Add
extends _DiffOp
{
704 function _DiffOp_Add ($lines) {
705 $this->closing
= $lines;
710 return new _DiffOp_Delete($this->closing
);
718 * @subpackage DifferenceEngine
720 class _DiffOp_Change
extends _DiffOp
{
721 var $type = 'change';
723 function _DiffOp_Change ($orig, $closing) {
725 $this->closing
= $closing;
729 return new _DiffOp_Change($this->closing
, $this->orig
);
735 * Class used internally by Diff to actually compute the diffs.
737 * The algorithm used here is mostly lifted from the perl module
738 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
739 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
741 * More ideas are taken from:
742 * http://www.ics.uci.edu/~eppstein/161/960229.html
744 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
745 * diffutils-2.7, which can be found at:
746 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
748 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
751 * Line length limits for robustness added by Tim Starling, 2005-08-31
753 * @author Geoffrey T. Dairiki, Tim Starling
756 * @subpackage DifferenceEngine
760 const MAX_XREF_LENGTH
= 10000;
762 function diff ($from_lines, $to_lines) {
763 $fname = '_DiffEngine::diff';
764 wfProfileIn( $fname );
766 $n_from = sizeof($from_lines);
767 $n_to = sizeof($to_lines);
769 $this->xchanged
= $this->ychanged
= array();
770 $this->xv
= $this->yv
= array();
771 $this->xind
= $this->yind
= array();
773 unset($this->in_seq
);
776 // Skip leading common lines.
777 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
778 if ($from_lines[$skip] !== $to_lines[$skip])
780 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
782 // Skip trailing common lines.
783 $xi = $n_from; $yi = $n_to;
784 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
785 if ($from_lines[$xi] !== $to_lines[$yi])
787 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
790 // Ignore lines which do not exist in both files.
791 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
792 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
795 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
796 $line = $to_lines[$yi];
797 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
799 $yhash[$this->_line_hash($line)] = 1;
803 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
804 $line = $from_lines[$xi];
805 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
812 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
814 // Merge edits when possible
815 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
816 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
818 // Compute the edit operations.
821 while ($xi < $n_from ||
$yi < $n_to) {
822 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
823 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
825 // Skip matching "snake".
827 while ( $xi < $n_from && $yi < $n_to
828 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
829 $copy[] = $from_lines[$xi++
];
833 $edits[] = new _DiffOp_Copy($copy);
835 // Find deletes & adds.
837 while ($xi < $n_from && $this->xchanged
[$xi])
838 $delete[] = $from_lines[$xi++
];
841 while ($yi < $n_to && $this->ychanged
[$yi])
842 $add[] = $to_lines[$yi++
];
845 $edits[] = new _DiffOp_Change($delete, $add);
847 $edits[] = new _DiffOp_Delete($delete);
849 $edits[] = new _DiffOp_Add($add);
851 wfProfileOut( $fname );
856 * Returns the whole line if it's small enough, or the MD5 hash otherwise
858 function _line_hash( $line ) {
859 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
867 /* Divide the Largest Common Subsequence (LCS) of the sequences
868 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
871 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
872 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
873 * sub sequences. The first sub-sequence is contained in [X0, X1),
874 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
875 * that (X0, Y0) == (XOFF, YOFF) and
876 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
878 * This function assumes that the first lines of the specified portions
879 * of the two files do not match, and likewise that the last lines do not
880 * match. The caller must trim matching lines from the beginning and end
881 * of the portions it is going to specify.
883 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
884 $fname = '_DiffEngine::_diag';
885 wfProfileIn( $fname );
888 if ($xlim - $xoff > $ylim - $yoff) {
889 // Things seems faster (I'm not sure I understand why)
890 // when the shortest sequence in X.
892 list ($xoff, $xlim, $yoff, $ylim)
893 = array( $yoff, $ylim, $xoff, $xlim);
897 for ($i = $ylim - 1; $i >= $yoff; $i--)
898 $ymatches[$this->xv
[$i]][] = $i;
900 for ($i = $ylim - 1; $i >= $yoff; $i--)
901 $ymatches[$this->yv
[$i]][] = $i;
904 $this->seq
[0]= $yoff - 1;
905 $this->in_seq
= array();
908 $numer = $xlim - $xoff +
$nchunks - 1;
910 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
911 wfProfileIn( "$fname-chunk" );
913 for ($i = 0; $i <= $this->lcs
; $i++
)
914 $ymids[$i][$chunk-1] = $this->seq
[$i];
916 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
917 for ( ; $x < $x1; $x++
) {
918 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
919 if (empty($ymatches[$line]))
921 $matches = $ymatches[$line];
923 while (list ($junk, $y) = each($matches))
924 if (empty($this->in_seq
[$y])) {
925 $k = $this->_lcs_pos($y);
926 USE_ASSERTS
&& assert($k > 0);
927 $ymids[$k] = $ymids[$k-1];
930 while (list ( /* $junk */, $y) = each($matches)) {
931 if ($y > $this->seq
[$k-1]) {
932 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
933 // Optimization: this is a common case:
934 // next match is just replacing previous match.
935 $this->in_seq
[$this->seq
[$k]] = false;
937 $this->in_seq
[$y] = 1;
938 } else if (empty($this->in_seq
[$y])) {
939 $k = $this->_lcs_pos($y);
940 USE_ASSERTS
&& assert($k > 0);
941 $ymids[$k] = $ymids[$k-1];
945 wfProfileOut( "$fname-chunk" );
948 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
949 $ymid = $ymids[$this->lcs
];
950 for ($n = 0; $n < $nchunks - 1; $n++
) {
951 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
953 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
955 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
957 wfProfileOut( $fname );
958 return array($this->lcs
, $seps);
961 function _lcs_pos ($ypos) {
962 $fname = '_DiffEngine::_lcs_pos';
963 wfProfileIn( $fname );
966 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
967 $this->seq
[++
$this->lcs
] = $ypos;
968 $this->in_seq
[$ypos] = 1;
969 wfProfileOut( $fname );
974 while ($beg < $end) {
975 $mid = (int)(($beg +
$end) / 2);
976 if ( $ypos > $this->seq
[$mid] )
982 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
984 $this->in_seq
[$this->seq
[$end]] = false;
985 $this->seq
[$end] = $ypos;
986 $this->in_seq
[$ypos] = 1;
987 wfProfileOut( $fname );
991 /* Find LCS of two sequences.
993 * The results are recorded in the vectors $this->{x,y}changed[], by
994 * storing a 1 in the element for each line that is an insertion
995 * or deletion (ie. is not in the LCS).
997 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
999 * Note that XLIM, YLIM are exclusive bounds.
1000 * All line numbers are origin-0 and discarded lines are not counted.
1002 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
1003 $fname = '_DiffEngine::_compareseq';
1004 wfProfileIn( $fname );
1006 // Slide down the bottom initial diagonal.
1007 while ($xoff < $xlim && $yoff < $ylim
1008 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
1013 // Slide up the top initial diagonal.
1014 while ($xlim > $xoff && $ylim > $yoff
1015 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
1020 if ($xoff == $xlim ||
$yoff == $ylim)
1023 // This is ad hoc but seems to work well.
1024 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
1025 //$nchunks = max(2,min(8,(int)$nchunks));
1026 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
1028 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
1032 // X and Y sequences have no common subsequence:
1033 // mark all changed.
1034 while ($yoff < $ylim)
1035 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
1036 while ($xoff < $xlim)
1037 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
1039 // Use the partitions to split this problem into subproblems.
1042 while ($pt2 = next($seps)) {
1043 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
1047 wfProfileOut( $fname );
1050 /* Adjust inserts/deletes of identical lines to join changes
1051 * as much as possible.
1053 * We do something when a run of changed lines include a
1054 * line at one end and has an excluded, identical line at the other.
1055 * We are free to choose which identical line is included.
1056 * `compareseq' usually chooses the one at the beginning,
1057 * but usually it is cleaner to consider the following identical line
1058 * to be the "change".
1060 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
1062 function _shift_boundaries ($lines, &$changed, $other_changed) {
1063 $fname = '_DiffEngine::_shift_boundaries';
1064 wfProfileIn( $fname );
1068 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
1069 $len = sizeof($lines);
1070 $other_len = sizeof($other_changed);
1074 * Scan forwards to find beginning of another run of changes.
1075 * Also keep track of the corresponding point in the other file.
1077 * Throughout this code, $i and $j are adjusted together so that
1078 * the first $i elements of $changed and the first $j elements
1079 * of $other_changed both contain the same number of zeros
1080 * (unchanged lines).
1081 * Furthermore, $j is always kept so that $j == $other_len or
1082 * $other_changed[$j] == false.
1084 while ($j < $other_len && $other_changed[$j])
1087 while ($i < $len && ! $changed[$i]) {
1088 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1090 while ($j < $other_len && $other_changed[$j])
1099 // Find the end of this run of changes.
1100 while (++
$i < $len && $changed[$i])
1105 * Record the length of this run of changes, so that
1106 * we can later determine whether the run has grown.
1108 $runlength = $i - $start;
1111 * Move the changed region back, so long as the
1112 * previous unchanged line matches the last changed one.
1113 * This merges with previous changed regions.
1115 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
1116 $changed[--$start] = 1;
1117 $changed[--$i] = false;
1118 while ($start > 0 && $changed[$start - 1])
1120 USE_ASSERTS
&& assert('$j > 0');
1121 while ($other_changed[--$j])
1123 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1127 * Set CORRESPONDING to the end of the changed run, at the last
1128 * point where it corresponds to a changed run in the other file.
1129 * CORRESPONDING == LEN means no such point has been found.
1131 $corresponding = $j < $other_len ?
$i : $len;
1134 * Move the changed region forward, so long as the
1135 * first changed line matches the following unchanged one.
1136 * This merges with following changed regions.
1137 * Do this second, so that if there are no merges,
1138 * the changed region is moved forward as far as possible.
1140 while ($i < $len && $lines[$start] == $lines[$i]) {
1141 $changed[$start++
] = false;
1143 while ($i < $len && $changed[$i])
1146 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
1148 if ($j < $other_len && $other_changed[$j]) {
1149 $corresponding = $i;
1150 while ($j < $other_len && $other_changed[$j])
1154 } while ($runlength != $i - $start);
1157 * If possible, move the fully-merged run of changes
1158 * back to a corresponding run in the other file.
1160 while ($corresponding < $i) {
1161 $changed[--$start] = 1;
1163 USE_ASSERTS
&& assert('$j > 0');
1164 while ($other_changed[--$j])
1166 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
1169 wfProfileOut( $fname );
1174 * Class representing a 'diff' between two sequences of strings.
1177 * @package MediaWiki
1178 * @subpackage DifferenceEngine
1186 * Computes diff between sequences of strings.
1188 * @param $from_lines array An array of strings.
1189 * (Typically these are lines from a file.)
1190 * @param $to_lines array An array of strings.
1192 function Diff($from_lines, $to_lines) {
1193 $eng = new _DiffEngine
;
1194 $this->edits
= $eng->diff($from_lines, $to_lines);
1195 //$this->_check($from_lines, $to_lines);
1199 * Compute reversed Diff.
1203 * $diff = new Diff($lines1, $lines2);
1204 * $rev = $diff->reverse();
1205 * @return object A Diff object representing the inverse of the
1208 function reverse () {
1210 $rev->edits
= array();
1211 foreach ($this->edits
as $edit) {
1212 $rev->edits
[] = $edit->reverse();
1218 * Check for empty diff.
1220 * @return bool True iff two sequences were identical.
1222 function isEmpty () {
1223 foreach ($this->edits
as $edit) {
1224 if ($edit->type
!= 'copy')
1231 * Compute the length of the Longest Common Subsequence (LCS).
1233 * This is mostly for diagnostic purposed.
1235 * @return int The length of the LCS.
1239 foreach ($this->edits
as $edit) {
1240 if ($edit->type
== 'copy')
1241 $lcs +
= sizeof($edit->orig
);
1247 * Get the original set of lines.
1249 * This reconstructs the $from_lines parameter passed to the
1252 * @return array The original sequence of strings.
1257 foreach ($this->edits
as $edit) {
1259 array_splice($lines, sizeof($lines), 0, $edit->orig
);
1265 * Get the closing set of lines.
1267 * This reconstructs the $to_lines parameter passed to the
1270 * @return array The sequence of strings.
1272 function closing() {
1275 foreach ($this->edits
as $edit) {
1277 array_splice($lines, sizeof($lines), 0, $edit->closing
);
1283 * Check a Diff for validity.
1285 * This is here only for debugging purposes.
1287 function _check ($from_lines, $to_lines) {
1288 $fname = 'Diff::_check';
1289 wfProfileIn( $fname );
1290 if (serialize($from_lines) != serialize($this->orig()))
1291 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
1292 if (serialize($to_lines) != serialize($this->closing()))
1293 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
1295 $rev = $this->reverse();
1296 if (serialize($to_lines) != serialize($rev->orig()))
1297 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
1298 if (serialize($from_lines) != serialize($rev->closing()))
1299 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
1303 foreach ($this->edits
as $edit) {
1304 if ( $prevtype == $edit->type
)
1305 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
1306 $prevtype = $edit->type
;
1309 $lcs = $this->lcs();
1310 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
1311 wfProfileOut( $fname );
1319 * @package MediaWiki
1320 * @subpackage DifferenceEngine
1322 class MappedDiff
extends Diff
1327 * Computes diff between sequences of strings.
1329 * This can be used to compute things like
1330 * case-insensitve diffs, or diffs which ignore
1331 * changes in white-space.
1333 * @param $from_lines array An array of strings.
1334 * (Typically these are lines from a file.)
1336 * @param $to_lines array An array of strings.
1338 * @param $mapped_from_lines array This array should
1339 * have the same size number of elements as $from_lines.
1340 * The elements in $mapped_from_lines and
1341 * $mapped_to_lines are what is actually compared
1342 * when computing the diff.
1344 * @param $mapped_to_lines array This array should
1345 * have the same number of elements as $to_lines.
1347 function MappedDiff($from_lines, $to_lines,
1348 $mapped_from_lines, $mapped_to_lines) {
1349 $fname = 'MappedDiff::MappedDiff';
1350 wfProfileIn( $fname );
1352 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1353 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1355 $this->Diff($mapped_from_lines, $mapped_to_lines);
1358 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
1359 $orig = &$this->edits
[$i]->orig
;
1360 if (is_array($orig)) {
1361 $orig = array_slice($from_lines, $xi, sizeof($orig));
1362 $xi +
= sizeof($orig);
1365 $closing = &$this->edits
[$i]->closing
;
1366 if (is_array($closing)) {
1367 $closing = array_slice($to_lines, $yi, sizeof($closing));
1368 $yi +
= sizeof($closing);
1371 wfProfileOut( $fname );
1376 * A class to format Diffs
1378 * This class formats the diff in classic diff format.
1379 * It is intended that this class be customized via inheritance,
1380 * to obtain fancier outputs.
1383 * @package MediaWiki
1384 * @subpackage DifferenceEngine
1389 * Number of leading context "lines" to preserve.
1391 * This should be left at zero for this class, but subclasses
1392 * may want to set this to other values.
1394 var $leading_context_lines = 0;
1397 * Number of trailing context "lines" to preserve.
1399 * This should be left at zero for this class, but subclasses
1400 * may want to set this to other values.
1402 var $trailing_context_lines = 0;
1407 * @param $diff object A Diff object.
1408 * @return string The formatted output.
1410 function format($diff) {
1411 $fname = 'DiffFormatter::format';
1412 wfProfileIn( $fname );
1418 $nlead = $this->leading_context_lines
;
1419 $ntrail = $this->trailing_context_lines
;
1421 $this->_start_diff();
1423 foreach ($diff->edits
as $edit) {
1424 if ($edit->type
== 'copy') {
1425 if (is_array($block)) {
1426 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
1431 $context = array_slice($edit->orig
, 0, $ntrail);
1432 $block[] = new _DiffOp_Copy($context);
1434 $this->_block($x0, $ntrail +
$xi - $x0,
1435 $y0, $ntrail +
$yi - $y0,
1440 $context = $edit->orig
;
1443 if (! is_array($block)) {
1444 $context = array_slice($context, sizeof($context) - $nlead);
1445 $x0 = $xi - sizeof($context);
1446 $y0 = $yi - sizeof($context);
1449 $block[] = new _DiffOp_Copy($context);
1455 $xi +
= sizeof($edit->orig
);
1457 $yi +
= sizeof($edit->closing
);
1460 if (is_array($block))
1461 $this->_block($x0, $xi - $x0,
1465 $end = $this->_end_diff();
1466 wfProfileOut( $fname );
1470 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1471 $fname = 'DiffFormatter::_block';
1472 wfProfileIn( $fname );
1473 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1474 foreach ($edits as $edit) {
1475 if ($edit->type
== 'copy')
1476 $this->_context($edit->orig
);
1477 elseif ($edit->type
== 'add')
1478 $this->_added($edit->closing
);
1479 elseif ($edit->type
== 'delete')
1480 $this->_deleted($edit->orig
);
1481 elseif ($edit->type
== 'change')
1482 $this->_changed($edit->orig
, $edit->closing
);
1484 trigger_error('Unknown edit type', E_USER_ERROR
);
1486 $this->_end_block();
1487 wfProfileOut( $fname );
1490 function _start_diff() {
1494 function _end_diff() {
1495 $val = ob_get_contents();
1500 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1502 $xbeg .= "," . ($xbeg +
$xlen - 1);
1504 $ybeg .= "," . ($ybeg +
$ylen - 1);
1506 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
1509 function _start_block($header) {
1513 function _end_block() {
1516 function _lines($lines, $prefix = ' ') {
1517 foreach ($lines as $line)
1518 echo "$prefix $line\n";
1521 function _context($lines) {
1522 $this->_lines($lines);
1525 function _added($lines) {
1526 $this->_lines($lines, '>');
1528 function _deleted($lines) {
1529 $this->_lines($lines, '<');
1532 function _changed($orig, $closing) {
1533 $this->_deleted($orig);
1535 $this->_added($closing);
1541 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1545 define('NBSP', ' '); // iso-8859-x non-breaking space.
1550 * @package MediaWiki
1551 * @subpackage DifferenceEngine
1553 class _HWLDF_WordAccumulator
{
1554 function _HWLDF_WordAccumulator () {
1555 $this->_lines
= array();
1561 function _flushGroup ($new_tag) {
1562 if ($this->_group
!== '') {
1563 if ($this->_tag
== 'mark')
1564 $this->_line
.= '<span class="diffchange">' .
1565 htmlspecialchars ( $this->_group
) . '</span>';
1567 $this->_line
.= htmlspecialchars ( $this->_group
);
1570 $this->_tag
= $new_tag;
1573 function _flushLine ($new_tag) {
1574 $this->_flushGroup($new_tag);
1575 if ($this->_line
!= '')
1576 array_push ( $this->_lines
, $this->_line
);
1578 # make empty lines visible by inserting an NBSP
1579 array_push ( $this->_lines
, NBSP
);
1583 function addWords ($words, $tag = '') {
1584 if ($tag != $this->_tag
)
1585 $this->_flushGroup($tag);
1587 foreach ($words as $word) {
1588 // new-line should only come as first char of word.
1591 if ($word[0] == "\n") {
1592 $this->_flushLine($tag);
1593 $word = substr($word, 1);
1595 assert(!strstr($word, "\n"));
1596 $this->_group
.= $word;
1600 function getLines() {
1601 $this->_flushLine('~done');
1602 return $this->_lines
;
1609 * @package MediaWiki
1610 * @subpackage DifferenceEngine
1612 class WordLevelDiff
extends MappedDiff
1614 const MAX_LINE_LENGTH
= 10000;
1616 function WordLevelDiff ($orig_lines, $closing_lines) {
1617 $fname = 'WordLevelDiff::WordLevelDiff';
1618 wfProfileIn( $fname );
1620 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1621 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1623 $this->MappedDiff($orig_words, $closing_words,
1624 $orig_stripped, $closing_stripped);
1625 wfProfileOut( $fname );
1628 function _split($lines) {
1629 $fname = 'WordLevelDiff::_split';
1630 wfProfileIn( $fname );
1633 $stripped = array();
1635 foreach ( $lines as $line ) {
1636 # If the line is too long, just pretend the entire line is one big word
1637 # This prevents resource exhaustion problems
1644 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1646 $stripped[] = $line;
1649 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1652 $words = array_merge( $words, $m[0] );
1653 $stripped = array_merge( $stripped, $m[1] );
1657 wfProfileOut( $fname );
1658 return array($words, $stripped);
1662 $fname = 'WordLevelDiff::orig';
1663 wfProfileIn( $fname );
1664 $orig = new _HWLDF_WordAccumulator
;
1666 foreach ($this->edits
as $edit) {
1667 if ($edit->type
== 'copy')
1668 $orig->addWords($edit->orig
);
1669 elseif ($edit->orig
)
1670 $orig->addWords($edit->orig
, 'mark');
1672 $lines = $orig->getLines();
1673 wfProfileOut( $fname );
1677 function closing () {
1678 $fname = 'WordLevelDiff::closing';
1679 wfProfileIn( $fname );
1680 $closing = new _HWLDF_WordAccumulator
;
1682 foreach ($this->edits
as $edit) {
1683 if ($edit->type
== 'copy')
1684 $closing->addWords($edit->closing
);
1685 elseif ($edit->closing
)
1686 $closing->addWords($edit->closing
, 'mark');
1688 $lines = $closing->getLines();
1689 wfProfileOut( $fname );
1695 * Wikipedia Table style diff formatter.
1698 * @package MediaWiki
1699 * @subpackage DifferenceEngine
1701 class TableDiffFormatter
extends DiffFormatter
1703 function TableDiffFormatter() {
1704 $this->leading_context_lines
= 2;
1705 $this->trailing_context_lines
= 2;
1708 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1709 $r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" .
1710 '<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n";
1714 function _start_block( $header ) {
1718 function _end_block() {
1721 function _lines( $lines, $prefix=' ', $color='white' ) {
1724 # HTML-escape parameter before calling this
1725 function addedLine( $line ) {
1726 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1729 # HTML-escape parameter before calling this
1730 function deletedLine( $line ) {
1731 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1734 # HTML-escape parameter before calling this
1735 function contextLine( $line ) {
1736 return "<td> </td><td class='diff-context'>{$line}</td>";
1739 function emptyLine() {
1740 return '<td colspan="2"> </td>';
1743 function _added( $lines ) {
1744 foreach ($lines as $line) {
1745 echo '<tr>' . $this->emptyLine() .
1746 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1750 function _deleted($lines) {
1751 foreach ($lines as $line) {
1752 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1753 $this->emptyLine() . "</tr>\n";
1757 function _context( $lines ) {
1758 foreach ($lines as $line) {
1760 $this->contextLine( htmlspecialchars ( $line ) ) .
1761 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1765 function _changed( $orig, $closing ) {
1766 $fname = 'TableDiffFormatter::_changed';
1767 wfProfileIn( $fname );
1769 $diff = new WordLevelDiff( $orig, $closing );
1770 $del = $diff->orig();
1771 $add = $diff->closing();
1773 # Notice that WordLevelDiff returns HTML-escaped output.
1774 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1776 while ( $line = array_shift( $del ) ) {
1777 $aline = array_shift( $add );
1778 echo '<tr>' . $this->deletedLine( $line ) .
1779 $this->addedLine( $aline ) . "</tr>\n";
1781 foreach ($add as $line) { # If any leftovers
1782 echo '<tr>' . $this->emptyLine() .
1783 $this->addedLine( $line ) . "</tr>\n";
1785 wfProfileOut( $fname );