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