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
24 trigger_error( 'pure virtual', E_USER_ERROR
);
28 return $this->orig ?
sizeof( $this->orig
) : 0;
32 return $this->closing ?
sizeof( $this->closing
) : 0;
39 * @ingroup DifferenceEngine
41 class _DiffOp_Copy
extends _DiffOp
{
44 function __construct ( $orig, $closing = false ) {
45 if ( !is_array( $closing ) )
48 $this->closing
= $closing;
52 return new _DiffOp_Copy( $this->closing
, $this->orig
);
59 * @ingroup DifferenceEngine
61 class _DiffOp_Delete
extends _DiffOp
{
64 function __construct ( $lines ) {
66 $this->closing
= false;
70 return new _DiffOp_Add( $this->orig
);
77 * @ingroup DifferenceEngine
79 class _DiffOp_Add
extends _DiffOp
{
82 function __construct ( $lines ) {
83 $this->closing
= $lines;
88 return new _DiffOp_Delete( $this->closing
);
95 * @ingroup DifferenceEngine
97 class _DiffOp_Change
extends _DiffOp
{
100 function __construct ( $orig, $closing ) {
102 $this->closing
= $closing;
106 return new _DiffOp_Change( $this->closing
, $this->orig
);
111 * Class used internally by Diff to actually compute the diffs.
113 * The algorithm used here is mostly lifted from the perl module
114 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
115 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
117 * More ideas are taken from:
118 * http://www.ics.uci.edu/~eppstein/161/960229.html
120 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
121 * diffutils-2.7, which can be found at:
122 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
124 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
127 * Line length limits for robustness added by Tim Starling, 2005-08-31
128 * Alternative implementation added by Guy Van den Broeck, 2008-07-30
130 * @author Geoffrey T. Dairiki, Tim Starling, Guy Van den Broeck
132 * @ingroup DifferenceEngine
136 const MAX_XREF_LENGTH
= 10000;
138 function diff ( $from_lines, $to_lines ) {
139 wfProfileIn( __METHOD__
);
141 // Diff and store locally
142 $this->diff_local( $from_lines, $to_lines );
144 // Merge edits when possible
145 $this->_shift_boundaries( $from_lines, $this->xchanged
, $this->ychanged
);
146 $this->_shift_boundaries( $to_lines, $this->ychanged
, $this->xchanged
);
148 // Compute the edit operations.
149 $n_from = sizeof( $from_lines );
150 $n_to = sizeof( $to_lines );
154 while ( $xi < $n_from ||
$yi < $n_to ) {
155 assert( $yi < $n_to ||
$this->xchanged
[$xi] );
156 assert( $xi < $n_from ||
$this->ychanged
[$yi] );
158 // Skip matching "snake".
160 while ( $xi < $n_from && $yi < $n_to
161 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi] ) {
162 $copy[] = $from_lines[$xi++
];
166 $edits[] = new _DiffOp_Copy( $copy );
168 // Find deletes & adds.
170 while ( $xi < $n_from && $this->xchanged
[$xi] )
171 $delete[] = $from_lines[$xi++
];
174 while ( $yi < $n_to && $this->ychanged
[$yi] )
175 $add[] = $to_lines[$yi++
];
177 if ( $delete && $add )
178 $edits[] = new _DiffOp_Change( $delete, $add );
180 $edits[] = new _DiffOp_Delete( $delete );
182 $edits[] = new _DiffOp_Add( $add );
184 wfProfileOut( __METHOD__
);
188 function diff_local ( $from_lines, $to_lines ) {
189 global $wgExternalDiffEngine;
190 wfProfileIn( __METHOD__
);
192 if ( $wgExternalDiffEngine == 'wikidiff3' ) {
194 $wikidiff3 = new WikiDiff3();
195 $wikidiff3->diff( $from_lines, $to_lines );
196 $this->xchanged
= $wikidiff3->removed
;
197 $this->ychanged
= $wikidiff3->added
;
201 $n_from = sizeof( $from_lines );
202 $n_to = sizeof( $to_lines );
203 $this->xchanged
= $this->ychanged
= array();
204 $this->xv
= $this->yv
= array();
205 $this->xind
= $this->yind
= array();
207 unset( $this->in_seq
);
210 // Skip leading common lines.
211 for ( $skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
212 if ( $from_lines[$skip] !== $to_lines[$skip] )
214 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
216 // Skip trailing common lines.
217 $xi = $n_from; $yi = $n_to;
218 for ( $endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
219 if ( $from_lines[$xi] !== $to_lines[$yi] )
221 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
224 // Ignore lines which do not exist in both files.
225 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
226 $xhash[$this->_line_hash( $from_lines[$xi] )] = 1;
229 for ( $yi = $skip; $yi < $n_to - $endskip; $yi++
) {
230 $line = $to_lines[$yi];
231 if ( ( $this->ychanged
[$yi] = empty( $xhash[$this->_line_hash( $line )] ) ) )
233 $yhash[$this->_line_hash( $line )] = 1;
237 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
238 $line = $from_lines[$xi];
239 if ( ( $this->xchanged
[$xi] = empty( $yhash[$this->_line_hash( $line )] ) ) )
246 $this->_compareseq( 0, sizeof( $this->xv
), 0, sizeof( $this->yv
) );
248 wfProfileOut( __METHOD__
);
252 * Returns the whole line if it's small enough, or the MD5 hash otherwise
254 function _line_hash( $line ) {
255 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
262 /* Divide the Largest Common Subsequence (LCS) of the sequences
263 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
266 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
267 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
268 * sub sequences. The first sub-sequence is contained in [X0, X1),
269 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
270 * that (X0, Y0) == (XOFF, YOFF) and
271 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
273 * This function assumes that the first lines of the specified portions
274 * of the two files do not match, and likewise that the last lines do not
275 * match. The caller must trim matching lines from the beginning and end
276 * of the portions it is going to specify.
278 function _diag ( $xoff, $xlim, $yoff, $ylim, $nchunks ) {
281 if ( $xlim - $xoff > $ylim - $yoff ) {
282 // Things seems faster (I'm not sure I understand why)
283 // when the shortest sequence in X.
285 list ( $xoff, $xlim, $yoff, $ylim )
286 = array( $yoff, $ylim, $xoff, $xlim );
290 for ( $i = $ylim - 1; $i >= $yoff; $i-- )
291 $ymatches[$this->xv
[$i]][] = $i;
293 for ( $i = $ylim - 1; $i >= $yoff; $i-- )
294 $ymatches[$this->yv
[$i]][] = $i;
297 $this->seq
[0] = $yoff - 1;
298 $this->in_seq
= array();
301 $numer = $xlim - $xoff +
$nchunks - 1;
303 for ( $chunk = 0; $chunk < $nchunks; $chunk++
) {
305 for ( $i = 0; $i <= $this->lcs
; $i++
)
306 $ymids[$i][$chunk -1] = $this->seq
[$i];
308 $x1 = $xoff +
(int)( ( $numer +
( $xlim -$xoff ) * $chunk ) / $nchunks );
309 for ( ; $x < $x1; $x++
) {
310 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
311 if ( empty( $ymatches[$line] ) ) {
314 $matches = $ymatches[$line];
316 while ( list( , $y ) = each( $matches ) ) {
317 if ( empty( $this->in_seq
[$y] ) ) {
318 $k = $this->_lcs_pos( $y );
320 $ymids[$k] = $ymids[$k -1];
324 while ( list ( , $y ) = each( $matches ) ) {
325 if ( $y > $this->seq
[$k -1] ) {
326 assert( $y < $this->seq
[$k] );
327 // Optimization: this is a common case:
328 // next match is just replacing previous match.
329 $this->in_seq
[$this->seq
[$k]] = false;
331 $this->in_seq
[$y] = 1;
332 } else if ( empty( $this->in_seq
[$y] ) ) {
333 $k = $this->_lcs_pos( $y );
335 $ymids[$k] = $ymids[$k -1];
341 $seps[] = $flip ?
array( $yoff, $xoff ) : array( $xoff, $yoff );
342 $ymid = $ymids[$this->lcs
];
343 for ( $n = 0; $n < $nchunks - 1; $n++
) {
344 $x1 = $xoff +
(int)( ( $numer +
( $xlim - $xoff ) * $n ) / $nchunks );
346 $seps[] = $flip ?
array( $y1, $x1 ) : array( $x1, $y1 );
348 $seps[] = $flip ?
array( $ylim, $xlim ) : array( $xlim, $ylim );
350 return array( $this->lcs
, $seps );
353 function _lcs_pos ( $ypos ) {
355 if ( $end == 0 ||
$ypos > $this->seq
[$end] ) {
356 $this->seq
[++
$this->lcs
] = $ypos;
357 $this->in_seq
[$ypos] = 1;
362 while ( $beg < $end ) {
363 $mid = (int)( ( $beg +
$end ) / 2 );
364 if ( $ypos > $this->seq
[$mid] )
370 assert( $ypos != $this->seq
[$end] );
372 $this->in_seq
[$this->seq
[$end]] = false;
373 $this->seq
[$end] = $ypos;
374 $this->in_seq
[$ypos] = 1;
378 /* Find LCS of two sequences.
380 * The results are recorded in the vectors $this->{x,y}changed[], by
381 * storing a 1 in the element for each line that is an insertion
382 * or deletion (ie. is not in the LCS).
384 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
386 * Note that XLIM, YLIM are exclusive bounds.
387 * All line numbers are origin-0 and discarded lines are not counted.
389 function _compareseq ( $xoff, $xlim, $yoff, $ylim ) {
390 // Slide down the bottom initial diagonal.
391 while ( $xoff < $xlim && $yoff < $ylim
392 && $this->xv
[$xoff] == $this->yv
[$yoff] ) {
397 // Slide up the top initial diagonal.
398 while ( $xlim > $xoff && $ylim > $yoff
399 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1] ) {
404 if ( $xoff == $xlim ||
$yoff == $ylim )
407 // This is ad hoc but seems to work well.
408 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
409 // $nchunks = max(2,min(8,(int)$nchunks));
410 $nchunks = min( 7, $xlim - $xoff, $ylim - $yoff ) +
1;
412 = $this->_diag( $xoff, $xlim, $yoff, $ylim, $nchunks );
416 // X and Y sequences have no common subsequence:
418 while ( $yoff < $ylim )
419 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
420 while ( $xoff < $xlim )
421 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
423 // Use the partitions to split this problem into subproblems.
426 while ( $pt2 = next( $seps ) ) {
427 $this->_compareseq ( $pt1[0], $pt2[0], $pt1[1], $pt2[1] );
433 /* Adjust inserts/deletes of identical lines to join changes
434 * as much as possible.
436 * We do something when a run of changed lines include a
437 * line at one end and has an excluded, identical line at the other.
438 * We are free to choose which identical line is included.
439 * `compareseq' usually chooses the one at the beginning,
440 * but usually it is cleaner to consider the following identical line
441 * to be the "change".
443 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
445 function _shift_boundaries ( $lines, &$changed, $other_changed ) {
446 wfProfileIn( __METHOD__
);
450 assert( 'sizeof($lines) == sizeof($changed)' );
451 $len = sizeof( $lines );
452 $other_len = sizeof( $other_changed );
456 * Scan forwards to find beginning of another run of changes.
457 * Also keep track of the corresponding point in the other file.
459 * Throughout this code, $i and $j are adjusted together so that
460 * the first $i elements of $changed and the first $j elements
461 * of $other_changed both contain the same number of zeros
463 * Furthermore, $j is always kept so that $j == $other_len or
464 * $other_changed[$j] == false.
466 while ( $j < $other_len && $other_changed[$j] )
469 while ( $i < $len && ! $changed[$i] ) {
470 assert( '$j < $other_len && ! $other_changed[$j]' );
472 while ( $j < $other_len && $other_changed[$j] )
481 // Find the end of this run of changes.
482 while ( ++
$i < $len && $changed[$i] )
487 * Record the length of this run of changes, so that
488 * we can later determine whether the run has grown.
490 $runlength = $i - $start;
493 * Move the changed region back, so long as the
494 * previous unchanged line matches the last changed one.
495 * This merges with previous changed regions.
497 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
498 $changed[--$start] = 1;
499 $changed[--$i] = false;
500 while ( $start > 0 && $changed[$start - 1] )
503 while ( $other_changed[--$j] )
505 assert( '$j >= 0 && !$other_changed[$j]' );
509 * Set CORRESPONDING to the end of the changed run, at the last
510 * point where it corresponds to a changed run in the other file.
511 * CORRESPONDING == LEN means no such point has been found.
513 $corresponding = $j < $other_len ?
$i : $len;
516 * Move the changed region forward, so long as the
517 * first changed line matches the following unchanged one.
518 * This merges with following changed regions.
519 * Do this second, so that if there are no merges,
520 * the changed region is moved forward as far as possible.
522 while ( $i < $len && $lines[$start] == $lines[$i] ) {
523 $changed[$start++
] = false;
525 while ( $i < $len && $changed[$i] )
528 assert( '$j < $other_len && ! $other_changed[$j]' );
530 if ( $j < $other_len && $other_changed[$j] ) {
532 while ( $j < $other_len && $other_changed[$j] )
536 } while ( $runlength != $i - $start );
539 * If possible, move the fully-merged run of changes
540 * back to a corresponding run in the other file.
542 while ( $corresponding < $i ) {
543 $changed[--$start] = 1;
546 while ( $other_changed[--$j] )
548 assert( '$j >= 0 && !$other_changed[$j]' );
551 wfProfileOut( __METHOD__
);
556 * Class representing a 'diff' between two sequences of strings.
559 * @ingroup DifferenceEngine
567 * Computes diff between sequences of strings.
569 * @param $from_lines array An array of strings.
570 * (Typically these are lines from a file.)
571 * @param $to_lines array An array of strings.
573 function __construct( $from_lines, $to_lines ) {
574 $eng = new _DiffEngine
;
575 $this->edits
= $eng->diff( $from_lines, $to_lines );
576 // $this->_check($from_lines, $to_lines);
580 * Compute reversed Diff.
584 * $diff = new Diff($lines1, $lines2);
585 * $rev = $diff->reverse();
586 * @return object A Diff object representing the inverse of the
589 function reverse () {
591 $rev->edits
= array();
592 foreach ( $this->edits
as $edit ) {
593 $rev->edits
[] = $edit->reverse();
599 * Check for empty diff.
601 * @return bool True iff two sequences were identical.
603 function isEmpty () {
604 foreach ( $this->edits
as $edit ) {
605 if ( $edit->type
!= 'copy' )
612 * Compute the length of the Longest Common Subsequence (LCS).
614 * This is mostly for diagnostic purposed.
616 * @return int The length of the LCS.
620 foreach ( $this->edits
as $edit ) {
621 if ( $edit->type
== 'copy' )
622 $lcs +
= sizeof( $edit->orig
);
628 * Get the original set of lines.
630 * This reconstructs the $from_lines parameter passed to the
633 * @return array The original sequence of strings.
638 foreach ( $this->edits
as $edit ) {
640 array_splice( $lines, sizeof( $lines ), 0, $edit->orig
);
646 * Get the closing set of lines.
648 * This reconstructs the $to_lines parameter passed to the
651 * @return array The sequence of strings.
656 foreach ( $this->edits
as $edit ) {
657 if ( $edit->closing
)
658 array_splice( $lines, sizeof( $lines ), 0, $edit->closing
);
664 * Check a Diff for validity.
666 * This is here only for debugging purposes.
668 function _check ( $from_lines, $to_lines ) {
669 wfProfileIn( __METHOD__
);
670 if ( serialize( $from_lines ) != serialize( $this->orig() ) )
671 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR
);
672 if ( serialize( $to_lines ) != serialize( $this->closing() ) )
673 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR
);
675 $rev = $this->reverse();
676 if ( serialize( $to_lines ) != serialize( $rev->orig() ) )
677 trigger_error( "Reversed original doesn't match", E_USER_ERROR
);
678 if ( serialize( $from_lines ) != serialize( $rev->closing() ) )
679 trigger_error( "Reversed closing doesn't match", E_USER_ERROR
);
683 foreach ( $this->edits
as $edit ) {
684 if ( $prevtype == $edit->type
)
685 trigger_error( "Edit sequence is non-optimal", E_USER_ERROR
);
686 $prevtype = $edit->type
;
690 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE
);
691 wfProfileOut( __METHOD__
);
696 * @todo document, bad name.
698 * @ingroup DifferenceEngine
700 class MappedDiff
extends Diff
705 * Computes diff between sequences of strings.
707 * This can be used to compute things like
708 * case-insensitve diffs, or diffs which ignore
709 * changes in white-space.
711 * @param $from_lines array An array of strings.
712 * (Typically these are lines from a file.)
714 * @param $to_lines array An array of strings.
716 * @param $mapped_from_lines array This array should
717 * have the same size number of elements as $from_lines.
718 * The elements in $mapped_from_lines and
719 * $mapped_to_lines are what is actually compared
720 * when computing the diff.
722 * @param $mapped_to_lines array This array should
723 * have the same number of elements as $to_lines.
725 function __construct( $from_lines, $to_lines,
726 $mapped_from_lines, $mapped_to_lines ) {
727 wfProfileIn( __METHOD__
);
729 assert( sizeof( $from_lines ) == sizeof( $mapped_from_lines ) );
730 assert( sizeof( $to_lines ) == sizeof( $mapped_to_lines ) );
732 parent
::__construct( $mapped_from_lines, $mapped_to_lines );
735 for ( $i = 0; $i < sizeof( $this->edits
); $i++
) {
736 $orig = &$this->edits
[$i]->orig
;
737 if ( is_array( $orig ) ) {
738 $orig = array_slice( $from_lines, $xi, sizeof( $orig ) );
739 $xi +
= sizeof( $orig );
742 $closing = &$this->edits
[$i]->closing
;
743 if ( is_array( $closing ) ) {
744 $closing = array_slice( $to_lines, $yi, sizeof( $closing ) );
745 $yi +
= sizeof( $closing );
748 wfProfileOut( __METHOD__
);
753 * A class to format Diffs
755 * This class formats the diff in classic diff format.
756 * It is intended that this class be customized via inheritance,
757 * to obtain fancier outputs.
760 * @ingroup DifferenceEngine
762 class DiffFormatter
{
764 * Number of leading context "lines" to preserve.
766 * This should be left at zero for this class, but subclasses
767 * may want to set this to other values.
769 var $leading_context_lines = 0;
772 * Number of trailing context "lines" to preserve.
774 * This should be left at zero for this class, but subclasses
775 * may want to set this to other values.
777 var $trailing_context_lines = 0;
782 * @param $diff object A Diff object.
783 * @return string The formatted output.
785 function format( $diff ) {
786 wfProfileIn( __METHOD__
);
792 $nlead = $this->leading_context_lines
;
793 $ntrail = $this->trailing_context_lines
;
795 $this->_start_diff();
797 foreach ( $diff->edits
as $edit ) {
798 if ( $edit->type
== 'copy' ) {
799 if ( is_array( $block ) ) {
800 if ( sizeof( $edit->orig
) <= $nlead +
$ntrail ) {
805 $context = array_slice( $edit->orig
, 0, $ntrail );
806 $block[] = new _DiffOp_Copy( $context );
808 $this->_block( $x0, $ntrail +
$xi - $x0,
809 $y0, $ntrail +
$yi - $y0,
814 $context = $edit->orig
;
817 if ( ! is_array( $block ) ) {
818 $context = array_slice( $context, sizeof( $context ) - $nlead );
819 $x0 = $xi - sizeof( $context );
820 $y0 = $yi - sizeof( $context );
823 $block[] = new _DiffOp_Copy( $context );
829 $xi +
= sizeof( $edit->orig
);
830 if ( $edit->closing
)
831 $yi +
= sizeof( $edit->closing
);
834 if ( is_array( $block ) )
835 $this->_block( $x0, $xi - $x0,
839 $end = $this->_end_diff();
840 wfProfileOut( __METHOD__
);
844 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
845 wfProfileIn( __METHOD__
);
846 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
847 foreach ( $edits as $edit ) {
848 if ( $edit->type
== 'copy' )
849 $this->_context( $edit->orig
);
850 elseif ( $edit->type
== 'add' )
851 $this->_added( $edit->closing
);
852 elseif ( $edit->type
== 'delete' )
853 $this->_deleted( $edit->orig
);
854 elseif ( $edit->type
== 'change' )
855 $this->_changed( $edit->orig
, $edit->closing
);
857 trigger_error( 'Unknown edit type', E_USER_ERROR
);
860 wfProfileOut( __METHOD__
);
863 function _start_diff() {
867 function _end_diff() {
868 $val = ob_get_contents();
873 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
875 $xbeg .= "," . ( $xbeg +
$xlen - 1 );
877 $ybeg .= "," . ( $ybeg +
$ylen - 1 );
879 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
882 function _start_block( $header ) {
886 function _end_block() {
889 function _lines( $lines, $prefix = ' ' ) {
890 foreach ( $lines as $line )
891 echo "$prefix $line\n";
894 function _context( $lines ) {
895 $this->_lines( $lines );
898 function _added( $lines ) {
899 $this->_lines( $lines, '>' );
901 function _deleted( $lines ) {
902 $this->_lines( $lines, '<' );
905 function _changed( $orig, $closing ) {
906 $this->_deleted( $orig );
908 $this->_added( $closing );
913 * A formatter that outputs unified diffs
914 * @ingroup DifferenceEngine
917 class UnifiedDiffFormatter
extends DiffFormatter
{
918 var $leading_context_lines = 2;
919 var $trailing_context_lines = 2;
921 function _added( $lines ) {
922 $this->_lines( $lines, '+' );
924 function _deleted( $lines ) {
925 $this->_lines( $lines, '-' );
927 function _changed( $orig, $closing ) {
928 $this->_deleted( $orig );
929 $this->_added( $closing );
931 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
932 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
937 * A pseudo-formatter that just passes along the Diff::$edits array
938 * @ingroup DifferenceEngine
940 class ArrayDiffFormatter
extends DiffFormatter
{
941 function format( $diff ) {
945 foreach ( $diff->edits
as $edit )
946 switch( $edit->type
) {
948 foreach ( $edit->closing
as $l ) {
952 'newline' => $newline++
957 foreach ( $edit->orig
as $l ) {
959 'action' => 'delete',
961 'oldline' => $oldline++
,
966 foreach ( $edit->orig
as $i => $l ) {
968 'action' => 'change',
970 'new' => @$edit->closing
[$i],
971 'oldline' => $oldline++
,
972 'newline' => $newline++
,
977 $oldline +
= count( $edit->orig
);
978 $newline +
= count( $edit->orig
);
985 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
989 define( 'NBSP', ' ' ); // iso-8859-x non-breaking space.
994 * @ingroup DifferenceEngine
996 class _HWLDF_WordAccumulator
{
997 function __construct () {
998 $this->_lines
= array();
1004 function _flushGroup ( $new_tag ) {
1005 if ( $this->_group
!== '' ) {
1006 if ( $this->_tag
== 'ins' )
1007 $this->_line
.= '<ins class="diffchange diffchange-inline">' .
1008 htmlspecialchars ( $this->_group
) . '</ins>';
1009 elseif ( $this->_tag
== 'del' )
1010 $this->_line
.= '<del class="diffchange diffchange-inline">' .
1011 htmlspecialchars ( $this->_group
) . '</del>';
1013 $this->_line
.= htmlspecialchars ( $this->_group
);
1016 $this->_tag
= $new_tag;
1019 function _flushLine ( $new_tag ) {
1020 $this->_flushGroup( $new_tag );
1021 if ( $this->_line
!= '' )
1022 array_push ( $this->_lines
, $this->_line
);
1024 # make empty lines visible by inserting an NBSP
1025 array_push ( $this->_lines
, NBSP
);
1029 function addWords ( $words, $tag = '' ) {
1030 if ( $tag != $this->_tag
)
1031 $this->_flushGroup( $tag );
1033 foreach ( $words as $word ) {
1034 // new-line should only come as first char of word.
1037 if ( $word[0] == "\n" ) {
1038 $this->_flushLine( $tag );
1039 $word = substr( $word, 1 );
1041 assert( !strstr( $word, "\n" ) );
1042 $this->_group
.= $word;
1046 function getLines() {
1047 $this->_flushLine( '~done' );
1048 return $this->_lines
;
1055 * @ingroup DifferenceEngine
1057 class WordLevelDiff
extends MappedDiff
{
1058 const MAX_LINE_LENGTH
= 10000;
1060 function __construct ( $orig_lines, $closing_lines ) {
1061 wfProfileIn( __METHOD__
);
1063 list ( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1064 list ( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1066 parent
::__construct( $orig_words, $closing_words,
1067 $orig_stripped, $closing_stripped );
1068 wfProfileOut( __METHOD__
);
1071 function _split( $lines ) {
1072 wfProfileIn( __METHOD__
);
1075 $stripped = array();
1077 foreach ( $lines as $line ) {
1078 # If the line is too long, just pretend the entire line is one big word
1079 # This prevents resource exhaustion problems
1086 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1088 $stripped[] = $line;
1091 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1094 $words = array_merge( $words, $m[0] );
1095 $stripped = array_merge( $stripped, $m[1] );
1099 wfProfileOut( __METHOD__
);
1100 return array( $words, $stripped );
1104 wfProfileIn( __METHOD__
);
1105 $orig = new _HWLDF_WordAccumulator
;
1107 foreach ( $this->edits
as $edit ) {
1108 if ( $edit->type
== 'copy' )
1109 $orig->addWords( $edit->orig
);
1110 elseif ( $edit->orig
)
1111 $orig->addWords( $edit->orig
, 'del' );
1113 $lines = $orig->getLines();
1114 wfProfileOut( __METHOD__
);
1118 function closing () {
1119 wfProfileIn( __METHOD__
);
1120 $closing = new _HWLDF_WordAccumulator
;
1122 foreach ( $this->edits
as $edit ) {
1123 if ( $edit->type
== 'copy' )
1124 $closing->addWords( $edit->closing
);
1125 elseif ( $edit->closing
)
1126 $closing->addWords( $edit->closing
, 'ins' );
1128 $lines = $closing->getLines();
1129 wfProfileOut( __METHOD__
);
1135 * Wikipedia Table style diff formatter.
1138 * @ingroup DifferenceEngine
1140 class TableDiffFormatter
extends DiffFormatter
{
1141 function __construct() {
1142 $this->leading_context_lines
= 2;
1143 $this->trailing_context_lines
= 2;
1146 public static function escapeWhiteSpace( $msg ) {
1147 $msg = preg_replace( '/^ /m', '  ', $msg );
1148 $msg = preg_replace( '/ $/m', '  ', $msg );
1149 $msg = preg_replace( '/ /', '  ', $msg );
1153 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1154 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1155 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1159 function _start_block( $header ) {
1163 function _end_block() {
1166 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1169 # HTML-escape parameter before calling this
1170 function addedLine( $line ) {
1171 return $this->wrapLine( '+', 'diff-addedline', $line );
1174 # HTML-escape parameter before calling this
1175 function deletedLine( $line ) {
1176 return $this->wrapLine( '−', 'diff-deletedline', $line );
1179 # HTML-escape parameter before calling this
1180 function contextLine( $line ) {
1181 return $this->wrapLine( ' ', 'diff-context', $line );
1184 private function wrapLine( $marker, $class, $line ) {
1185 if ( $line !== '' ) {
1186 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1187 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1189 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1192 function emptyLine() {
1193 return '<td colspan="2"> </td>';
1196 function _added( $lines ) {
1197 foreach ( $lines as $line ) {
1198 echo '<tr>' . $this->emptyLine() .
1199 $this->addedLine( '<ins class="diffchange">' .
1200 htmlspecialchars ( $line ) . '</ins>' ) . "</tr>\n";
1204 function _deleted( $lines ) {
1205 foreach ( $lines as $line ) {
1206 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1207 htmlspecialchars ( $line ) . '</del>' ) .
1208 $this->emptyLine() . "</tr>\n";
1212 function _context( $lines ) {
1213 foreach ( $lines as $line ) {
1215 $this->contextLine( htmlspecialchars ( $line ) ) .
1216 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1220 function _changed( $orig, $closing ) {
1221 wfProfileIn( __METHOD__
);
1223 $diff = new WordLevelDiff( $orig, $closing );
1224 $del = $diff->orig();
1225 $add = $diff->closing();
1227 # Notice that WordLevelDiff returns HTML-escaped output.
1228 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1230 while ( $line = array_shift( $del ) ) {
1231 $aline = array_shift( $add );
1232 echo '<tr>' . $this->deletedLine( $line ) .
1233 $this->addedLine( $aline ) . "</tr>\n";
1235 foreach ( $add as $line ) { # If any leftovers
1236 echo '<tr>' . $this->emptyLine() .
1237 $this->addedLine( $line ) . "</tr>\n";
1239 wfProfileOut( __METHOD__
);