758a6e06018576f5e7409cccb4398de4e1eb17fb
[lhc/web/wiklou.git] / includes / DifferenceEngine.php
1 <?php
2 /**
3 * See diff.doc
4 * @package MediaWiki
5 * @subpackage DifferenceEngine
6 */
7
8 require_once( 'Revision.php' );
9
10 /**
11 * @todo document
12 * @access public
13 */
14 class DifferenceEngine {
15 /* private */ var $mOldid, $mNewid;
16 /* private */ var $mOldtitle, $mNewtitle, $mPagetitle;
17 /* private */ var $mOldtext, $mNewtext;
18 /* private */ var $mOldUser, $mNewUser;
19 /* private */ var $mOldComment, $mNewComment;
20 /* private */ var $mOldPage, $mNewPage;
21 /* private */ var $mRcidMarkPatrolled;
22
23 function DifferenceEngine( $old, $new, $rcid = 0 )
24 {
25 global $wgTitle;
26 if ( 'prev' == $new ) {
27 # Show diff between revision $old and the previous one.
28 # Get previous one from DB.
29 #
30 $this->mNewid = intval($old);
31
32 $this->mOldid = $wgTitle->getPreviousRevisionID( $this->mNewid );
33
34 } elseif ( 'next' == $new ) {
35
36 # Show diff between revision $old and the previous one.
37 # Get previous one from DB.
38 #
39 $this->mOldid = intval($old);
40 $this->mNewid = $wgTitle->getNextRevisionID( $this->mOldid );
41 if ( false === $this->mNewid ) {
42 # if no result, NewId points to the newest old revision. The only newer
43 # revision is cur, which is "0".
44 $this->mNewid = 0;
45 }
46
47 } else {
48
49 $this->mOldid = intval($old);
50 $this->mNewid = intval($new);
51 }
52 $this->mRcidMarkPatrolled = intval($rcid); # force it to be an integer
53 }
54
55 function showDiffPage()
56 {
57 global $wgUser, $wgTitle, $wgOut, $wgContLang, $wgOnlySysopsCanPatrol, $wgUseRCPatrol;
58 $fname = 'DifferenceEngine::showDiffPage';
59 wfProfileIn( $fname );
60
61 # mOldid is false if the difference engine is called with a "vague" query for
62 # a diff between a version V and its previous version V' AND the version V
63 # is the first version of that article. In that case, V' does not exist.
64 if ( $this->mOldid === false ) {
65 $this->showFirstRevision();
66 wfProfileOut( $fname );
67 return;
68 }
69
70 $t = $wgTitle->getPrefixedText() . " (Diff: {$this->mOldid}, " .
71 "{$this->mNewid})";
72 $mtext = wfMsg( 'missingarticle', $t );
73
74 $wgOut->setArticleFlag( false );
75 if ( ! $this->loadText() ) {
76 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
77 $wgOut->addHTML( $mtext );
78 wfProfileOut( $fname );
79 return;
80 }
81 $wgOut->suppressQuickbar();
82
83 $oldTitle = $this->mOldPage->getPrefixedText();
84 $newTitle = $this->mNewPage->getPrefixedText();
85 if( $oldTitle == $newTitle ) {
86 $wgOut->setPageTitle( $newTitle );
87 } else {
88 $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
89 }
90 $wgOut->setSubtitle( wfMsg( 'difference' ) );
91 $wgOut->setRobotpolicy( 'noindex,follow' );
92
93 if ( !( $this->mOldPage->userCanRead() && $this->mNewPage->userCanRead() ) ) {
94 $wgOut->loginToUse();
95 $wgOut->output();
96 wfProfileOut( $fname );
97 exit;
98 }
99
100 $sk = $wgUser->getSkin();
101 $talk = $wgContLang->getNsText( NS_TALK );
102 $contribs = wfMsg( 'contribslink' );
103
104 $this->mOldComment = $sk->formatComment($this->mOldComment);
105 $this->mNewComment = $sk->formatComment($this->mNewComment);
106
107 $oldUserLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER, $this->mOldUser ), $this->mOldUser );
108 $newUserLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER, $this->mNewUser ), $this->mNewUser );
109 $oldUTLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER_TALK, $this->mOldUser ), $talk );
110 $newUTLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER_TALK, $this->mNewUser ), $talk );
111 $oldContribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), $contribs,
112 'target=' . urlencode($this->mOldUser) );
113 $newContribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), $contribs,
114 'target=' . urlencode($this->mNewUser) );
115 if ( !$this->mNewid && $wgUser->isAllowed('rollback') ) {
116 $rollback = '&nbsp;&nbsp;&nbsp;<strong>[' . $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'rollbacklink' ),
117 'action=rollback&from=' . urlencode($this->mNewUser) ) . ']</strong>';
118 } else {
119 $rollback = '';
120 }
121 if ( $wgUseRCPatrol && $this->mRcidMarkPatrolled != 0 && $wgUser->getID() != 0 &&
122 ( $wgUser->isAllowed('rollback') || !$wgOnlySysopsCanPatrol ) )
123 {
124 $patrol = ' [' . $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'markaspatrolleddiff' ),
125 "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']';
126 } else {
127 $patrol = '';
128 }
129
130 $prevlink = $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'previousdiff' ), 'diff=prev&oldid='.$this->mOldid );
131 if ( $this->mNewid == 0 ) {
132 $nextlink = '';
133 } else {
134 $nextlink = $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid );
135 }
136
137 $oldHeader = "<strong>{$this->mOldtitle}</strong><br />$oldUserLink " .
138 "($oldUTLink | $oldContribs)<br />" . $this->mOldComment .
139 '<br />' . $prevlink;
140 $newHeader = "<strong>{$this->mNewtitle}</strong><br />$newUserLink " .
141 "($newUTLink | $newContribs) $rollback<br />" . $this->mNewComment .
142 '<br />' . $nextlink . $patrol;
143
144 DifferenceEngine::showDiff( $this->mOldtext, $this->mNewtext,
145 $oldHeader, $newHeader );
146 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
147 $wgOut->addWikiText( $this->mNewtext );
148
149 wfProfileOut( $fname );
150 }
151
152 # Show the first revision of an article. Uses normal diff headers in contrast to normal
153 # "old revision" display style.
154 #
155 function showFirstRevision()
156 {
157 global $wgOut, $wgTitle, $wgUser, $wgLang;
158
159 $fname = 'DifferenceEngine::showFirstRevision';
160 wfProfileIn( $fname );
161
162
163 $this->mOldid = $this->mNewid; # hack to make loadText() work.
164
165 # Get article text from the DB
166 #
167 if ( ! $this->loadText() ) {
168 $t = $wgTitle->getPrefixedText() . " (Diff: {$this->mOldid}, " .
169 "{$this->mNewid})";
170 $mtext = wfMsg( 'missingarticle', $t );
171 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
172 $wgOut->addHTML( $mtext );
173 wfProfileOut( $fname );
174 return;
175 }
176
177 # Check if user is allowed to look at this page. If not, bail out.
178 #
179 if ( !( $this->mOldPage->userCanRead() ) ) {
180 $wgOut->loginToUse();
181 $wgOut->output();
182 wfProfileOut( $fname );
183 exit;
184 }
185
186 # Prepare the header box
187 #
188 $sk = $wgUser->getSkin();
189
190 $uTLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER_TALK, $this->mOldUser ), $wgLang->getNsText( NS_TALK ) );
191 $userLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER, $this->mOldUser ), $this->mOldUser );
192 $contribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), wfMsg( 'contribslink' ),
193 'target=' . urlencode($this->mOldUser) );
194 $nextlink = $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid );
195 $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />$userLink " .
196 "($uTLink | $contribs)<br />" . $this->mOldComment .
197 '<br />' . $nextlink. "</div>\n";
198
199 $wgOut->addHTML( $header );
200
201 $wgOut->setSubtitle( wfMsg( 'difference' ) );
202 $wgOut->setRobotpolicy( 'noindex,follow' );
203
204
205 # Show current revision
206 #
207 $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
208 $wgOut->addWikiText( $this->mNewtext );
209
210 wfProfileOut( $fname );
211 }
212
213 function showDiff( $otext, $ntext, $otitle, $ntitle )
214 {
215 global $wgOut;
216 $wgOut->addHTML( DifferenceEngine::getDiff( $otext, $ntext, $otitle, $ntitle ) );
217 }
218
219 function getDiff( $otext, $ntext, $otitle, $ntitle ) {
220 global $wgUseExternalDiffEngine, $wgContLang;
221 $out = "
222 <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
223 <tr>
224 <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
225 <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
226 </tr>
227 ";
228 $otext = $wgContLang->segmentForDiff($otext);
229 $ntext = $wgContLang->segmentForDiff($ntext);
230 $difftext='';
231 if ( $wgUseExternalDiffEngine ) {
232 # For historical reasons, external diff engine expects
233 # input text to be HTML-escaped already
234 $otext = str_replace( "\r\n", "\n", htmlspecialchars ( $otext ) );
235 $ntext = str_replace( "\r\n", "\n", htmlspecialchars ( $ntext ) );
236 if( !function_exists( 'wikidiff_do_diff' ) ) {
237 dl('php_wikidiff.so');
238 }
239 $difftext = wikidiff_do_diff( $otext, $ntext, 2 );
240 } else {
241 $ota = explode( "\n", str_replace( "\r\n", "\n", $otext ) );
242 $nta = explode( "\n", str_replace( "\r\n", "\n", $ntext ) );
243 $diffs =& new Diff( $ota, $nta );
244 $formatter =& new TableDiffFormatter();
245 $difftext = $formatter->format( $diffs );
246 }
247 $difftext = $wgContLang->unsegmentForDiff($difftext);
248 $out .= $difftext."</table>\n";
249 return $out;
250 }
251
252 # Load the text of the articles to compare. If newid is 0, then compare
253 # the old article in oldid to the current article; if oldid is 0, then
254 # compare the current article to the immediately previous one (ignoring
255 # the value of newid).
256 #
257 function loadText()
258 {
259 global $wgTitle, $wgOut, $wgLang;
260 $fname = 'DifferenceEngine::loadText';
261
262 $dbr =& wfGetDB( DB_SLAVE );
263 if( $this->mNewid ) {
264 $this->newRev =& Revision::newFromId( $this->mNewid );
265 } else {
266 $this->newRev =& Revision::newFromTitle( $wgTitle );
267 }
268
269 if( $this->newRev->isCurrent() ) {
270 $this->mPagetitle = htmlspecialchars( wfMsg( 'currentrev' ) );
271 $this->mNewPage = $wgTitle;
272 $newLink = $this->mNewPage->escapeLocalUrl();
273 $this->mNewtitle = "<a href='$newLink'>{$this->mPagetitle}</a>";
274 } else {
275 $this->mNewPage = $this->newRev->getTitle();
276 $newLink = $this->mNewPage->escapeLocalUrl ('oldid=' . $this->mNewid );
277 $t = $wgLang->timeanddate( $this->newRev->getTimestamp(), true );
278 $this->mPagetitle = htmlspecialchars( wfMsg( 'revisionasof', $t ) );
279 $this->mNewtitle = "<a href='$newLink'>{$this->mPagetitle}</a>";
280 }
281
282 if( $this->mOldid ) {
283 $this->oldRev =& Revision::newFromId( $this->mOldid );
284 } else {
285 $this->oldRev =& $this->newRev->getPrevious();
286 $this->mOldid = $this->oldRev->getId();
287 }
288
289 $this->mOldPage = $this->oldRev->getTitle();
290
291 $t = $wgLang->timeanddate( $this->oldRev->getTimestamp(), true );
292 $oldLink = $this->mOldPage->escapeLocalUrl( 'oldid=' . $this->mOldid );
293 $this->mOldtitle = "<a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) ) . '</a>';
294
295 $this->mNewUser = $this->newRev->getUserText();
296 $this->mNewComment = $this->newRev->getComment();
297 $this->mNewtext = $this->newRev->getText();
298
299 $this->mOldUser = $this->oldRev->getUserText();
300 $this->mOldComment = $this->oldRev->getComment();
301 $this->mOldtext = $this->oldRev->getText();
302
303 return true;
304 }
305 }
306
307 // A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
308 //
309 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
310 // You may copy this code freely under the conditions of the GPL.
311 //
312
313 define('USE_ASSERTS', function_exists('assert'));
314
315 /**
316 * @todo document
317 * @access private
318 */
319 class _DiffOp {
320 var $type;
321 var $orig;
322 var $closing;
323
324 function reverse() {
325 trigger_error('pure virtual', E_USER_ERROR);
326 }
327
328 function norig() {
329 return $this->orig ? sizeof($this->orig) : 0;
330 }
331
332 function nclosing() {
333 return $this->closing ? sizeof($this->closing) : 0;
334 }
335 }
336
337 /**
338 * @todo document
339 * @access private
340 */
341 class _DiffOp_Copy extends _DiffOp {
342 var $type = 'copy';
343
344 function _DiffOp_Copy ($orig, $closing = false) {
345 if (!is_array($closing))
346 $closing = $orig;
347 $this->orig = $orig;
348 $this->closing = $closing;
349 }
350
351 function reverse() {
352 return new _DiffOp_Copy($this->closing, $this->orig);
353 }
354 }
355
356 /**
357 * @todo document
358 * @access private
359 */
360 class _DiffOp_Delete extends _DiffOp {
361 var $type = 'delete';
362
363 function _DiffOp_Delete ($lines) {
364 $this->orig = $lines;
365 $this->closing = false;
366 }
367
368 function reverse() {
369 return new _DiffOp_Add($this->orig);
370 }
371 }
372
373 /**
374 * @todo document
375 * @access private
376 */
377 class _DiffOp_Add extends _DiffOp {
378 var $type = 'add';
379
380 function _DiffOp_Add ($lines) {
381 $this->closing = $lines;
382 $this->orig = false;
383 }
384
385 function reverse() {
386 return new _DiffOp_Delete($this->closing);
387 }
388 }
389
390 /**
391 * @todo document
392 * @access private
393 */
394 class _DiffOp_Change extends _DiffOp {
395 var $type = 'change';
396
397 function _DiffOp_Change ($orig, $closing) {
398 $this->orig = $orig;
399 $this->closing = $closing;
400 }
401
402 function reverse() {
403 return new _DiffOp_Change($this->closing, $this->orig);
404 }
405 }
406
407
408 /**
409 * Class used internally by Diff to actually compute the diffs.
410 *
411 * The algorithm used here is mostly lifted from the perl module
412 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
413 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
414 *
415 * More ideas are taken from:
416 * http://www.ics.uci.edu/~eppstein/161/960229.html
417 *
418 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
419 * diffutils-2.7, which can be found at:
420 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
421 *
422 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
423 * are my own.
424 *
425 * @author Geoffrey T. Dairiki
426 * @access private
427 */
428 class _DiffEngine
429 {
430 function diff ($from_lines, $to_lines) {
431 $fname = '_DiffEngine::diff';
432 wfProfileIn( $fname );
433
434 $n_from = sizeof($from_lines);
435 $n_to = sizeof($to_lines);
436
437 $this->xchanged = $this->ychanged = array();
438 $this->xv = $this->yv = array();
439 $this->xind = $this->yind = array();
440 unset($this->seq);
441 unset($this->in_seq);
442 unset($this->lcs);
443
444 // Skip leading common lines.
445 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {
446 if ($from_lines[$skip] != $to_lines[$skip])
447 break;
448 $this->xchanged[$skip] = $this->ychanged[$skip] = false;
449 }
450 // Skip trailing common lines.
451 $xi = $n_from; $yi = $n_to;
452 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) {
453 if ($from_lines[$xi] != $to_lines[$yi])
454 break;
455 $this->xchanged[$xi] = $this->ychanged[$yi] = false;
456 }
457
458 // Ignore lines which do not exist in both files.
459 for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
460 $xhash[$from_lines[$xi]] = 1;
461 for ($yi = $skip; $yi < $n_to - $endskip; $yi++) {
462 $line = $to_lines[$yi];
463 if ( ($this->ychanged[$yi] = empty($xhash[$line])) )
464 continue;
465 $yhash[$line] = 1;
466 $this->yv[] = $line;
467 $this->yind[] = $yi;
468 }
469 for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
470 $line = $from_lines[$xi];
471 if ( ($this->xchanged[$xi] = empty($yhash[$line])) )
472 continue;
473 $this->xv[] = $line;
474 $this->xind[] = $xi;
475 }
476
477 // Find the LCS.
478 $this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv));
479
480 // Merge edits when possible
481 $this->_shift_boundaries($from_lines, $this->xchanged, $this->ychanged);
482 $this->_shift_boundaries($to_lines, $this->ychanged, $this->xchanged);
483
484 // Compute the edit operations.
485 $edits = array();
486 $xi = $yi = 0;
487 while ($xi < $n_from || $yi < $n_to) {
488 USE_ASSERTS && assert($yi < $n_to || $this->xchanged[$xi]);
489 USE_ASSERTS && assert($xi < $n_from || $this->ychanged[$yi]);
490
491 // Skip matching "snake".
492 $copy = array();
493 while ( $xi < $n_from && $yi < $n_to
494 && !$this->xchanged[$xi] && !$this->ychanged[$yi]) {
495 $copy[] = $from_lines[$xi++];
496 ++$yi;
497 }
498 if ($copy)
499 $edits[] = new _DiffOp_Copy($copy);
500
501 // Find deletes & adds.
502 $delete = array();
503 while ($xi < $n_from && $this->xchanged[$xi])
504 $delete[] = $from_lines[$xi++];
505
506 $add = array();
507 while ($yi < $n_to && $this->ychanged[$yi])
508 $add[] = $to_lines[$yi++];
509
510 if ($delete && $add)
511 $edits[] = new _DiffOp_Change($delete, $add);
512 elseif ($delete)
513 $edits[] = new _DiffOp_Delete($delete);
514 elseif ($add)
515 $edits[] = new _DiffOp_Add($add);
516 }
517 wfProfileOut( $fname );
518 return $edits;
519 }
520
521
522 /* Divide the Largest Common Subsequence (LCS) of the sequences
523 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
524 * sized segments.
525 *
526 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
527 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
528 * sub sequences. The first sub-sequence is contained in [X0, X1),
529 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
530 * that (X0, Y0) == (XOFF, YOFF) and
531 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
532 *
533 * This function assumes that the first lines of the specified portions
534 * of the two files do not match, and likewise that the last lines do not
535 * match. The caller must trim matching lines from the beginning and end
536 * of the portions it is going to specify.
537 */
538 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
539 $fname = '_DiffEngine::_diag';
540 wfProfileIn( $fname );
541 $flip = false;
542
543 if ($xlim - $xoff > $ylim - $yoff) {
544 // Things seems faster (I'm not sure I understand why)
545 // when the shortest sequence in X.
546 $flip = true;
547 list ($xoff, $xlim, $yoff, $ylim)
548 = array( $yoff, $ylim, $xoff, $xlim);
549 }
550
551 if ($flip)
552 for ($i = $ylim - 1; $i >= $yoff; $i--)
553 $ymatches[$this->xv[$i]][] = $i;
554 else
555 for ($i = $ylim - 1; $i >= $yoff; $i--)
556 $ymatches[$this->yv[$i]][] = $i;
557
558 $this->lcs = 0;
559 $this->seq[0]= $yoff - 1;
560 $this->in_seq = array();
561 $ymids[0] = array();
562
563 $numer = $xlim - $xoff + $nchunks - 1;
564 $x = $xoff;
565 for ($chunk = 0; $chunk < $nchunks; $chunk++) {
566 wfProfileIn( "$fname-chunk" );
567 if ($chunk > 0)
568 for ($i = 0; $i <= $this->lcs; $i++)
569 $ymids[$i][$chunk-1] = $this->seq[$i];
570
571 $x1 = $xoff + (int)(($numer + ($xlim-$xoff)*$chunk) / $nchunks);
572 for ( ; $x < $x1; $x++) {
573 $line = $flip ? $this->yv[$x] : $this->xv[$x];
574 if (empty($ymatches[$line]))
575 continue;
576 $matches = $ymatches[$line];
577 reset($matches);
578 while (list ($junk, $y) = each($matches))
579 if (empty($this->in_seq[$y])) {
580 $k = $this->_lcs_pos($y);
581 USE_ASSERTS && assert($k > 0);
582 $ymids[$k] = $ymids[$k-1];
583 break;
584 }
585 while (list ($junk, $y) = each($matches)) {
586 if ($y > $this->seq[$k-1]) {
587 USE_ASSERTS && assert($y < $this->seq[$k]);
588 // Optimization: this is a common case:
589 // next match is just replacing previous match.
590 $this->in_seq[$this->seq[$k]] = false;
591 $this->seq[$k] = $y;
592 $this->in_seq[$y] = 1;
593 } else if (empty($this->in_seq[$y])) {
594 $k = $this->_lcs_pos($y);
595 USE_ASSERTS && assert($k > 0);
596 $ymids[$k] = $ymids[$k-1];
597 }
598 }
599 }
600 wfProfileOut( "$fname-chunk" );
601 }
602
603 $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
604 $ymid = $ymids[$this->lcs];
605 for ($n = 0; $n < $nchunks - 1; $n++) {
606 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
607 $y1 = $ymid[$n] + 1;
608 $seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
609 }
610 $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
611
612 wfProfileOut( $fname );
613 return array($this->lcs, $seps);
614 }
615
616 function _lcs_pos ($ypos) {
617 $fname = '_DiffEngine::_lcs_pos';
618 wfProfileIn( $fname );
619
620 $end = $this->lcs;
621 if ($end == 0 || $ypos > $this->seq[$end]) {
622 $this->seq[++$this->lcs] = $ypos;
623 $this->in_seq[$ypos] = 1;
624 wfProfileOut( $fname );
625 return $this->lcs;
626 }
627
628 $beg = 1;
629 while ($beg < $end) {
630 $mid = (int)(($beg + $end) / 2);
631 if ( $ypos > $this->seq[$mid] )
632 $beg = $mid + 1;
633 else
634 $end = $mid;
635 }
636
637 USE_ASSERTS && assert($ypos != $this->seq[$end]);
638
639 $this->in_seq[$this->seq[$end]] = false;
640 $this->seq[$end] = $ypos;
641 $this->in_seq[$ypos] = 1;
642 wfProfileOut( $fname );
643 return $end;
644 }
645
646 /* Find LCS of two sequences.
647 *
648 * The results are recorded in the vectors $this->{x,y}changed[], by
649 * storing a 1 in the element for each line that is an insertion
650 * or deletion (ie. is not in the LCS).
651 *
652 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
653 *
654 * Note that XLIM, YLIM are exclusive bounds.
655 * All line numbers are origin-0 and discarded lines are not counted.
656 */
657 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
658 $fname = '_DiffEngine::_compareseq';
659 wfProfileIn( $fname );
660
661 // Slide down the bottom initial diagonal.
662 while ($xoff < $xlim && $yoff < $ylim
663 && $this->xv[$xoff] == $this->yv[$yoff]) {
664 ++$xoff;
665 ++$yoff;
666 }
667
668 // Slide up the top initial diagonal.
669 while ($xlim > $xoff && $ylim > $yoff
670 && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
671 --$xlim;
672 --$ylim;
673 }
674
675 if ($xoff == $xlim || $yoff == $ylim)
676 $lcs = 0;
677 else {
678 // This is ad hoc but seems to work well.
679 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
680 //$nchunks = max(2,min(8,(int)$nchunks));
681 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
682 list ($lcs, $seps)
683 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
684 }
685
686 if ($lcs == 0) {
687 // X and Y sequences have no common subsequence:
688 // mark all changed.
689 while ($yoff < $ylim)
690 $this->ychanged[$this->yind[$yoff++]] = 1;
691 while ($xoff < $xlim)
692 $this->xchanged[$this->xind[$xoff++]] = 1;
693 } else {
694 // Use the partitions to split this problem into subproblems.
695 reset($seps);
696 $pt1 = $seps[0];
697 while ($pt2 = next($seps)) {
698 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
699 $pt1 = $pt2;
700 }
701 }
702 wfProfileOut( $fname );
703 }
704
705 /* Adjust inserts/deletes of identical lines to join changes
706 * as much as possible.
707 *
708 * We do something when a run of changed lines include a
709 * line at one end and has an excluded, identical line at the other.
710 * We are free to choose which identical line is included.
711 * `compareseq' usually chooses the one at the beginning,
712 * but usually it is cleaner to consider the following identical line
713 * to be the "change".
714 *
715 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
716 */
717 function _shift_boundaries ($lines, &$changed, $other_changed) {
718 $fname = '_DiffEngine::_shift_boundaries';
719 wfProfileIn( $fname );
720 $i = 0;
721 $j = 0;
722
723 USE_ASSERTS && assert('sizeof($lines) == sizeof($changed)');
724 $len = sizeof($lines);
725 $other_len = sizeof($other_changed);
726
727 while (1) {
728 /*
729 * Scan forwards to find beginning of another run of changes.
730 * Also keep track of the corresponding point in the other file.
731 *
732 * Throughout this code, $i and $j are adjusted together so that
733 * the first $i elements of $changed and the first $j elements
734 * of $other_changed both contain the same number of zeros
735 * (unchanged lines).
736 * Furthermore, $j is always kept so that $j == $other_len or
737 * $other_changed[$j] == false.
738 */
739 while ($j < $other_len && $other_changed[$j])
740 $j++;
741
742 while ($i < $len && ! $changed[$i]) {
743 USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
744 $i++; $j++;
745 while ($j < $other_len && $other_changed[$j])
746 $j++;
747 }
748
749 if ($i == $len)
750 break;
751
752 $start = $i;
753
754 // Find the end of this run of changes.
755 while (++$i < $len && $changed[$i])
756 continue;
757
758 do {
759 /*
760 * Record the length of this run of changes, so that
761 * we can later determine whether the run has grown.
762 */
763 $runlength = $i - $start;
764
765 /*
766 * Move the changed region back, so long as the
767 * previous unchanged line matches the last changed one.
768 * This merges with previous changed regions.
769 */
770 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
771 $changed[--$start] = 1;
772 $changed[--$i] = false;
773 while ($start > 0 && $changed[$start - 1])
774 $start--;
775 USE_ASSERTS && assert('$j > 0');
776 while ($other_changed[--$j])
777 continue;
778 USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');
779 }
780
781 /*
782 * Set CORRESPONDING to the end of the changed run, at the last
783 * point where it corresponds to a changed run in the other file.
784 * CORRESPONDING == LEN means no such point has been found.
785 */
786 $corresponding = $j < $other_len ? $i : $len;
787
788 /*
789 * Move the changed region forward, so long as the
790 * first changed line matches the following unchanged one.
791 * This merges with following changed regions.
792 * Do this second, so that if there are no merges,
793 * the changed region is moved forward as far as possible.
794 */
795 while ($i < $len && $lines[$start] == $lines[$i]) {
796 $changed[$start++] = false;
797 $changed[$i++] = 1;
798 while ($i < $len && $changed[$i])
799 $i++;
800
801 USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
802 $j++;
803 if ($j < $other_len && $other_changed[$j]) {
804 $corresponding = $i;
805 while ($j < $other_len && $other_changed[$j])
806 $j++;
807 }
808 }
809 } while ($runlength != $i - $start);
810
811 /*
812 * If possible, move the fully-merged run of changes
813 * back to a corresponding run in the other file.
814 */
815 while ($corresponding < $i) {
816 $changed[--$start] = 1;
817 $changed[--$i] = 0;
818 USE_ASSERTS && assert('$j > 0');
819 while ($other_changed[--$j])
820 continue;
821 USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');
822 }
823 }
824 wfProfileOut( $fname );
825 }
826 }
827
828 /**
829 * Class representing a 'diff' between two sequences of strings.
830 * @todo document
831 * @access private
832 */
833 class Diff
834 {
835 var $edits;
836
837 /**
838 * Constructor.
839 * Computes diff between sequences of strings.
840 *
841 * @param $from_lines array An array of strings.
842 * (Typically these are lines from a file.)
843 * @param $to_lines array An array of strings.
844 */
845 function Diff($from_lines, $to_lines) {
846 $eng = new _DiffEngine;
847 $this->edits = $eng->diff($from_lines, $to_lines);
848 //$this->_check($from_lines, $to_lines);
849 }
850
851 /**
852 * Compute reversed Diff.
853 *
854 * SYNOPSIS:
855 *
856 * $diff = new Diff($lines1, $lines2);
857 * $rev = $diff->reverse();
858 * @return object A Diff object representing the inverse of the
859 * original diff.
860 */
861 function reverse () {
862 $rev = $this;
863 $rev->edits = array();
864 foreach ($this->edits as $edit) {
865 $rev->edits[] = $edit->reverse();
866 }
867 return $rev;
868 }
869
870 /**
871 * Check for empty diff.
872 *
873 * @return bool True iff two sequences were identical.
874 */
875 function isEmpty () {
876 foreach ($this->edits as $edit) {
877 if ($edit->type != 'copy')
878 return false;
879 }
880 return true;
881 }
882
883 /**
884 * Compute the length of the Longest Common Subsequence (LCS).
885 *
886 * This is mostly for diagnostic purposed.
887 *
888 * @return int The length of the LCS.
889 */
890 function lcs () {
891 $lcs = 0;
892 foreach ($this->edits as $edit) {
893 if ($edit->type == 'copy')
894 $lcs += sizeof($edit->orig);
895 }
896 return $lcs;
897 }
898
899 /**
900 * Get the original set of lines.
901 *
902 * This reconstructs the $from_lines parameter passed to the
903 * constructor.
904 *
905 * @return array The original sequence of strings.
906 */
907 function orig() {
908 $lines = array();
909
910 foreach ($this->edits as $edit) {
911 if ($edit->orig)
912 array_splice($lines, sizeof($lines), 0, $edit->orig);
913 }
914 return $lines;
915 }
916
917 /**
918 * Get the closing set of lines.
919 *
920 * This reconstructs the $to_lines parameter passed to the
921 * constructor.
922 *
923 * @return array The sequence of strings.
924 */
925 function closing() {
926 $lines = array();
927
928 foreach ($this->edits as $edit) {
929 if ($edit->closing)
930 array_splice($lines, sizeof($lines), 0, $edit->closing);
931 }
932 return $lines;
933 }
934
935 /**
936 * Check a Diff for validity.
937 *
938 * This is here only for debugging purposes.
939 */
940 function _check ($from_lines, $to_lines) {
941 $fname = 'Diff::_check';
942 wfProfileIn( $fname );
943 if (serialize($from_lines) != serialize($this->orig()))
944 trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
945 if (serialize($to_lines) != serialize($this->closing()))
946 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR);
947
948 $rev = $this->reverse();
949 if (serialize($to_lines) != serialize($rev->orig()))
950 trigger_error("Reversed original doesn't match", E_USER_ERROR);
951 if (serialize($from_lines) != serialize($rev->closing()))
952 trigger_error("Reversed closing doesn't match", E_USER_ERROR);
953
954
955 $prevtype = 'none';
956 foreach ($this->edits as $edit) {
957 if ( $prevtype == $edit->type )
958 trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
959 $prevtype = $edit->type;
960 }
961
962 $lcs = $this->lcs();
963 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE);
964 wfProfileOut( $fname );
965 }
966 }
967
968 /**
969 * FIXME: bad name.
970 * @todo document
971 * @access private
972 */
973 class MappedDiff extends Diff
974 {
975 /**
976 * Constructor.
977 *
978 * Computes diff between sequences of strings.
979 *
980 * This can be used to compute things like
981 * case-insensitve diffs, or diffs which ignore
982 * changes in white-space.
983 *
984 * @param $from_lines array An array of strings.
985 * (Typically these are lines from a file.)
986 *
987 * @param $to_lines array An array of strings.
988 *
989 * @param $mapped_from_lines array This array should
990 * have the same size number of elements as $from_lines.
991 * The elements in $mapped_from_lines and
992 * $mapped_to_lines are what is actually compared
993 * when computing the diff.
994 *
995 * @param $mapped_to_lines array This array should
996 * have the same number of elements as $to_lines.
997 */
998 function MappedDiff($from_lines, $to_lines,
999 $mapped_from_lines, $mapped_to_lines) {
1000 $fname = 'MappedDiff::MappedDiff';
1001 wfProfileIn( $fname );
1002
1003 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
1004 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
1005
1006 $this->Diff($mapped_from_lines, $mapped_to_lines);
1007
1008 $xi = $yi = 0;
1009 for ($i = 0; $i < sizeof($this->edits); $i++) {
1010 $orig = &$this->edits[$i]->orig;
1011 if (is_array($orig)) {
1012 $orig = array_slice($from_lines, $xi, sizeof($orig));
1013 $xi += sizeof($orig);
1014 }
1015
1016 $closing = &$this->edits[$i]->closing;
1017 if (is_array($closing)) {
1018 $closing = array_slice($to_lines, $yi, sizeof($closing));
1019 $yi += sizeof($closing);
1020 }
1021 }
1022 wfProfileOut( $fname );
1023 }
1024 }
1025
1026 /**
1027 * A class to format Diffs
1028 *
1029 * This class formats the diff in classic diff format.
1030 * It is intended that this class be customized via inheritance,
1031 * to obtain fancier outputs.
1032 * @todo document
1033 * @access private
1034 */
1035 class DiffFormatter
1036 {
1037 /**
1038 * Number of leading context "lines" to preserve.
1039 *
1040 * This should be left at zero for this class, but subclasses
1041 * may want to set this to other values.
1042 */
1043 var $leading_context_lines = 0;
1044
1045 /**
1046 * Number of trailing context "lines" to preserve.
1047 *
1048 * This should be left at zero for this class, but subclasses
1049 * may want to set this to other values.
1050 */
1051 var $trailing_context_lines = 0;
1052
1053 /**
1054 * Format a diff.
1055 *
1056 * @param $diff object A Diff object.
1057 * @return string The formatted output.
1058 */
1059 function format($diff) {
1060 $fname = 'DiffFormatter::format';
1061 wfProfileIn( $fname );
1062
1063 $xi = $yi = 1;
1064 $block = false;
1065 $context = array();
1066
1067 $nlead = $this->leading_context_lines;
1068 $ntrail = $this->trailing_context_lines;
1069
1070 $this->_start_diff();
1071
1072 foreach ($diff->edits as $edit) {
1073 if ($edit->type == 'copy') {
1074 if (is_array($block)) {
1075 if (sizeof($edit->orig) <= $nlead + $ntrail) {
1076 $block[] = $edit;
1077 }
1078 else{
1079 if ($ntrail) {
1080 $context = array_slice($edit->orig, 0, $ntrail);
1081 $block[] = new _DiffOp_Copy($context);
1082 }
1083 $this->_block($x0, $ntrail + $xi - $x0,
1084 $y0, $ntrail + $yi - $y0,
1085 $block);
1086 $block = false;
1087 }
1088 }
1089 $context = $edit->orig;
1090 }
1091 else {
1092 if (! is_array($block)) {
1093 $context = array_slice($context, sizeof($context) - $nlead);
1094 $x0 = $xi - sizeof($context);
1095 $y0 = $yi - sizeof($context);
1096 $block = array();
1097 if ($context)
1098 $block[] = new _DiffOp_Copy($context);
1099 }
1100 $block[] = $edit;
1101 }
1102
1103 if ($edit->orig)
1104 $xi += sizeof($edit->orig);
1105 if ($edit->closing)
1106 $yi += sizeof($edit->closing);
1107 }
1108
1109 if (is_array($block))
1110 $this->_block($x0, $xi - $x0,
1111 $y0, $yi - $y0,
1112 $block);
1113
1114 $end = $this->_end_diff();
1115 wfProfileOut( $fname );
1116 return $end;
1117 }
1118
1119 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
1120 $fname = 'DiffFormatter::_block';
1121 wfProfileIn( $fname );
1122 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
1123 foreach ($edits as $edit) {
1124 if ($edit->type == 'copy')
1125 $this->_context($edit->orig);
1126 elseif ($edit->type == 'add')
1127 $this->_added($edit->closing);
1128 elseif ($edit->type == 'delete')
1129 $this->_deleted($edit->orig);
1130 elseif ($edit->type == 'change')
1131 $this->_changed($edit->orig, $edit->closing);
1132 else
1133 trigger_error('Unknown edit type', E_USER_ERROR);
1134 }
1135 $this->_end_block();
1136 wfProfileOut( $fname );
1137 }
1138
1139 function _start_diff() {
1140 ob_start();
1141 }
1142
1143 function _end_diff() {
1144 $val = ob_get_contents();
1145 ob_end_clean();
1146 return $val;
1147 }
1148
1149 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
1150 if ($xlen > 1)
1151 $xbeg .= "," . ($xbeg + $xlen - 1);
1152 if ($ylen > 1)
1153 $ybeg .= "," . ($ybeg + $ylen - 1);
1154
1155 return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
1156 }
1157
1158 function _start_block($header) {
1159 echo $header;
1160 }
1161
1162 function _end_block() {
1163 }
1164
1165 function _lines($lines, $prefix = ' ') {
1166 foreach ($lines as $line)
1167 echo "$prefix $line\n";
1168 }
1169
1170 function _context($lines) {
1171 $this->_lines($lines);
1172 }
1173
1174 function _added($lines) {
1175 $this->_lines($lines, '>');
1176 }
1177 function _deleted($lines) {
1178 $this->_lines($lines, '<');
1179 }
1180
1181 function _changed($orig, $closing) {
1182 $this->_deleted($orig);
1183 echo "---\n";
1184 $this->_added($closing);
1185 }
1186 }
1187
1188
1189 /**
1190 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1191 *
1192 */
1193
1194 define('NBSP', '&#160;'); // iso-8859-x non-breaking space.
1195
1196 /**
1197 * @todo document
1198 * @access private
1199 */
1200 class _HWLDF_WordAccumulator {
1201 function _HWLDF_WordAccumulator () {
1202 $this->_lines = array();
1203 $this->_line = '';
1204 $this->_group = '';
1205 $this->_tag = '';
1206 }
1207
1208 function _flushGroup ($new_tag) {
1209 if ($this->_group !== '') {
1210 if ($this->_tag == 'mark')
1211 $this->_line .= '<span class="diffchange">' .
1212 htmlspecialchars ( $this->_group ) . '</span>';
1213 else
1214 $this->_line .= htmlspecialchars ( $this->_group );
1215 }
1216 $this->_group = '';
1217 $this->_tag = $new_tag;
1218 }
1219
1220 function _flushLine ($new_tag) {
1221 $this->_flushGroup($new_tag);
1222 if ($this->_line != '')
1223 array_push ( $this->_lines, $this->_line );
1224 else
1225 # make empty lines visible by inserting an NBSP
1226 array_push ( $this->_lines, NBSP );
1227 $this->_line = '';
1228 }
1229
1230 function addWords ($words, $tag = '') {
1231 if ($tag != $this->_tag)
1232 $this->_flushGroup($tag);
1233
1234 foreach ($words as $word) {
1235 // new-line should only come as first char of word.
1236 if ($word == '')
1237 continue;
1238 if ($word[0] == "\n") {
1239 $this->_flushLine($tag);
1240 $word = substr($word, 1);
1241 }
1242 assert(!strstr($word, "\n"));
1243 $this->_group .= $word;
1244 }
1245 }
1246
1247 function getLines() {
1248 $this->_flushLine('~done');
1249 return $this->_lines;
1250 }
1251 }
1252
1253 /**
1254 * @todo document
1255 * @access private
1256 */
1257 class WordLevelDiff extends MappedDiff
1258 {
1259 function WordLevelDiff ($orig_lines, $closing_lines) {
1260 $fname = 'WordLevelDiff::WordLevelDiff';
1261 wfProfileIn( $fname );
1262
1263 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1264 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1265
1266
1267 $this->MappedDiff($orig_words, $closing_words,
1268 $orig_stripped, $closing_stripped);
1269 wfProfileOut( $fname );
1270 }
1271
1272 function _split($lines) {
1273 $fname = 'WordLevelDiff::_split';
1274 wfProfileIn( $fname );
1275 if (!preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1276 implode("\n", $lines),
1277 $m)) {
1278 wfProfileOut( $fname );
1279 return array(array(''), array(''));
1280 }
1281 wfProfileOut( $fname );
1282 return array($m[0], $m[1]);
1283 }
1284
1285 function orig () {
1286 $fname = 'WordLevelDiff::orig';
1287 wfProfileIn( $fname );
1288 $orig = new _HWLDF_WordAccumulator;
1289
1290 foreach ($this->edits as $edit) {
1291 if ($edit->type == 'copy')
1292 $orig->addWords($edit->orig);
1293 elseif ($edit->orig)
1294 $orig->addWords($edit->orig, 'mark');
1295 }
1296 $lines = $orig->getLines();
1297 wfProfileOut( $fname );
1298 return $lines;
1299 }
1300
1301 function closing () {
1302 $fname = 'WordLevelDiff::closing';
1303 wfProfileIn( $fname );
1304 $closing = new _HWLDF_WordAccumulator;
1305
1306 foreach ($this->edits as $edit) {
1307 if ($edit->type == 'copy')
1308 $closing->addWords($edit->closing);
1309 elseif ($edit->closing)
1310 $closing->addWords($edit->closing, 'mark');
1311 }
1312 $lines = $closing->getLines();
1313 wfProfileOut( $fname );
1314 return $lines;
1315 }
1316 }
1317
1318 /**
1319 * Wikipedia Table style diff formatter.
1320 * @todo document
1321 * @access private
1322 */
1323 class TableDiffFormatter extends DiffFormatter
1324 {
1325 function TableDiffFormatter() {
1326 $this->leading_context_lines = 2;
1327 $this->trailing_context_lines = 2;
1328 }
1329
1330 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1331 $l1 = wfMsg( 'lineno', $xbeg );
1332 $l2 = wfMsg( 'lineno', $ybeg );
1333
1334 $r = '<tr><td colspan="2" align="left"><strong>'.$l1."</strong></td>\n" .
1335 '<td colspan="2" align="left"><strong>'.$l2."</strong></td></tr>\n";
1336 return $r;
1337 }
1338
1339 function _start_block( $header ) {
1340 global $wgOut;
1341 echo $header;
1342 }
1343
1344 function _end_block() {
1345 }
1346
1347 function _lines( $lines, $prefix=' ', $color='white' ) {
1348 }
1349
1350 # HTML-escape parameter before calling this
1351 function addedLine( $line ) {
1352 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
1353 }
1354
1355 # HTML-escape parameter before calling this
1356 function deletedLine( $line ) {
1357 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
1358 }
1359
1360 # HTML-escape parameter before calling this
1361 function contextLine( $line ) {
1362 return "<td> </td><td class='diff-context'>{$line}</td>";
1363 }
1364
1365 function emptyLine() {
1366 return '<td colspan="2">&nbsp;</td>';
1367 }
1368
1369 function _added( $lines ) {
1370 foreach ($lines as $line) {
1371 echo '<tr>' . $this->emptyLine() .
1372 $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1373 }
1374 }
1375
1376 function _deleted($lines) {
1377 foreach ($lines as $line) {
1378 echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
1379 $this->emptyLine() . "</tr>\n";
1380 }
1381 }
1382
1383 function _context( $lines ) {
1384 foreach ($lines as $line) {
1385 echo '<tr>' .
1386 $this->contextLine( htmlspecialchars ( $line ) ) .
1387 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1388 }
1389 }
1390
1391 function _changed( $orig, $closing ) {
1392 $fname = 'TableDiffFormatter::_changed';
1393 wfProfileIn( $fname );
1394
1395 $diff = new WordLevelDiff( $orig, $closing );
1396 $del = $diff->orig();
1397 $add = $diff->closing();
1398
1399 # Notice that WordLevelDiff returns HTML-escaped output.
1400 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1401
1402 while ( $line = array_shift( $del ) ) {
1403 $aline = array_shift( $add );
1404 echo '<tr>' . $this->deletedLine( $line ) .
1405 $this->addedLine( $aline ) . "</tr>\n";
1406 }
1407 foreach ($add as $line) { # If any leftovers
1408 echo '<tr>' . $this->emptyLine() .
1409 $this->addedLine( $line ) . "</tr>\n";
1410 }
1411 wfProfileOut( $fname );
1412 }
1413 }
1414
1415 ?>