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.
276 for ( $endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
277 if ( $from_lines[$xi] !== $to_lines[$yi] ) {
280 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
283 // Ignore lines which do not exist in both files.
284 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
285 $xhash[$this->_line_hash( $from_lines[$xi] )] = 1;
288 for ( $yi = $skip; $yi < $n_to - $endskip; $yi++
) {
289 $line = $to_lines[$yi];
290 if ( ( $this->ychanged
[$yi] = empty( $xhash[$this->_line_hash( $line )] ) ) ) {
293 $yhash[$this->_line_hash( $line )] = 1;
297 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
298 $line = $from_lines[$xi];
299 if ( ( $this->xchanged
[$xi] = empty( $yhash[$this->_line_hash( $line )] ) ) ) {
307 $this->_compareseq( 0, count( $this->xv
), 0, count( $this->yv
) );
309 wfProfileOut( __METHOD__
);
313 * Returns the whole line if it's small enough, or the MD5 hash otherwise
314 * @param $line string
317 function _line_hash( $line ) {
318 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
326 * Divide the Largest Common Subsequence (LCS) of the sequences
327 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
330 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
331 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
332 * sub sequences. The first sub-sequence is contained in [X0, X1),
333 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
334 * that (X0, Y0) == (XOFF, YOFF) and
335 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
337 * This function assumes that the first lines of the specified portions
338 * of the two files do not match, and likewise that the last lines do not
339 * match. The caller must trim matching lines from the beginning and end
340 * of the portions it is going to specify.
348 function _diag( $xoff, $xlim, $yoff, $ylim, $nchunks ) {
351 if ( $xlim - $xoff > $ylim - $yoff ) {
352 // Things seems faster (I'm not sure I understand why)
353 // when the shortest sequence in X.
355 list( $xoff, $xlim, $yoff, $ylim ) = array( $yoff, $ylim, $xoff, $xlim );
359 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
360 $ymatches[$this->xv
[$i]][] = $i;
363 for ( $i = $ylim - 1; $i >= $yoff; $i-- ) {
364 $ymatches[$this->yv
[$i]][] = $i;
369 $this->seq
[0] = $yoff - 1;
370 $this->in_seq
= array();
373 $numer = $xlim - $xoff +
$nchunks - 1;
375 for ( $chunk = 0; $chunk < $nchunks; $chunk++
) {
377 for ( $i = 0; $i <= $this->lcs
; $i++
) {
378 $ymids[$i][$chunk -1] = $this->seq
[$i];
382 $x1 = $xoff +
(int)( ( $numer +
( $xlim -$xoff ) * $chunk ) / $nchunks );
383 for ( ; $x < $x1; $x++
) {
384 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
385 if ( empty( $ymatches[$line] ) ) {
388 $matches = $ymatches[$line];
390 while ( list( , $y ) = each( $matches ) ) {
391 if ( empty( $this->in_seq
[$y] ) ) {
392 $k = $this->_lcs_pos( $y );
394 $ymids[$k] = $ymids[$k -1];
398 while ( list( , $y ) = each( $matches ) ) {
399 if ( $y > $this->seq
[$k -1] ) {
400 assert( '$y < $this->seq[$k]' );
401 // Optimization: this is a common case:
402 // next match is just replacing previous match.
403 $this->in_seq
[$this->seq
[$k]] = false;
405 $this->in_seq
[$y] = 1;
406 } elseif ( empty( $this->in_seq
[$y] ) ) {
407 $k = $this->_lcs_pos( $y );
409 $ymids[$k] = $ymids[$k -1];
415 $seps[] = $flip ?
array( $yoff, $xoff ) : array( $xoff, $yoff );
416 $ymid = $ymids[$this->lcs
];
417 for ( $n = 0; $n < $nchunks - 1; $n++
) {
418 $x1 = $xoff +
(int)( ( $numer +
( $xlim - $xoff ) * $n ) / $nchunks );
420 $seps[] = $flip ?
array( $y1, $x1 ) : array( $x1, $y1 );
422 $seps[] = $flip ?
array( $ylim, $xlim ) : array( $xlim, $ylim );
424 return array( $this->lcs
, $seps );
431 function _lcs_pos( $ypos ) {
433 if ( $end == 0 ||
$ypos > $this->seq
[$end] ) {
434 $this->seq
[++
$this->lcs
] = $ypos;
435 $this->in_seq
[$ypos] = 1;
440 while ( $beg < $end ) {
441 $mid = (int)( ( $beg +
$end ) / 2 );
442 if ( $ypos > $this->seq
[$mid] ) {
449 assert( '$ypos != $this->seq[$end]' );
451 $this->in_seq
[$this->seq
[$end]] = false;
452 $this->seq
[$end] = $ypos;
453 $this->in_seq
[$ypos] = 1;
458 * Find LCS of two sequences.
460 * The results are recorded in the vectors $this->{x,y}changed[], by
461 * storing a 1 in the element for each line that is an insertion
462 * or deletion (ie. is not in the LCS).
464 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
466 * Note that XLIM, YLIM are exclusive bounds.
467 * All line numbers are origin-0 and discarded lines are not counted.
473 function _compareseq( $xoff, $xlim, $yoff, $ylim ) {
474 // Slide down the bottom initial diagonal.
475 while ( $xoff < $xlim && $yoff < $ylim && $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]' );
559 while ( $j < $other_len && $other_changed[$j] ) {
570 // Find the end of this run of changes.
571 while ( ++
$i < $len && $changed[$i] ) {
577 * Record the length of this run of changes, so that
578 * we can later determine whether the run has grown.
580 $runlength = $i - $start;
583 * Move the changed region back, so long as the
584 * previous unchanged line matches the last changed one.
585 * This merges with previous changed regions.
587 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
588 $changed[--$start] = 1;
589 $changed[--$i] = false;
590 while ( $start > 0 && $changed[$start - 1] ) {
594 while ( $other_changed[--$j] ) {
597 assert( '$j >= 0 && !$other_changed[$j]' );
601 * Set CORRESPONDING to the end of the changed run, at the last
602 * point where it corresponds to a changed run in the other file.
603 * CORRESPONDING == LEN means no such point has been found.
605 $corresponding = $j < $other_len ?
$i : $len;
608 * Move the changed region forward, so long as the
609 * first changed line matches the following unchanged one.
610 * This merges with following changed regions.
611 * Do this second, so that if there are no merges,
612 * the changed region is moved forward as far as possible.
614 while ( $i < $len && $lines[$start] == $lines[$i] ) {
615 $changed[$start++
] = false;
617 while ( $i < $len && $changed[$i] ) {
621 assert( '$j < $other_len && ! $other_changed[$j]' );
623 if ( $j < $other_len && $other_changed[$j] ) {
625 while ( $j < $other_len && $other_changed[$j] ) {
630 } while ( $runlength != $i - $start );
633 * If possible, move the fully-merged run of changes
634 * back to a corresponding run in the other file.
636 while ( $corresponding < $i ) {
637 $changed[--$start] = 1;
640 while ( $other_changed[--$j] ) {
643 assert( '$j >= 0 && !$other_changed[$j]' );
646 wfProfileOut( __METHOD__
);
651 * Class representing a 'diff' between two sequences of strings.
654 * @ingroup DifferenceEngine
661 * Computes diff between sequences of strings.
663 * @param $from_lines array An array of strings.
664 * (Typically these are lines from a file.)
665 * @param $to_lines array An array of strings.
667 function __construct( $from_lines, $to_lines ) {
668 $eng = new _DiffEngine
;
669 $this->edits
= $eng->diff( $from_lines, $to_lines );
670 // $this->_check($from_lines, $to_lines);
674 * Compute reversed Diff.
678 * $diff = new Diff($lines1, $lines2);
679 * $rev = $diff->reverse();
680 * @return Object A Diff object representing the inverse of the
685 $rev->edits
= array();
686 foreach ( $this->edits
as $edit ) {
687 $rev->edits
[] = $edit->reverse();
693 * Check for empty diff.
695 * @return bool True iff two sequences were identical.
698 foreach ( $this->edits
as $edit ) {
699 if ( $edit->type
!= 'copy' ) {
707 * Compute the length of the Longest Common Subsequence (LCS).
709 * This is mostly for diagnostic purposed.
711 * @return int The length of the LCS.
715 foreach ( $this->edits
as $edit ) {
716 if ( $edit->type
== 'copy' ) {
717 $lcs +
= count( $edit->orig
);
724 * Get the original set of lines.
726 * This reconstructs the $from_lines parameter passed to the
729 * @return array The original sequence of strings.
734 foreach ( $this->edits
as $edit ) {
736 array_splice( $lines, count( $lines ), 0, $edit->orig
);
743 * Get the closing set of lines.
745 * This reconstructs the $to_lines parameter passed to the
748 * @return array The sequence of strings.
753 foreach ( $this->edits
as $edit ) {
754 if ( $edit->closing
) {
755 array_splice( $lines, count( $lines ), 0, $edit->closing
);
762 * Check a Diff for validity.
764 * This is here only for debugging purposes.
768 function _check( $from_lines, $to_lines ) {
769 wfProfileIn( __METHOD__
);
770 if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
771 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR
);
773 if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
774 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR
);
777 $rev = $this->reverse();
778 if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
779 trigger_error( "Reversed original doesn't match", E_USER_ERROR
);
781 if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
782 trigger_error( "Reversed closing doesn't match", E_USER_ERROR
);
786 foreach ( $this->edits
as $edit ) {
787 if ( $prevtype == $edit->type
) {
788 trigger_error( 'Edit sequence is non-optimal', E_USER_ERROR
);
790 $prevtype = $edit->type
;
794 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE
);
795 wfProfileOut( __METHOD__
);
800 * @todo document, bad name.
802 * @ingroup DifferenceEngine
804 class MappedDiff
extends Diff
{
808 * Computes diff between sequences of strings.
810 * This can be used to compute things like
811 * case-insensitve diffs, or diffs which ignore
812 * changes in white-space.
814 * @param $from_lines array An array of strings.
815 * (Typically these are lines from a file.)
817 * @param $to_lines array An array of strings.
819 * @param $mapped_from_lines array This array should
820 * have the same size number of elements as $from_lines.
821 * The elements in $mapped_from_lines and
822 * $mapped_to_lines are what is actually compared
823 * when computing the diff.
825 * @param $mapped_to_lines array This array should
826 * have the same number of elements as $to_lines.
828 function __construct( $from_lines, $to_lines,
829 $mapped_from_lines, $mapped_to_lines ) {
830 wfProfileIn( __METHOD__
);
832 assert( 'count( $from_lines ) == count( $mapped_from_lines )' );
833 assert( 'count( $to_lines ) == count( $mapped_to_lines )' );
835 parent
::__construct( $mapped_from_lines, $mapped_to_lines );
838 for ( $i = 0; $i < count( $this->edits
); $i++
) {
839 $orig = &$this->edits
[$i]->orig
;
840 if ( is_array( $orig ) ) {
841 $orig = array_slice( $from_lines, $xi, count( $orig ) );
842 $xi +
= count( $orig );
845 $closing = &$this->edits
[$i]->closing
;
846 if ( is_array( $closing ) ) {
847 $closing = array_slice( $to_lines, $yi, count( $closing ) );
848 $yi +
= count( $closing );
851 wfProfileOut( __METHOD__
);
856 * A class to format Diffs
858 * This class formats the diff in classic diff format.
859 * It is intended that this class be customized via inheritance,
860 * to obtain fancier outputs.
863 * @ingroup DifferenceEngine
865 class DiffFormatter
{
867 * Number of leading context "lines" to preserve.
869 * This should be left at zero for this class, but subclasses
870 * may want to set this to other values.
872 var $leading_context_lines = 0;
875 * Number of trailing context "lines" to preserve.
877 * This should be left at zero for this class, but subclasses
878 * may want to set this to other values.
880 var $trailing_context_lines = 0;
885 * @param $diff Diff A Diff object.
886 * @return string The formatted output.
888 function format( $diff ) {
889 wfProfileIn( __METHOD__
);
895 $nlead = $this->leading_context_lines
;
896 $ntrail = $this->trailing_context_lines
;
898 $this->_start_diff();
900 foreach ( $diff->edits
as $edit ) {
901 if ( $edit->type
== 'copy' ) {
902 if ( is_array( $block ) ) {
903 if ( count( $edit->orig
) <= $nlead +
$ntrail ) {
907 $context = array_slice( $edit->orig
, 0, $ntrail );
908 $block[] = new _DiffOp_Copy( $context );
910 $this->_block( $x0, $ntrail +
$xi - $x0,
911 $y0, $ntrail +
$yi - $y0,
916 $context = $edit->orig
;
918 if ( !is_array( $block ) ) {
919 $context = array_slice( $context, count( $context ) - $nlead );
920 $x0 = $xi - count( $context );
921 $y0 = $yi - count( $context );
924 $block[] = new _DiffOp_Copy( $context );
931 $xi +
= count( $edit->orig
);
933 if ( $edit->closing
) {
934 $yi +
= count( $edit->closing
);
938 if ( is_array( $block ) ) {
939 $this->_block( $x0, $xi - $x0,
944 $end = $this->_end_diff();
945 wfProfileOut( __METHOD__
);
956 function _block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
957 wfProfileIn( __METHOD__
);
958 $this->_start_block( $this->_block_header( $xbeg, $xlen, $ybeg, $ylen ) );
959 foreach ( $edits as $edit ) {
960 if ( $edit->type
== 'copy' ) {
961 $this->_context( $edit->orig
);
962 } elseif ( $edit->type
== 'add' ) {
963 $this->_added( $edit->closing
);
964 } elseif ( $edit->type
== 'delete' ) {
965 $this->_deleted( $edit->orig
);
966 } elseif ( $edit->type
== 'change' ) {
967 $this->_changed( $edit->orig
, $edit->closing
);
969 trigger_error( 'Unknown edit type', E_USER_ERROR
);
973 wfProfileOut( __METHOD__
);
976 function _start_diff() {
983 function _end_diff() {
984 $val = ob_get_contents();
996 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
998 $xbeg .= ',' . ( $xbeg +
$xlen - 1 );
1001 $ybeg .= ',' . ( $ybeg +
$ylen - 1 );
1004 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
1007 function _start_block( $header ) {
1008 echo $header . "\n";
1011 function _end_block() {
1016 * @param $prefix string
1018 function _lines( $lines, $prefix = ' ' ) {
1019 foreach ( $lines as $line ) {
1020 echo "$prefix $line\n";
1027 function _context( $lines ) {
1028 $this->_lines( $lines );
1034 function _added( $lines ) {
1035 $this->_lines( $lines, '>' );
1041 function _deleted( $lines ) {
1042 $this->_lines( $lines, '<' );
1049 function _changed( $orig, $closing ) {
1050 $this->_deleted( $orig );
1052 $this->_added( $closing );
1057 * A formatter that outputs unified diffs
1058 * @ingroup DifferenceEngine
1060 class UnifiedDiffFormatter
extends DiffFormatter
{
1061 var $leading_context_lines = 2;
1062 var $trailing_context_lines = 2;
1067 function _added( $lines ) {
1068 $this->_lines( $lines, '+' );
1074 function _deleted( $lines ) {
1075 $this->_lines( $lines, '-' );
1082 function _changed( $orig, $closing ) {
1083 $this->_deleted( $orig );
1084 $this->_added( $closing );
1094 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1095 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
1100 * A pseudo-formatter that just passes along the Diff::$edits array
1101 * @ingroup DifferenceEngine
1103 class ArrayDiffFormatter
extends DiffFormatter
{
1109 function format( $diff ) {
1113 foreach ( $diff->edits
as $edit ) {
1114 switch ( $edit->type
) {
1116 foreach ( $edit->closing
as $l ) {
1120 'newline' => $newline++
1125 foreach ( $edit->orig
as $l ) {
1127 'action' => 'delete',
1129 'oldline' => $oldline++
,
1134 foreach ( $edit->orig
as $i => $l ) {
1136 'action' => 'change',
1138 'new' => isset( $edit->closing
[$i] ) ?
$edit->closing
[$i] : null,
1139 'oldline' => $oldline++
,
1140 'newline' => $newline++
,
1145 $oldline +
= count( $edit->orig
);
1146 $newline +
= count( $edit->orig
);
1154 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
1157 define( 'NBSP', ' ' ); // iso-8859-x non-breaking space.
1162 * @ingroup DifferenceEngine
1164 class _HWLDF_WordAccumulator
{
1165 function __construct() {
1166 $this->_lines
= array();
1175 function _flushGroup( $new_tag ) {
1176 if ( $this->_group
!== '' ) {
1177 if ( $this->_tag
== 'ins' ) {
1178 $this->_line
.= '<ins class="diffchange diffchange-inline">' .
1179 htmlspecialchars( $this->_group
) . '</ins>';
1180 } elseif ( $this->_tag
== 'del' ) {
1181 $this->_line
.= '<del class="diffchange diffchange-inline">' .
1182 htmlspecialchars( $this->_group
) . '</del>';
1184 $this->_line
.= htmlspecialchars( $this->_group
);
1188 $this->_tag
= $new_tag;
1194 function _flushLine( $new_tag ) {
1195 $this->_flushGroup( $new_tag );
1196 if ( $this->_line
!= '' ) {
1197 array_push( $this->_lines
, $this->_line
);
1199 # make empty lines visible by inserting an NBSP
1200 array_push( $this->_lines
, NBSP
);
1207 * @param $tag string
1209 function addWords( $words, $tag = '' ) {
1210 if ( $tag != $this->_tag
) {
1211 $this->_flushGroup( $tag );
1214 foreach ( $words as $word ) {
1215 // new-line should only come as first char of word.
1216 if ( $word == '' ) {
1219 if ( $word[0] == "\n" ) {
1220 $this->_flushLine( $tag );
1221 $word = substr( $word, 1 );
1223 assert( '!strstr( $word, "\n" )' );
1224 $this->_group
.= $word;
1231 function getLines() {
1232 $this->_flushLine( '~done' );
1233 return $this->_lines
;
1240 * @ingroup DifferenceEngine
1242 class WordLevelDiff
extends MappedDiff
{
1243 const MAX_LINE_LENGTH
= 10000;
1246 * @param $orig_lines
1247 * @param $closing_lines
1249 function __construct( $orig_lines, $closing_lines ) {
1250 wfProfileIn( __METHOD__
);
1252 list( $orig_words, $orig_stripped ) = $this->_split( $orig_lines );
1253 list( $closing_words, $closing_stripped ) = $this->_split( $closing_lines );
1255 parent
::__construct( $orig_words, $closing_words,
1256 $orig_stripped, $closing_stripped );
1257 wfProfileOut( __METHOD__
);
1264 function _split( $lines ) {
1265 wfProfileIn( __METHOD__
);
1268 $stripped = array();
1270 foreach ( $lines as $line ) {
1271 # If the line is too long, just pretend the entire line is one big word
1272 # This prevents resource exhaustion problems
1279 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1281 $stripped[] = $line;
1284 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1287 $words = array_merge( $words, $m[0] );
1288 $stripped = array_merge( $stripped, $m[1] );
1292 wfProfileOut( __METHOD__
);
1293 return array( $words, $stripped );
1300 wfProfileIn( __METHOD__
);
1301 $orig = new _HWLDF_WordAccumulator
;
1303 foreach ( $this->edits
as $edit ) {
1304 if ( $edit->type
== 'copy' ) {
1305 $orig->addWords( $edit->orig
);
1306 } elseif ( $edit->orig
) {
1307 $orig->addWords( $edit->orig
, 'del' );
1310 $lines = $orig->getLines();
1311 wfProfileOut( __METHOD__
);
1318 function closing() {
1319 wfProfileIn( __METHOD__
);
1320 $closing = new _HWLDF_WordAccumulator
;
1322 foreach ( $this->edits
as $edit ) {
1323 if ( $edit->type
== 'copy' ) {
1324 $closing->addWords( $edit->closing
);
1325 } elseif ( $edit->closing
) {
1326 $closing->addWords( $edit->closing
, 'ins' );
1329 $lines = $closing->getLines();
1330 wfProfileOut( __METHOD__
);
1336 * Wikipedia Table style diff formatter.
1339 * @ingroup DifferenceEngine
1341 class TableDiffFormatter
extends DiffFormatter
{
1342 function __construct() {
1343 $this->leading_context_lines
= 2;
1344 $this->trailing_context_lines
= 2;
1352 public static function escapeWhiteSpace( $msg ) {
1353 $msg = preg_replace( '/^ /m', '  ', $msg );
1354 $msg = preg_replace( '/ $/m', '  ', $msg );
1355 $msg = preg_replace( '/ /', '  ', $msg );
1366 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1367 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE ' . $xbeg . "--></td>\n" .
1368 '<td colspan="2" class="diff-lineno"><!--LINE ' . $ybeg . "--></td></tr>\n";
1375 function _start_block( $header ) {
1379 function _end_block() {
1382 function _lines( $lines, $prefix = ' ', $color = 'white' ) {
1386 * HTML-escape parameter before calling this
1390 function addedLine( $line ) {
1391 return $this->wrapLine( '+', 'diff-addedline', $line );
1395 * HTML-escape parameter before calling this
1399 function deletedLine( $line ) {
1400 return $this->wrapLine( '−', 'diff-deletedline', $line );
1404 * HTML-escape parameter before calling this
1408 function contextLine( $line ) {
1409 return $this->wrapLine( ' ', 'diff-context', $line );
1418 private function wrapLine( $marker, $class, $line ) {
1419 if ( $line !== '' ) {
1420 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1421 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1423 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1429 function emptyLine() {
1430 return '<td colspan="2"> </td>';
1434 * @param $lines array
1436 function _added( $lines ) {
1437 foreach ( $lines as $line ) {
1438 echo '<tr>' . $this->emptyLine() .
1439 $this->addedLine( '<ins class="diffchange">' .
1440 htmlspecialchars( $line ) . '</ins>' ) . "</tr>\n";
1447 function _deleted( $lines ) {
1448 foreach ( $lines as $line ) {
1449 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1450 htmlspecialchars( $line ) . '</del>' ) .
1451 $this->emptyLine() . "</tr>\n";
1458 function _context( $lines ) {
1459 foreach ( $lines as $line ) {
1461 $this->contextLine( htmlspecialchars( $line ) ) .
1462 $this->contextLine( htmlspecialchars( $line ) ) . "</tr>\n";
1470 function _changed( $orig, $closing ) {
1471 wfProfileIn( __METHOD__
);
1473 $diff = new WordLevelDiff( $orig, $closing );
1474 $del = $diff->orig();
1475 $add = $diff->closing();
1477 # Notice that WordLevelDiff returns HTML-escaped output.
1478 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1480 while ( $line = array_shift( $del ) ) {
1481 $aline = array_shift( $add );
1482 echo '<tr>' . $this->deletedLine( $line ) .
1483 $this->addedLine( $aline ) . "</tr>\n";
1485 foreach ( $add as $line ) { # If any leftovers
1486 echo '<tr>' . $this->emptyLine() .
1487 $this->addedLine( $line ) . "</tr>\n";
1489 wfProfileOut( __METHOD__
);