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.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup DifferenceEngine
25 * @defgroup DifferenceEngine DifferenceEngine
31 * @ingroup DifferenceEngine
39 trigger_error( 'pure virtual', E_USER_ERROR
);
46 return $this->orig ?
count( $this->orig
) : 0;
53 return $this->closing ?
count( $this->closing
) : 0;
60 * @ingroup DifferenceEngine
62 class _DiffOp_Copy
extends _DiffOp
{
65 function __construct( $orig, $closing = false ) {
66 if ( !is_array( $closing ) ) {
70 $this->closing
= $closing;
74 * @return _DiffOp_Copy
77 return new _DiffOp_Copy( $this->closing
, $this->orig
);
84 * @ingroup DifferenceEngine
86 class _DiffOp_Delete
extends _DiffOp
{
89 function __construct( $lines ) {
91 $this->closing
= false;
98 return new _DiffOp_Add( $this->orig
);
105 * @ingroup DifferenceEngine
107 class _DiffOp_Add
extends _DiffOp
{
110 function __construct( $lines ) {
111 $this->closing
= $lines;
116 * @return _DiffOp_Delete
119 return new _DiffOp_Delete( $this->closing
);
126 * @ingroup DifferenceEngine
128 class _DiffOp_Change
extends _DiffOp
{
129 var $type = 'change';
131 function __construct( $orig, $closing ) {
133 $this->closing
= $closing;
137 * @return _DiffOp_Change
140 return new _DiffOp_Change( $this->closing
, $this->orig
);
145 * Class used internally by Diff to actually compute the diffs.
147 * The algorithm used here is mostly lifted from the perl module
148 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
149 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
151 * More ideas are taken from:
152 * http://www.ics.uci.edu/~eppstein/161/960229.html
154 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
155 * diffutils-2.7, which can be found at:
156 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
158 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
161 * Line length limits for robustness added by Tim Starling, 2005-08-31
162 * Alternative implementation added by Guy Van den Broeck, 2008-07-30
164 * @author Geoffrey T. Dairiki, Tim Starling, Guy Van den Broeck
166 * @ingroup DifferenceEngine
170 const MAX_XREF_LENGTH
= 10000;
172 protected $xchanged, $ychanged;
174 protected $xv = array(), $yv = array();
175 protected $xind = array(), $yind = array();
177 protected $seq = array(), $in_seq = array();
186 function diff( $from_lines, $to_lines ) {
187 wfProfileIn( __METHOD__
);
189 // Diff and store locally
190 $this->diff_local( $from_lines, $to_lines );
192 // Merge edits when possible
193 $this->_shift_boundaries( $from_lines, $this->xchanged
, $this->ychanged
);
194 $this->_shift_boundaries( $to_lines, $this->ychanged
, $this->xchanged
);
196 // Compute the edit operations.
197 $n_from = count( $from_lines );
198 $n_to = count( $to_lines );
202 while ( $xi < $n_from ||
$yi < $n_to ) {
203 assert( '$yi < $n_to || $this->xchanged[$xi]' );
204 assert( '$xi < $n_from || $this->ychanged[$yi]' );
206 // Skip matching "snake".
208 while ( $xi < $n_from && $yi < $n_to
209 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi] ) {
210 $copy[] = $from_lines[$xi++
];
214 $edits[] = new _DiffOp_Copy( $copy );
217 // Find deletes & adds.
219 while ( $xi < $n_from && $this->xchanged
[$xi] ) {
220 $delete[] = $from_lines[$xi++
];
224 while ( $yi < $n_to && $this->ychanged
[$yi] ) {
225 $add[] = $to_lines[$yi++
];
228 if ( $delete && $add ) {
229 $edits[] = new _DiffOp_Change( $delete, $add );
230 } elseif ( $delete ) {
231 $edits[] = new _DiffOp_Delete( $delete );
233 $edits[] = new _DiffOp_Add( $add );
236 wfProfileOut( __METHOD__
);
244 function diff_local( $from_lines, $to_lines ) {
245 global $wgExternalDiffEngine;
246 wfProfileIn( __METHOD__
);
248 if ( $wgExternalDiffEngine == 'wikidiff3' ) {
250 $wikidiff3 = new WikiDiff3();
251 $wikidiff3->diff( $from_lines, $to_lines );
252 $this->xchanged
= $wikidiff3->removed
;
253 $this->ychanged
= $wikidiff3->added
;
257 $n_from = count( $from_lines );
258 $n_to = count( $to_lines );
259 $this->xchanged
= $this->ychanged
= array();
260 $this->xv
= $this->yv
= array();
261 $this->xind
= $this->yind
= array();
263 unset( $this->in_seq
);
266 // Skip leading common lines.
267 for ( $skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
268 if ( $from_lines[$skip] !== $to_lines[$skip] ) {
271 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
273 // Skip trailing common lines.
274 $xi = $n_from; $yi = $n_to;
275 for ( $endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
276 if ( $from_lines[$xi] !== $to_lines[$yi] ) {
279 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
282 // Ignore lines which do not exist in both files.
283 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
284 $xhash[$this->_line_hash( $from_lines[$xi] )] = 1;
287 for ( $yi = $skip; $yi < $n_to - $endskip; $yi++
) {
288 $line = $to_lines[$yi];
289 if ( ( $this->ychanged
[$yi] = empty( $xhash[$this->_line_hash( $line )] ) ) ) {
292 $yhash[$this->_line_hash( $line )] = 1;
296 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
297 $line = $from_lines[$xi];
298 if ( ( $this->xchanged
[$xi] = empty( $yhash[$this->_line_hash( $line )] ) ) ) {
306 $this->_compareseq( 0, count( $this->xv
), 0, count( $this->yv
) );
308 wfProfileOut( __METHOD__
);
312 * Returns the whole line if it's small enough, or the MD5 hash otherwise
313 * @param $line string
316 function _line_hash( $line ) {
317 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
325 * Divide the Largest Common Subsequence (LCS) of the sequences
326 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
329 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
330 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
331 * sub sequences. The first sub-sequence is contained in [X0, X1),
332 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
333 * that (X0, Y0) == (XOFF, YOFF) and
334 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
336 * This function assumes that the first lines of the specified portions
337 * of the two files do not match, and likewise that the last lines do not
338 * match. The caller must trim matching lines from the beginning and end
339 * of the portions it is going to specify.
347 function _diag( $xoff, $xlim, $yoff, $ylim, $nchunks ) {
350 if ( $xlim - $xoff > $ylim - $yoff ) {
351 // Things seems faster (I'm not sure I understand why)
352 // when the shortest sequence in X.
354 list( $xoff, $xlim, $yoff, $ylim ) = array( $yoff, $ylim, $xoff, $xlim );
358 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
359 $ymatches[$this->xv
[$i]][] = $i;
362 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
363 $ymatches[$this->yv
[$i]][] = $i;
368 $this->seq
[0] = $yoff - 1;
369 $this->in_seq
= array();
372 $numer = $xlim - $xoff +
$nchunks - 1;
374 for ( $chunk = 0; $chunk < $nchunks; $chunk++
) {
376 for ( $i = 0; $i <= $this->lcs
; $i++
) {
377 $ymids[$i][$chunk -1] = $this->seq
[$i];
381 $x1 = $xoff +
(int)( ( $numer +
( $xlim -$xoff ) * $chunk ) / $nchunks );
382 for ( ; $x < $x1; $x++
) {
383 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
384 if ( empty( $ymatches[$line] ) ) {
387 $matches = $ymatches[$line];
389 while ( list( , $y ) = each( $matches ) ) {
390 if ( empty( $this->in_seq
[$y] ) ) {
391 $k = $this->_lcs_pos( $y );
393 $ymids[$k] = $ymids[$k -1];
397 while ( list ( , $y ) = each( $matches ) ) {
398 if ( $y > $this->seq
[$k -1] ) {
399 assert( '$y < $this->seq[$k]' );
400 // Optimization: this is a common case:
401 // next match is just replacing previous match.
402 $this->in_seq
[$this->seq
[$k]] = false;
404 $this->in_seq
[$y] = 1;
405 } elseif ( empty( $this->in_seq
[$y] ) ) {
406 $k = $this->_lcs_pos( $y );
408 $ymids[$k] = $ymids[$k -1];
414 $seps[] = $flip ?
array( $yoff, $xoff ) : array( $xoff, $yoff );
415 $ymid = $ymids[$this->lcs
];
416 for ( $n = 0; $n < $nchunks - 1; $n++
) {
417 $x1 = $xoff +
(int)( ( $numer +
( $xlim - $xoff ) * $n ) / $nchunks );
419 $seps[] = $flip ?
array( $y1, $x1 ) : array( $x1, $y1 );
421 $seps[] = $flip ?
array( $ylim, $xlim ) : array( $xlim, $ylim );
423 return array( $this->lcs
, $seps );
430 function _lcs_pos( $ypos ) {
432 if ( $end == 0 ||
$ypos > $this->seq
[$end] ) {
433 $this->seq
[++
$this->lcs
] = $ypos;
434 $this->in_seq
[$ypos] = 1;
439 while ( $beg < $end ) {
440 $mid = (int)( ( $beg +
$end ) / 2 );
441 if ( $ypos > $this->seq
[$mid] ) {
448 assert( '$ypos != $this->seq[$end]' );
450 $this->in_seq
[$this->seq
[$end]] = false;
451 $this->seq
[$end] = $ypos;
452 $this->in_seq
[$ypos] = 1;
457 * Find LCS of two sequences.
459 * The results are recorded in the vectors $this->{x,y}changed[], by
460 * storing a 1 in the element for each line that is an insertion
461 * or deletion (ie. is not in the LCS).
463 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
465 * Note that XLIM, YLIM are exclusive bounds.
466 * All line numbers are origin-0 and discarded lines are not counted.
472 function _compareseq( $xoff, $xlim, $yoff, $ylim ) {
473 // Slide down the bottom initial diagonal.
474 while ( $xoff < $xlim && $yoff < $ylim
475 && $this->xv
[$xoff] == $this->yv
[$yoff] ) {
480 // Slide up the top initial diagonal.
481 while ( $xlim > $xoff && $ylim > $yoff
482 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1] ) {
487 if ( $xoff == $xlim ||
$yoff == $ylim ) {
490 // This is ad hoc but seems to work well.
491 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
492 // $nchunks = max(2,min(8,(int)$nchunks));
493 $nchunks = min( 7, $xlim - $xoff, $ylim - $yoff ) +
1;
494 list ( $lcs, $seps ) = $this->_diag( $xoff, $xlim, $yoff, $ylim, $nchunks );
498 // X and Y sequences have no common subsequence:
500 while ( $yoff < $ylim ) {
501 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
503 while ( $xoff < $xlim ) {
504 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
507 // Use the partitions to split this problem into subproblems.
510 while ( $pt2 = next( $seps ) ) {
511 $this->_compareseq( $pt1[0], $pt2[0], $pt1[1], $pt2[1] );
518 * Adjust inserts/deletes of identical lines to join changes
519 * as much as possible.
521 * We do something when a run of changed lines include a
522 * line at one end and has an excluded, identical line at the other.
523 * We are free to choose which identical line is included.
524 * `compareseq' usually chooses the one at the beginning,
525 * but usually it is cleaner to consider the following identical line
526 * to be the "change".
528 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
530 function _shift_boundaries( $lines, &$changed, $other_changed ) {
531 wfProfileIn( __METHOD__
);
535 assert( 'count($lines) == count($changed)' );
536 $len = count( $lines );
537 $other_len = count( $other_changed );
541 * Scan forwards to find beginning of another run of changes.
542 * Also keep track of the corresponding point in the other file.
544 * Throughout this code, $i and $j are adjusted together so that
545 * the first $i elements of $changed and the first $j elements
546 * of $other_changed both contain the same number of zeros
548 * Furthermore, $j is always kept so that $j == $other_len or
549 * $other_changed[$j] == false.
551 while ( $j < $other_len && $other_changed[$j] ) {
555 while ( $i < $len && ! $changed[$i] ) {
556 assert( '$j < $other_len && ! $other_changed[$j]' );
558 while ( $j < $other_len && $other_changed[$j] )
568 // Find the end of this run of changes.
569 while ( ++
$i < $len && $changed[$i] ) {
575 * Record the length of this run of changes, so that
576 * we can later determine whether the run has grown.
578 $runlength = $i - $start;
581 * Move the changed region back, so long as the
582 * previous unchanged line matches the last changed one.
583 * This merges with previous changed regions.
585 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
586 $changed[--$start] = 1;
587 $changed[--$i] = false;
588 while ( $start > 0 && $changed[$start - 1] ) {
592 while ( $other_changed[--$j] ) {
595 assert( '$j >= 0 && !$other_changed[$j]' );
599 * Set CORRESPONDING to the end of the changed run, at the last
600 * point where it corresponds to a changed run in the other file.
601 * CORRESPONDING == LEN means no such point has been found.
603 $corresponding = $j < $other_len ?
$i : $len;
606 * Move the changed region forward, so long as the
607 * first changed line matches the following unchanged one.
608 * This merges with following changed regions.
609 * Do this second, so that if there are no merges,
610 * the changed region is moved forward as far as possible.
612 while ( $i < $len && $lines[$start] == $lines[$i] ) {
613 $changed[$start++
] = false;
615 while ( $i < $len && $changed[$i] ) {
619 assert( '$j < $other_len && ! $other_changed[$j]' );
621 if ( $j < $other_len && $other_changed[$j] ) {
623 while ( $j < $other_len && $other_changed[$j] ) {
628 } while ( $runlength != $i - $start );
631 * If possible, move the fully-merged run of changes
632 * back to a corresponding run in the other file.
634 while ( $corresponding < $i ) {
635 $changed[--$start] = 1;
638 while ( $other_changed[--$j] ) {
641 assert( '$j >= 0 && !$other_changed[$j]' );
644 wfProfileOut( __METHOD__
);
649 * Class representing a 'diff' between two sequences of strings.
652 * @ingroup DifferenceEngine
659 * Computes diff between sequences of strings.
661 * @param $from_lines array An array of strings.
662 * (Typically these are lines from a file.)
663 * @param $to_lines array An array of strings.
665 function __construct( $from_lines, $to_lines ) {
666 $eng = new _DiffEngine
;
667 $this->edits
= $eng->diff( $from_lines, $to_lines );
668 // $this->_check($from_lines, $to_lines);
672 * Compute reversed Diff.
676 * $diff = new Diff($lines1, $lines2);
677 * $rev = $diff->reverse();
678 * @return Object A Diff object representing the inverse of the
683 $rev->edits
= array();
684 foreach ( $this->edits
as $edit ) {
685 $rev->edits
[] = $edit->reverse();
691 * Check for empty diff.
693 * @return bool True iff two sequences were identical.
696 foreach ( $this->edits
as $edit ) {
697 if ( $edit->type
!= 'copy' ) {
705 * Compute the length of the Longest Common Subsequence (LCS).
707 * This is mostly for diagnostic purposed.
709 * @return int The length of the LCS.
713 foreach ( $this->edits
as $edit ) {
714 if ( $edit->type
== 'copy' ) {
715 $lcs +
= count( $edit->orig
);
722 * Get the original set of lines.
724 * This reconstructs the $from_lines parameter passed to the
727 * @return array The original sequence of strings.
732 foreach ( $this->edits
as $edit ) {
734 array_splice( $lines, count( $lines ), 0, $edit->orig
);
741 * Get the closing set of lines.
743 * This reconstructs the $to_lines parameter passed to the
746 * @return array The sequence of strings.
751 foreach ( $this->edits
as $edit ) {
752 if ( $edit->closing
) {
753 array_splice( $lines, count( $lines ), 0, $edit->closing
);
760 * Check a Diff for validity.
762 * This is here only for debugging purposes.
766 function _check( $from_lines, $to_lines ) {
767 wfProfileIn( __METHOD__
);
768 if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
769 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR
);
771 if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
772 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR
);
775 $rev = $this->reverse();
776 if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
777 trigger_error( "Reversed original doesn't match", E_USER_ERROR
);
779 if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
780 trigger_error( "Reversed closing doesn't match", E_USER_ERROR
);
784 foreach ( $this->edits
as $edit ) {
785 if ( $prevtype == $edit->type
) {
786 trigger_error( 'Edit sequence is non-optimal', E_USER_ERROR
);
788 $prevtype = $edit->type
;
792 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE
);
793 wfProfileOut( __METHOD__
);
798 * @todo document, bad name.
800 * @ingroup DifferenceEngine
802 class MappedDiff
extends Diff
{
806 * Computes diff between sequences of strings.
808 * This can be used to compute things like
809 * case-insensitve diffs, or diffs which ignore
810 * changes in white-space.
812 * @param $from_lines array An array of strings.
813 * (Typically these are lines from a file.)
815 * @param $to_lines array An array of strings.
817 * @param $mapped_from_lines array This array should
818 * have the same size number of elements as $from_lines.
819 * The elements in $mapped_from_lines and
820 * $mapped_to_lines are what is actually compared
821 * when computing the diff.
823 * @param $mapped_to_lines array This array should
824 * have the same number of elements as $to_lines.
826 function __construct( $from_lines, $to_lines,
827 $mapped_from_lines, $mapped_to_lines ) {
828 wfProfileIn( __METHOD__
);
830 assert( 'count( $from_lines ) == count( $mapped_from_lines )' );
831 assert( 'count( $to_lines ) == count( $mapped_to_lines )' );
833 parent
::__construct( $mapped_from_lines, $mapped_to_lines );
836 for ( $i = 0; $i < count( $this->edits
); $i++
) {
837 $orig = &$this->edits
[$i]->orig
;
838 if ( is_array( $orig ) ) {
839 $orig = array_slice( $from_lines, $xi, count( $orig ) );
840 $xi +
= count( $orig );
843 $closing = &$this->edits
[$i]->closing
;
844 if ( is_array( $closing ) ) {
845 $closing = array_slice( $to_lines, $yi, count( $closing ) );
846 $yi +
= count( $closing );
849 wfProfileOut( __METHOD__
);
854 * A class to format Diffs
856 * This class formats the diff in classic diff format.
857 * It is intended that this class be customized via inheritance,
858 * to obtain fancier outputs.
861 * @ingroup DifferenceEngine
863 class DiffFormatter
{
865 * Number of leading context "lines" to preserve.
867 * This should be left at zero for this class, but subclasses
868 * may want to set this to other values.
870 var $leading_context_lines = 0;
873 * Number of trailing context "lines" to preserve.
875 * This should be left at zero for this class, but subclasses
876 * may want to set this to other values.
878 var $trailing_context_lines = 0;
883 * @param $diff Diff A Diff object.
884 * @return string The formatted output.
886 function format( $diff ) {
887 wfProfileIn( __METHOD__
);
893 $nlead = $this->leading_context_lines
;
894 $ntrail = $this->trailing_context_lines
;
896 $this->_start_diff();
898 foreach ( $diff->edits
as $edit ) {
899 if ( $edit->type
== 'copy' ) {
900 if ( is_array( $block ) ) {
901 if ( count( $edit->orig
) <= $nlead +
$ntrail ) {
905 $context = array_slice( $edit->orig
, 0, $ntrail );
906 $block[] = new _DiffOp_Copy( $context );
908 $this->_block( $x0, $ntrail +
$xi - $x0,
909 $y0, $ntrail +
$yi - $y0,
914 $context = $edit->orig
;
916 if ( !is_array( $block ) ) {
917 $context = array_slice( $context, count( $context ) - $nlead );
918 $x0 = $xi - count( $context );
919 $y0 = $yi - count( $context );
922 $block[] = new _DiffOp_Copy( $context );
929 $xi +
= count( $edit->orig
);
931 if ( $edit->closing
) {
932 $yi +
= count( $edit->closing
);
936 if ( is_array( $block ) ) {
937 $this->_block( $x0, $xi - $x0,
942 $end = $this->_end_diff();
943 wfProfileOut( __METHOD__
);
954 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
955 wfProfileIn( __METHOD__
);
956 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
957 foreach ( $edits as $edit ) {
958 if ( $edit->type
== 'copy' ) {
959 $this->_context( $edit->orig
);
960 } elseif ( $edit->type
== 'add' ) {
961 $this->_added( $edit->closing
);
962 } elseif ( $edit->type
== 'delete' ) {
963 $this->_deleted( $edit->orig
);
964 } elseif ( $edit->type
== 'change' ) {
965 $this->_changed( $edit->orig
, $edit->closing
);
967 trigger_error( 'Unknown edit type', E_USER_ERROR
);
971 wfProfileOut( __METHOD__
);
974 function _start_diff() {
981 function _end_diff() {
982 $val = ob_get_contents();
994 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
996 $xbeg .= ',' . ( $xbeg +
$xlen - 1 );
999 $ybeg .= ',' . ( $ybeg +
$ylen - 1 );
1002 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
1005 function _start_block( $header ) {
1006 echo $header . "\n";
1009 function _end_block() {
1014 * @param $prefix string
1016 function _lines( $lines, $prefix = ' ' ) {
1017 foreach ( $lines as $line ) {
1018 echo "$prefix $line\n";
1025 function _context( $lines ) {
1026 $this->_lines( $lines );
1032 function _added( $lines ) {
1033 $this->_lines( $lines, '>' );
1039 function _deleted( $lines ) {
1040 $this->_lines( $lines, '<' );
1047 function _changed( $orig, $closing ) {
1048 $this->_deleted( $orig );
1050 $this->_added( $closing );
1055 * A formatter that outputs unified diffs
1056 * @ingroup DifferenceEngine
1058 class UnifiedDiffFormatter
extends DiffFormatter
{
1059 var $leading_context_lines = 2;
1060 var $trailing_context_lines = 2;
1065 function _added( $lines ) {
1066 $this->_lines( $lines, '+' );
1072 function _deleted( $lines ) {
1073 $this->_lines( $lines, '-' );
1080 function _changed( $orig, $closing ) {
1081 $this->_deleted( $orig );
1082 $this->_added( $closing );
1092 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1093 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
1098 * A pseudo-formatter that just passes along the Diff::$edits array
1099 * @ingroup DifferenceEngine
1101 class ArrayDiffFormatter
extends DiffFormatter
{
1107 function format( $diff ) {
1111 foreach ( $diff->edits
as $edit ) {
1112 switch( $edit->type
) {
1114 foreach ( $edit->closing
as $l ) {
1118 'newline' => $newline++
1123 foreach ( $edit->orig
as $l ) {
1125 'action' => 'delete',
1127 'oldline' => $oldline++
,
1132 foreach ( $edit->orig
as $i => $l ) {
1134 'action' => 'change',
1136 'new' => isset( $edit->closing
[$i] ) ?
$edit->closing
[$i] : null,
1137 'oldline' => $oldline++
,
1138 'newline' => $newline++
,
1143 $oldline +
= count( $edit->orig
);
1144 $newline +
= count( $edit->orig
);
1152 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1155 define( 'NBSP', ' ' ); // iso-8859-x non-breaking space.
1160 * @ingroup DifferenceEngine
1162 class _HWLDF_WordAccumulator
{
1163 function __construct() {
1164 $this->_lines
= array();
1173 function _flushGroup( $new_tag ) {
1174 if ( $this->_group
!== '' ) {
1175 if ( $this->_tag
== 'ins' ) {
1176 $this->_line
.= '<ins class="diffchange diffchange-inline">' .
1177 htmlspecialchars( $this->_group
) . '</ins>';
1178 } elseif ( $this->_tag
== 'del' ) {
1179 $this->_line
.= '<del class="diffchange diffchange-inline">' .
1180 htmlspecialchars( $this->_group
) . '</del>';
1182 $this->_line
.= htmlspecialchars( $this->_group
);
1186 $this->_tag
= $new_tag;
1192 function _flushLine( $new_tag ) {
1193 $this->_flushGroup( $new_tag );
1194 if ( $this->_line
!= '' ) {
1195 array_push( $this->_lines
, $this->_line
);
1197 # make empty lines visible by inserting an NBSP
1198 array_push( $this->_lines
, NBSP
);
1205 * @param $tag string
1207 function addWords( $words, $tag = '' ) {
1208 if ( $tag != $this->_tag
) {
1209 $this->_flushGroup( $tag );
1212 foreach ( $words as $word ) {
1213 // new-line should only come as first char of word.
1214 if ( $word == '' ) {
1217 if ( $word[0] == "\n" ) {
1218 $this->_flushLine( $tag );
1219 $word = substr( $word, 1 );
1221 assert( '!strstr( $word, "\n" )' );
1222 $this->_group
.= $word;
1229 function getLines() {
1230 $this->_flushLine( '~done' );
1231 return $this->_lines
;
1238 * @ingroup DifferenceEngine
1240 class WordLevelDiff
extends MappedDiff
{
1241 const MAX_LINE_LENGTH
= 10000;
1244 * @param $orig_lines
1245 * @param $closing_lines
1247 function __construct( $orig_lines, $closing_lines ) {
1248 wfProfileIn( __METHOD__
);
1250 list( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1251 list( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1253 parent
::__construct( $orig_words, $closing_words,
1254 $orig_stripped, $closing_stripped );
1255 wfProfileOut( __METHOD__
);
1262 function _split( $lines ) {
1263 wfProfileIn( __METHOD__
);
1266 $stripped = array();
1268 foreach ( $lines as $line ) {
1269 # If the line is too long, just pretend the entire line is one big word
1270 # This prevents resource exhaustion problems
1277 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1279 $stripped[] = $line;
1282 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1285 $words = array_merge( $words, $m[0] );
1286 $stripped = array_merge( $stripped, $m[1] );
1290 wfProfileOut( __METHOD__
);
1291 return array( $words, $stripped );
1298 wfProfileIn( __METHOD__
);
1299 $orig = new _HWLDF_WordAccumulator
;
1301 foreach ( $this->edits
as $edit ) {
1302 if ( $edit->type
== 'copy' ) {
1303 $orig->addWords( $edit->orig
);
1304 } elseif ( $edit->orig
) {
1305 $orig->addWords( $edit->orig
, 'del' );
1308 $lines = $orig->getLines();
1309 wfProfileOut( __METHOD__
);
1316 function closing() {
1317 wfProfileIn( __METHOD__
);
1318 $closing = new _HWLDF_WordAccumulator
;
1320 foreach ( $this->edits
as $edit ) {
1321 if ( $edit->type
== 'copy' ) {
1322 $closing->addWords( $edit->closing
);
1323 } elseif ( $edit->closing
) {
1324 $closing->addWords( $edit->closing
, 'ins' );
1327 $lines = $closing->getLines();
1328 wfProfileOut( __METHOD__
);
1334 * Wikipedia Table style diff formatter.
1337 * @ingroup DifferenceEngine
1339 class TableDiffFormatter
extends DiffFormatter
{
1340 function __construct() {
1341 $this->leading_context_lines
= 2;
1342 $this->trailing_context_lines
= 2;
1350 public static function escapeWhiteSpace( $msg ) {
1351 $msg = preg_replace( '/^ /m', '  ', $msg );
1352 $msg = preg_replace( '/ $/m', '  ', $msg );
1353 $msg = preg_replace( '/ /', '  ', $msg );
1364 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1365 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1366 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1373 function _start_block( $header ) {
1377 function _end_block() {
1380 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1384 * HTML-escape parameter before calling this
1388 function addedLine( $line ) {
1389 return $this->wrapLine( '+', 'diff-addedline', $line );
1393 * HTML-escape parameter before calling this
1397 function deletedLine( $line ) {
1398 return $this->wrapLine( '−', 'diff-deletedline', $line );
1402 * HTML-escape parameter before calling this
1406 function contextLine( $line ) {
1407 return $this->wrapLine( ' ', 'diff-context', $line );
1416 private function wrapLine( $marker, $class, $line ) {
1417 if ( $line !== '' ) {
1418 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1419 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1421 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1427 function emptyLine() {
1428 return '<td colspan="2"> </td>';
1432 * @param $lines array
1434 function _added( $lines ) {
1435 foreach ( $lines as $line ) {
1436 echo '<tr>' . $this->emptyLine() .
1437 $this->addedLine( '<ins class="diffchange">' .
1438 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
1445 function _deleted( $lines ) {
1446 foreach ( $lines as $line ) {
1447 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1448 htmlspecialchars( $line ) . '</del>' ) .
1449 $this->emptyLine() . "</tr>\n";
1456 function _context( $lines ) {
1457 foreach ( $lines as $line ) {
1459 $this->contextLine( htmlspecialchars( $line ) ) .
1460 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
1468 function _changed( $orig, $closing ) {
1469 wfProfileIn( __METHOD__
);
1471 $diff = new WordLevelDiff( $orig, $closing );
1472 $del = $diff->orig();
1473 $add = $diff->closing();
1475 # Notice that WordLevelDiff returns HTML-escaped output.
1476 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1478 while ( $line = array_shift( $del ) ) {
1479 $aline = array_shift( $add );
1480 echo '<tr>' . $this->deletedLine( $line ) .
1481 $this->addedLine( $aline ) . "</tr>\n";
1483 foreach ( $add as $line ) { # If any leftovers
1484 echo '<tr>' . $this->emptyLine() .
1485 $this->addedLine( $line ) . "</tr>\n";
1487 wfProfileOut( __METHOD__
);