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