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