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