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
33 abstract class DiffOp
{
38 abstract public function reverse();
44 return $this->orig ?
count( $this->orig
) : 0;
51 return $this->closing ?
count( $this->closing
) : 0;
58 * @ingroup DifferenceEngine
60 class DiffOpCopy
extends DiffOp
{
61 public $type = 'copy';
63 function __construct( $orig, $closing = false ) {
64 if ( !is_array( $closing ) ) {
68 $this->closing
= $closing;
75 return new DiffOpCopy( $this->closing
, $this->orig
);
82 * @ingroup DifferenceEngine
84 class DiffOpDelete
extends DiffOp
{
85 public $type = 'delete';
87 function __construct( $lines ) {
89 $this->closing
= false;
96 return new DiffOpAdd( $this->orig
);
103 * @ingroup DifferenceEngine
105 class DiffOpAdd
extends DiffOp
{
106 public $type = 'add';
108 function __construct( $lines ) {
109 $this->closing
= $lines;
114 * @return DiffOpDelete
117 return new DiffOpDelete( $this->closing
);
124 * @ingroup DifferenceEngine
126 class DiffOpChange
extends DiffOp
{
127 public $type = 'change';
129 function __construct( $orig, $closing ) {
131 $this->closing
= $closing;
135 * @return DiffOpChange
138 return new DiffOpChange( $this->closing
, $this->orig
);
143 * Class used internally by Diff to actually compute the diffs.
145 * The algorithm used here is mostly lifted from the perl module
146 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
147 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
149 * More ideas are taken from:
150 * http://www.ics.uci.edu/~eppstein/161/960229.html
152 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
153 * diffutils-2.7, which can be found at:
154 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
156 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
159 * Line length limits for robustness added by Tim Starling, 2005-08-31
160 * Alternative implementation added by Guy Van den Broeck, 2008-07-30
162 * @author Geoffrey T. Dairiki, Tim Starling, Guy Van den Broeck
164 * @ingroup DifferenceEngine
167 const MAX_XREF_LENGTH
= 10000;
169 protected $xchanged, $ychanged;
171 protected $xv = array(), $yv = array();
172 protected $xind = array(), $yind = array();
174 protected $seq = array(), $in_seq = array();
183 function diff( $from_lines, $to_lines ) {
184 wfProfileIn( __METHOD__
);
186 // Diff and store locally
187 $this->diffLocal( $from_lines, $to_lines );
189 // Merge edits when possible
190 $this->shiftBoundaries( $from_lines, $this->xchanged
, $this->ychanged
);
191 $this->shiftBoundaries( $to_lines, $this->ychanged
, $this->xchanged
);
193 // Compute the edit operations.
194 $n_from = count( $from_lines );
195 $n_to = count( $to_lines );
199 while ( $xi < $n_from ||
$yi < $n_to ) {
200 assert( '$yi < $n_to || $this->xchanged[$xi]' );
201 assert( '$xi < $n_from || $this->ychanged[$yi]' );
203 // Skip matching "snake".
205 while ( $xi < $n_from && $yi < $n_to
206 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]
208 $copy[] = $from_lines[$xi++
];
212 $edits[] = new DiffOpCopy( $copy );
215 // Find deletes & adds.
217 while ( $xi < $n_from && $this->xchanged
[$xi] ) {
218 $delete[] = $from_lines[$xi++
];
222 while ( $yi < $n_to && $this->ychanged
[$yi] ) {
223 $add[] = $to_lines[$yi++
];
226 if ( $delete && $add ) {
227 $edits[] = new DiffOpChange( $delete, $add );
228 } elseif ( $delete ) {
229 $edits[] = new DiffOpDelete( $delete );
231 $edits[] = new DiffOpAdd( $add );
234 wfProfileOut( __METHOD__
);
243 private function diffLocal( $from_lines, $to_lines ) {
244 global $wgExternalDiffEngine;
245 wfProfileIn( __METHOD__
);
247 if ( $wgExternalDiffEngine == 'wikidiff3' ) {
249 $wikidiff3 = new WikiDiff3();
250 $wikidiff3->diff( $from_lines, $to_lines );
251 $this->xchanged
= $wikidiff3->removed
;
252 $this->ychanged
= $wikidiff3->added
;
256 $n_from = count( $from_lines );
257 $n_to = count( $to_lines );
258 $this->xchanged
= $this->ychanged
= array();
259 $this->xv
= $this->yv
= array();
260 $this->xind
= $this->yind
= array();
262 unset( $this->in_seq
);
265 // Skip leading common lines.
266 for ( $skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
267 if ( $from_lines[$skip] !== $to_lines[$skip] ) {
270 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
272 // Skip trailing common lines.
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->lineHash( $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->lineHash( $line )] ) ) ) {
292 $yhash[$this->lineHash( $line )] = 1;
296 for ( $xi = $skip; $xi < $n_from - $endskip; $xi++
) {
297 $line = $from_lines[$xi];
298 if ( ( $this->xchanged
[$xi] = empty( $yhash[$this->lineHash( $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 private function lineHash( $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 private 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] ) ) {
389 $matches = $ymatches[$line];
391 while ( list( , $y ) = each( $matches ) ) {
392 if ( empty( $this->in_seq
[$y] ) ) {
393 $k = $this->lcsPos( $y );
395 $ymids[$k] = $ymids[$k - 1];
400 while ( list( , $y ) = each( $matches ) ) {
401 if ( $y > $this->seq
[$k - 1] ) {
402 assert( '$y < $this->seq[$k]' );
403 // Optimization: this is a common case:
404 // next match is just replacing previous match.
405 $this->in_seq
[$this->seq
[$k]] = false;
407 $this->in_seq
[$y] = 1;
408 } elseif ( empty( $this->in_seq
[$y] ) ) {
409 $k = $this->lcsPos( $y );
411 $ymids[$k] = $ymids[$k - 1];
417 $seps[] = $flip ?
array( $yoff, $xoff ) : array( $xoff, $yoff );
418 $ymid = $ymids[$this->lcs
];
419 for ( $n = 0; $n < $nchunks - 1; $n++
) {
420 $x1 = $xoff +
(int)( ( $numer +
( $xlim - $xoff ) * $n ) / $nchunks );
422 $seps[] = $flip ?
array( $y1, $x1 ) : array( $x1, $y1 );
424 $seps[] = $flip ?
array( $ylim, $xlim ) : array( $xlim, $ylim );
426 return array( $this->lcs
, $seps );
433 private function lcsPos( $ypos ) {
435 if ( $end == 0 ||
$ypos > $this->seq
[$end] ) {
436 $this->seq
[++
$this->lcs
] = $ypos;
437 $this->in_seq
[$ypos] = 1;
443 while ( $beg < $end ) {
444 $mid = (int)( ( $beg +
$end ) / 2 );
445 if ( $ypos > $this->seq
[$mid] ) {
452 assert( '$ypos != $this->seq[$end]' );
454 $this->in_seq
[$this->seq
[$end]] = false;
455 $this->seq
[$end] = $ypos;
456 $this->in_seq
[$ypos] = 1;
462 * Find LCS of two sequences.
464 * The results are recorded in the vectors $this->{x,y}changed[], by
465 * storing a 1 in the element for each line that is an insertion
466 * or deletion (ie. is not in the LCS).
468 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
470 * Note that XLIM, YLIM are exclusive bounds.
471 * All line numbers are origin-0 and discarded lines are not counted.
477 private function compareSeq( $xoff, $xlim, $yoff, $ylim ) {
478 // Slide down the bottom initial diagonal.
479 while ( $xoff < $xlim && $yoff < $ylim && $this->xv
[$xoff] == $this->yv
[$yoff] ) {
484 // Slide up the top initial diagonal.
485 while ( $xlim > $xoff && $ylim > $yoff
486 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]
492 if ( $xoff == $xlim ||
$yoff == $ylim ) {
495 // This is ad hoc but seems to work well.
496 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
497 // $nchunks = max(2,min(8,(int)$nchunks));
498 $nchunks = min( 7, $xlim - $xoff, $ylim - $yoff ) +
1;
499 list( $lcs, $seps ) = $this->diag( $xoff, $xlim, $yoff, $ylim, $nchunks );
503 // X and Y sequences have no common subsequence:
505 while ( $yoff < $ylim ) {
506 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
508 while ( $xoff < $xlim ) {
509 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
512 // Use the partitions to split this problem into subproblems.
515 while ( $pt2 = next( $seps ) ) {
516 $this->compareSeq( $pt1[0], $pt2[0], $pt1[1], $pt2[1] );
523 * Adjust inserts/deletes of identical lines to join changes
524 * as much as possible.
526 * We do something when a run of changed lines include a
527 * line at one end and has an excluded, identical line at the other.
528 * We are free to choose which identical line is included.
529 * `compareseq' usually chooses the one at the beginning,
530 * but usually it is cleaner to consider the following identical line
531 * to be the "change".
533 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
535 private function shiftBoundaries( $lines, &$changed, $other_changed ) {
536 wfProfileIn( __METHOD__
);
540 assert( 'count($lines) == count($changed)' );
541 $len = count( $lines );
542 $other_len = count( $other_changed );
546 * Scan forwards to find beginning of another run of changes.
547 * Also keep track of the corresponding point in the other file.
549 * Throughout this code, $i and $j are adjusted together so that
550 * the first $i elements of $changed and the first $j elements
551 * of $other_changed both contain the same number of zeros
553 * Furthermore, $j is always kept so that $j == $other_len or
554 * $other_changed[$j] == false.
556 while ( $j < $other_len && $other_changed[$j] ) {
560 while ( $i < $len && !$changed[$i] ) {
561 assert( '$j < $other_len && ! $other_changed[$j]' );
564 while ( $j < $other_len && $other_changed[$j] ) {
575 // Find the end of this run of changes.
576 while ( ++
$i < $len && $changed[$i] ) {
582 * Record the length of this run of changes, so that
583 * we can later determine whether the run has grown.
585 $runlength = $i - $start;
588 * Move the changed region back, so long as the
589 * previous unchanged line matches the last changed one.
590 * This merges with previous changed regions.
592 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
593 $changed[--$start] = 1;
594 $changed[--$i] = false;
595 while ( $start > 0 && $changed[$start - 1] ) {
599 while ( $other_changed[--$j] ) {
602 assert( '$j >= 0 && !$other_changed[$j]' );
606 * Set CORRESPONDING to the end of the changed run, at the last
607 * point where it corresponds to a changed run in the other file.
608 * CORRESPONDING == LEN means no such point has been found.
610 $corresponding = $j < $other_len ?
$i : $len;
613 * Move the changed region forward, so long as the
614 * first changed line matches the following unchanged one.
615 * This merges with following changed regions.
616 * Do this second, so that if there are no merges,
617 * the changed region is moved forward as far as possible.
619 while ( $i < $len && $lines[$start] == $lines[$i] ) {
620 $changed[$start++
] = false;
622 while ( $i < $len && $changed[$i] ) {
626 assert( '$j < $other_len && ! $other_changed[$j]' );
628 if ( $j < $other_len && $other_changed[$j] ) {
630 while ( $j < $other_len && $other_changed[$j] ) {
635 } while ( $runlength != $i - $start );
638 * If possible, move the fully-merged run of changes
639 * back to a corresponding run in the other file.
641 while ( $corresponding < $i ) {
642 $changed[--$start] = 1;
645 while ( $other_changed[--$j] ) {
648 assert( '$j >= 0 && !$other_changed[$j]' );
651 wfProfileOut( __METHOD__
);
656 * Class representing a 'diff' between two sequences of strings.
659 * @ingroup DifferenceEngine
666 * Computes diff between sequences of strings.
668 * @param $from_lines array An array of strings.
669 * Typically these are lines from a file.
670 * @param $to_lines array An array of strings.
672 function __construct( $from_lines, $to_lines ) {
673 $eng = new DiffEngine
;
674 $this->edits
= $eng->diff( $from_lines, $to_lines );
678 * Compute reversed Diff.
682 * $diff = new Diff($lines1, $lines2);
683 * $rev = $diff->reverse();
684 * @return Object A Diff object representing the inverse of the
689 $rev->edits
= array();
690 /** @var DiffOp $edit */
691 foreach ( $this->edits
as $edit ) {
692 $rev->edits
[] = $edit->reverse();
699 * Check for empty diff.
701 * @return bool True if two sequences were identical.
704 foreach ( $this->edits
as $edit ) {
705 if ( $edit->type
!= 'copy' ) {
714 * Compute the length of the Longest Common Subsequence (LCS).
716 * This is mostly for diagnostic purposed.
718 * @return int The length of the LCS.
722 foreach ( $this->edits
as $edit ) {
723 if ( $edit->type
== 'copy' ) {
724 $lcs +
= count( $edit->orig
);
732 * Get the original set of lines.
734 * This reconstructs the $from_lines parameter passed to the
737 * @return array The original sequence of strings.
742 foreach ( $this->edits
as $edit ) {
744 array_splice( $lines, count( $lines ), 0, $edit->orig
);
752 * Get the closing set of lines.
754 * This reconstructs the $to_lines parameter passed to the
757 * @return array The sequence of strings.
762 foreach ( $this->edits
as $edit ) {
763 if ( $edit->closing
) {
764 array_splice( $lines, count( $lines ), 0, $edit->closing
);
773 * @todo document, bad name.
775 * @ingroup DifferenceEngine
777 class MappedDiff
extends Diff
{
781 * Computes diff between sequences of strings.
783 * This can be used to compute things like
784 * case-insensitve diffs, or diffs which ignore
785 * changes in white-space.
787 * @param $from_lines array An array of strings.
788 * Typically these are lines from a file.
790 * @param $to_lines array An array of strings.
792 * @param $mapped_from_lines array This array should
793 * have the same size number of elements as $from_lines.
794 * The elements in $mapped_from_lines and
795 * $mapped_to_lines are what is actually compared
796 * when computing the diff.
798 * @param $mapped_to_lines array This array should
799 * have the same number of elements as $to_lines.
801 function __construct( $from_lines, $to_lines,
802 $mapped_from_lines, $mapped_to_lines ) {
803 wfProfileIn( __METHOD__
);
805 assert( 'count( $from_lines ) == count( $mapped_from_lines )' );
806 assert( 'count( $to_lines ) == count( $mapped_to_lines )' );
808 parent
::__construct( $mapped_from_lines, $mapped_to_lines );
811 $editCount = count( $this->edits
);
812 for ( $i = 0; $i < $editCount; $i++
) {
813 $orig = &$this->edits
[$i]->orig
;
814 if ( is_array( $orig ) ) {
815 $orig = array_slice( $from_lines, $xi, count( $orig ) );
816 $xi +
= count( $orig );
819 $closing = &$this->edits
[$i]->closing
;
820 if ( is_array( $closing ) ) {
821 $closing = array_slice( $to_lines, $yi, count( $closing ) );
822 $yi +
= count( $closing );
825 wfProfileOut( __METHOD__
);
830 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
836 * @ingroup DifferenceEngine
838 class HWLDFWordAccumulator
{
839 public $insClass = ' class="diffchange diffchange-inline"';
840 public $delClass = ' class="diffchange diffchange-inline"';
842 private $lines = array();
850 private function flushGroup( $new_tag ) {
851 if ( $this->group
!== '' ) {
852 if ( $this->tag
== 'ins' ) {
853 $this->line
.= "<ins{$this->insClass}>" .
854 htmlspecialchars( $this->group
) . '</ins>';
855 } elseif ( $this->tag
== 'del' ) {
856 $this->line
.= "<del{$this->delClass}>" .
857 htmlspecialchars( $this->group
) . '</del>';
859 $this->line
.= htmlspecialchars( $this->group
);
863 $this->tag
= $new_tag;
869 private function flushLine( $new_tag ) {
870 $this->flushGroup( $new_tag );
871 if ( $this->line
!= '' ) {
872 array_push( $this->lines
, $this->line
);
874 # make empty lines visible by inserting an NBSP
875 array_push( $this->lines
, ' ' );
884 public function addWords( $words, $tag = '' ) {
885 if ( $tag != $this->tag
) {
886 $this->flushGroup( $tag );
889 foreach ( $words as $word ) {
890 // new-line should only come as first char of word.
894 if ( $word[0] == "\n" ) {
895 $this->flushLine( $tag );
896 $word = substr( $word, 1 );
898 assert( '!strstr( $word, "\n" )' );
899 $this->group
.= $word;
906 public function getLines() {
907 $this->flushLine( '~done' );
916 * @ingroup DifferenceEngine
918 class WordLevelDiff
extends MappedDiff
{
919 const MAX_LINE_LENGTH
= 10000;
923 * @param $closing_lines
925 function __construct( $orig_lines, $closing_lines ) {
926 wfProfileIn( __METHOD__
);
928 list( $orig_words, $orig_stripped ) = $this->split( $orig_lines );
929 list( $closing_words, $closing_stripped ) = $this->split( $closing_lines );
931 parent
::__construct( $orig_words, $closing_words,
932 $orig_stripped, $closing_stripped );
933 wfProfileOut( __METHOD__
);
940 private function split( $lines ) {
941 wfProfileIn( __METHOD__
);
946 foreach ( $lines as $line ) {
947 # If the line is too long, just pretend the entire line is one big word
948 # This prevents resource exhaustion problems
955 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
960 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
963 foreach ( $m[0] as $word ) {
966 foreach ( $m[1] as $stripped_word ) {
967 $stripped[] = $stripped_word;
972 wfProfileOut( __METHOD__
);
974 return array( $words, $stripped );
980 public function orig() {
981 wfProfileIn( __METHOD__
);
982 $orig = new HWLDFWordAccumulator
;
984 foreach ( $this->edits
as $edit ) {
985 if ( $edit->type
== 'copy' ) {
986 $orig->addWords( $edit->orig
);
987 } elseif ( $edit->orig
) {
988 $orig->addWords( $edit->orig
, 'del' );
991 $lines = $orig->getLines();
992 wfProfileOut( __METHOD__
);
1000 public function closing() {
1001 wfProfileIn( __METHOD__
);
1002 $closing = new HWLDFWordAccumulator
;
1004 foreach ( $this->edits
as $edit ) {
1005 if ( $edit->type
== 'copy' ) {
1006 $closing->addWords( $edit->closing
);
1007 } elseif ( $edit->closing
) {
1008 $closing->addWords( $edit->closing
, 'ins' );
1011 $lines = $closing->getLines();
1012 wfProfileOut( __METHOD__
);