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