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