Merge branch 'master' into Wikidata
[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 * This reconstructs the $to_lines parameter passed to the
755 * constructor.
756 *
757 * @return array The sequence of strings.
758 */
759 function closing() {
760 $lines = array();
761
762 foreach ( $this->edits as $edit ) {
763 if ( $edit->closing ) {
764 array_splice( $lines, sizeof( $lines ), 0, $edit->closing );
765 }
766 }
767 return $lines;
768 }
769
770 /**
771 * Check a Diff for validity.
772 *
773 * This is here only for debugging purposes.
774 * @param $from_lines
775 * @param $to_lines
776 */
777 function _check( $from_lines, $to_lines ) {
778 wfProfileIn( __METHOD__ );
779 if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
780 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR );
781 }
782 if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
783 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR );
784 }
785
786 $rev = $this->reverse();
787 if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
788 trigger_error( "Reversed original doesn't match", E_USER_ERROR );
789 }
790 if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
791 trigger_error( "Reversed closing doesn't match", E_USER_ERROR );
792 }
793
794
795 $prevtype = 'none';
796 foreach ( $this->edits as $edit ) {
797 if ( $prevtype == $edit->type ) {
798 trigger_error( 'Edit sequence is non-optimal', E_USER_ERROR );
799 }
800 $prevtype = $edit->type;
801 }
802
803 $lcs = $this->lcs();
804 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE );
805 wfProfileOut( __METHOD__ );
806 }
807 }
808
809 /**
810 * @todo document, bad name.
811 * @private
812 * @ingroup DifferenceEngine
813 */
814 class MappedDiff extends Diff {
815 /**
816 * Constructor.
817 *
818 * Computes diff between sequences of strings.
819 *
820 * This can be used to compute things like
821 * case-insensitve diffs, or diffs which ignore
822 * changes in white-space.
823 *
824 * @param $from_lines array An array of strings.
825 * (Typically these are lines from a file.)
826 *
827 * @param $to_lines array An array of strings.
828 *
829 * @param $mapped_from_lines array This array should
830 * have the same size number of elements as $from_lines.
831 * The elements in $mapped_from_lines and
832 * $mapped_to_lines are what is actually compared
833 * when computing the diff.
834 *
835 * @param $mapped_to_lines array This array should
836 * have the same number of elements as $to_lines.
837 */
838 function __construct( $from_lines, $to_lines,
839 $mapped_from_lines, $mapped_to_lines ) {
840 wfProfileIn( __METHOD__ );
841
842 assert( 'sizeof( $from_lines ) == sizeof( $mapped_from_lines )' );
843 assert( 'sizeof( $to_lines ) == sizeof( $mapped_to_lines )' );
844
845 parent::__construct( $mapped_from_lines, $mapped_to_lines );
846
847 $xi = $yi = 0;
848 for ( $i = 0; $i < sizeof( $this->edits ); $i++ ) {
849 $orig = &$this->edits[$i]->orig;
850 if ( is_array( $orig ) ) {
851 $orig = array_slice( $from_lines, $xi, sizeof( $orig ) );
852 $xi += sizeof( $orig );
853 }
854
855 $closing = &$this->edits[$i]->closing;
856 if ( is_array( $closing ) ) {
857 $closing = array_slice( $to_lines, $yi, sizeof( $closing ) );
858 $yi += sizeof( $closing );
859 }
860 }
861 wfProfileOut( __METHOD__ );
862 }
863 }
864
865 /**
866 * A class to format Diffs
867 *
868 * This class formats the diff in classic diff format.
869 * It is intended that this class be customized via inheritance,
870 * to obtain fancier outputs.
871 * @todo document
872 * @private
873 * @ingroup DifferenceEngine
874 */
875 class DiffFormatter {
876 /**
877 * Number of leading context "lines" to preserve.
878 *
879 * This should be left at zero for this class, but subclasses
880 * may want to set this to other values.
881 */
882 var $leading_context_lines = 0;
883
884 /**
885 * Number of trailing context "lines" to preserve.
886 *
887 * This should be left at zero for this class, but subclasses
888 * may want to set this to other values.
889 */
890 var $trailing_context_lines = 0;
891
892 /**
893 * Format a diff.
894 *
895 * @param $diff Diff A Diff object.
896 * @return string The formatted output.
897 */
898 function format( $diff ) {
899 wfProfileIn( __METHOD__ );
900
901 $xi = $yi = 1;
902 $block = false;
903 $context = array();
904
905 $nlead = $this->leading_context_lines;
906 $ntrail = $this->trailing_context_lines;
907
908 $this->_start_diff();
909
910 foreach ( $diff->edits as $edit ) {
911 if ( $edit->type == 'copy' ) {
912 if ( is_array( $block ) ) {
913 if ( sizeof( $edit->orig ) <= $nlead + $ntrail ) {
914 $block[] = $edit;
915 } else {
916 if ( $ntrail ) {
917 $context = array_slice( $edit->orig, 0, $ntrail );
918 $block[] = new _DiffOp_Copy( $context );
919 }
920 $this->_block( $x0, $ntrail + $xi - $x0,
921 $y0, $ntrail + $yi - $y0,
922 $block );
923 $block = false;
924 }
925 }
926 $context = $edit->orig;
927 } else {
928 if ( !is_array( $block ) ) {
929 $context = array_slice( $context, sizeof( $context ) - $nlead );
930 $x0 = $xi - sizeof( $context );
931 $y0 = $yi - sizeof( $context );
932 $block = array();
933 if ( $context ) {
934 $block[] = new _DiffOp_Copy( $context );
935 }
936 }
937 $block[] = $edit;
938 }
939
940 if ( $edit->orig ) {
941 $xi += sizeof( $edit->orig );
942 }
943 if ( $edit->closing ) {
944 $yi += sizeof( $edit->closing );
945 }
946 }
947
948 if ( is_array( $block ) ) {
949 $this->_block( $x0, $xi - $x0,
950 $y0, $yi - $y0,
951 $block );
952 }
953
954 $end = $this->_end_diff();
955 wfProfileOut( __METHOD__ );
956 return $end;
957 }
958
959 /**
960 * @param $xbeg
961 * @param $xlen
962 * @param $ybeg
963 * @param $ylen
964 * @param $edits
965 */
966 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
967 wfProfileIn( __METHOD__ );
968 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
969 foreach ( $edits as $edit ) {
970 if ( $edit->type == 'copy' ) {
971 $this->_context( $edit->orig );
972 } elseif ( $edit->type == 'add' ) {
973 $this->_added( $edit->closing );
974 } elseif ( $edit->type == 'delete' ) {
975 $this->_deleted( $edit->orig );
976 } elseif ( $edit->type == 'change' ) {
977 $this->_changed( $edit->orig, $edit->closing );
978 } else {
979 trigger_error( 'Unknown edit type', E_USER_ERROR );
980 }
981 }
982 $this->_end_block();
983 wfProfileOut( __METHOD__ );
984 }
985
986 function _start_diff() {
987 ob_start();
988 }
989
990 /**
991 * @return string
992 */
993 function _end_diff() {
994 $val = ob_get_contents();
995 ob_end_clean();
996 return $val;
997 }
998
999 /**
1000 * @param $xbeg
1001 * @param $xlen
1002 * @param $ybeg
1003 * @param $ylen
1004 * @return string
1005 */
1006 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1007 if ( $xlen > 1 ) {
1008 $xbeg .= ',' . ( $xbeg + $xlen - 1 );
1009 }
1010 if ( $ylen > 1 ) {
1011 $ybeg .= ',' . ( $ybeg + $ylen - 1 );
1012 }
1013
1014 return $xbeg . ( $xlen ? ( $ylen ? 'c' : 'd' ) : 'a' ) . $ybeg;
1015 }
1016
1017 function _start_block( $header ) {
1018 echo $header . "\n";
1019 }
1020
1021 function _end_block() {
1022 }
1023
1024 /**
1025 * @param $lines
1026 * @param $prefix string
1027 */
1028 function _lines( $lines, $prefix = ' ' ) {
1029 foreach ( $lines as $line ) {
1030 echo "$prefix $line\n";
1031 }
1032 }
1033
1034 /**
1035 * @param $lines
1036 */
1037 function _context( $lines ) {
1038 $this->_lines( $lines );
1039 }
1040
1041 /**
1042 * @param $lines
1043 */
1044 function _added( $lines ) {
1045 $this->_lines( $lines, '>' );
1046 }
1047
1048 /**
1049 * @param $lines
1050 */
1051 function _deleted( $lines ) {
1052 $this->_lines( $lines, '<' );
1053 }
1054
1055 /**
1056 * @param $orig
1057 * @param $closing
1058 */
1059 function _changed( $orig, $closing ) {
1060 $this->_deleted( $orig );
1061 echo "---\n";
1062 $this->_added( $closing );
1063 }
1064 }
1065
1066 /**
1067 * A formatter that outputs unified diffs
1068 * @ingroup DifferenceEngine
1069 */
1070 class UnifiedDiffFormatter extends DiffFormatter {
1071 var $leading_context_lines = 2;
1072 var $trailing_context_lines = 2;
1073
1074 /**
1075 * @param $lines
1076 */
1077 function _added( $lines ) {
1078 $this->_lines( $lines, '+' );
1079 }
1080
1081 /**
1082 * @param $lines
1083 */
1084 function _deleted( $lines ) {
1085 $this->_lines( $lines, '-' );
1086 }
1087
1088 /**
1089 * @param $orig
1090 * @param $closing
1091 */
1092 function _changed( $orig, $closing ) {
1093 $this->_deleted( $orig );
1094 $this->_added( $closing );
1095 }
1096
1097 /**
1098 * @param $xbeg
1099 * @param $xlen
1100 * @param $ybeg
1101 * @param $ylen
1102 * @return string
1103 */
1104 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1105 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
1106 }
1107 }
1108
1109 /**
1110 * A pseudo-formatter that just passes along the Diff::$edits array
1111 * @ingroup DifferenceEngine
1112 */
1113 class ArrayDiffFormatter extends DiffFormatter {
1114
1115 /**
1116 * @param $diff
1117 * @return array
1118 */
1119 function format( $diff ) {
1120 $oldline = 1;
1121 $newline = 1;
1122 $retval = array();
1123 foreach ( $diff->edits as $edit ) {
1124 switch( $edit->type ) {
1125 case 'add':
1126 foreach ( $edit->closing as $l ) {
1127 $retval[] = array(
1128 'action' => 'add',
1129 'new' => $l,
1130 'newline' => $newline++
1131 );
1132 }
1133 break;
1134 case 'delete':
1135 foreach ( $edit->orig as $l ) {
1136 $retval[] = array(
1137 'action' => 'delete',
1138 'old' => $l,
1139 'oldline' => $oldline++,
1140 );
1141 }
1142 break;
1143 case 'change':
1144 foreach ( $edit->orig as $i => $l ) {
1145 $retval[] = array(
1146 'action' => 'change',
1147 'old' => $l,
1148 'new' => isset( $edit->closing[$i] ) ? $edit->closing[$i] : null,
1149 'oldline' => $oldline++,
1150 'newline' => $newline++,
1151 );
1152 }
1153 break;
1154 case 'copy':
1155 $oldline += count( $edit->orig );
1156 $newline += count( $edit->orig );
1157 }
1158 }
1159 return $retval;
1160 }
1161 }
1162
1163 /**
1164 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1165 */
1166
1167 define( 'NBSP', '&#160;' ); // iso-8859-x non-breaking space.
1168
1169 /**
1170 * @todo document
1171 * @private
1172 * @ingroup DifferenceEngine
1173 */
1174 class _HWLDF_WordAccumulator {
1175 function __construct() {
1176 $this->_lines = array();
1177 $this->_line = '';
1178 $this->_group = '';
1179 $this->_tag = '';
1180 }
1181
1182 /**
1183 * @param $new_tag
1184 */
1185 function _flushGroup( $new_tag ) {
1186 if ( $this->_group !== '' ) {
1187 if ( $this->_tag == 'ins' ) {
1188 $this->_line .= '<ins class="diffchange diffchange-inline">' .
1189 htmlspecialchars( $this->_group ) . '</ins>';
1190 } elseif ( $this->_tag == 'del' ) {
1191 $this->_line .= '<del class="diffchange diffchange-inline">' .
1192 htmlspecialchars( $this->_group ) . '</del>';
1193 } else {
1194 $this->_line .= htmlspecialchars( $this->_group );
1195 }
1196 }
1197 $this->_group = '';
1198 $this->_tag = $new_tag;
1199 }
1200
1201 /**
1202 * @param $new_tag
1203 */
1204 function _flushLine( $new_tag ) {
1205 $this->_flushGroup( $new_tag );
1206 if ( $this->_line != '' ) {
1207 array_push( $this->_lines, $this->_line );
1208 } else {
1209 # make empty lines visible by inserting an NBSP
1210 array_push( $this->_lines, NBSP );
1211 }
1212 $this->_line = '';
1213 }
1214
1215 /**
1216 * @param $words
1217 * @param $tag string
1218 */
1219 function addWords ( $words, $tag = '' ) {
1220 if ( $tag != $this->_tag ) {
1221 $this->_flushGroup( $tag );
1222 }
1223
1224 foreach ( $words as $word ) {
1225 // new-line should only come as first char of word.
1226 if ( $word == '' ) {
1227 continue;
1228 }
1229 if ( $word[0] == "\n" ) {
1230 $this->_flushLine( $tag );
1231 $word = substr( $word, 1 );
1232 }
1233 assert( '!strstr( $word, "\n" )' );
1234 $this->_group .= $word;
1235 }
1236 }
1237
1238 /**
1239 * @return array
1240 */
1241 function getLines() {
1242 $this->_flushLine( '~done' );
1243 return $this->_lines;
1244 }
1245 }
1246
1247 /**
1248 * @todo document
1249 * @private
1250 * @ingroup DifferenceEngine
1251 */
1252 class WordLevelDiff extends MappedDiff {
1253 const MAX_LINE_LENGTH = 10000;
1254
1255 /**
1256 * @param $orig_lines
1257 * @param $closing_lines
1258 */
1259 function __construct ( $orig_lines, $closing_lines ) {
1260 wfProfileIn( __METHOD__ );
1261
1262 list( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1263 list( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1264
1265 parent::__construct( $orig_words, $closing_words,
1266 $orig_stripped, $closing_stripped );
1267 wfProfileOut( __METHOD__ );
1268 }
1269
1270 /**
1271 * @param $lines
1272 * @return array
1273 */
1274 function _split( $lines ) {
1275 wfProfileIn( __METHOD__ );
1276
1277 $words = array();
1278 $stripped = array();
1279 $first = true;
1280 foreach ( $lines as $line ) {
1281 # If the line is too long, just pretend the entire line is one big word
1282 # This prevents resource exhaustion problems
1283 if ( $first ) {
1284 $first = false;
1285 } else {
1286 $words[] = "\n";
1287 $stripped[] = "\n";
1288 }
1289 if ( strlen( $line ) > self::MAX_LINE_LENGTH ) {
1290 $words[] = $line;
1291 $stripped[] = $line;
1292 } else {
1293 $m = array();
1294 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1295 $line, $m ) )
1296 {
1297 $words = array_merge( $words, $m[0] );
1298 $stripped = array_merge( $stripped, $m[1] );
1299 }
1300 }
1301 }
1302 wfProfileOut( __METHOD__ );
1303 return array( $words, $stripped );
1304 }
1305
1306 /**
1307 * @return array
1308 */
1309 function orig() {
1310 wfProfileIn( __METHOD__ );
1311 $orig = new _HWLDF_WordAccumulator;
1312
1313 foreach ( $this->edits as $edit ) {
1314 if ( $edit->type == 'copy' ) {
1315 $orig->addWords( $edit->orig );
1316 } elseif ( $edit->orig ) {
1317 $orig->addWords( $edit->orig, 'del' );
1318 }
1319 }
1320 $lines = $orig->getLines();
1321 wfProfileOut( __METHOD__ );
1322 return $lines;
1323 }
1324
1325 /**
1326 * @return array
1327 */
1328 function closing() {
1329 wfProfileIn( __METHOD__ );
1330 $closing = new _HWLDF_WordAccumulator;
1331
1332 foreach ( $this->edits as $edit ) {
1333 if ( $edit->type == 'copy' ) {
1334 $closing->addWords( $edit->closing );
1335 } elseif ( $edit->closing ) {
1336 $closing->addWords( $edit->closing, 'ins' );
1337 }
1338 }
1339 $lines = $closing->getLines();
1340 wfProfileOut( __METHOD__ );
1341 return $lines;
1342 }
1343 }
1344
1345 /**
1346 * Wikipedia Table style diff formatter.
1347 * @todo document
1348 * @private
1349 * @ingroup DifferenceEngine
1350 */
1351 class TableDiffFormatter extends DiffFormatter {
1352 function __construct() {
1353 $this->leading_context_lines = 2;
1354 $this->trailing_context_lines = 2;
1355 }
1356
1357 /**
1358 * @static
1359 * @param $msg
1360 * @return mixed
1361 */
1362 public static function escapeWhiteSpace( $msg ) {
1363 $msg = preg_replace( '/^ /m', '&#160; ', $msg );
1364 $msg = preg_replace( '/ $/m', ' &#160;', $msg );
1365 $msg = preg_replace( '/ /', '&#160; ', $msg );
1366 return $msg;
1367 }
1368
1369 /**
1370 * @param $xbeg
1371 * @param $xlen
1372 * @param $ybeg
1373 * @param $ylen
1374 * @return string
1375 */
1376 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1377 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1378 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1379 return $r;
1380 }
1381
1382 /**
1383 * @param $header
1384 */
1385 function _start_block( $header ) {
1386 echo $header;
1387 }
1388
1389 function _end_block() {
1390 }
1391
1392 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1393 }
1394
1395 /**
1396 * HTML-escape parameter before calling this
1397 * @param $line
1398 * @return string
1399 */
1400 function addedLine( $line ) {
1401 return $this->wrapLine( '+', 'diff-addedline', $line );
1402 }
1403
1404 /**
1405 * HTML-escape parameter before calling this
1406 * @param $line
1407 * @return string
1408 */
1409 function deletedLine( $line ) {
1410 return $this->wrapLine( '−', 'diff-deletedline', $line );
1411 }
1412
1413 /**
1414 * HTML-escape parameter before calling this
1415 * @param $line
1416 * @return string
1417 */
1418 function contextLine( $line ) {
1419 return $this->wrapLine( '&#160;', 'diff-context', $line );
1420 }
1421
1422 /**
1423 * @param $marker
1424 * @param $class
1425 * @param $line
1426 * @return string
1427 */
1428 private function wrapLine( $marker, $class, $line ) {
1429 if ( $line !== '' ) {
1430 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1431 $line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1432 }
1433 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1434 }
1435
1436 /**
1437 * @return string
1438 */
1439 function emptyLine() {
1440 return '<td colspan="2">&#160;</td>';
1441 }
1442
1443 /**
1444 * @param $lines array
1445 */
1446 function _added( $lines ) {
1447 foreach ( $lines as $line ) {
1448 echo '<tr>' . $this->emptyLine() .
1449 $this->addedLine( '<ins class="diffchange">' .
1450 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
1451 }
1452 }
1453
1454 /**
1455 * @param $lines
1456 */
1457 function _deleted( $lines ) {
1458 foreach ( $lines as $line ) {
1459 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1460 htmlspecialchars( $line ) . '</del>' ) .
1461 $this->emptyLine() . "</tr>\n";
1462 }
1463 }
1464
1465 /**
1466 * @param $lines
1467 */
1468 function _context( $lines ) {
1469 foreach ( $lines as $line ) {
1470 echo '<tr>' .
1471 $this->contextLine( htmlspecialchars( $line ) ) .
1472 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
1473 }
1474 }
1475
1476 /**
1477 * @param $orig
1478 * @param $closing
1479 */
1480 function _changed( $orig, $closing ) {
1481 wfProfileIn( __METHOD__ );
1482
1483 $diff = new WordLevelDiff( $orig, $closing );
1484 $del = $diff->orig();
1485 $add = $diff->closing();
1486
1487 # Notice that WordLevelDiff returns HTML-escaped output.
1488 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1489
1490 while ( $line = array_shift( $del ) ) {
1491 $aline = array_shift( $add );
1492 echo '<tr>' . $this->deletedLine( $line ) .
1493 $this->addedLine( $aline ) . "</tr>\n";
1494 }
1495 foreach ( $add as $line ) { # If any leftovers
1496 echo '<tr>' . $this->emptyLine() .
1497 $this->addedLine( $line ) . "</tr>\n";
1498 }
1499 wfProfileOut( __METHOD__ );
1500 }
1501 }