3 * A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
5 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
6 * You may copy this code freely under the conditions of the GPL.
9 * @ingroup DifferenceEngine
10 * @defgroup DifferenceEngine DifferenceEngine
16 * @ingroup DifferenceEngine
18 class _DiffOp
{ #FIXME: no longer private!
24 trigger_error( 'pure virtual', E_USER_ERROR
);
31 return $this->orig ?
sizeof( $this->orig
) : 0;
38 return $this->closing ?
sizeof( $this->closing
) : 0;
45 * @ingroup DifferenceEngine
47 class _DiffOp_Copy
extends _DiffOp
{ #FIXME: no longer private!
50 function __construct( $orig, $closing = false ) {
51 if ( !is_array( $closing ) ) {
55 $this->closing
= $closing;
59 * @return _DiffOp_Copy
62 return new _DiffOp_Copy( $this->closing
, $this->orig
);
69 * @ingroup DifferenceEngine
71 class _DiffOp_Delete
extends _DiffOp
{ #FIXME: no longer private!
74 function __construct( $lines ) {
76 $this->closing
= false;
83 return new _DiffOp_Add( $this->orig
);
90 * @ingroup DifferenceEngine
92 class _DiffOp_Add
extends _DiffOp
{ #FIXME: no longer private!
95 function __construct( $lines ) {
96 $this->closing
= $lines;
101 * @return _DiffOp_Delete
104 return new _DiffOp_Delete( $this->closing
);
111 * @ingroup DifferenceEngine
113 class _DiffOp_Change
extends _DiffOp
{ #FIXME: no longer private!
114 var $type = 'change';
116 function __construct( $orig, $closing ) {
118 $this->closing
= $closing;
122 * @return _DiffOp_Change
125 return new _DiffOp_Change( $this->closing
, $this->orig
);
130 * Class used internally by Diff to actually compute the diffs.
132 * The algorithm used here is mostly lifted from the perl module
133 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
134 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
136 * More ideas are taken from:
137 * http://www.ics.uci.edu/~eppstein/161/960229.html
139 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
140 * diffutils-2.7, which can be found at:
141 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
143 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
146 * Line length limits for robustness added by Tim Starling, 2005-08-31
147 * Alternative implementation added by Guy Van den Broeck, 2008-07-30
149 * @author Geoffrey T. Dairiki, Tim Starling, Guy Van den Broeck
151 * @ingroup DifferenceEngine
153 class _DiffEngine
{ #FIXME: no longer private!
155 const MAX_XREF_LENGTH
= 10000;
157 protected $xchanged, $ychanged;
159 protected $xv = array(), $yv = array();
160 protected $xind = array(), $yind = array();
162 protected $seq = array(), $in_seq = array();
171 function diff ( $from_lines, $to_lines ) {
172 wfProfileIn( __METHOD__
);
174 // Diff and store locally
175 $this->diff_local( $from_lines, $to_lines );
177 // Merge edits when possible
178 $this->_shift_boundaries( $from_lines, $this->xchanged
, $this->ychanged
);
179 $this->_shift_boundaries( $to_lines, $this->ychanged
, $this->xchanged
);
181 // Compute the edit operations.
182 $n_from = sizeof( $from_lines );
183 $n_to = sizeof( $to_lines );
187 while ( $xi < $n_from ||
$yi < $n_to ) {
188 assert( '$yi < $n_to || $this->xchanged[$xi]' );
189 assert( '$xi < $n_from || $this->ychanged[$yi]' );
191 // Skip matching "snake".
193 while ( $xi < $n_from && $yi < $n_to
194 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi] ) {
195 $copy[] = $from_lines[$xi++
];
199 $edits[] = new _DiffOp_Copy( $copy );
202 // Find deletes & adds.
204 while ( $xi < $n_from && $this->xchanged
[$xi] ) {
205 $delete[] = $from_lines[$xi++
];
209 while ( $yi < $n_to && $this->ychanged
[$yi] ) {
210 $add[] = $to_lines[$yi++
];
213 if ( $delete && $add ) {
214 $edits[] = new _DiffOp_Change( $delete, $add );
215 } elseif ( $delete ) {
216 $edits[] = new _DiffOp_Delete( $delete );
218 $edits[] = new _DiffOp_Add( $add );
221 wfProfileOut( __METHOD__
);
229 function diff_local ( $from_lines, $to_lines ) {
230 global $wgExternalDiffEngine;
231 wfProfileIn( __METHOD__
);
233 if ( $wgExternalDiffEngine == 'wikidiff3' ) {
235 $wikidiff3 = new WikiDiff3();
236 $wikidiff3->diff( $from_lines, $to_lines );
237 $this->xchanged
= $wikidiff3->removed
;
238 $this->ychanged
= $wikidiff3->added
;
242 $n_from = sizeof( $from_lines );
243 $n_to = sizeof( $to_lines );
244 $this->xchanged
= $this->ychanged
= array();
245 $this->xv
= $this->yv
= array();
246 $this->xind
= $this->yind
= array();
248 unset( $this->in_seq
);
251 // Skip leading common lines.
252 for ( $skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
253 if ( $from_lines[$skip] !== $to_lines[$skip] ) {
256 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
258 // Skip trailing common lines.
259 $xi = $n_from; $yi = $n_to;
260 for ( $endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
261 if ( $from_lines[$xi] !== $to_lines[$yi] ) {
264 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
267 // Ignore lines which do not exist in both files.
268 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
269 $xhash[$this->_line_hash( $from_lines[$xi] )] = 1;
272 for ( $yi = $skip; $yi < $n_to - $endskip; $yi++
) {
273 $line = $to_lines[$yi];
274 if ( ( $this->ychanged
[$yi] = empty( $xhash[$this->_line_hash( $line )] ) ) ) {
277 $yhash[$this->_line_hash( $line )] = 1;
281 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
282 $line = $from_lines[$xi];
283 if ( ( $this->xchanged
[$xi] = empty( $yhash[$this->_line_hash( $line )] ) ) ) {
291 $this->_compareseq( 0, sizeof( $this->xv
), 0, sizeof( $this->yv
) );
293 wfProfileOut( __METHOD__
);
297 * Returns the whole line if it's small enough, or the MD5 hash otherwise
298 * @param $line string
301 function _line_hash( $line ) {
302 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
310 * Divide the Largest Common Subsequence (LCS) of the sequences
311 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
314 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
315 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
316 * sub sequences. The first sub-sequence is contained in [X0, X1),
317 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
318 * that (X0, Y0) == (XOFF, YOFF) and
319 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
321 * This function assumes that the first lines of the specified portions
322 * of the two files do not match, and likewise that the last lines do not
323 * match. The caller must trim matching lines from the beginning and end
324 * of the portions it is going to specify.
332 function _diag( $xoff, $xlim, $yoff, $ylim, $nchunks ) {
335 if ( $xlim - $xoff > $ylim - $yoff ) {
336 // Things seems faster (I'm not sure I understand why)
337 // when the shortest sequence in X.
339 list( $xoff, $xlim, $yoff, $ylim ) = array( $yoff, $ylim, $xoff, $xlim );
343 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
344 $ymatches[$this->xv
[$i]][] = $i;
347 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
348 $ymatches[$this->yv
[$i]][] = $i;
353 $this->seq
[0] = $yoff - 1;
354 $this->in_seq
= array();
357 $numer = $xlim - $xoff +
$nchunks - 1;
359 for ( $chunk = 0; $chunk < $nchunks; $chunk++
) {
361 for ( $i = 0; $i <= $this->lcs
; $i++
) {
362 $ymids[$i][$chunk -1] = $this->seq
[$i];
366 $x1 = $xoff +
(int)( ( $numer +
( $xlim -$xoff ) * $chunk ) / $nchunks );
367 for ( ; $x < $x1; $x++
) {
368 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
369 if ( empty( $ymatches[$line] ) ) {
372 $matches = $ymatches[$line];
374 while ( list( , $y ) = each( $matches ) ) {
375 if ( empty( $this->in_seq
[$y] ) ) {
376 $k = $this->_lcs_pos( $y );
378 $ymids[$k] = $ymids[$k -1];
382 while ( list ( , $y ) = each( $matches ) ) {
383 if ( $y > $this->seq
[$k -1] ) {
384 assert( '$y < $this->seq[$k]' );
385 // Optimization: this is a common case:
386 // next match is just replacing previous match.
387 $this->in_seq
[$this->seq
[$k]] = false;
389 $this->in_seq
[$y] = 1;
390 } elseif ( empty( $this->in_seq
[$y] ) ) {
391 $k = $this->_lcs_pos( $y );
393 $ymids[$k] = $ymids[$k -1];
399 $seps[] = $flip ?
array( $yoff, $xoff ) : array( $xoff, $yoff );
400 $ymid = $ymids[$this->lcs
];
401 for ( $n = 0; $n < $nchunks - 1; $n++
) {
402 $x1 = $xoff +
(int)( ( $numer +
( $xlim - $xoff ) * $n ) / $nchunks );
404 $seps[] = $flip ?
array( $y1, $x1 ) : array( $x1, $y1 );
406 $seps[] = $flip ?
array( $ylim, $xlim ) : array( $xlim, $ylim );
408 return array( $this->lcs
, $seps );
415 function _lcs_pos( $ypos ) {
417 if ( $end == 0 ||
$ypos > $this->seq
[$end] ) {
418 $this->seq
[++
$this->lcs
] = $ypos;
419 $this->in_seq
[$ypos] = 1;
424 while ( $beg < $end ) {
425 $mid = (int)( ( $beg +
$end ) / 2 );
426 if ( $ypos > $this->seq
[$mid] ) {
433 assert( '$ypos != $this->seq[$end]' );
435 $this->in_seq
[$this->seq
[$end]] = false;
436 $this->seq
[$end] = $ypos;
437 $this->in_seq
[$ypos] = 1;
442 * Find LCS of two sequences.
444 * The results are recorded in the vectors $this->{x,y}changed[], by
445 * storing a 1 in the element for each line that is an insertion
446 * or deletion (ie. is not in the LCS).
448 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
450 * Note that XLIM, YLIM are exclusive bounds.
451 * All line numbers are origin-0 and discarded lines are not counted.
457 function _compareseq ( $xoff, $xlim, $yoff, $ylim ) {
458 // Slide down the bottom initial diagonal.
459 while ( $xoff < $xlim && $yoff < $ylim
460 && $this->xv
[$xoff] == $this->yv
[$yoff] ) {
465 // Slide up the top initial diagonal.
466 while ( $xlim > $xoff && $ylim > $yoff
467 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1] ) {
472 if ( $xoff == $xlim ||
$yoff == $ylim ) {
475 // This is ad hoc but seems to work well.
476 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
477 // $nchunks = max(2,min(8,(int)$nchunks));
478 $nchunks = min( 7, $xlim - $xoff, $ylim - $yoff ) +
1;
480 = $this->_diag( $xoff, $xlim, $yoff, $ylim, $nchunks );
484 // X and Y sequences have no common subsequence:
486 while ( $yoff < $ylim ) {
487 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
489 while ( $xoff < $xlim ) {
490 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
493 // Use the partitions to split this problem into subproblems.
496 while ( $pt2 = next( $seps ) ) {
497 $this->_compareseq ( $pt1[0], $pt2[0], $pt1[1], $pt2[1] );
504 * Adjust inserts/deletes of identical lines to join changes
505 * as much as possible.
507 * We do something when a run of changed lines include a
508 * line at one end and has an excluded, identical line at the other.
509 * We are free to choose which identical line is included.
510 * `compareseq' usually chooses the one at the beginning,
511 * but usually it is cleaner to consider the following identical line
512 * to be the "change".
514 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
516 function _shift_boundaries( $lines, &$changed, $other_changed ) {
517 wfProfileIn( __METHOD__
);
521 assert( 'sizeof($lines) == sizeof($changed)' );
522 $len = sizeof( $lines );
523 $other_len = sizeof( $other_changed );
527 * Scan forwards to find beginning of another run of changes.
528 * Also keep track of the corresponding point in the other file.
530 * Throughout this code, $i and $j are adjusted together so that
531 * the first $i elements of $changed and the first $j elements
532 * of $other_changed both contain the same number of zeros
534 * Furthermore, $j is always kept so that $j == $other_len or
535 * $other_changed[$j] == false.
537 while ( $j < $other_len && $other_changed[$j] ) {
541 while ( $i < $len && ! $changed[$i] ) {
542 assert( '$j < $other_len && ! $other_changed[$j]' );
544 while ( $j < $other_len && $other_changed[$j] )
554 // Find the end of this run of changes.
555 while ( ++
$i < $len && $changed[$i] ) {
561 * Record the length of this run of changes, so that
562 * we can later determine whether the run has grown.
564 $runlength = $i - $start;
567 * Move the changed region back, so long as the
568 * previous unchanged line matches the last changed one.
569 * This merges with previous changed regions.
571 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
572 $changed[--$start] = 1;
573 $changed[--$i] = false;
574 while ( $start > 0 && $changed[$start - 1] ) {
578 while ( $other_changed[--$j] ) {
581 assert( '$j >= 0 && !$other_changed[$j]' );
585 * Set CORRESPONDING to the end of the changed run, at the last
586 * point where it corresponds to a changed run in the other file.
587 * CORRESPONDING == LEN means no such point has been found.
589 $corresponding = $j < $other_len ?
$i : $len;
592 * Move the changed region forward, so long as the
593 * first changed line matches the following unchanged one.
594 * This merges with following changed regions.
595 * Do this second, so that if there are no merges,
596 * the changed region is moved forward as far as possible.
598 while ( $i < $len && $lines[$start] == $lines[$i] ) {
599 $changed[$start++
] = false;
601 while ( $i < $len && $changed[$i] ) {
605 assert( '$j < $other_len && ! $other_changed[$j]' );
607 if ( $j < $other_len && $other_changed[$j] ) {
609 while ( $j < $other_len && $other_changed[$j] ) {
614 } while ( $runlength != $i - $start );
617 * If possible, move the fully-merged run of changes
618 * back to a corresponding run in the other file.
620 while ( $corresponding < $i ) {
621 $changed[--$start] = 1;
624 while ( $other_changed[--$j] ) {
627 assert( '$j >= 0 && !$other_changed[$j]' );
630 wfProfileOut( __METHOD__
);
635 * Class representing a 'diff' between two sequences of strings.
638 * @ingroup DifferenceEngine
640 class Diff
extends DiffResult
{
645 * Computes diff between sequences of strings.
647 * @param $from_lines array An array of strings.
648 * (Typically these are lines from a file.)
649 * @param $to_lines array An array of strings.
650 * @param $eng _DiffEngine|null The diff engine to use.
652 function __construct( $from_lines, $to_lines, $eng = null ) {
654 $eng = new _DiffEngine();
657 $edits = $eng->diff( $from_lines, $to_lines );
659 parent
::__construct( $edits );
661 //$this->_check( $from_lines, $to_lines );
666 * Class representing the result of 'diffin' two sequences of strings.
669 * @ingroup DifferenceEngine
676 * @param $edits array An array of Edit.
678 function __construct( $edits ) {
679 $this->edits
= $edits;
683 * Compute reversed Diff.
687 * $diff = new Diff($lines1, $lines2);
688 * $rev = $diff->reverse();
689 * @return Object A Diff object representing the inverse of the
694 $rev->edits
= array();
695 foreach ( $this->edits
as $edit ) {
696 $rev->edits
[] = $edit->reverse();
702 * Check for empty diff.
704 * @return bool True iff two sequences were identical.
707 foreach ( $this->edits
as $edit ) {
708 if ( $edit->type
!= 'copy' ) {
716 * Compute the length of the Longest Common Subsequence (LCS).
718 * This is mostly for diagnostic purposed.
720 * @return int The length of the LCS.
724 foreach ( $this->edits
as $edit ) {
725 if ( $edit->type
== 'copy' ) {
726 $lcs +
= sizeof( $edit->orig
);
733 * Get the original set of lines.
735 * This reconstructs the $from_lines parameter passed to the
738 * @return array The original sequence of strings.
743 foreach ( $this->edits
as $edit ) {
745 array_splice( $lines, sizeof( $lines ), 0, $edit->orig
);
752 * Get the closing set of lines.
754 * @return array The sequence of strings.
759 foreach ( $this->edits
as $edit ) {
760 if ( $edit->closing
) {
761 array_splice( $lines, sizeof( $lines ), 0, $edit->closing
);
768 * Check a Diff for validity.
770 * This is here only for debugging purposes.
774 function _check( $from_lines, $to_lines ) {
775 wfProfileIn( __METHOD__
);
776 if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
777 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR
);
779 if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
780 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR
);
783 $rev = $this->reverse();
784 if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
785 trigger_error( "Reversed original doesn't match", E_USER_ERROR
);
787 if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
788 trigger_error( "Reversed closing doesn't match", E_USER_ERROR
);
793 foreach ( $this->edits
as $edit ) {
794 if ( $prevtype == $edit->type
) {
795 trigger_error( 'Edit sequence is non-optimal', E_USER_ERROR
);
797 $prevtype = $edit->type
;
801 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE
);
802 wfProfileOut( __METHOD__
);
807 * @todo document, bad name.
809 * @ingroup DifferenceEngine
811 class MappedDiff
extends Diff
{
815 * Computes diff between sequences of strings.
817 * This can be used to compute things like
818 * case-insensitve diffs, or diffs which ignore
819 * changes in white-space.
821 * @param $from_lines array An array of strings.
822 * (Typically these are lines from a file.)
824 * @param $to_lines array An array of strings.
826 * @param $mapped_from_lines array This array should
827 * have the same size number of elements as $from_lines.
828 * The elements in $mapped_from_lines and
829 * $mapped_to_lines are what is actually compared
830 * when computing the diff.
832 * @param $mapped_to_lines array This array should
833 * have the same number of elements as $to_lines.
835 function __construct( $from_lines, $to_lines,
836 $mapped_from_lines, $mapped_to_lines ) {
837 wfProfileIn( __METHOD__
);
839 assert( 'sizeof( $from_lines ) == sizeof( $mapped_from_lines )' );
840 assert( 'sizeof( $to_lines ) == sizeof( $mapped_to_lines )' );
842 parent
::__construct( $mapped_from_lines, $mapped_to_lines );
845 for ( $i = 0; $i < sizeof( $this->edits
); $i++
) {
846 $orig = &$this->edits
[$i]->orig
;
847 if ( is_array( $orig ) ) {
848 $orig = array_slice( $from_lines, $xi, sizeof( $orig ) );
849 $xi +
= sizeof( $orig );
852 $closing = &$this->edits
[$i]->closing
;
853 if ( is_array( $closing ) ) {
854 $closing = array_slice( $to_lines, $yi, sizeof( $closing ) );
855 $yi +
= sizeof( $closing );
858 wfProfileOut( __METHOD__
);
863 * A class to format Diffs
865 * This class formats the diff in classic diff format.
866 * It is intended that this class be customized via inheritance,
867 * to obtain fancier outputs.
870 * @ingroup DifferenceEngine
872 class DiffFormatter
{
874 * Number of leading context "lines" to preserve.
876 * This should be left at zero for this class, but subclasses
877 * may want to set this to other values.
879 var $leading_context_lines = 0;
882 * Number of trailing context "lines" to preserve.
884 * This should be left at zero for this class, but subclasses
885 * may want to set this to other values.
887 var $trailing_context_lines = 0;
892 * @param $diff Diff A Diff object.
893 * @return string The formatted output.
895 function format( $diff ) {
896 wfProfileIn( __METHOD__
);
902 $nlead = $this->leading_context_lines
;
903 $ntrail = $this->trailing_context_lines
;
905 $this->_start_diff();
907 foreach ( $diff->edits
as $edit ) {
908 if ( $edit->type
== 'copy' ) {
909 if ( is_array( $block ) ) {
910 if ( sizeof( $edit->orig
) <= $nlead +
$ntrail ) {
914 $context = array_slice( $edit->orig
, 0, $ntrail );
915 $block[] = new _DiffOp_Copy( $context );
917 $this->_block( $x0, $ntrail +
$xi - $x0,
918 $y0, $ntrail +
$yi - $y0,
923 $context = $edit->orig
;
925 if ( !is_array( $block ) ) {
926 $context = array_slice( $context, sizeof( $context ) - $nlead );
927 $x0 = $xi - sizeof( $context );
928 $y0 = $yi - sizeof( $context );
931 $block[] = new _DiffOp_Copy( $context );
938 $xi +
= sizeof( $edit->orig
);
940 if ( $edit->closing
) {
941 $yi +
= sizeof( $edit->closing
);
945 if ( is_array( $block ) ) {
946 $this->_block( $x0, $xi - $x0,
951 $end = $this->_end_diff();
952 wfProfileOut( __METHOD__
);
963 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
964 wfProfileIn( __METHOD__
);
965 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
966 foreach ( $edits as $edit ) {
967 if ( $edit->type
== 'copy' ) {
968 $this->_context( $edit->orig
);
969 } elseif ( $edit->type
== 'add' ) {
970 $this->_added( $edit->closing
);
971 } elseif ( $edit->type
== 'delete' ) {
972 $this->_deleted( $edit->orig
);
973 } elseif ( $edit->type
== 'change' ) {
974 $this->_changed( $edit->orig
, $edit->closing
);
976 trigger_error( 'Unknown edit type', E_USER_ERROR
);
980 wfProfileOut( __METHOD__
);
983 function _start_diff() {
990 function _end_diff() {
991 $val = ob_get_contents();
1003 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1005 $xbeg .= ',' . ( $xbeg +
$xlen - 1 );
1008 $ybeg .= ',' . ( $ybeg +
$ylen - 1 );
1011 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
1014 function _start_block( $header ) {
1015 echo $header . "\n";
1018 function _end_block() {
1023 * @param $prefix string
1025 function _lines( $lines, $prefix = ' ' ) {
1026 foreach ( $lines as $line ) {
1027 echo "$prefix $line\n";
1034 function _context( $lines ) {
1035 $this->_lines( $lines );
1041 function _added( $lines ) {
1042 $this->_lines( $lines, '>' );
1048 function _deleted( $lines ) {
1049 $this->_lines( $lines, '<' );
1056 function _changed( $orig, $closing ) {
1057 $this->_deleted( $orig );
1059 $this->_added( $closing );
1064 * A formatter that outputs unified diffs
1065 * @ingroup DifferenceEngine
1067 class UnifiedDiffFormatter
extends DiffFormatter
{
1068 var $leading_context_lines = 2;
1069 var $trailing_context_lines = 2;
1074 function _added( $lines ) {
1075 $this->_lines( $lines, '+' );
1081 function _deleted( $lines ) {
1082 $this->_lines( $lines, '-' );
1089 function _changed( $orig, $closing ) {
1090 $this->_deleted( $orig );
1091 $this->_added( $closing );
1101 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1102 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
1107 * A pseudo-formatter that just passes along the Diff::$edits array
1108 * @ingroup DifferenceEngine
1110 class ArrayDiffFormatter
extends DiffFormatter
{
1116 function format( $diff ) {
1120 foreach ( $diff->edits
as $edit ) {
1121 switch( $edit->type
) {
1123 foreach ( $edit->closing
as $l ) {
1127 'newline' => $newline++
1132 foreach ( $edit->orig
as $l ) {
1134 'action' => 'delete',
1136 'oldline' => $oldline++
,
1141 foreach ( $edit->orig
as $i => $l ) {
1143 'action' => 'change',
1145 'new' => isset( $edit->closing
[$i] ) ?
$edit->closing
[$i] : null,
1146 'oldline' => $oldline++
,
1147 'newline' => $newline++
,
1152 $oldline +
= count( $edit->orig
);
1153 $newline +
= count( $edit->orig
);
1161 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1164 define( 'NBSP', ' ' ); // iso-8859-x non-breaking space.
1169 * @ingroup DifferenceEngine
1171 class _HWLDF_WordAccumulator
{
1172 function __construct() {
1173 $this->_lines
= array();
1182 function _flushGroup( $new_tag ) {
1183 if ( $this->_group
!== '' ) {
1184 if ( $this->_tag
== 'ins' ) {
1185 $this->_line
.= '<ins class="diffchange diffchange-inline">' .
1186 htmlspecialchars( $this->_group
) . '</ins>';
1187 } elseif ( $this->_tag
== 'del' ) {
1188 $this->_line
.= '<del class="diffchange diffchange-inline">' .
1189 htmlspecialchars( $this->_group
) . '</del>';
1191 $this->_line
.= htmlspecialchars( $this->_group
);
1195 $this->_tag
= $new_tag;
1201 function _flushLine( $new_tag ) {
1202 $this->_flushGroup( $new_tag );
1203 if ( $this->_line
!= '' ) {
1204 array_push( $this->_lines
, $this->_line
);
1206 # make empty lines visible by inserting an NBSP
1207 array_push( $this->_lines
, NBSP
);
1214 * @param $tag string
1216 function addWords ( $words, $tag = '' ) {
1217 if ( $tag != $this->_tag
) {
1218 $this->_flushGroup( $tag );
1221 foreach ( $words as $word ) {
1222 // new-line should only come as first char of word.
1223 if ( $word == '' ) {
1226 if ( $word[0] == "\n" ) {
1227 $this->_flushLine( $tag );
1228 $word = substr( $word, 1 );
1230 assert( '!strstr( $word, "\n" )' );
1231 $this->_group
.= $word;
1238 function getLines() {
1239 $this->_flushLine( '~done' );
1240 return $this->_lines
;
1247 * @ingroup DifferenceEngine
1249 class WordLevelDiff
extends MappedDiff
{
1250 const MAX_LINE_LENGTH
= 10000;
1253 * @param $orig_lines
1254 * @param $closing_lines
1256 function __construct ( $orig_lines, $closing_lines ) {
1257 wfProfileIn( __METHOD__
);
1259 list( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1260 list( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1262 parent
::__construct( $orig_words, $closing_words,
1263 $orig_stripped, $closing_stripped );
1264 wfProfileOut( __METHOD__
);
1271 function _split( $lines ) {
1272 wfProfileIn( __METHOD__
);
1275 $stripped = array();
1277 foreach ( $lines as $line ) {
1278 # If the line is too long, just pretend the entire line is one big word
1279 # This prevents resource exhaustion problems
1286 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1288 $stripped[] = $line;
1291 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1294 $words = array_merge( $words, $m[0] );
1295 $stripped = array_merge( $stripped, $m[1] );
1299 wfProfileOut( __METHOD__
);
1300 return array( $words, $stripped );
1307 wfProfileIn( __METHOD__
);
1308 $orig = new _HWLDF_WordAccumulator
;
1310 foreach ( $this->edits
as $edit ) {
1311 if ( $edit->type
== 'copy' ) {
1312 $orig->addWords( $edit->orig
);
1313 } elseif ( $edit->orig
) {
1314 $orig->addWords( $edit->orig
, 'del' );
1317 $lines = $orig->getLines();
1318 wfProfileOut( __METHOD__
);
1325 function closing() {
1326 wfProfileIn( __METHOD__
);
1327 $closing = new _HWLDF_WordAccumulator
;
1329 foreach ( $this->edits
as $edit ) {
1330 if ( $edit->type
== 'copy' ) {
1331 $closing->addWords( $edit->closing
);
1332 } elseif ( $edit->closing
) {
1333 $closing->addWords( $edit->closing
, 'ins' );
1336 $lines = $closing->getLines();
1337 wfProfileOut( __METHOD__
);
1343 * Wikipedia Table style diff formatter.
1346 * @ingroup DifferenceEngine
1348 class TableDiffFormatter
extends DiffFormatter
{
1349 function __construct() {
1350 $this->leading_context_lines
= 2;
1351 $this->trailing_context_lines
= 2;
1359 public static function escapeWhiteSpace( $msg ) {
1360 $msg = preg_replace( '/^ /m', '  ', $msg );
1361 $msg = preg_replace( '/ $/m', '  ', $msg );
1362 $msg = preg_replace( '/ /', '  ', $msg );
1373 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1374 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1375 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1382 function _start_block( $header ) {
1386 function _end_block() {
1389 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1393 * HTML-escape parameter before calling this
1397 function addedLine( $line ) {
1398 return $this->wrapLine( '+', 'diff-addedline', $line );
1402 * HTML-escape parameter before calling this
1406 function deletedLine( $line ) {
1407 return $this->wrapLine( '−', 'diff-deletedline', $line );
1411 * HTML-escape parameter before calling this
1415 function contextLine( $line ) {
1416 return $this->wrapLine( ' ', 'diff-context', $line );
1425 private function wrapLine( $marker, $class, $line ) {
1426 if ( $line !== '' ) {
1427 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1428 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1430 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1436 function emptyLine() {
1437 return '<td colspan="2"> </td>';
1441 * @param $lines array
1443 function _added( $lines ) {
1444 foreach ( $lines as $line ) {
1445 echo '<tr>' . $this->emptyLine() .
1446 $this->addedLine( '<ins class="diffchange">' .
1447 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
1454 function _deleted( $lines ) {
1455 foreach ( $lines as $line ) {
1456 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1457 htmlspecialchars( $line ) . '</del>' ) .
1458 $this->emptyLine() . "</tr>\n";
1465 function _context( $lines ) {
1466 foreach ( $lines as $line ) {
1468 $this->contextLine( htmlspecialchars( $line ) ) .
1469 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
1477 function _changed( $orig, $closing ) {
1478 wfProfileIn( __METHOD__
);
1480 $diff = new WordLevelDiff( $orig, $closing );
1481 $del = $diff->orig();
1482 $add = $diff->closing();
1484 # Notice that WordLevelDiff returns HTML-escaped output.
1485 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1487 while ( $line = array_shift( $del ) ) {
1488 $aline = array_shift( $add );
1489 echo '<tr>' . $this->deletedLine( $line ) .
1490 $this->addedLine( $aline ) . "</tr>\n";
1492 foreach ( $add as $line ) { # If any leftovers
1493 echo '<tr>' . $this->emptyLine() .
1494 $this->addedLine( $line ) . "</tr>\n";
1496 wfProfileOut( __METHOD__
);