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