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