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
);
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
{
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
{
74 function __construct( $lines ) {
76 $this->closing
= false;
83 return new _DiffOp_Add( $this->orig
);
90 * @ingroup DifferenceEngine
92 class _DiffOp_Add
extends _DiffOp
{
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
{
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
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
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.
651 function __construct( $from_lines, $to_lines ) {
652 $eng = new _DiffEngine
;
653 $this->edits
= $eng->diff( $from_lines, $to_lines );
654 // $this->_check($from_lines, $to_lines);
658 * Compute reversed Diff.
662 * $diff = new Diff($lines1, $lines2);
663 * $rev = $diff->reverse();
664 * @return Object A Diff object representing the inverse of the
669 $rev->edits
= array();
670 foreach ( $this->edits
as $edit ) {
671 $rev->edits
[] = $edit->reverse();
677 * Check for empty diff.
679 * @return bool True iff two sequences were identical.
682 foreach ( $this->edits
as $edit ) {
683 if ( $edit->type
!= 'copy' ) {
691 * Compute the length of the Longest Common Subsequence (LCS).
693 * This is mostly for diagnostic purposed.
695 * @return int The length of the LCS.
699 foreach ( $this->edits
as $edit ) {
700 if ( $edit->type
== 'copy' ) {
701 $lcs +
= sizeof( $edit->orig
);
708 * Get the original set of lines.
710 * This reconstructs the $from_lines parameter passed to the
713 * @return array The original sequence of strings.
718 foreach ( $this->edits
as $edit ) {
720 array_splice( $lines, sizeof( $lines ), 0, $edit->orig
);
727 * Get the closing set of lines.
729 * This reconstructs the $to_lines parameter passed to the
732 * @return array The sequence of strings.
737 foreach ( $this->edits
as $edit ) {
738 if ( $edit->closing
) {
739 array_splice( $lines, sizeof( $lines ), 0, $edit->closing
);
746 * Check a Diff for validity.
748 * This is here only for debugging purposes.
752 function _check( $from_lines, $to_lines ) {
753 wfProfileIn( __METHOD__
);
754 if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
755 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR
);
757 if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
758 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR
);
761 $rev = $this->reverse();
762 if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
763 trigger_error( "Reversed original doesn't match", E_USER_ERROR
);
765 if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
766 trigger_error( "Reversed closing doesn't match", E_USER_ERROR
);
771 foreach ( $this->edits
as $edit ) {
772 if ( $prevtype == $edit->type
) {
773 trigger_error( 'Edit sequence is non-optimal', E_USER_ERROR
);
775 $prevtype = $edit->type
;
779 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE
);
780 wfProfileOut( __METHOD__
);
785 * @todo document, bad name.
787 * @ingroup DifferenceEngine
789 class MappedDiff
extends Diff
{
793 * Computes diff between sequences of strings.
795 * This can be used to compute things like
796 * case-insensitve diffs, or diffs which ignore
797 * changes in white-space.
799 * @param $from_lines array An array of strings.
800 * (Typically these are lines from a file.)
802 * @param $to_lines array An array of strings.
804 * @param $mapped_from_lines array This array should
805 * have the same size number of elements as $from_lines.
806 * The elements in $mapped_from_lines and
807 * $mapped_to_lines are what is actually compared
808 * when computing the diff.
810 * @param $mapped_to_lines array This array should
811 * have the same number of elements as $to_lines.
813 function __construct( $from_lines, $to_lines,
814 $mapped_from_lines, $mapped_to_lines ) {
815 wfProfileIn( __METHOD__
);
817 assert( 'sizeof( $from_lines ) == sizeof( $mapped_from_lines )' );
818 assert( 'sizeof( $to_lines ) == sizeof( $mapped_to_lines )' );
820 parent
::__construct( $mapped_from_lines, $mapped_to_lines );
823 for ( $i = 0; $i < sizeof( $this->edits
); $i++
) {
824 $orig = &$this->edits
[$i]->orig
;
825 if ( is_array( $orig ) ) {
826 $orig = array_slice( $from_lines, $xi, sizeof( $orig ) );
827 $xi +
= sizeof( $orig );
830 $closing = &$this->edits
[$i]->closing
;
831 if ( is_array( $closing ) ) {
832 $closing = array_slice( $to_lines, $yi, sizeof( $closing ) );
833 $yi +
= sizeof( $closing );
836 wfProfileOut( __METHOD__
);
841 * A class to format Diffs
843 * This class formats the diff in classic diff format.
844 * It is intended that this class be customized via inheritance,
845 * to obtain fancier outputs.
848 * @ingroup DifferenceEngine
850 class DiffFormatter
{
852 * Number of leading context "lines" to preserve.
854 * This should be left at zero for this class, but subclasses
855 * may want to set this to other values.
857 var $leading_context_lines = 0;
860 * Number of trailing context "lines" to preserve.
862 * This should be left at zero for this class, but subclasses
863 * may want to set this to other values.
865 var $trailing_context_lines = 0;
870 * @param $diff Diff A Diff object.
871 * @return string The formatted output.
873 function format( $diff ) {
874 wfProfileIn( __METHOD__
);
880 $nlead = $this->leading_context_lines
;
881 $ntrail = $this->trailing_context_lines
;
883 $this->_start_diff();
885 foreach ( $diff->edits
as $edit ) {
886 if ( $edit->type
== 'copy' ) {
887 if ( is_array( $block ) ) {
888 if ( sizeof( $edit->orig
) <= $nlead +
$ntrail ) {
892 $context = array_slice( $edit->orig
, 0, $ntrail );
893 $block[] = new _DiffOp_Copy( $context );
895 $this->_block( $x0, $ntrail +
$xi - $x0,
896 $y0, $ntrail +
$yi - $y0,
901 $context = $edit->orig
;
903 if ( !is_array( $block ) ) {
904 $context = array_slice( $context, sizeof( $context ) - $nlead );
905 $x0 = $xi - sizeof( $context );
906 $y0 = $yi - sizeof( $context );
909 $block[] = new _DiffOp_Copy( $context );
916 $xi +
= sizeof( $edit->orig
);
918 if ( $edit->closing
) {
919 $yi +
= sizeof( $edit->closing
);
923 if ( is_array( $block ) ) {
924 $this->_block( $x0, $xi - $x0,
929 $end = $this->_end_diff();
930 wfProfileOut( __METHOD__
);
941 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
942 wfProfileIn( __METHOD__
);
943 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
944 foreach ( $edits as $edit ) {
945 if ( $edit->type
== 'copy' ) {
946 $this->_context( $edit->orig
);
947 } elseif ( $edit->type
== 'add' ) {
948 $this->_added( $edit->closing
);
949 } elseif ( $edit->type
== 'delete' ) {
950 $this->_deleted( $edit->orig
);
951 } elseif ( $edit->type
== 'change' ) {
952 $this->_changed( $edit->orig
, $edit->closing
);
954 trigger_error( 'Unknown edit type', E_USER_ERROR
);
958 wfProfileOut( __METHOD__
);
961 function _start_diff() {
968 function _end_diff() {
969 $val = ob_get_contents();
981 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
983 $xbeg .= ',' . ( $xbeg +
$xlen - 1 );
986 $ybeg .= ',' . ( $ybeg +
$ylen - 1 );
989 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
992 function _start_block( $header ) {
996 function _end_block() {
1001 * @param $prefix string
1003 function _lines( $lines, $prefix = ' ' ) {
1004 foreach ( $lines as $line ) {
1005 echo "$prefix $line\n";
1012 function _context( $lines ) {
1013 $this->_lines( $lines );
1019 function _added( $lines ) {
1020 $this->_lines( $lines, '>' );
1026 function _deleted( $lines ) {
1027 $this->_lines( $lines, '<' );
1034 function _changed( $orig, $closing ) {
1035 $this->_deleted( $orig );
1037 $this->_added( $closing );
1042 * A formatter that outputs unified diffs
1043 * @ingroup DifferenceEngine
1045 class UnifiedDiffFormatter
extends DiffFormatter
{
1046 var $leading_context_lines = 2;
1047 var $trailing_context_lines = 2;
1052 function _added( $lines ) {
1053 $this->_lines( $lines, '+' );
1059 function _deleted( $lines ) {
1060 $this->_lines( $lines, '-' );
1067 function _changed( $orig, $closing ) {
1068 $this->_deleted( $orig );
1069 $this->_added( $closing );
1079 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1080 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
1085 * A pseudo-formatter that just passes along the Diff::$edits array
1086 * @ingroup DifferenceEngine
1088 class ArrayDiffFormatter
extends DiffFormatter
{
1094 function format( $diff ) {
1098 foreach ( $diff->edits
as $edit ) {
1099 switch( $edit->type
) {
1101 foreach ( $edit->closing
as $l ) {
1105 'newline' => $newline++
1110 foreach ( $edit->orig
as $l ) {
1112 'action' => 'delete',
1114 'oldline' => $oldline++
,
1119 foreach ( $edit->orig
as $i => $l ) {
1121 'action' => 'change',
1123 'new' => isset( $edit->closing
[$i] ) ?
$edit->closing
[$i] : null,
1124 'oldline' => $oldline++
,
1125 'newline' => $newline++
,
1130 $oldline +
= count( $edit->orig
);
1131 $newline +
= count( $edit->orig
);
1139 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1142 define( 'NBSP', ' ' ); // iso-8859-x non-breaking space.
1147 * @ingroup DifferenceEngine
1149 class _HWLDF_WordAccumulator
{
1150 function __construct() {
1151 $this->_lines
= array();
1160 function _flushGroup( $new_tag ) {
1161 if ( $this->_group
!== '' ) {
1162 if ( $this->_tag
== 'ins' ) {
1163 $this->_line
.= '<ins class="diffchange diffchange-inline">' .
1164 htmlspecialchars( $this->_group
) . '</ins>';
1165 } elseif ( $this->_tag
== 'del' ) {
1166 $this->_line
.= '<del class="diffchange diffchange-inline">' .
1167 htmlspecialchars( $this->_group
) . '</del>';
1169 $this->_line
.= htmlspecialchars( $this->_group
);
1173 $this->_tag
= $new_tag;
1179 function _flushLine( $new_tag ) {
1180 $this->_flushGroup( $new_tag );
1181 if ( $this->_line
!= '' ) {
1182 array_push( $this->_lines
, $this->_line
);
1184 # make empty lines visible by inserting an NBSP
1185 array_push( $this->_lines
, NBSP
);
1192 * @param $tag string
1194 function addWords ( $words, $tag = '' ) {
1195 if ( $tag != $this->_tag
) {
1196 $this->_flushGroup( $tag );
1199 foreach ( $words as $word ) {
1200 // new-line should only come as first char of word.
1201 if ( $word == '' ) {
1204 if ( $word[0] == "\n" ) {
1205 $this->_flushLine( $tag );
1206 $word = substr( $word, 1 );
1208 assert( '!strstr( $word, "\n" )' );
1209 $this->_group
.= $word;
1216 function getLines() {
1217 $this->_flushLine( '~done' );
1218 return $this->_lines
;
1225 * @ingroup DifferenceEngine
1227 class WordLevelDiff
extends MappedDiff
{
1228 const MAX_LINE_LENGTH
= 10000;
1231 * @param $orig_lines
1232 * @param $closing_lines
1234 function __construct ( $orig_lines, $closing_lines ) {
1235 wfProfileIn( __METHOD__
);
1237 list( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1238 list( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1240 parent
::__construct( $orig_words, $closing_words,
1241 $orig_stripped, $closing_stripped );
1242 wfProfileOut( __METHOD__
);
1249 function _split( $lines ) {
1250 wfProfileIn( __METHOD__
);
1253 $stripped = array();
1255 foreach ( $lines as $line ) {
1256 # If the line is too long, just pretend the entire line is one big word
1257 # This prevents resource exhaustion problems
1264 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1266 $stripped[] = $line;
1269 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1272 $words = array_merge( $words, $m[0] );
1273 $stripped = array_merge( $stripped, $m[1] );
1277 wfProfileOut( __METHOD__
);
1278 return array( $words, $stripped );
1285 wfProfileIn( __METHOD__
);
1286 $orig = new _HWLDF_WordAccumulator
;
1288 foreach ( $this->edits
as $edit ) {
1289 if ( $edit->type
== 'copy' ) {
1290 $orig->addWords( $edit->orig
);
1291 } elseif ( $edit->orig
) {
1292 $orig->addWords( $edit->orig
, 'del' );
1295 $lines = $orig->getLines();
1296 wfProfileOut( __METHOD__
);
1303 function closing() {
1304 wfProfileIn( __METHOD__
);
1305 $closing = new _HWLDF_WordAccumulator
;
1307 foreach ( $this->edits
as $edit ) {
1308 if ( $edit->type
== 'copy' ) {
1309 $closing->addWords( $edit->closing
);
1310 } elseif ( $edit->closing
) {
1311 $closing->addWords( $edit->closing
, 'ins' );
1314 $lines = $closing->getLines();
1315 wfProfileOut( __METHOD__
);
1321 * Wikipedia Table style diff formatter.
1324 * @ingroup DifferenceEngine
1326 class TableDiffFormatter
extends DiffFormatter
{
1327 function __construct() {
1328 $this->leading_context_lines
= 2;
1329 $this->trailing_context_lines
= 2;
1337 public static function escapeWhiteSpace( $msg ) {
1338 $msg = preg_replace( '/^ /m', '  ', $msg );
1339 $msg = preg_replace( '/ $/m', '  ', $msg );
1340 $msg = preg_replace( '/ /', '  ', $msg );
1351 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1352 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1353 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1360 function _start_block( $header ) {
1364 function _end_block() {
1367 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1371 * HTML-escape parameter before calling this
1375 function addedLine( $line ) {
1376 return $this->wrapLine( '+', 'diff-addedline', $line );
1380 * HTML-escape parameter before calling this
1384 function deletedLine( $line ) {
1385 return $this->wrapLine( '−', 'diff-deletedline', $line );
1389 * HTML-escape parameter before calling this
1393 function contextLine( $line ) {
1394 return $this->wrapLine( ' ', 'diff-context', $line );
1403 private function wrapLine( $marker, $class, $line ) {
1404 if ( $line !== '' ) {
1405 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1406 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1408 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1414 function emptyLine() {
1415 return '<td colspan="2"> </td>';
1419 * @param $lines array
1421 function _added( $lines ) {
1422 foreach ( $lines as $line ) {
1423 echo '<tr>' . $this->emptyLine() .
1424 $this->addedLine( '<ins class="diffchange">' .
1425 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
1432 function _deleted( $lines ) {
1433 foreach ( $lines as $line ) {
1434 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1435 htmlspecialchars( $line ) . '</del>' ) .
1436 $this->emptyLine() . "</tr>\n";
1443 function _context( $lines ) {
1444 foreach ( $lines as $line ) {
1446 $this->contextLine( htmlspecialchars( $line ) ) .
1447 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
1455 function _changed( $orig, $closing ) {
1456 wfProfileIn( __METHOD__
);
1458 $diff = new WordLevelDiff( $orig, $closing );
1459 $del = $diff->orig();
1460 $add = $diff->closing();
1462 # Notice that WordLevelDiff returns HTML-escaped output.
1463 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1465 while ( $line = array_shift( $del ) ) {
1466 $aline = array_shift( $add );
1467 echo '<tr>' . $this->deletedLine( $line ) .
1468 $this->addedLine( $aline ) . "</tr>\n";
1470 foreach ( $add as $line ) { # If any leftovers
1471 echo '<tr>' . $this->emptyLine() .
1472 $this->addedLine( $line ) . "</tr>\n";
1474 wfProfileOut( __METHOD__
);