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 protected $xv = array(), $yv = array();
141 protected $xind = array(), $yind = array();
143 protected $seq = array(), $in_seq = array();
147 function diff ( $from_lines, $to_lines ) {
148 wfProfileIn( __METHOD__
);
150 // Diff and store locally
151 $this->diff_local( $from_lines, $to_lines );
153 // Merge edits when possible
154 $this->_shift_boundaries( $from_lines, $this->xchanged
, $this->ychanged
);
155 $this->_shift_boundaries( $to_lines, $this->ychanged
, $this->xchanged
);
157 // Compute the edit operations.
158 $n_from = sizeof( $from_lines );
159 $n_to = sizeof( $to_lines );
163 while ( $xi < $n_from ||
$yi < $n_to ) {
164 assert( $yi < $n_to ||
$this->xchanged
[$xi] );
165 assert( $xi < $n_from ||
$this->ychanged
[$yi] );
167 // Skip matching "snake".
169 while ( $xi < $n_from && $yi < $n_to
170 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi] ) {
171 $copy[] = $from_lines[$xi++
];
175 $edits[] = new _DiffOp_Copy( $copy );
178 // Find deletes & adds.
180 while ( $xi < $n_from && $this->xchanged
[$xi] ) {
181 $delete[] = $from_lines[$xi++
];
185 while ( $yi < $n_to && $this->ychanged
[$yi] ) {
186 $add[] = $to_lines[$yi++
];
189 if ( $delete && $add ) {
190 $edits[] = new _DiffOp_Change( $delete, $add );
191 } elseif ( $delete ) {
192 $edits[] = new _DiffOp_Delete( $delete );
194 $edits[] = new _DiffOp_Add( $add );
197 wfProfileOut( __METHOD__
);
201 function diff_local ( $from_lines, $to_lines ) {
202 global $wgExternalDiffEngine;
203 wfProfileIn( __METHOD__
);
205 if ( $wgExternalDiffEngine == 'wikidiff3' ) {
207 $wikidiff3 = new WikiDiff3();
208 $wikidiff3->diff( $from_lines, $to_lines );
209 $this->xchanged
= $wikidiff3->removed
;
210 $this->ychanged
= $wikidiff3->added
;
214 $n_from = sizeof( $from_lines );
215 $n_to = sizeof( $to_lines );
216 $this->xchanged
= $this->ychanged
= array();
217 $this->xv
= $this->yv
= array();
218 $this->xind
= $this->yind
= array();
220 unset( $this->in_seq
);
223 // Skip leading common lines.
224 for ( $skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
225 if ( $from_lines[$skip] !== $to_lines[$skip] )
227 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
229 // Skip trailing common lines.
230 $xi = $n_from; $yi = $n_to;
231 for ( $endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
232 if ( $from_lines[$xi] !== $to_lines[$yi] )
234 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
237 // Ignore lines which do not exist in both files.
238 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
239 $xhash[$this->_line_hash( $from_lines[$xi] )] = 1;
242 for ( $yi = $skip; $yi < $n_to - $endskip; $yi++
) {
243 $line = $to_lines[$yi];
244 if ( ( $this->ychanged
[$yi] = empty( $xhash[$this->_line_hash( $line )] ) ) )
246 $yhash[$this->_line_hash( $line )] = 1;
250 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
251 $line = $from_lines[$xi];
252 if ( ( $this->xchanged
[$xi] = empty( $yhash[$this->_line_hash( $line )] ) ) )
259 $this->_compareseq( 0, sizeof( $this->xv
), 0, sizeof( $this->yv
) );
261 wfProfileOut( __METHOD__
);
265 * Returns the whole line if it's small enough, or the MD5 hash otherwise
267 function _line_hash( $line ) {
268 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
275 /* Divide the Largest Common Subsequence (LCS) of the sequences
276 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
279 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
280 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
281 * sub sequences. The first sub-sequence is contained in [X0, X1),
282 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
283 * that (X0, Y0) == (XOFF, YOFF) and
284 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
286 * This function assumes that the first lines of the specified portions
287 * of the two files do not match, and likewise that the last lines do not
288 * match. The caller must trim matching lines from the beginning and end
289 * of the portions it is going to specify.
291 function _diag ( $xoff, $xlim, $yoff, $ylim, $nchunks ) {
294 if ( $xlim - $xoff > $ylim - $yoff ) {
295 // Things seems faster (I'm not sure I understand why)
296 // when the shortest sequence in X.
298 list ( $xoff, $xlim, $yoff, $ylim ) = array( $yoff, $ylim, $xoff, $xlim );
302 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
303 $ymatches[$this->xv
[$i]][] = $i;
306 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
307 $ymatches[$this->yv
[$i]][] = $i;
312 $this->seq
[0] = $yoff - 1;
313 $this->in_seq
= array();
316 $numer = $xlim - $xoff +
$nchunks - 1;
318 for ( $chunk = 0; $chunk < $nchunks; $chunk++
) {
320 for ( $i = 0; $i <= $this->lcs
; $i++
) {
321 $ymids[$i][$chunk -1] = $this->seq
[$i];
325 $x1 = $xoff +
(int)( ( $numer +
( $xlim -$xoff ) * $chunk ) / $nchunks );
326 for ( ; $x < $x1; $x++
) {
327 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
328 if ( empty( $ymatches[$line] ) ) {
331 $matches = $ymatches[$line];
333 while ( list( , $y ) = each( $matches ) ) {
334 if ( empty( $this->in_seq
[$y] ) ) {
335 $k = $this->_lcs_pos( $y );
337 $ymids[$k] = $ymids[$k -1];
341 while ( list ( , $y ) = each( $matches ) ) {
342 if ( $y > $this->seq
[$k -1] ) {
343 assert( $y < $this->seq
[$k] );
344 // Optimization: this is a common case:
345 // next match is just replacing previous match.
346 $this->in_seq
[$this->seq
[$k]] = false;
348 $this->in_seq
[$y] = 1;
349 } else if ( empty( $this->in_seq
[$y] ) ) {
350 $k = $this->_lcs_pos( $y );
352 $ymids[$k] = $ymids[$k -1];
358 $seps[] = $flip ?
array( $yoff, $xoff ) : array( $xoff, $yoff );
359 $ymid = $ymids[$this->lcs
];
360 for ( $n = 0; $n < $nchunks - 1; $n++
) {
361 $x1 = $xoff +
(int)( ( $numer +
( $xlim - $xoff ) * $n ) / $nchunks );
363 $seps[] = $flip ?
array( $y1, $x1 ) : array( $x1, $y1 );
365 $seps[] = $flip ?
array( $ylim, $xlim ) : array( $xlim, $ylim );
367 return array( $this->lcs
, $seps );
370 function _lcs_pos ( $ypos ) {
372 if ( $end == 0 ||
$ypos > $this->seq
[$end] ) {
373 $this->seq
[++
$this->lcs
] = $ypos;
374 $this->in_seq
[$ypos] = 1;
379 while ( $beg < $end ) {
380 $mid = (int)( ( $beg +
$end ) / 2 );
381 if ( $ypos > $this->seq
[$mid] )
387 assert( $ypos != $this->seq
[$end] );
389 $this->in_seq
[$this->seq
[$end]] = false;
390 $this->seq
[$end] = $ypos;
391 $this->in_seq
[$ypos] = 1;
395 /* Find LCS of two sequences.
397 * The results are recorded in the vectors $this->{x,y}changed[], by
398 * storing a 1 in the element for each line that is an insertion
399 * or deletion (ie. is not in the LCS).
401 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
403 * Note that XLIM, YLIM are exclusive bounds.
404 * All line numbers are origin-0 and discarded lines are not counted.
406 function _compareseq ( $xoff, $xlim, $yoff, $ylim ) {
407 // Slide down the bottom initial diagonal.
408 while ( $xoff < $xlim && $yoff < $ylim
409 && $this->xv
[$xoff] == $this->yv
[$yoff] ) {
414 // Slide up the top initial diagonal.
415 while ( $xlim > $xoff && $ylim > $yoff
416 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1] ) {
421 if ( $xoff == $xlim ||
$yoff == $ylim ) {
424 // This is ad hoc but seems to work well.
425 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
426 // $nchunks = max(2,min(8,(int)$nchunks));
427 $nchunks = min( 7, $xlim - $xoff, $ylim - $yoff ) +
1;
429 = $this->_diag( $xoff, $xlim, $yoff, $ylim, $nchunks );
433 // X and Y sequences have no common subsequence:
435 while ( $yoff < $ylim ) {
436 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
438 while ( $xoff < $xlim ) {
439 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
442 // Use the partitions to split this problem into subproblems.
445 while ( $pt2 = next( $seps ) ) {
446 $this->_compareseq ( $pt1[0], $pt2[0], $pt1[1], $pt2[1] );
452 /* Adjust inserts/deletes of identical lines to join changes
453 * as much as possible.
455 * We do something when a run of changed lines include a
456 * line at one end and has an excluded, identical line at the other.
457 * We are free to choose which identical line is included.
458 * `compareseq' usually chooses the one at the beginning,
459 * but usually it is cleaner to consider the following identical line
460 * to be the "change".
462 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
464 function _shift_boundaries ( $lines, &$changed, $other_changed ) {
465 wfProfileIn( __METHOD__
);
469 assert( 'sizeof($lines) == sizeof($changed)' );
470 $len = sizeof( $lines );
471 $other_len = sizeof( $other_changed );
475 * Scan forwards to find beginning of another run of changes.
476 * Also keep track of the corresponding point in the other file.
478 * Throughout this code, $i and $j are adjusted together so that
479 * the first $i elements of $changed and the first $j elements
480 * of $other_changed both contain the same number of zeros
482 * Furthermore, $j is always kept so that $j == $other_len or
483 * $other_changed[$j] == false.
485 while ( $j < $other_len && $other_changed[$j] ) {
489 while ( $i < $len && ! $changed[$i] ) {
490 assert( '$j < $other_len && ! $other_changed[$j]' );
492 while ( $j < $other_len && $other_changed[$j] )
502 // Find the end of this run of changes.
503 while ( ++
$i < $len && $changed[$i] ) {
509 * Record the length of this run of changes, so that
510 * we can later determine whether the run has grown.
512 $runlength = $i - $start;
515 * Move the changed region back, so long as the
516 * previous unchanged line matches the last changed one.
517 * This merges with previous changed regions.
519 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
520 $changed[--$start] = 1;
521 $changed[--$i] = false;
522 while ( $start > 0 && $changed[$start - 1] ) {
526 while ( $other_changed[--$j] ) {
529 assert( '$j >= 0 && !$other_changed[$j]' );
533 * Set CORRESPONDING to the end of the changed run, at the last
534 * point where it corresponds to a changed run in the other file.
535 * CORRESPONDING == LEN means no such point has been found.
537 $corresponding = $j < $other_len ?
$i : $len;
540 * Move the changed region forward, so long as the
541 * first changed line matches the following unchanged one.
542 * This merges with following changed regions.
543 * Do this second, so that if there are no merges,
544 * the changed region is moved forward as far as possible.
546 while ( $i < $len && $lines[$start] == $lines[$i] ) {
547 $changed[$start++
] = false;
549 while ( $i < $len && $changed[$i] ) {
553 assert( '$j < $other_len && ! $other_changed[$j]' );
555 if ( $j < $other_len && $other_changed[$j] ) {
557 while ( $j < $other_len && $other_changed[$j] ) {
562 } while ( $runlength != $i - $start );
565 * If possible, move the fully-merged run of changes
566 * back to a corresponding run in the other file.
568 while ( $corresponding < $i ) {
569 $changed[--$start] = 1;
572 while ( $other_changed[--$j] ) {
575 assert( '$j >= 0 && !$other_changed[$j]' );
578 wfProfileOut( __METHOD__
);
583 * Class representing a 'diff' between two sequences of strings.
586 * @ingroup DifferenceEngine
594 * Computes diff between sequences of strings.
596 * @param $from_lines array An array of strings.
597 * (Typically these are lines from a file.)
598 * @param $to_lines array An array of strings.
600 function __construct( $from_lines, $to_lines ) {
601 $eng = new _DiffEngine
;
602 $this->edits
= $eng->diff( $from_lines, $to_lines );
603 // $this->_check($from_lines, $to_lines);
607 * Compute reversed Diff.
611 * $diff = new Diff($lines1, $lines2);
612 * $rev = $diff->reverse();
613 * @return object A Diff object representing the inverse of the
616 function reverse () {
618 $rev->edits
= array();
619 foreach ( $this->edits
as $edit ) {
620 $rev->edits
[] = $edit->reverse();
626 * Check for empty diff.
628 * @return bool True iff two sequences were identical.
630 function isEmpty () {
631 foreach ( $this->edits
as $edit ) {
632 if ( $edit->type
!= 'copy' ) {
640 * Compute the length of the Longest Common Subsequence (LCS).
642 * This is mostly for diagnostic purposed.
644 * @return int The length of the LCS.
648 foreach ( $this->edits
as $edit ) {
649 if ( $edit->type
== 'copy' ) {
650 $lcs +
= sizeof( $edit->orig
);
657 * Get the original set of lines.
659 * This reconstructs the $from_lines parameter passed to the
662 * @return array The original sequence of strings.
667 foreach ( $this->edits
as $edit ) {
669 array_splice( $lines, sizeof( $lines ), 0, $edit->orig
);
676 * Get the closing set of lines.
678 * This reconstructs the $to_lines parameter passed to the
681 * @return array The sequence of strings.
686 foreach ( $this->edits
as $edit ) {
687 if ( $edit->closing
) {
688 array_splice( $lines, sizeof( $lines ), 0, $edit->closing
);
695 * Check a Diff for validity.
697 * This is here only for debugging purposes.
699 function _check ( $from_lines, $to_lines ) {
700 wfProfileIn( __METHOD__
);
701 if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
702 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR
);
704 if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
705 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR
);
708 $rev = $this->reverse();
709 if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
710 trigger_error( "Reversed original doesn't match", E_USER_ERROR
);
712 if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
713 trigger_error( "Reversed closing doesn't match", E_USER_ERROR
);
718 foreach ( $this->edits
as $edit ) {
719 if ( $prevtype == $edit->type
) {
720 trigger_error( "Edit sequence is non-optimal", E_USER_ERROR
);
722 $prevtype = $edit->type
;
726 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE
);
727 wfProfileOut( __METHOD__
);
732 * @todo document, bad name.
734 * @ingroup DifferenceEngine
736 class MappedDiff
extends Diff
741 * Computes diff between sequences of strings.
743 * This can be used to compute things like
744 * case-insensitve diffs, or diffs which ignore
745 * changes in white-space.
747 * @param $from_lines array An array of strings.
748 * (Typically these are lines from a file.)
750 * @param $to_lines array An array of strings.
752 * @param $mapped_from_lines array This array should
753 * have the same size number of elements as $from_lines.
754 * The elements in $mapped_from_lines and
755 * $mapped_to_lines are what is actually compared
756 * when computing the diff.
758 * @param $mapped_to_lines array This array should
759 * have the same number of elements as $to_lines.
761 function __construct( $from_lines, $to_lines,
762 $mapped_from_lines, $mapped_to_lines ) {
763 wfProfileIn( __METHOD__
);
765 assert( sizeof( $from_lines ) == sizeof( $mapped_from_lines ) );
766 assert( sizeof( $to_lines ) == sizeof( $mapped_to_lines ) );
768 parent
::__construct( $mapped_from_lines, $mapped_to_lines );
771 for ( $i = 0; $i < sizeof( $this->edits
); $i++
) {
772 $orig = &$this->edits
[$i]->orig
;
773 if ( is_array( $orig ) ) {
774 $orig = array_slice( $from_lines, $xi, sizeof( $orig ) );
775 $xi +
= sizeof( $orig );
778 $closing = &$this->edits
[$i]->closing
;
779 if ( is_array( $closing ) ) {
780 $closing = array_slice( $to_lines, $yi, sizeof( $closing ) );
781 $yi +
= sizeof( $closing );
784 wfProfileOut( __METHOD__
);
789 * A class to format Diffs
791 * This class formats the diff in classic diff format.
792 * It is intended that this class be customized via inheritance,
793 * to obtain fancier outputs.
796 * @ingroup DifferenceEngine
798 class DiffFormatter
{
800 * Number of leading context "lines" to preserve.
802 * This should be left at zero for this class, but subclasses
803 * may want to set this to other values.
805 var $leading_context_lines = 0;
808 * Number of trailing context "lines" to preserve.
810 * This should be left at zero for this class, but subclasses
811 * may want to set this to other values.
813 var $trailing_context_lines = 0;
818 * @param $diff Diff A Diff object.
819 * @return string The formatted output.
821 function format( $diff ) {
822 wfProfileIn( __METHOD__
);
828 $nlead = $this->leading_context_lines
;
829 $ntrail = $this->trailing_context_lines
;
831 $this->_start_diff();
833 foreach ( $diff->edits
as $edit ) {
834 if ( $edit->type
== 'copy' ) {
835 if ( is_array( $block ) ) {
836 if ( sizeof( $edit->orig
) <= $nlead +
$ntrail ) {
841 $context = array_slice( $edit->orig
, 0, $ntrail );
842 $block[] = new _DiffOp_Copy( $context );
844 $this->_block( $x0, $ntrail +
$xi - $x0,
845 $y0, $ntrail +
$yi - $y0,
850 $context = $edit->orig
;
852 if ( ! is_array( $block ) ) {
853 $context = array_slice( $context, sizeof( $context ) - $nlead );
854 $x0 = $xi - sizeof( $context );
855 $y0 = $yi - sizeof( $context );
858 $block[] = new _DiffOp_Copy( $context );
865 $xi +
= sizeof( $edit->orig
);
867 if ( $edit->closing
) {
868 $yi +
= sizeof( $edit->closing
);
872 if ( is_array( $block ) ) {
873 $this->_block( $x0, $xi - $x0,
878 $end = $this->_end_diff();
879 wfProfileOut( __METHOD__
);
883 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
884 wfProfileIn( __METHOD__
);
885 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
886 foreach ( $edits as $edit ) {
887 if ( $edit->type
== 'copy' ) {
888 $this->_context( $edit->orig
);
889 } elseif ( $edit->type
== 'add' ) {
890 $this->_added( $edit->closing
);
891 } elseif ( $edit->type
== 'delete' ) {
892 $this->_deleted( $edit->orig
);
893 } elseif ( $edit->type
== 'change' ) {
894 $this->_changed( $edit->orig
, $edit->closing
);
896 trigger_error( 'Unknown edit type', E_USER_ERROR
);
900 wfProfileOut( __METHOD__
);
903 function _start_diff() {
907 function _end_diff() {
908 $val = ob_get_contents();
913 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
915 $xbeg .= "," . ( $xbeg +
$xlen - 1 );
918 $ybeg .= "," . ( $ybeg +
$ylen - 1 );
921 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
924 function _start_block( $header ) {
928 function _end_block() {
931 function _lines( $lines, $prefix = ' ' ) {
932 foreach ( $lines as $line ) {
933 echo "$prefix $line\n";
937 function _context( $lines ) {
938 $this->_lines( $lines );
941 function _added( $lines ) {
942 $this->_lines( $lines, '>' );
944 function _deleted( $lines ) {
945 $this->_lines( $lines, '<' );
948 function _changed( $orig, $closing ) {
949 $this->_deleted( $orig );
951 $this->_added( $closing );
956 * A formatter that outputs unified diffs
957 * @ingroup DifferenceEngine
960 class UnifiedDiffFormatter
extends DiffFormatter
{
961 var $leading_context_lines = 2;
962 var $trailing_context_lines = 2;
964 function _added( $lines ) {
965 $this->_lines( $lines, '+' );
967 function _deleted( $lines ) {
968 $this->_lines( $lines, '-' );
970 function _changed( $orig, $closing ) {
971 $this->_deleted( $orig );
972 $this->_added( $closing );
974 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
975 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
980 * A pseudo-formatter that just passes along the Diff::$edits array
981 * @ingroup DifferenceEngine
983 class ArrayDiffFormatter
extends DiffFormatter
{
984 function format( $diff ) {
988 foreach ( $diff->edits
as $edit ) {
989 switch( $edit->type
) {
991 foreach ( $edit->closing
as $l ) {
995 'newline' => $newline++
1000 foreach ( $edit->orig
as $l ) {
1002 'action' => 'delete',
1004 'oldline' => $oldline++
,
1009 foreach ( $edit->orig
as $i => $l ) {
1011 'action' => 'change',
1013 'new' => @$edit->closing
[$i],
1014 'oldline' => $oldline++
,
1015 'newline' => $newline++
,
1020 $oldline +
= count( $edit->orig
);
1021 $newline +
= count( $edit->orig
);
1029 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1033 define( 'NBSP', ' ' ); // iso-8859-x non-breaking space.
1038 * @ingroup DifferenceEngine
1040 class _HWLDF_WordAccumulator
{
1041 function __construct () {
1042 $this->_lines
= array();
1048 function _flushGroup ( $new_tag ) {
1049 if ( $this->_group
!== '' ) {
1050 if ( $this->_tag
== 'ins' ) {
1051 $this->_line
.= '<ins class="diffchange diffchange-inline">' .
1052 htmlspecialchars ( $this->_group
) . '</ins>';
1053 } elseif ( $this->_tag
== 'del' ) {
1054 $this->_line
.= '<del class="diffchange diffchange-inline">' .
1055 htmlspecialchars ( $this->_group
) . '</del>';
1057 $this->_line
.= htmlspecialchars ( $this->_group
);
1061 $this->_tag
= $new_tag;
1064 function _flushLine ( $new_tag ) {
1065 $this->_flushGroup( $new_tag );
1066 if ( $this->_line
!= '' ) {
1067 array_push ( $this->_lines
, $this->_line
);
1069 # make empty lines visible by inserting an NBSP
1070 array_push ( $this->_lines
, NBSP
);
1075 function addWords ( $words, $tag = '' ) {
1076 if ( $tag != $this->_tag
) {
1077 $this->_flushGroup( $tag );
1080 foreach ( $words as $word ) {
1081 // new-line should only come as first char of word.
1082 if ( $word == '' ) {
1085 if ( $word[0] == "\n" ) {
1086 $this->_flushLine( $tag );
1087 $word = substr( $word, 1 );
1089 assert( !strstr( $word, "\n" ) );
1090 $this->_group
.= $word;
1094 function getLines() {
1095 $this->_flushLine( '~done' );
1096 return $this->_lines
;
1103 * @ingroup DifferenceEngine
1105 class WordLevelDiff
extends MappedDiff
{
1106 const MAX_LINE_LENGTH
= 10000;
1108 function __construct ( $orig_lines, $closing_lines ) {
1109 wfProfileIn( __METHOD__
);
1111 list ( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1112 list ( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1114 parent
::__construct( $orig_words, $closing_words,
1115 $orig_stripped, $closing_stripped );
1116 wfProfileOut( __METHOD__
);
1119 function _split( $lines ) {
1120 wfProfileIn( __METHOD__
);
1123 $stripped = array();
1125 foreach ( $lines as $line ) {
1126 # If the line is too long, just pretend the entire line is one big word
1127 # This prevents resource exhaustion problems
1134 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1136 $stripped[] = $line;
1139 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1142 $words = array_merge( $words, $m[0] );
1143 $stripped = array_merge( $stripped, $m[1] );
1147 wfProfileOut( __METHOD__
);
1148 return array( $words, $stripped );
1152 wfProfileIn( __METHOD__
);
1153 $orig = new _HWLDF_WordAccumulator
;
1155 foreach ( $this->edits
as $edit ) {
1156 if ( $edit->type
== 'copy' ) {
1157 $orig->addWords( $edit->orig
);
1158 } elseif ( $edit->orig
) {
1159 $orig->addWords( $edit->orig
, 'del' );
1162 $lines = $orig->getLines();
1163 wfProfileOut( __METHOD__
);
1167 function closing () {
1168 wfProfileIn( __METHOD__
);
1169 $closing = new _HWLDF_WordAccumulator
;
1171 foreach ( $this->edits
as $edit ) {
1172 if ( $edit->type
== 'copy' ) {
1173 $closing->addWords( $edit->closing
);
1174 } elseif ( $edit->closing
) {
1175 $closing->addWords( $edit->closing
, 'ins' );
1178 $lines = $closing->getLines();
1179 wfProfileOut( __METHOD__
);
1185 * Wikipedia Table style diff formatter.
1188 * @ingroup DifferenceEngine
1190 class TableDiffFormatter
extends DiffFormatter
{
1191 function __construct() {
1192 $this->leading_context_lines
= 2;
1193 $this->trailing_context_lines
= 2;
1196 public static function escapeWhiteSpace( $msg ) {
1197 $msg = preg_replace( '/^ /m', '  ', $msg );
1198 $msg = preg_replace( '/ $/m', '  ', $msg );
1199 $msg = preg_replace( '/ /', '  ', $msg );
1203 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1204 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1205 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1209 function _start_block( $header ) {
1213 function _end_block() {
1216 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1219 # HTML-escape parameter before calling this
1220 function addedLine( $line ) {
1221 return $this->wrapLine( '+', 'diff-addedline', $line );
1224 # HTML-escape parameter before calling this
1225 function deletedLine( $line ) {
1226 return $this->wrapLine( '−', 'diff-deletedline', $line );
1229 # HTML-escape parameter before calling this
1230 function contextLine( $line ) {
1231 return $this->wrapLine( ' ', 'diff-context', $line );
1234 private function wrapLine( $marker, $class, $line ) {
1235 if ( $line !== '' ) {
1236 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1237 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1239 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1242 function emptyLine() {
1243 return '<td colspan="2"> </td>';
1246 function _added( $lines ) {
1247 foreach ( $lines as $line ) {
1248 echo '<tr>' . $this->emptyLine() .
1249 $this->addedLine( '<ins class="diffchange">' .
1250 htmlspecialchars ( $line ) . '</ins>' ) . "</tr>\n";
1254 function _deleted( $lines ) {
1255 foreach ( $lines as $line ) {
1256 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1257 htmlspecialchars ( $line ) . '</del>' ) .
1258 $this->emptyLine() . "</tr>\n";
1262 function _context( $lines ) {
1263 foreach ( $lines as $line ) {
1265 $this->contextLine( htmlspecialchars ( $line ) ) .
1266 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1270 function _changed( $orig, $closing ) {
1271 wfProfileIn( __METHOD__
);
1273 $diff = new WordLevelDiff( $orig, $closing );
1274 $del = $diff->orig();
1275 $add = $diff->closing();
1277 # Notice that WordLevelDiff returns HTML-escaped output.
1278 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1280 while ( $line = array_shift( $del ) ) {
1281 $aline = array_shift( $add );
1282 echo '<tr>' . $this->deletedLine( $line ) .
1283 $this->addedLine( $aline ) . "</tr>\n";
1285 foreach ( $add as $line ) { # If any leftovers
1286 echo '<tr>' . $this->emptyLine() .
1287 $this->addedLine( $line ) . "</tr>\n";
1289 wfProfileOut( __METHOD__
);