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