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