Merge "Monobook: Solve padding issues with #content and #firstheading"
[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; $yi = $n_to;
275 for ( $endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++ ) {
276 if ( $from_lines[$xi] !== $to_lines[$yi] ) {
277 break;
278 }
279 $this->xchanged[$xi] = $this->ychanged[$yi] = false;
280 }
281
282 // Ignore lines which do not exist in both files.
283 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++ ) {
284 $xhash[$this->_line_hash( $from_lines[$xi] )] = 1;
285 }
286
287 for ( $yi = $skip; $yi < $n_to - $endskip; $yi++ ) {
288 $line = $to_lines[$yi];
289 if ( ( $this->ychanged[$yi] = empty( $xhash[$this->_line_hash( $line )] ) ) ) {
290 continue;
291 }
292 $yhash[$this->_line_hash( $line )] = 1;
293 $this->yv[] = $line;
294 $this->yind[] = $yi;
295 }
296 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++ ) {
297 $line = $from_lines[$xi];
298 if ( ( $this->xchanged[$xi] = empty( $yhash[$this->_line_hash( $line )] ) ) ) {
299 continue;
300 }
301 $this->xv[] = $line;
302 $this->xind[] = $xi;
303 }
304
305 // Find the LCS.
306 $this->_compareseq( 0, count( $this->xv ), 0, count( $this->yv ) );
307 }
308 wfProfileOut( __METHOD__ );
309 }
310
311 /**
312 * Returns the whole line if it's small enough, or the MD5 hash otherwise
313 * @param $line string
314 * @return string
315 */
316 function _line_hash( $line ) {
317 if ( strlen( $line ) > self::MAX_XREF_LENGTH ) {
318 return md5( $line );
319 } else {
320 return $line;
321 }
322 }
323
324 /**
325 * Divide the Largest Common Subsequence (LCS) of the sequences
326 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
327 * sized segments.
328 *
329 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
330 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
331 * sub sequences. The first sub-sequence is contained in [X0, X1),
332 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
333 * that (X0, Y0) == (XOFF, YOFF) and
334 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
335 *
336 * This function assumes that the first lines of the specified portions
337 * of the two files do not match, and likewise that the last lines do not
338 * match. The caller must trim matching lines from the beginning and end
339 * of the portions it is going to specify.
340 * @param $xoff
341 * @param $xlim
342 * @param $yoff
343 * @param $ylim
344 * @param $nchunks
345 * @return array
346 */
347 function _diag( $xoff, $xlim, $yoff, $ylim, $nchunks ) {
348 $flip = false;
349
350 if ( $xlim - $xoff > $ylim - $yoff ) {
351 // Things seems faster (I'm not sure I understand why)
352 // when the shortest sequence in X.
353 $flip = true;
354 list( $xoff, $xlim, $yoff, $ylim ) = array( $yoff, $ylim, $xoff, $xlim );
355 }
356
357 if ( $flip ) {
358 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
359 $ymatches[$this->xv[$i]][] = $i;
360 }
361 } else {
362 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
363 $ymatches[$this->yv[$i]][] = $i;
364 }
365 }
366
367 $this->lcs = 0;
368 $this->seq[0] = $yoff - 1;
369 $this->in_seq = array();
370 $ymids[0] = array();
371
372 $numer = $xlim - $xoff + $nchunks - 1;
373 $x = $xoff;
374 for ( $chunk = 0; $chunk < $nchunks; $chunk++ ) {
375 if ( $chunk > 0 ) {
376 for ( $i = 0; $i <= $this->lcs; $i++ ) {
377 $ymids[$i][$chunk -1] = $this->seq[$i];
378 }
379 }
380
381 $x1 = $xoff + (int)( ( $numer + ( $xlim -$xoff ) * $chunk ) / $nchunks );
382 for ( ; $x < $x1; $x++ ) {
383 $line = $flip ? $this->yv[$x] : $this->xv[$x];
384 if ( empty( $ymatches[$line] ) ) {
385 continue;
386 }
387 $matches = $ymatches[$line];
388 reset( $matches );
389 while ( list( , $y ) = each( $matches ) ) {
390 if ( empty( $this->in_seq[$y] ) ) {
391 $k = $this->_lcs_pos( $y );
392 assert( '$k > 0' );
393 $ymids[$k] = $ymids[$k -1];
394 break;
395 }
396 }
397 while ( list ( , $y ) = each( $matches ) ) {
398 if ( $y > $this->seq[$k -1] ) {
399 assert( '$y < $this->seq[$k]' );
400 // Optimization: this is a common case:
401 // next match is just replacing previous match.
402 $this->in_seq[$this->seq[$k]] = false;
403 $this->seq[$k] = $y;
404 $this->in_seq[$y] = 1;
405 } elseif ( empty( $this->in_seq[$y] ) ) {
406 $k = $this->_lcs_pos( $y );
407 assert( '$k > 0' );
408 $ymids[$k] = $ymids[$k -1];
409 }
410 }
411 }
412 }
413
414 $seps[] = $flip ? array( $yoff, $xoff ) : array( $xoff, $yoff );
415 $ymid = $ymids[$this->lcs];
416 for ( $n = 0; $n < $nchunks - 1; $n++ ) {
417 $x1 = $xoff + (int)( ( $numer + ( $xlim - $xoff ) * $n ) / $nchunks );
418 $y1 = $ymid[$n] + 1;
419 $seps[] = $flip ? array( $y1, $x1 ) : array( $x1, $y1 );
420 }
421 $seps[] = $flip ? array( $ylim, $xlim ) : array( $xlim, $ylim );
422
423 return array( $this->lcs, $seps );
424 }
425
426 /**
427 * @param $ypos
428 * @return int
429 */
430 function _lcs_pos( $ypos ) {
431 $end = $this->lcs;
432 if ( $end == 0 || $ypos > $this->seq[$end] ) {
433 $this->seq[++$this->lcs] = $ypos;
434 $this->in_seq[$ypos] = 1;
435 return $this->lcs;
436 }
437
438 $beg = 1;
439 while ( $beg < $end ) {
440 $mid = (int)( ( $beg + $end ) / 2 );
441 if ( $ypos > $this->seq[$mid] ) {
442 $beg = $mid + 1;
443 } else {
444 $end = $mid;
445 }
446 }
447
448 assert( '$ypos != $this->seq[$end]' );
449
450 $this->in_seq[$this->seq[$end]] = false;
451 $this->seq[$end] = $ypos;
452 $this->in_seq[$ypos] = 1;
453 return $end;
454 }
455
456 /**
457 * Find LCS of two sequences.
458 *
459 * The results are recorded in the vectors $this->{x,y}changed[], by
460 * storing a 1 in the element for each line that is an insertion
461 * or deletion (ie. is not in the LCS).
462 *
463 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
464 *
465 * Note that XLIM, YLIM are exclusive bounds.
466 * All line numbers are origin-0 and discarded lines are not counted.
467 * @param $xoff
468 * @param $xlim
469 * @param $yoff
470 * @param $ylim
471 */
472 function _compareseq( $xoff, $xlim, $yoff, $ylim ) {
473 // Slide down the bottom initial diagonal.
474 while ( $xoff < $xlim && $yoff < $ylim
475 && $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++; $j++;
558 while ( $j < $other_len && $other_changed[$j] )
559 $j++;
560 }
561
562 if ( $i == $len ) {
563 break;
564 }
565
566 $start = $i;
567
568 // Find the end of this run of changes.
569 while ( ++$i < $len && $changed[$i] ) {
570 continue;
571 }
572
573 do {
574 /*
575 * Record the length of this run of changes, so that
576 * we can later determine whether the run has grown.
577 */
578 $runlength = $i - $start;
579
580 /*
581 * Move the changed region back, so long as the
582 * previous unchanged line matches the last changed one.
583 * This merges with previous changed regions.
584 */
585 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
586 $changed[--$start] = 1;
587 $changed[--$i] = false;
588 while ( $start > 0 && $changed[$start - 1] ) {
589 $start--;
590 }
591 assert( '$j > 0' );
592 while ( $other_changed[--$j] ) {
593 continue;
594 }
595 assert( '$j >= 0 && !$other_changed[$j]' );
596 }
597
598 /*
599 * Set CORRESPONDING to the end of the changed run, at the last
600 * point where it corresponds to a changed run in the other file.
601 * CORRESPONDING == LEN means no such point has been found.
602 */
603 $corresponding = $j < $other_len ? $i : $len;
604
605 /*
606 * Move the changed region forward, so long as the
607 * first changed line matches the following unchanged one.
608 * This merges with following changed regions.
609 * Do this second, so that if there are no merges,
610 * the changed region is moved forward as far as possible.
611 */
612 while ( $i < $len && $lines[$start] == $lines[$i] ) {
613 $changed[$start++] = false;
614 $changed[$i++] = 1;
615 while ( $i < $len && $changed[$i] ) {
616 $i++;
617 }
618
619 assert( '$j < $other_len && ! $other_changed[$j]' );
620 $j++;
621 if ( $j < $other_len && $other_changed[$j] ) {
622 $corresponding = $i;
623 while ( $j < $other_len && $other_changed[$j] ) {
624 $j++;
625 }
626 }
627 }
628 } while ( $runlength != $i - $start );
629
630 /*
631 * If possible, move the fully-merged run of changes
632 * back to a corresponding run in the other file.
633 */
634 while ( $corresponding < $i ) {
635 $changed[--$start] = 1;
636 $changed[--$i] = 0;
637 assert( '$j > 0' );
638 while ( $other_changed[--$j] ) {
639 continue;
640 }
641 assert( '$j >= 0 && !$other_changed[$j]' );
642 }
643 }
644 wfProfileOut( __METHOD__ );
645 }
646 }
647
648 /**
649 * Class representing a 'diff' between two sequences of strings.
650 * @todo document
651 * @private
652 * @ingroup DifferenceEngine
653 */
654 class Diff {
655 var $edits;
656
657 /**
658 * Constructor.
659 * Computes diff between sequences of strings.
660 *
661 * @param $from_lines array An array of strings.
662 * (Typically these are lines from a file.)
663 * @param $to_lines array An array of strings.
664 */
665 function __construct( $from_lines, $to_lines ) {
666 $eng = new _DiffEngine;
667 $this->edits = $eng->diff( $from_lines, $to_lines );
668 // $this->_check($from_lines, $to_lines);
669 }
670
671 /**
672 * Compute reversed Diff.
673 *
674 * SYNOPSIS:
675 *
676 * $diff = new Diff($lines1, $lines2);
677 * $rev = $diff->reverse();
678 * @return Object A Diff object representing the inverse of the
679 * original diff.
680 */
681 function reverse() {
682 $rev = $this;
683 $rev->edits = array();
684 foreach ( $this->edits as $edit ) {
685 $rev->edits[] = $edit->reverse();
686 }
687 return $rev;
688 }
689
690 /**
691 * Check for empty diff.
692 *
693 * @return bool True iff two sequences were identical.
694 */
695 function isEmpty() {
696 foreach ( $this->edits as $edit ) {
697 if ( $edit->type != 'copy' ) {
698 return false;
699 }
700 }
701 return true;
702 }
703
704 /**
705 * Compute the length of the Longest Common Subsequence (LCS).
706 *
707 * This is mostly for diagnostic purposed.
708 *
709 * @return int The length of the LCS.
710 */
711 function lcs() {
712 $lcs = 0;
713 foreach ( $this->edits as $edit ) {
714 if ( $edit->type == 'copy' ) {
715 $lcs += count( $edit->orig );
716 }
717 }
718 return $lcs;
719 }
720
721 /**
722 * Get the original set of lines.
723 *
724 * This reconstructs the $from_lines parameter passed to the
725 * constructor.
726 *
727 * @return array The original sequence of strings.
728 */
729 function orig() {
730 $lines = array();
731
732 foreach ( $this->edits as $edit ) {
733 if ( $edit->orig ) {
734 array_splice( $lines, count( $lines ), 0, $edit->orig );
735 }
736 }
737 return $lines;
738 }
739
740 /**
741 * Get the closing set of lines.
742 *
743 * This reconstructs the $to_lines parameter passed to the
744 * constructor.
745 *
746 * @return array The sequence of strings.
747 */
748 function closing() {
749 $lines = array();
750
751 foreach ( $this->edits as $edit ) {
752 if ( $edit->closing ) {
753 array_splice( $lines, count( $lines ), 0, $edit->closing );
754 }
755 }
756 return $lines;
757 }
758
759 /**
760 * Check a Diff for validity.
761 *
762 * This is here only for debugging purposes.
763 * @param $from_lines
764 * @param $to_lines
765 */
766 function _check( $from_lines, $to_lines ) {
767 wfProfileIn( __METHOD__ );
768 if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
769 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR );
770 }
771 if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
772 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR );
773 }
774
775 $rev = $this->reverse();
776 if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
777 trigger_error( "Reversed original doesn't match", E_USER_ERROR );
778 }
779 if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
780 trigger_error( "Reversed closing doesn't match", E_USER_ERROR );
781 }
782
783 $prevtype = 'none';
784 foreach ( $this->edits as $edit ) {
785 if ( $prevtype == $edit->type ) {
786 trigger_error( 'Edit sequence is non-optimal', E_USER_ERROR );
787 }
788 $prevtype = $edit->type;
789 }
790
791 $lcs = $this->lcs();
792 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE );
793 wfProfileOut( __METHOD__ );
794 }
795 }
796
797 /**
798 * @todo document, bad name.
799 * @private
800 * @ingroup DifferenceEngine
801 */
802 class MappedDiff extends Diff {
803 /**
804 * Constructor.
805 *
806 * Computes diff between sequences of strings.
807 *
808 * This can be used to compute things like
809 * case-insensitve diffs, or diffs which ignore
810 * changes in white-space.
811 *
812 * @param $from_lines array An array of strings.
813 * (Typically these are lines from a file.)
814 *
815 * @param $to_lines array An array of strings.
816 *
817 * @param $mapped_from_lines array This array should
818 * have the same size number of elements as $from_lines.
819 * The elements in $mapped_from_lines and
820 * $mapped_to_lines are what is actually compared
821 * when computing the diff.
822 *
823 * @param $mapped_to_lines array This array should
824 * have the same number of elements as $to_lines.
825 */
826 function __construct( $from_lines, $to_lines,
827 $mapped_from_lines, $mapped_to_lines ) {
828 wfProfileIn( __METHOD__ );
829
830 assert( 'count( $from_lines ) == count( $mapped_from_lines )' );
831 assert( 'count( $to_lines ) == count( $mapped_to_lines )' );
832
833 parent::__construct( $mapped_from_lines, $mapped_to_lines );
834
835 $xi = $yi = 0;
836 for ( $i = 0; $i < count( $this->edits ); $i++ ) {
837 $orig = &$this->edits[$i]->orig;
838 if ( is_array( $orig ) ) {
839 $orig = array_slice( $from_lines, $xi, count( $orig ) );
840 $xi += count( $orig );
841 }
842
843 $closing = &$this->edits[$i]->closing;
844 if ( is_array( $closing ) ) {
845 $closing = array_slice( $to_lines, $yi, count( $closing ) );
846 $yi += count( $closing );
847 }
848 }
849 wfProfileOut( __METHOD__ );
850 }
851 }
852
853 /**
854 * A class to format Diffs
855 *
856 * This class formats the diff in classic diff format.
857 * It is intended that this class be customized via inheritance,
858 * to obtain fancier outputs.
859 * @todo document
860 * @private
861 * @ingroup DifferenceEngine
862 */
863 class DiffFormatter {
864 /**
865 * Number of leading context "lines" to preserve.
866 *
867 * This should be left at zero for this class, but subclasses
868 * may want to set this to other values.
869 */
870 var $leading_context_lines = 0;
871
872 /**
873 * Number of trailing context "lines" to preserve.
874 *
875 * This should be left at zero for this class, but subclasses
876 * may want to set this to other values.
877 */
878 var $trailing_context_lines = 0;
879
880 /**
881 * Format a diff.
882 *
883 * @param $diff Diff A Diff object.
884 * @return string The formatted output.
885 */
886 function format( $diff ) {
887 wfProfileIn( __METHOD__ );
888
889 $xi = $yi = 1;
890 $block = false;
891 $context = array();
892
893 $nlead = $this->leading_context_lines;
894 $ntrail = $this->trailing_context_lines;
895
896 $this->_start_diff();
897
898 foreach ( $diff->edits as $edit ) {
899 if ( $edit->type == 'copy' ) {
900 if ( is_array( $block ) ) {
901 if ( count( $edit->orig ) <= $nlead + $ntrail ) {
902 $block[] = $edit;
903 } else {
904 if ( $ntrail ) {
905 $context = array_slice( $edit->orig, 0, $ntrail );
906 $block[] = new _DiffOp_Copy( $context );
907 }
908 $this->_block( $x0, $ntrail + $xi - $x0,
909 $y0, $ntrail + $yi - $y0,
910 $block );
911 $block = false;
912 }
913 }
914 $context = $edit->orig;
915 } else {
916 if ( !is_array( $block ) ) {
917 $context = array_slice( $context, count( $context ) - $nlead );
918 $x0 = $xi - count( $context );
919 $y0 = $yi - count( $context );
920 $block = array();
921 if ( $context ) {
922 $block[] = new _DiffOp_Copy( $context );
923 }
924 }
925 $block[] = $edit;
926 }
927
928 if ( $edit->orig ) {
929 $xi += count( $edit->orig );
930 }
931 if ( $edit->closing ) {
932 $yi += count( $edit->closing );
933 }
934 }
935
936 if ( is_array( $block ) ) {
937 $this->_block( $x0, $xi - $x0,
938 $y0, $yi - $y0,
939 $block );
940 }
941
942 $end = $this->_end_diff();
943 wfProfileOut( __METHOD__ );
944 return $end;
945 }
946
947 /**
948 * @param $xbeg
949 * @param $xlen
950 * @param $ybeg
951 * @param $ylen
952 * @param $edits
953 */
954 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
955 wfProfileIn( __METHOD__ );
956 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
957 foreach ( $edits as $edit ) {
958 if ( $edit->type == 'copy' ) {
959 $this->_context( $edit->orig );
960 } elseif ( $edit->type == 'add' ) {
961 $this->_added( $edit->closing );
962 } elseif ( $edit->type == 'delete' ) {
963 $this->_deleted( $edit->orig );
964 } elseif ( $edit->type == 'change' ) {
965 $this->_changed( $edit->orig, $edit->closing );
966 } else {
967 trigger_error( 'Unknown edit type', E_USER_ERROR );
968 }
969 }
970 $this->_end_block();
971 wfProfileOut( __METHOD__ );
972 }
973
974 function _start_diff() {
975 ob_start();
976 }
977
978 /**
979 * @return string
980 */
981 function _end_diff() {
982 $val = ob_get_contents();
983 ob_end_clean();
984 return $val;
985 }
986
987 /**
988 * @param $xbeg
989 * @param $xlen
990 * @param $ybeg
991 * @param $ylen
992 * @return string
993 */
994 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
995 if ( $xlen > 1 ) {
996 $xbeg .= ',' . ( $xbeg + $xlen - 1 );
997 }
998 if ( $ylen > 1 ) {
999 $ybeg .= ',' . ( $ybeg + $ylen - 1 );
1000 }
1001
1002 return $xbeg . ( $xlen ? ( $ylen ? 'c' : 'd' ) : 'a' ) . $ybeg;
1003 }
1004
1005 function _start_block( $header ) {
1006 echo $header . "\n";
1007 }
1008
1009 function _end_block() {
1010 }
1011
1012 /**
1013 * @param $lines
1014 * @param $prefix string
1015 */
1016 function _lines( $lines, $prefix = ' ' ) {
1017 foreach ( $lines as $line ) {
1018 echo "$prefix $line\n";
1019 }
1020 }
1021
1022 /**
1023 * @param $lines
1024 */
1025 function _context( $lines ) {
1026 $this->_lines( $lines );
1027 }
1028
1029 /**
1030 * @param $lines
1031 */
1032 function _added( $lines ) {
1033 $this->_lines( $lines, '>' );
1034 }
1035
1036 /**
1037 * @param $lines
1038 */
1039 function _deleted( $lines ) {
1040 $this->_lines( $lines, '<' );
1041 }
1042
1043 /**
1044 * @param $orig
1045 * @param $closing
1046 */
1047 function _changed( $orig, $closing ) {
1048 $this->_deleted( $orig );
1049 echo "---\n";
1050 $this->_added( $closing );
1051 }
1052 }
1053
1054 /**
1055 * A formatter that outputs unified diffs
1056 * @ingroup DifferenceEngine
1057 */
1058 class UnifiedDiffFormatter extends DiffFormatter {
1059 var $leading_context_lines = 2;
1060 var $trailing_context_lines = 2;
1061
1062 /**
1063 * @param $lines
1064 */
1065 function _added( $lines ) {
1066 $this->_lines( $lines, '+' );
1067 }
1068
1069 /**
1070 * @param $lines
1071 */
1072 function _deleted( $lines ) {
1073 $this->_lines( $lines, '-' );
1074 }
1075
1076 /**
1077 * @param $orig
1078 * @param $closing
1079 */
1080 function _changed( $orig, $closing ) {
1081 $this->_deleted( $orig );
1082 $this->_added( $closing );
1083 }
1084
1085 /**
1086 * @param $xbeg
1087 * @param $xlen
1088 * @param $ybeg
1089 * @param $ylen
1090 * @return string
1091 */
1092 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1093 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
1094 }
1095 }
1096
1097 /**
1098 * A pseudo-formatter that just passes along the Diff::$edits array
1099 * @ingroup DifferenceEngine
1100 */
1101 class ArrayDiffFormatter extends DiffFormatter {
1102
1103 /**
1104 * @param $diff
1105 * @return array
1106 */
1107 function format( $diff ) {
1108 $oldline = 1;
1109 $newline = 1;
1110 $retval = array();
1111 foreach ( $diff->edits as $edit ) {
1112 switch( $edit->type ) {
1113 case 'add':
1114 foreach ( $edit->closing as $l ) {
1115 $retval[] = array(
1116 'action' => 'add',
1117 'new' => $l,
1118 'newline' => $newline++
1119 );
1120 }
1121 break;
1122 case 'delete':
1123 foreach ( $edit->orig as $l ) {
1124 $retval[] = array(
1125 'action' => 'delete',
1126 'old' => $l,
1127 'oldline' => $oldline++,
1128 );
1129 }
1130 break;
1131 case 'change':
1132 foreach ( $edit->orig as $i => $l ) {
1133 $retval[] = array(
1134 'action' => 'change',
1135 'old' => $l,
1136 'new' => isset( $edit->closing[$i] ) ? $edit->closing[$i] : null,
1137 'oldline' => $oldline++,
1138 'newline' => $newline++,
1139 );
1140 }
1141 break;
1142 case 'copy':
1143 $oldline += count( $edit->orig );
1144 $newline += count( $edit->orig );
1145 }
1146 }
1147 return $retval;
1148 }
1149 }
1150
1151 /**
1152 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1153 */
1154
1155 define( 'NBSP', '&#160;' ); // iso-8859-x non-breaking space.
1156
1157 /**
1158 * @todo document
1159 * @private
1160 * @ingroup DifferenceEngine
1161 */
1162 class _HWLDF_WordAccumulator {
1163 function __construct() {
1164 $this->_lines = array();
1165 $this->_line = '';
1166 $this->_group = '';
1167 $this->_tag = '';
1168 }
1169
1170 /**
1171 * @param $new_tag
1172 */
1173 function _flushGroup( $new_tag ) {
1174 if ( $this->_group !== '' ) {
1175 if ( $this->_tag == 'ins' ) {
1176 $this->_line .= '<ins class="diffchange diffchange-inline">' .
1177 htmlspecialchars( $this->_group ) . '</ins>';
1178 } elseif ( $this->_tag == 'del' ) {
1179 $this->_line .= '<del class="diffchange diffchange-inline">' .
1180 htmlspecialchars( $this->_group ) . '</del>';
1181 } else {
1182 $this->_line .= htmlspecialchars( $this->_group );
1183 }
1184 }
1185 $this->_group = '';
1186 $this->_tag = $new_tag;
1187 }
1188
1189 /**
1190 * @param $new_tag
1191 */
1192 function _flushLine( $new_tag ) {
1193 $this->_flushGroup( $new_tag );
1194 if ( $this->_line != '' ) {
1195 array_push( $this->_lines, $this->_line );
1196 } else {
1197 # make empty lines visible by inserting an NBSP
1198 array_push( $this->_lines, NBSP );
1199 }
1200 $this->_line = '';
1201 }
1202
1203 /**
1204 * @param $words
1205 * @param $tag string
1206 */
1207 function addWords( $words, $tag = '' ) {
1208 if ( $tag != $this->_tag ) {
1209 $this->_flushGroup( $tag );
1210 }
1211
1212 foreach ( $words as $word ) {
1213 // new-line should only come as first char of word.
1214 if ( $word == '' ) {
1215 continue;
1216 }
1217 if ( $word[0] == "\n" ) {
1218 $this->_flushLine( $tag );
1219 $word = substr( $word, 1 );
1220 }
1221 assert( '!strstr( $word, "\n" )' );
1222 $this->_group .= $word;
1223 }
1224 }
1225
1226 /**
1227 * @return array
1228 */
1229 function getLines() {
1230 $this->_flushLine( '~done' );
1231 return $this->_lines;
1232 }
1233 }
1234
1235 /**
1236 * @todo document
1237 * @private
1238 * @ingroup DifferenceEngine
1239 */
1240 class WordLevelDiff extends MappedDiff {
1241 const MAX_LINE_LENGTH = 10000;
1242
1243 /**
1244 * @param $orig_lines
1245 * @param $closing_lines
1246 */
1247 function __construct( $orig_lines, $closing_lines ) {
1248 wfProfileIn( __METHOD__ );
1249
1250 list( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1251 list( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1252
1253 parent::__construct( $orig_words, $closing_words,
1254 $orig_stripped, $closing_stripped );
1255 wfProfileOut( __METHOD__ );
1256 }
1257
1258 /**
1259 * @param $lines
1260 * @return array
1261 */
1262 function _split( $lines ) {
1263 wfProfileIn( __METHOD__ );
1264
1265 $words = array();
1266 $stripped = array();
1267 $first = true;
1268 foreach ( $lines as $line ) {
1269 # If the line is too long, just pretend the entire line is one big word
1270 # This prevents resource exhaustion problems
1271 if ( $first ) {
1272 $first = false;
1273 } else {
1274 $words[] = "\n";
1275 $stripped[] = "\n";
1276 }
1277 if ( strlen( $line ) > self::MAX_LINE_LENGTH ) {
1278 $words[] = $line;
1279 $stripped[] = $line;
1280 } else {
1281 $m = array();
1282 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1283 $line, $m ) )
1284 {
1285 $words = array_merge( $words, $m[0] );
1286 $stripped = array_merge( $stripped, $m[1] );
1287 }
1288 }
1289 }
1290 wfProfileOut( __METHOD__ );
1291 return array( $words, $stripped );
1292 }
1293
1294 /**
1295 * @return array
1296 */
1297 function orig() {
1298 wfProfileIn( __METHOD__ );
1299 $orig = new _HWLDF_WordAccumulator;
1300
1301 foreach ( $this->edits as $edit ) {
1302 if ( $edit->type == 'copy' ) {
1303 $orig->addWords( $edit->orig );
1304 } elseif ( $edit->orig ) {
1305 $orig->addWords( $edit->orig, 'del' );
1306 }
1307 }
1308 $lines = $orig->getLines();
1309 wfProfileOut( __METHOD__ );
1310 return $lines;
1311 }
1312
1313 /**
1314 * @return array
1315 */
1316 function closing() {
1317 wfProfileIn( __METHOD__ );
1318 $closing = new _HWLDF_WordAccumulator;
1319
1320 foreach ( $this->edits as $edit ) {
1321 if ( $edit->type == 'copy' ) {
1322 $closing->addWords( $edit->closing );
1323 } elseif ( $edit->closing ) {
1324 $closing->addWords( $edit->closing, 'ins' );
1325 }
1326 }
1327 $lines = $closing->getLines();
1328 wfProfileOut( __METHOD__ );
1329 return $lines;
1330 }
1331 }
1332
1333 /**
1334 * Wikipedia Table style diff formatter.
1335 * @todo document
1336 * @private
1337 * @ingroup DifferenceEngine
1338 */
1339 class TableDiffFormatter extends DiffFormatter {
1340 function __construct() {
1341 $this->leading_context_lines = 2;
1342 $this->trailing_context_lines = 2;
1343 }
1344
1345 /**
1346 * @static
1347 * @param $msg
1348 * @return mixed
1349 */
1350 public static function escapeWhiteSpace( $msg ) {
1351 $msg = preg_replace( '/^ /m', '&#160; ', $msg );
1352 $msg = preg_replace( '/ $/m', ' &#160;', $msg );
1353 $msg = preg_replace( '/ /', '&#160; ', $msg );
1354 return $msg;
1355 }
1356
1357 /**
1358 * @param $xbeg
1359 * @param $xlen
1360 * @param $ybeg
1361 * @param $ylen
1362 * @return string
1363 */
1364 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1365 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1366 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1367 return $r;
1368 }
1369
1370 /**
1371 * @param $header
1372 */
1373 function _start_block( $header ) {
1374 echo $header;
1375 }
1376
1377 function _end_block() {
1378 }
1379
1380 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1381 }
1382
1383 /**
1384 * HTML-escape parameter before calling this
1385 * @param $line
1386 * @return string
1387 */
1388 function addedLine( $line ) {
1389 return $this->wrapLine( '+', 'diff-addedline', $line );
1390 }
1391
1392 /**
1393 * HTML-escape parameter before calling this
1394 * @param $line
1395 * @return string
1396 */
1397 function deletedLine( $line ) {
1398 return $this->wrapLine( '−', 'diff-deletedline', $line );
1399 }
1400
1401 /**
1402 * HTML-escape parameter before calling this
1403 * @param $line
1404 * @return string
1405 */
1406 function contextLine( $line ) {
1407 return $this->wrapLine( '&#160;', 'diff-context', $line );
1408 }
1409
1410 /**
1411 * @param $marker
1412 * @param $class
1413 * @param $line
1414 * @return string
1415 */
1416 private function wrapLine( $marker, $class, $line ) {
1417 if ( $line !== '' ) {
1418 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1419 $line = Xml::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1420 }
1421 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1422 }
1423
1424 /**
1425 * @return string
1426 */
1427 function emptyLine() {
1428 return '<td colspan="2">&#160;</td>';
1429 }
1430
1431 /**
1432 * @param $lines array
1433 */
1434 function _added( $lines ) {
1435 foreach ( $lines as $line ) {
1436 echo '<tr>' . $this->emptyLine() .
1437 $this->addedLine( '<ins class="diffchange">' .
1438 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
1439 }
1440 }
1441
1442 /**
1443 * @param $lines
1444 */
1445 function _deleted( $lines ) {
1446 foreach ( $lines as $line ) {
1447 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1448 htmlspecialchars( $line ) . '</del>' ) .
1449 $this->emptyLine() . "</tr>\n";
1450 }
1451 }
1452
1453 /**
1454 * @param $lines
1455 */
1456 function _context( $lines ) {
1457 foreach ( $lines as $line ) {
1458 echo '<tr>' .
1459 $this->contextLine( htmlspecialchars( $line ) ) .
1460 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
1461 }
1462 }
1463
1464 /**
1465 * @param $orig
1466 * @param $closing
1467 */
1468 function _changed( $orig, $closing ) {
1469 wfProfileIn( __METHOD__ );
1470
1471 $diff = new WordLevelDiff( $orig, $closing );
1472 $del = $diff->orig();
1473 $add = $diff->closing();
1474
1475 # Notice that WordLevelDiff returns HTML-escaped output.
1476 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1477
1478 while ( $line = array_shift( $del ) ) {
1479 $aline = array_shift( $add );
1480 echo '<tr>' . $this->deletedLine( $line ) .
1481 $this->addedLine( $aline ) . "</tr>\n";
1482 }
1483 foreach ( $add as $line ) { # If any leftovers
1484 echo '<tr>' . $this->emptyLine() .
1485 $this->addedLine( $line ) . "</tr>\n";
1486 }
1487 wfProfileOut( __METHOD__ );
1488 }
1489 }