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
);
785 foreach ( $this->edits
as $edit ) {
786 if ( $prevtype == $edit->type
) {
787 trigger_error( 'Edit sequence is non-optimal', E_USER_ERROR
);
789 $prevtype = $edit->type
;
793 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE
);
794 wfProfileOut( __METHOD__
);
799 * @todo document, bad name.
801 * @ingroup DifferenceEngine
803 class MappedDiff
extends Diff
{
807 * Computes diff between sequences of strings.
809 * This can be used to compute things like
810 * case-insensitve diffs, or diffs which ignore
811 * changes in white-space.
813 * @param $from_lines array An array of strings.
814 * (Typically these are lines from a file.)
816 * @param $to_lines array An array of strings.
818 * @param $mapped_from_lines array This array should
819 * have the same size number of elements as $from_lines.
820 * The elements in $mapped_from_lines and
821 * $mapped_to_lines are what is actually compared
822 * when computing the diff.
824 * @param $mapped_to_lines array This array should
825 * have the same number of elements as $to_lines.
827 function __construct( $from_lines, $to_lines,
828 $mapped_from_lines, $mapped_to_lines ) {
829 wfProfileIn( __METHOD__
);
831 assert( 'count( $from_lines ) == count( $mapped_from_lines )' );
832 assert( 'count( $to_lines ) == count( $mapped_to_lines )' );
834 parent
::__construct( $mapped_from_lines, $mapped_to_lines );
837 for ( $i = 0; $i < count( $this->edits
); $i++
) {
838 $orig = &$this->edits
[$i]->orig
;
839 if ( is_array( $orig ) ) {
840 $orig = array_slice( $from_lines, $xi, count( $orig ) );
841 $xi +
= count( $orig );
844 $closing = &$this->edits
[$i]->closing
;
845 if ( is_array( $closing ) ) {
846 $closing = array_slice( $to_lines, $yi, count( $closing ) );
847 $yi +
= count( $closing );
850 wfProfileOut( __METHOD__
);
855 * A class to format Diffs
857 * This class formats the diff in classic diff format.
858 * It is intended that this class be customized via inheritance,
859 * to obtain fancier outputs.
862 * @ingroup DifferenceEngine
864 class DiffFormatter
{
866 * Number of leading context "lines" to preserve.
868 * This should be left at zero for this class, but subclasses
869 * may want to set this to other values.
871 var $leading_context_lines = 0;
874 * Number of trailing context "lines" to preserve.
876 * This should be left at zero for this class, but subclasses
877 * may want to set this to other values.
879 var $trailing_context_lines = 0;
884 * @param $diff Diff A Diff object.
885 * @return string The formatted output.
887 function format( $diff ) {
888 wfProfileIn( __METHOD__
);
894 $nlead = $this->leading_context_lines
;
895 $ntrail = $this->trailing_context_lines
;
897 $this->_start_diff();
899 foreach ( $diff->edits
as $edit ) {
900 if ( $edit->type
== 'copy' ) {
901 if ( is_array( $block ) ) {
902 if ( count( $edit->orig
) <= $nlead +
$ntrail ) {
906 $context = array_slice( $edit->orig
, 0, $ntrail );
907 $block[] = new _DiffOp_Copy( $context );
909 $this->_block( $x0, $ntrail +
$xi - $x0,
910 $y0, $ntrail +
$yi - $y0,
915 $context = $edit->orig
;
917 if ( !is_array( $block ) ) {
918 $context = array_slice( $context, count( $context ) - $nlead );
919 $x0 = $xi - count( $context );
920 $y0 = $yi - count( $context );
923 $block[] = new _DiffOp_Copy( $context );
930 $xi +
= count( $edit->orig
);
932 if ( $edit->closing
) {
933 $yi +
= count( $edit->closing
);
937 if ( is_array( $block ) ) {
938 $this->_block( $x0, $xi - $x0,
943 $end = $this->_end_diff();
944 wfProfileOut( __METHOD__
);
955 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
956 wfProfileIn( __METHOD__
);
957 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
958 foreach ( $edits as $edit ) {
959 if ( $edit->type
== 'copy' ) {
960 $this->_context( $edit->orig
);
961 } elseif ( $edit->type
== 'add' ) {
962 $this->_added( $edit->closing
);
963 } elseif ( $edit->type
== 'delete' ) {
964 $this->_deleted( $edit->orig
);
965 } elseif ( $edit->type
== 'change' ) {
966 $this->_changed( $edit->orig
, $edit->closing
);
968 trigger_error( 'Unknown edit type', E_USER_ERROR
);
972 wfProfileOut( __METHOD__
);
975 function _start_diff() {
982 function _end_diff() {
983 $val = ob_get_contents();
995 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
997 $xbeg .= ',' . ( $xbeg +
$xlen - 1 );
1000 $ybeg .= ',' . ( $ybeg +
$ylen - 1 );
1003 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
1006 function _start_block( $header ) {
1007 echo $header . "\n";
1010 function _end_block() {
1015 * @param $prefix string
1017 function _lines( $lines, $prefix = ' ' ) {
1018 foreach ( $lines as $line ) {
1019 echo "$prefix $line\n";
1026 function _context( $lines ) {
1027 $this->_lines( $lines );
1033 function _added( $lines ) {
1034 $this->_lines( $lines, '>' );
1040 function _deleted( $lines ) {
1041 $this->_lines( $lines, '<' );
1048 function _changed( $orig, $closing ) {
1049 $this->_deleted( $orig );
1051 $this->_added( $closing );
1056 * A formatter that outputs unified diffs
1057 * @ingroup DifferenceEngine
1059 class UnifiedDiffFormatter
extends DiffFormatter
{
1060 var $leading_context_lines = 2;
1061 var $trailing_context_lines = 2;
1066 function _added( $lines ) {
1067 $this->_lines( $lines, '+' );
1073 function _deleted( $lines ) {
1074 $this->_lines( $lines, '-' );
1081 function _changed( $orig, $closing ) {
1082 $this->_deleted( $orig );
1083 $this->_added( $closing );
1093 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1094 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
1099 * A pseudo-formatter that just passes along the Diff::$edits array
1100 * @ingroup DifferenceEngine
1102 class ArrayDiffFormatter
extends DiffFormatter
{
1108 function format( $diff ) {
1112 foreach ( $diff->edits
as $edit ) {
1113 switch( $edit->type
) {
1115 foreach ( $edit->closing
as $l ) {
1119 'newline' => $newline++
1124 foreach ( $edit->orig
as $l ) {
1126 'action' => 'delete',
1128 'oldline' => $oldline++
,
1133 foreach ( $edit->orig
as $i => $l ) {
1135 'action' => 'change',
1137 'new' => isset( $edit->closing
[$i] ) ?
$edit->closing
[$i] : null,
1138 'oldline' => $oldline++
,
1139 'newline' => $newline++
,
1144 $oldline +
= count( $edit->orig
);
1145 $newline +
= count( $edit->orig
);
1153 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1156 define( 'NBSP', ' ' ); // iso-8859-x non-breaking space.
1161 * @ingroup DifferenceEngine
1163 class _HWLDF_WordAccumulator
{
1164 function __construct() {
1165 $this->_lines
= array();
1174 function _flushGroup( $new_tag ) {
1175 if ( $this->_group
!== '' ) {
1176 if ( $this->_tag
== 'ins' ) {
1177 $this->_line
.= '<ins class="diffchange diffchange-inline">' .
1178 htmlspecialchars( $this->_group
) . '</ins>';
1179 } elseif ( $this->_tag
== 'del' ) {
1180 $this->_line
.= '<del class="diffchange diffchange-inline">' .
1181 htmlspecialchars( $this->_group
) . '</del>';
1183 $this->_line
.= htmlspecialchars( $this->_group
);
1187 $this->_tag
= $new_tag;
1193 function _flushLine( $new_tag ) {
1194 $this->_flushGroup( $new_tag );
1195 if ( $this->_line
!= '' ) {
1196 array_push( $this->_lines
, $this->_line
);
1198 # make empty lines visible by inserting an NBSP
1199 array_push( $this->_lines
, NBSP
);
1206 * @param $tag string
1208 function addWords ( $words, $tag = '' ) {
1209 if ( $tag != $this->_tag
) {
1210 $this->_flushGroup( $tag );
1213 foreach ( $words as $word ) {
1214 // new-line should only come as first char of word.
1215 if ( $word == '' ) {
1218 if ( $word[0] == "\n" ) {
1219 $this->_flushLine( $tag );
1220 $word = substr( $word, 1 );
1222 assert( '!strstr( $word, "\n" )' );
1223 $this->_group
.= $word;
1230 function getLines() {
1231 $this->_flushLine( '~done' );
1232 return $this->_lines
;
1239 * @ingroup DifferenceEngine
1241 class WordLevelDiff
extends MappedDiff
{
1242 const MAX_LINE_LENGTH
= 10000;
1245 * @param $orig_lines
1246 * @param $closing_lines
1248 function __construct ( $orig_lines, $closing_lines ) {
1249 wfProfileIn( __METHOD__
);
1251 list( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1252 list( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1254 parent
::__construct( $orig_words, $closing_words,
1255 $orig_stripped, $closing_stripped );
1256 wfProfileOut( __METHOD__
);
1263 function _split( $lines ) {
1264 wfProfileIn( __METHOD__
);
1267 $stripped = array();
1269 foreach ( $lines as $line ) {
1270 # If the line is too long, just pretend the entire line is one big word
1271 # This prevents resource exhaustion problems
1278 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1280 $stripped[] = $line;
1283 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1286 $words = array_merge( $words, $m[0] );
1287 $stripped = array_merge( $stripped, $m[1] );
1291 wfProfileOut( __METHOD__
);
1292 return array( $words, $stripped );
1299 wfProfileIn( __METHOD__
);
1300 $orig = new _HWLDF_WordAccumulator
;
1302 foreach ( $this->edits
as $edit ) {
1303 if ( $edit->type
== 'copy' ) {
1304 $orig->addWords( $edit->orig
);
1305 } elseif ( $edit->orig
) {
1306 $orig->addWords( $edit->orig
, 'del' );
1309 $lines = $orig->getLines();
1310 wfProfileOut( __METHOD__
);
1317 function closing() {
1318 wfProfileIn( __METHOD__
);
1319 $closing = new _HWLDF_WordAccumulator
;
1321 foreach ( $this->edits
as $edit ) {
1322 if ( $edit->type
== 'copy' ) {
1323 $closing->addWords( $edit->closing
);
1324 } elseif ( $edit->closing
) {
1325 $closing->addWords( $edit->closing
, 'ins' );
1328 $lines = $closing->getLines();
1329 wfProfileOut( __METHOD__
);
1335 * Wikipedia Table style diff formatter.
1338 * @ingroup DifferenceEngine
1340 class TableDiffFormatter
extends DiffFormatter
{
1341 function __construct() {
1342 $this->leading_context_lines
= 2;
1343 $this->trailing_context_lines
= 2;
1351 public static function escapeWhiteSpace( $msg ) {
1352 $msg = preg_replace( '/^ /m', '  ', $msg );
1353 $msg = preg_replace( '/ $/m', '  ', $msg );
1354 $msg = preg_replace( '/ /', '  ', $msg );
1365 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1366 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1367 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1374 function _start_block( $header ) {
1378 function _end_block() {
1381 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1385 * HTML-escape parameter before calling this
1389 function addedLine( $line ) {
1390 return $this->wrapLine( '+', 'diff-addedline', $line );
1394 * HTML-escape parameter before calling this
1398 function deletedLine( $line ) {
1399 return $this->wrapLine( '−', 'diff-deletedline', $line );
1403 * HTML-escape parameter before calling this
1407 function contextLine( $line ) {
1408 return $this->wrapLine( ' ', 'diff-context', $line );
1417 private function wrapLine( $marker, $class, $line ) {
1418 if ( $line !== '' ) {
1419 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1420 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1422 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1428 function emptyLine() {
1429 return '<td colspan="2"> </td>';
1433 * @param $lines array
1435 function _added( $lines ) {
1436 foreach ( $lines as $line ) {
1437 echo '<tr>' . $this->emptyLine() .
1438 $this->addedLine( '<ins class="diffchange">' .
1439 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
1446 function _deleted( $lines ) {
1447 foreach ( $lines as $line ) {
1448 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1449 htmlspecialchars( $line ) . '</del>' ) .
1450 $this->emptyLine() . "</tr>\n";
1457 function _context( $lines ) {
1458 foreach ( $lines as $line ) {
1460 $this->contextLine( htmlspecialchars( $line ) ) .
1461 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
1469 function _changed( $orig, $closing ) {
1470 wfProfileIn( __METHOD__
);
1472 $diff = new WordLevelDiff( $orig, $closing );
1473 $del = $diff->orig();
1474 $add = $diff->closing();
1476 # Notice that WordLevelDiff returns HTML-escaped output.
1477 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1479 while ( $line = array_shift( $del ) ) {
1480 $aline = array_shift( $add );
1481 echo '<tr>' . $this->deletedLine( $line ) .
1482 $this->addedLine( $aline ) . "</tr>\n";
1484 foreach ( $add as $line ) { # If any leftovers
1485 echo '<tr>' . $this->emptyLine() .
1486 $this->addedLine( $line ) . "</tr>\n";
1488 wfProfileOut( __METHOD__
);