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