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 protected $xchanged, $ychanged;
140 function diff ( $from_lines, $to_lines ) {
141 wfProfileIn( __METHOD__
);
143 // Diff and store locally
144 $this->diff_local( $from_lines, $to_lines );
146 // Merge edits when possible
147 $this->_shift_boundaries( $from_lines, $this->xchanged
, $this->ychanged
);
148 $this->_shift_boundaries( $to_lines, $this->ychanged
, $this->xchanged
);
150 // Compute the edit operations.
151 $n_from = sizeof( $from_lines );
152 $n_to = sizeof( $to_lines );
156 while ( $xi < $n_from ||
$yi < $n_to ) {
157 assert( $yi < $n_to ||
$this->xchanged
[$xi] );
158 assert( $xi < $n_from ||
$this->ychanged
[$yi] );
160 // Skip matching "snake".
162 while ( $xi < $n_from && $yi < $n_to
163 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi] ) {
164 $copy[] = $from_lines[$xi++
];
168 $edits[] = new _DiffOp_Copy( $copy );
171 // Find deletes & adds.
173 while ( $xi < $n_from && $this->xchanged
[$xi] ) {
174 $delete[] = $from_lines[$xi++
];
178 while ( $yi < $n_to && $this->ychanged
[$yi] ) {
179 $add[] = $to_lines[$yi++
];
182 if ( $delete && $add ) {
183 $edits[] = new _DiffOp_Change( $delete, $add );
184 } elseif ( $delete ) {
185 $edits[] = new _DiffOp_Delete( $delete );
187 $edits[] = new _DiffOp_Add( $add );
190 wfProfileOut( __METHOD__
);
194 function diff_local ( $from_lines, $to_lines ) {
195 global $wgExternalDiffEngine;
196 wfProfileIn( __METHOD__
);
198 if ( $wgExternalDiffEngine == 'wikidiff3' ) {
200 $wikidiff3 = new WikiDiff3();
201 $wikidiff3->diff( $from_lines, $to_lines );
202 $this->xchanged
= $wikidiff3->removed
;
203 $this->ychanged
= $wikidiff3->added
;
207 $n_from = sizeof( $from_lines );
208 $n_to = sizeof( $to_lines );
209 $this->xchanged
= $this->ychanged
= array();
210 $this->xv
= $this->yv
= array();
211 $this->xind
= $this->yind
= array();
213 unset( $this->in_seq
);
216 // Skip leading common lines.
217 for ( $skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
218 if ( $from_lines[$skip] !== $to_lines[$skip] )
220 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
222 // Skip trailing common lines.
223 $xi = $n_from; $yi = $n_to;
224 for ( $endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
225 if ( $from_lines[$xi] !== $to_lines[$yi] )
227 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
230 // Ignore lines which do not exist in both files.
231 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
232 $xhash[$this->_line_hash( $from_lines[$xi] )] = 1;
235 for ( $yi = $skip; $yi < $n_to - $endskip; $yi++
) {
236 $line = $to_lines[$yi];
237 if ( ( $this->ychanged
[$yi] = empty( $xhash[$this->_line_hash( $line )] ) ) )
239 $yhash[$this->_line_hash( $line )] = 1;
243 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
244 $line = $from_lines[$xi];
245 if ( ( $this->xchanged
[$xi] = empty( $yhash[$this->_line_hash( $line )] ) ) )
252 $this->_compareseq( 0, sizeof( $this->xv
), 0, sizeof( $this->yv
) );
254 wfProfileOut( __METHOD__
);
258 * Returns the whole line if it's small enough, or the MD5 hash otherwise
260 function _line_hash( $line ) {
261 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
268 /* Divide the Largest Common Subsequence (LCS) of the sequences
269 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
272 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
273 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
274 * sub sequences. The first sub-sequence is contained in [X0, X1),
275 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
276 * that (X0, Y0) == (XOFF, YOFF) and
277 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
279 * This function assumes that the first lines of the specified portions
280 * of the two files do not match, and likewise that the last lines do not
281 * match. The caller must trim matching lines from the beginning and end
282 * of the portions it is going to specify.
284 function _diag ( $xoff, $xlim, $yoff, $ylim, $nchunks ) {
287 if ( $xlim - $xoff > $ylim - $yoff ) {
288 // Things seems faster (I'm not sure I understand why)
289 // when the shortest sequence in X.
291 list ( $xoff, $xlim, $yoff, $ylim ) = array( $yoff, $ylim, $xoff, $xlim );
295 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
296 $ymatches[$this->xv
[$i]][] = $i;
299 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
300 $ymatches[$this->yv
[$i]][] = $i;
305 $this->seq
[0] = $yoff - 1;
306 $this->in_seq
= array();
309 $numer = $xlim - $xoff +
$nchunks - 1;
311 for ( $chunk = 0; $chunk < $nchunks; $chunk++
) {
313 for ( $i = 0; $i <= $this->lcs
; $i++
) {
314 $ymids[$i][$chunk -1] = $this->seq
[$i];
318 $x1 = $xoff +
(int)( ( $numer +
( $xlim -$xoff ) * $chunk ) / $nchunks );
319 for ( ; $x < $x1; $x++
) {
320 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
321 if ( empty( $ymatches[$line] ) ) {
324 $matches = $ymatches[$line];
326 while ( list( , $y ) = each( $matches ) ) {
327 if ( empty( $this->in_seq
[$y] ) ) {
328 $k = $this->_lcs_pos( $y );
330 $ymids[$k] = $ymids[$k -1];
334 while ( list ( , $y ) = each( $matches ) ) {
335 if ( $y > $this->seq
[$k -1] ) {
336 assert( $y < $this->seq
[$k] );
337 // Optimization: this is a common case:
338 // next match is just replacing previous match.
339 $this->in_seq
[$this->seq
[$k]] = false;
341 $this->in_seq
[$y] = 1;
342 } else if ( empty( $this->in_seq
[$y] ) ) {
343 $k = $this->_lcs_pos( $y );
345 $ymids[$k] = $ymids[$k -1];
351 $seps[] = $flip ?
array( $yoff, $xoff ) : array( $xoff, $yoff );
352 $ymid = $ymids[$this->lcs
];
353 for ( $n = 0; $n < $nchunks - 1; $n++
) {
354 $x1 = $xoff +
(int)( ( $numer +
( $xlim - $xoff ) * $n ) / $nchunks );
356 $seps[] = $flip ?
array( $y1, $x1 ) : array( $x1, $y1 );
358 $seps[] = $flip ?
array( $ylim, $xlim ) : array( $xlim, $ylim );
360 return array( $this->lcs
, $seps );
363 function _lcs_pos ( $ypos ) {
365 if ( $end == 0 ||
$ypos > $this->seq
[$end] ) {
366 $this->seq
[++
$this->lcs
] = $ypos;
367 $this->in_seq
[$ypos] = 1;
372 while ( $beg < $end ) {
373 $mid = (int)( ( $beg +
$end ) / 2 );
374 if ( $ypos > $this->seq
[$mid] )
380 assert( $ypos != $this->seq
[$end] );
382 $this->in_seq
[$this->seq
[$end]] = false;
383 $this->seq
[$end] = $ypos;
384 $this->in_seq
[$ypos] = 1;
388 /* Find LCS of two sequences.
390 * The results are recorded in the vectors $this->{x,y}changed[], by
391 * storing a 1 in the element for each line that is an insertion
392 * or deletion (ie. is not in the LCS).
394 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
396 * Note that XLIM, YLIM are exclusive bounds.
397 * All line numbers are origin-0 and discarded lines are not counted.
399 function _compareseq ( $xoff, $xlim, $yoff, $ylim ) {
400 // Slide down the bottom initial diagonal.
401 while ( $xoff < $xlim && $yoff < $ylim
402 && $this->xv
[$xoff] == $this->yv
[$yoff] ) {
407 // Slide up the top initial diagonal.
408 while ( $xlim > $xoff && $ylim > $yoff
409 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1] ) {
414 if ( $xoff == $xlim ||
$yoff == $ylim ) {
417 // This is ad hoc but seems to work well.
418 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
419 // $nchunks = max(2,min(8,(int)$nchunks));
420 $nchunks = min( 7, $xlim - $xoff, $ylim - $yoff ) +
1;
422 = $this->_diag( $xoff, $xlim, $yoff, $ylim, $nchunks );
426 // X and Y sequences have no common subsequence:
428 while ( $yoff < $ylim ) {
429 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
431 while ( $xoff < $xlim ) {
432 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
435 // Use the partitions to split this problem into subproblems.
438 while ( $pt2 = next( $seps ) ) {
439 $this->_compareseq ( $pt1[0], $pt2[0], $pt1[1], $pt2[1] );
445 /* Adjust inserts/deletes of identical lines to join changes
446 * as much as possible.
448 * We do something when a run of changed lines include a
449 * line at one end and has an excluded, identical line at the other.
450 * We are free to choose which identical line is included.
451 * `compareseq' usually chooses the one at the beginning,
452 * but usually it is cleaner to consider the following identical line
453 * to be the "change".
455 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
457 function _shift_boundaries ( $lines, &$changed, $other_changed ) {
458 wfProfileIn( __METHOD__
);
462 assert( 'sizeof($lines) == sizeof($changed)' );
463 $len = sizeof( $lines );
464 $other_len = sizeof( $other_changed );
468 * Scan forwards to find beginning of another run of changes.
469 * Also keep track of the corresponding point in the other file.
471 * Throughout this code, $i and $j are adjusted together so that
472 * the first $i elements of $changed and the first $j elements
473 * of $other_changed both contain the same number of zeros
475 * Furthermore, $j is always kept so that $j == $other_len or
476 * $other_changed[$j] == false.
478 while ( $j < $other_len && $other_changed[$j] ) {
482 while ( $i < $len && ! $changed[$i] ) {
483 assert( '$j < $other_len && ! $other_changed[$j]' );
485 while ( $j < $other_len && $other_changed[$j] )
495 // Find the end of this run of changes.
496 while ( ++
$i < $len && $changed[$i] ) {
502 * Record the length of this run of changes, so that
503 * we can later determine whether the run has grown.
505 $runlength = $i - $start;
508 * Move the changed region back, so long as the
509 * previous unchanged line matches the last changed one.
510 * This merges with previous changed regions.
512 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
513 $changed[--$start] = 1;
514 $changed[--$i] = false;
515 while ( $start > 0 && $changed[$start - 1] ) {
519 while ( $other_changed[--$j] ) {
522 assert( '$j >= 0 && !$other_changed[$j]' );
526 * Set CORRESPONDING to the end of the changed run, at the last
527 * point where it corresponds to a changed run in the other file.
528 * CORRESPONDING == LEN means no such point has been found.
530 $corresponding = $j < $other_len ?
$i : $len;
533 * Move the changed region forward, so long as the
534 * first changed line matches the following unchanged one.
535 * This merges with following changed regions.
536 * Do this second, so that if there are no merges,
537 * the changed region is moved forward as far as possible.
539 while ( $i < $len && $lines[$start] == $lines[$i] ) {
540 $changed[$start++
] = false;
542 while ( $i < $len && $changed[$i] ) {
546 assert( '$j < $other_len && ! $other_changed[$j]' );
548 if ( $j < $other_len && $other_changed[$j] ) {
550 while ( $j < $other_len && $other_changed[$j] ) {
555 } while ( $runlength != $i - $start );
558 * If possible, move the fully-merged run of changes
559 * back to a corresponding run in the other file.
561 while ( $corresponding < $i ) {
562 $changed[--$start] = 1;
565 while ( $other_changed[--$j] ) {
568 assert( '$j >= 0 && !$other_changed[$j]' );
571 wfProfileOut( __METHOD__
);
576 * Class representing a 'diff' between two sequences of strings.
579 * @ingroup DifferenceEngine
587 * Computes diff between sequences of strings.
589 * @param $from_lines array An array of strings.
590 * (Typically these are lines from a file.)
591 * @param $to_lines array An array of strings.
593 function __construct( $from_lines, $to_lines ) {
594 $eng = new _DiffEngine
;
595 $this->edits
= $eng->diff( $from_lines, $to_lines );
596 // $this->_check($from_lines, $to_lines);
600 * Compute reversed Diff.
604 * $diff = new Diff($lines1, $lines2);
605 * $rev = $diff->reverse();
606 * @return object A Diff object representing the inverse of the
609 function reverse () {
611 $rev->edits
= array();
612 foreach ( $this->edits
as $edit ) {
613 $rev->edits
[] = $edit->reverse();
619 * Check for empty diff.
621 * @return bool True iff two sequences were identical.
623 function isEmpty () {
624 foreach ( $this->edits
as $edit ) {
625 if ( $edit->type
!= 'copy' ) {
633 * Compute the length of the Longest Common Subsequence (LCS).
635 * This is mostly for diagnostic purposed.
637 * @return int The length of the LCS.
641 foreach ( $this->edits
as $edit ) {
642 if ( $edit->type
== 'copy' ) {
643 $lcs +
= sizeof( $edit->orig
);
650 * Get the original set of lines.
652 * This reconstructs the $from_lines parameter passed to the
655 * @return array The original sequence of strings.
660 foreach ( $this->edits
as $edit ) {
662 array_splice( $lines, sizeof( $lines ), 0, $edit->orig
);
669 * Get the closing set of lines.
671 * This reconstructs the $to_lines parameter passed to the
674 * @return array The sequence of strings.
679 foreach ( $this->edits
as $edit ) {
680 if ( $edit->closing
) {
681 array_splice( $lines, sizeof( $lines ), 0, $edit->closing
);
688 * Check a Diff for validity.
690 * This is here only for debugging purposes.
692 function _check ( $from_lines, $to_lines ) {
693 wfProfileIn( __METHOD__
);
694 if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
695 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR
);
697 if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
698 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR
);
701 $rev = $this->reverse();
702 if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
703 trigger_error( "Reversed original doesn't match", E_USER_ERROR
);
705 if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
706 trigger_error( "Reversed closing doesn't match", E_USER_ERROR
);
711 foreach ( $this->edits
as $edit ) {
712 if ( $prevtype == $edit->type
) {
713 trigger_error( "Edit sequence is non-optimal", E_USER_ERROR
);
715 $prevtype = $edit->type
;
719 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE
);
720 wfProfileOut( __METHOD__
);
725 * @todo document, bad name.
727 * @ingroup DifferenceEngine
729 class MappedDiff
extends Diff
734 * Computes diff between sequences of strings.
736 * This can be used to compute things like
737 * case-insensitve diffs, or diffs which ignore
738 * changes in white-space.
740 * @param $from_lines array An array of strings.
741 * (Typically these are lines from a file.)
743 * @param $to_lines array An array of strings.
745 * @param $mapped_from_lines array This array should
746 * have the same size number of elements as $from_lines.
747 * The elements in $mapped_from_lines and
748 * $mapped_to_lines are what is actually compared
749 * when computing the diff.
751 * @param $mapped_to_lines array This array should
752 * have the same number of elements as $to_lines.
754 function __construct( $from_lines, $to_lines,
755 $mapped_from_lines, $mapped_to_lines ) {
756 wfProfileIn( __METHOD__
);
758 assert( sizeof( $from_lines ) == sizeof( $mapped_from_lines ) );
759 assert( sizeof( $to_lines ) == sizeof( $mapped_to_lines ) );
761 parent
::__construct( $mapped_from_lines, $mapped_to_lines );
764 for ( $i = 0; $i < sizeof( $this->edits
); $i++
) {
765 $orig = &$this->edits
[$i]->orig
;
766 if ( is_array( $orig ) ) {
767 $orig = array_slice( $from_lines, $xi, sizeof( $orig ) );
768 $xi +
= sizeof( $orig );
771 $closing = &$this->edits
[$i]->closing
;
772 if ( is_array( $closing ) ) {
773 $closing = array_slice( $to_lines, $yi, sizeof( $closing ) );
774 $yi +
= sizeof( $closing );
777 wfProfileOut( __METHOD__
);
782 * A class to format Diffs
784 * This class formats the diff in classic diff format.
785 * It is intended that this class be customized via inheritance,
786 * to obtain fancier outputs.
789 * @ingroup DifferenceEngine
791 class DiffFormatter
{
793 * Number of leading context "lines" to preserve.
795 * This should be left at zero for this class, but subclasses
796 * may want to set this to other values.
798 var $leading_context_lines = 0;
801 * Number of trailing context "lines" to preserve.
803 * This should be left at zero for this class, but subclasses
804 * may want to set this to other values.
806 var $trailing_context_lines = 0;
811 * @param $diff Diff A Diff object.
812 * @return string The formatted output.
814 function format( $diff ) {
815 wfProfileIn( __METHOD__
);
821 $nlead = $this->leading_context_lines
;
822 $ntrail = $this->trailing_context_lines
;
824 $this->_start_diff();
826 foreach ( $diff->edits
as $edit ) {
827 if ( $edit->type
== 'copy' ) {
828 if ( is_array( $block ) ) {
829 if ( sizeof( $edit->orig
) <= $nlead +
$ntrail ) {
834 $context = array_slice( $edit->orig
, 0, $ntrail );
835 $block[] = new _DiffOp_Copy( $context );
837 $this->_block( $x0, $ntrail +
$xi - $x0,
838 $y0, $ntrail +
$yi - $y0,
843 $context = $edit->orig
;
846 if ( ! is_array( $block ) ) {
847 $context = array_slice( $context, sizeof( $context ) - $nlead );
848 $x0 = $xi - sizeof( $context );
849 $y0 = $yi - sizeof( $context );
852 $block[] = new _DiffOp_Copy( $context );
859 $xi +
= sizeof( $edit->orig
);
861 if ( $edit->closing
) {
862 $yi +
= sizeof( $edit->closing
);
866 if ( is_array( $block ) ) {
867 $this->_block( $x0, $xi - $x0,
872 $end = $this->_end_diff();
873 wfProfileOut( __METHOD__
);
877 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
878 wfProfileIn( __METHOD__
);
879 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
880 foreach ( $edits as $edit ) {
881 if ( $edit->type
== 'copy' ) {
882 $this->_context( $edit->orig
);
883 } elseif ( $edit->type
== 'add' ) {
884 $this->_added( $edit->closing
);
885 } elseif ( $edit->type
== 'delete' ) {
886 $this->_deleted( $edit->orig
);
887 } elseif ( $edit->type
== 'change' ) {
888 $this->_changed( $edit->orig
, $edit->closing
);
890 trigger_error( 'Unknown edit type', E_USER_ERROR
);
894 wfProfileOut( __METHOD__
);
897 function _start_diff() {
901 function _end_diff() {
902 $val = ob_get_contents();
907 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
909 $xbeg .= "," . ( $xbeg +
$xlen - 1 );
912 $ybeg .= "," . ( $ybeg +
$ylen - 1 );
915 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
918 function _start_block( $header ) {
922 function _end_block() {
925 function _lines( $lines, $prefix = ' ' ) {
926 foreach ( $lines as $line ) {
927 echo "$prefix $line\n";
931 function _context( $lines ) {
932 $this->_lines( $lines );
935 function _added( $lines ) {
936 $this->_lines( $lines, '>' );
938 function _deleted( $lines ) {
939 $this->_lines( $lines, '<' );
942 function _changed( $orig, $closing ) {
943 $this->_deleted( $orig );
945 $this->_added( $closing );
950 * A formatter that outputs unified diffs
951 * @ingroup DifferenceEngine
954 class UnifiedDiffFormatter
extends DiffFormatter
{
955 var $leading_context_lines = 2;
956 var $trailing_context_lines = 2;
958 function _added( $lines ) {
959 $this->_lines( $lines, '+' );
961 function _deleted( $lines ) {
962 $this->_lines( $lines, '-' );
964 function _changed( $orig, $closing ) {
965 $this->_deleted( $orig );
966 $this->_added( $closing );
968 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
969 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
974 * A pseudo-formatter that just passes along the Diff::$edits array
975 * @ingroup DifferenceEngine
977 class ArrayDiffFormatter
extends DiffFormatter
{
978 function format( $diff ) {
982 foreach ( $diff->edits
as $edit ) {
983 switch( $edit->type
) {
985 foreach ( $edit->closing
as $l ) {
989 'newline' => $newline++
994 foreach ( $edit->orig
as $l ) {
996 'action' => 'delete',
998 'oldline' => $oldline++
,
1003 foreach ( $edit->orig
as $i => $l ) {
1005 'action' => 'change',
1007 'new' => @$edit->closing
[$i],
1008 'oldline' => $oldline++
,
1009 'newline' => $newline++
,
1014 $oldline +
= count( $edit->orig
);
1015 $newline +
= count( $edit->orig
);
1023 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1027 define( 'NBSP', ' ' ); // iso-8859-x non-breaking space.
1032 * @ingroup DifferenceEngine
1034 class _HWLDF_WordAccumulator
{
1035 function __construct () {
1036 $this->_lines
= array();
1042 function _flushGroup ( $new_tag ) {
1043 if ( $this->_group
!== '' ) {
1044 if ( $this->_tag
== 'ins' ) {
1045 $this->_line
.= '<ins class="diffchange diffchange-inline">' .
1046 htmlspecialchars ( $this->_group
) . '</ins>';
1047 } elseif ( $this->_tag
== 'del' ) {
1048 $this->_line
.= '<del class="diffchange diffchange-inline">' .
1049 htmlspecialchars ( $this->_group
) . '</del>';
1051 $this->_line
.= htmlspecialchars ( $this->_group
);
1055 $this->_tag
= $new_tag;
1058 function _flushLine ( $new_tag ) {
1059 $this->_flushGroup( $new_tag );
1060 if ( $this->_line
!= '' ) {
1061 array_push ( $this->_lines
, $this->_line
);
1063 # make empty lines visible by inserting an NBSP
1064 array_push ( $this->_lines
, NBSP
);
1069 function addWords ( $words, $tag = '' ) {
1070 if ( $tag != $this->_tag
) {
1071 $this->_flushGroup( $tag );
1074 foreach ( $words as $word ) {
1075 // new-line should only come as first char of word.
1076 if ( $word == '' ) {
1079 if ( $word[0] == "\n" ) {
1080 $this->_flushLine( $tag );
1081 $word = substr( $word, 1 );
1083 assert( !strstr( $word, "\n" ) );
1084 $this->_group
.= $word;
1088 function getLines() {
1089 $this->_flushLine( '~done' );
1090 return $this->_lines
;
1097 * @ingroup DifferenceEngine
1099 class WordLevelDiff
extends MappedDiff
{
1100 const MAX_LINE_LENGTH
= 10000;
1102 function __construct ( $orig_lines, $closing_lines ) {
1103 wfProfileIn( __METHOD__
);
1105 list ( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1106 list ( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1108 parent
::__construct( $orig_words, $closing_words,
1109 $orig_stripped, $closing_stripped );
1110 wfProfileOut( __METHOD__
);
1113 function _split( $lines ) {
1114 wfProfileIn( __METHOD__
);
1117 $stripped = array();
1119 foreach ( $lines as $line ) {
1120 # If the line is too long, just pretend the entire line is one big word
1121 # This prevents resource exhaustion problems
1128 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1130 $stripped[] = $line;
1133 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1136 $words = array_merge( $words, $m[0] );
1137 $stripped = array_merge( $stripped, $m[1] );
1141 wfProfileOut( __METHOD__
);
1142 return array( $words, $stripped );
1146 wfProfileIn( __METHOD__
);
1147 $orig = new _HWLDF_WordAccumulator
;
1149 foreach ( $this->edits
as $edit ) {
1150 if ( $edit->type
== 'copy' ) {
1151 $orig->addWords( $edit->orig
);
1152 } elseif ( $edit->orig
) {
1153 $orig->addWords( $edit->orig
, 'del' );
1156 $lines = $orig->getLines();
1157 wfProfileOut( __METHOD__
);
1161 function closing () {
1162 wfProfileIn( __METHOD__
);
1163 $closing = new _HWLDF_WordAccumulator
;
1165 foreach ( $this->edits
as $edit ) {
1166 if ( $edit->type
== 'copy' ) {
1167 $closing->addWords( $edit->closing
);
1168 } elseif ( $edit->closing
) {
1169 $closing->addWords( $edit->closing
, 'ins' );
1172 $lines = $closing->getLines();
1173 wfProfileOut( __METHOD__
);
1179 * Wikipedia Table style diff formatter.
1182 * @ingroup DifferenceEngine
1184 class TableDiffFormatter
extends DiffFormatter
{
1185 function __construct() {
1186 $this->leading_context_lines
= 2;
1187 $this->trailing_context_lines
= 2;
1190 public static function escapeWhiteSpace( $msg ) {
1191 $msg = preg_replace( '/^ /m', '  ', $msg );
1192 $msg = preg_replace( '/ $/m', '  ', $msg );
1193 $msg = preg_replace( '/ /', '  ', $msg );
1197 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1198 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1199 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1203 function _start_block( $header ) {
1207 function _end_block() {
1210 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1213 # HTML-escape parameter before calling this
1214 function addedLine( $line ) {
1215 return $this->wrapLine( '+', 'diff-addedline', $line );
1218 # HTML-escape parameter before calling this
1219 function deletedLine( $line ) {
1220 return $this->wrapLine( '−', 'diff-deletedline', $line );
1223 # HTML-escape parameter before calling this
1224 function contextLine( $line ) {
1225 return $this->wrapLine( ' ', 'diff-context', $line );
1228 private function wrapLine( $marker, $class, $line ) {
1229 if ( $line !== '' ) {
1230 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1231 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1233 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1236 function emptyLine() {
1237 return '<td colspan="2"> </td>';
1240 function _added( $lines ) {
1241 foreach ( $lines as $line ) {
1242 echo '<tr>' . $this->emptyLine() .
1243 $this->addedLine( '<ins class="diffchange">' .
1244 htmlspecialchars ( $line ) . '</ins>' ) . "</tr>\n";
1248 function _deleted( $lines ) {
1249 foreach ( $lines as $line ) {
1250 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1251 htmlspecialchars ( $line ) . '</del>' ) .
1252 $this->emptyLine() . "</tr>\n";
1256 function _context( $lines ) {
1257 foreach ( $lines as $line ) {
1259 $this->contextLine( htmlspecialchars ( $line ) ) .
1260 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1264 function _changed( $orig, $closing ) {
1265 wfProfileIn( __METHOD__
);
1267 $diff = new WordLevelDiff( $orig, $closing );
1268 $del = $diff->orig();
1269 $add = $diff->closing();
1271 # Notice that WordLevelDiff returns HTML-escaped output.
1272 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1274 while ( $line = array_shift( $del ) ) {
1275 $aline = array_shift( $add );
1276 echo '<tr>' . $this->deletedLine( $line ) .
1277 $this->addedLine( $aline ) . "</tr>\n";
1279 foreach ( $add as $line ) { # If any leftovers
1280 echo '<tr>' . $this->emptyLine() .
1281 $this->addedLine( $line ) . "</tr>\n";
1283 wfProfileOut( __METHOD__
);