a1f5276c6d7967faa162b5afaccb97a4d22e059b
3 * A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
5 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
6 * You may copy this code freely under the conditions of the GPL.
9 * @ingroup DifferenceEngine
10 * @defgroup DifferenceEngine DifferenceEngine
16 * @ingroup DifferenceEngine
24 trigger_error('pure virtual', E_USER_ERROR
);
28 return $this->orig ?
sizeof($this->orig
) : 0;
32 return $this->closing ?
sizeof($this->closing
) : 0;
39 * @ingroup DifferenceEngine
41 class _DiffOp_Copy
extends _DiffOp
{
44 function __construct ($orig, $closing = false) {
45 if (!is_array($closing))
48 $this->closing
= $closing;
52 return new _DiffOp_Copy($this->closing
, $this->orig
);
59 * @ingroup DifferenceEngine
61 class _DiffOp_Delete
extends _DiffOp
{
64 function __construct ($lines) {
66 $this->closing
= false;
70 return new _DiffOp_Add($this->orig
);
77 * @ingroup DifferenceEngine
79 class _DiffOp_Add
extends _DiffOp
{
82 function __construct ($lines) {
83 $this->closing
= $lines;
88 return new _DiffOp_Delete($this->closing
);
95 * @ingroup DifferenceEngine
97 class _DiffOp_Change
extends _DiffOp
{
100 function __construct ($orig, $closing) {
102 $this->closing
= $closing;
106 return new _DiffOp_Change($this->closing
, $this->orig
);
111 * Class used internally by Diff to actually compute the diffs.
113 * The algorithm used here is mostly lifted from the perl module
114 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
115 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
117 * More ideas are taken from:
118 * http://www.ics.uci.edu/~eppstein/161/960229.html
120 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
121 * diffutils-2.7, which can be found at:
122 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
124 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
127 * Line length limits for robustness added by Tim Starling, 2005-08-31
128 * Alternative implementation added by Guy Van den Broeck, 2008-07-30
130 * @author Geoffrey T. Dairiki, Tim Starling, Guy Van den Broeck
132 * @ingroup DifferenceEngine
136 const MAX_XREF_LENGTH
= 10000;
138 function diff ($from_lines, $to_lines){
139 wfProfileIn( __METHOD__
);
141 // Diff and store locally
142 $this->diff_local($from_lines, $to_lines);
144 // Merge edits when possible
145 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
146 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
148 // Compute the edit operations.
149 $n_from = sizeof($from_lines);
150 $n_to = sizeof($to_lines);
154 while ($xi < $n_from ||
$yi < $n_to) {
155 assert($yi < $n_to ||
$this->xchanged
[$xi]);
156 assert($xi < $n_from ||
$this->ychanged
[$yi]);
158 // Skip matching "snake".
160 while ( $xi < $n_from && $yi < $n_to
161 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
162 $copy[] = $from_lines[$xi++
];
166 $edits[] = new _DiffOp_Copy($copy);
168 // Find deletes & adds.
170 while ($xi < $n_from && $this->xchanged
[$xi])
171 $delete[] = $from_lines[$xi++
];
174 while ($yi < $n_to && $this->ychanged
[$yi])
175 $add[] = $to_lines[$yi++
];
178 $edits[] = new _DiffOp_Change($delete, $add);
180 $edits[] = new _DiffOp_Delete($delete);
182 $edits[] = new _DiffOp_Add($add);
184 wfProfileOut( __METHOD__
);
188 function diff_local ($from_lines, $to_lines) {
189 global $wgExternalDiffEngine;
190 wfProfileIn( __METHOD__
);
192 if($wgExternalDiffEngine == 'wikidiff3'){
194 $wikidiff3 = new WikiDiff3();
195 $wikidiff3->diff($from_lines, $to_lines);
196 $this->xchanged
= $wikidiff3->removed
;
197 $this->ychanged
= $wikidiff3->added
;
201 $n_from = sizeof($from_lines);
202 $n_to = sizeof($to_lines);
203 $this->xchanged
= $this->ychanged
= array();
204 $this->xv
= $this->yv
= array();
205 $this->xind
= $this->yind
= array();
207 unset($this->in_seq
);
210 // Skip leading common lines.
211 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
212 if ($from_lines[$skip] !== $to_lines[$skip])
214 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
216 // Skip trailing common lines.
217 $xi = $n_from; $yi = $n_to;
218 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
219 if ($from_lines[$xi] !== $to_lines[$yi])
221 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
224 // Ignore lines which do not exist in both files.
225 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
226 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
229 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
230 $line = $to_lines[$yi];
231 if ( ($this->ychanged
[$yi] = empty($xhash[$this->_line_hash($line)])) )
233 $yhash[$this->_line_hash($line)] = 1;
237 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
238 $line = $from_lines[$xi];
239 if ( ($this->xchanged
[$xi] = empty($yhash[$this->_line_hash($line)])) )
246 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
248 wfProfileOut( __METHOD__
);
252 * Returns the whole line if it's small enough, or the MD5 hash otherwise
254 function _line_hash( $line ) {
255 if ( strlen( $line ) > self
::MAX_XREF_LENGTH
) {
262 /* Divide the Largest Common Subsequence (LCS) of the sequences
263 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
266 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
267 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
268 * sub sequences. The first sub-sequence is contained in [X0, X1),
269 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
270 * that (X0, Y0) == (XOFF, YOFF) and
271 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
273 * This function assumes that the first lines of the specified portions
274 * of the two files do not match, and likewise that the last lines do not
275 * match. The caller must trim matching lines from the beginning and end
276 * of the portions it is going to specify.
278 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
281 if ($xlim - $xoff > $ylim - $yoff) {
282 // Things seems faster (I'm not sure I understand why)
283 // when the shortest sequence in X.
285 list ($xoff, $xlim, $yoff, $ylim)
286 = array( $yoff, $ylim, $xoff, $xlim);
290 for ($i = $ylim - 1; $i >= $yoff; $i--)
291 $ymatches[$this->xv
[$i]][] = $i;
293 for ($i = $ylim - 1; $i >= $yoff; $i--)
294 $ymatches[$this->yv
[$i]][] = $i;
297 $this->seq
[0]= $yoff - 1;
298 $this->in_seq
= array();
301 $numer = $xlim - $xoff +
$nchunks - 1;
303 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
305 for ($i = 0; $i <= $this->lcs
; $i++
)
306 $ymids[$i][$chunk-1] = $this->seq
[$i];
308 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
309 for ( ; $x < $x1; $x++
) {
310 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
311 if (empty($ymatches[$line]))
313 $matches = $ymatches[$line];
315 while (list ($junk, $y) = each($matches))
316 if (empty($this->in_seq
[$y])) {
317 $k = $this->_lcs_pos($y);
319 $ymids[$k] = $ymids[$k-1];
322 while (list ( /* $junk */, $y) = each($matches)) {
323 if ($y > $this->seq
[$k-1]) {
324 assert($y < $this->seq
[$k]);
325 // Optimization: this is a common case:
326 // next match is just replacing previous match.
327 $this->in_seq
[$this->seq
[$k]] = false;
329 $this->in_seq
[$y] = 1;
330 } else if (empty($this->in_seq
[$y])) {
331 $k = $this->_lcs_pos($y);
333 $ymids[$k] = $ymids[$k-1];
339 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
340 $ymid = $ymids[$this->lcs
];
341 for ($n = 0; $n < $nchunks - 1; $n++
) {
342 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
344 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
346 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
348 return array($this->lcs
, $seps);
351 function _lcs_pos ($ypos) {
353 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
354 $this->seq
[++
$this->lcs
] = $ypos;
355 $this->in_seq
[$ypos] = 1;
360 while ($beg < $end) {
361 $mid = (int)(($beg +
$end) / 2);
362 if ( $ypos > $this->seq
[$mid] )
368 assert($ypos != $this->seq
[$end]);
370 $this->in_seq
[$this->seq
[$end]] = false;
371 $this->seq
[$end] = $ypos;
372 $this->in_seq
[$ypos] = 1;
376 /* Find LCS of two sequences.
378 * The results are recorded in the vectors $this->{x,y}changed[], by
379 * storing a 1 in the element for each line that is an insertion
380 * or deletion (ie. is not in the LCS).
382 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
384 * Note that XLIM, YLIM are exclusive bounds.
385 * All line numbers are origin-0 and discarded lines are not counted.
387 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
388 // Slide down the bottom initial diagonal.
389 while ($xoff < $xlim && $yoff < $ylim
390 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
395 // Slide up the top initial diagonal.
396 while ($xlim > $xoff && $ylim > $yoff
397 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
402 if ($xoff == $xlim ||
$yoff == $ylim)
405 // This is ad hoc but seems to work well.
406 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
407 //$nchunks = max(2,min(8,(int)$nchunks));
408 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
410 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
414 // X and Y sequences have no common subsequence:
416 while ($yoff < $ylim)
417 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
418 while ($xoff < $xlim)
419 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
421 // Use the partitions to split this problem into subproblems.
424 while ($pt2 = next($seps)) {
425 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
431 /* Adjust inserts/deletes of identical lines to join changes
432 * as much as possible.
434 * We do something when a run of changed lines include a
435 * line at one end and has an excluded, identical line at the other.
436 * We are free to choose which identical line is included.
437 * `compareseq' usually chooses the one at the beginning,
438 * but usually it is cleaner to consider the following identical line
439 * to be the "change".
441 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
443 function _shift_boundaries ($lines, &$changed, $other_changed) {
444 wfProfileIn( __METHOD__
);
448 assert('sizeof($lines) == sizeof($changed)');
449 $len = sizeof($lines);
450 $other_len = sizeof($other_changed);
454 * Scan forwards to find beginning of another run of changes.
455 * Also keep track of the corresponding point in the other file.
457 * Throughout this code, $i and $j are adjusted together so that
458 * the first $i elements of $changed and the first $j elements
459 * of $other_changed both contain the same number of zeros
461 * Furthermore, $j is always kept so that $j == $other_len or
462 * $other_changed[$j] == false.
464 while ($j < $other_len && $other_changed[$j])
467 while ($i < $len && ! $changed[$i]) {
468 assert('$j < $other_len && ! $other_changed[$j]');
470 while ($j < $other_len && $other_changed[$j])
479 // Find the end of this run of changes.
480 while (++
$i < $len && $changed[$i])
485 * Record the length of this run of changes, so that
486 * we can later determine whether the run has grown.
488 $runlength = $i - $start;
491 * Move the changed region back, so long as the
492 * previous unchanged line matches the last changed one.
493 * This merges with previous changed regions.
495 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
496 $changed[--$start] = 1;
497 $changed[--$i] = false;
498 while ($start > 0 && $changed[$start - 1])
501 while ($other_changed[--$j])
503 assert('$j >= 0 && !$other_changed[$j]');
507 * Set CORRESPONDING to the end of the changed run, at the last
508 * point where it corresponds to a changed run in the other file.
509 * CORRESPONDING == LEN means no such point has been found.
511 $corresponding = $j < $other_len ?
$i : $len;
514 * Move the changed region forward, so long as the
515 * first changed line matches the following unchanged one.
516 * This merges with following changed regions.
517 * Do this second, so that if there are no merges,
518 * the changed region is moved forward as far as possible.
520 while ($i < $len && $lines[$start] == $lines[$i]) {
521 $changed[$start++
] = false;
523 while ($i < $len && $changed[$i])
526 assert('$j < $other_len && ! $other_changed[$j]');
528 if ($j < $other_len && $other_changed[$j]) {
530 while ($j < $other_len && $other_changed[$j])
534 } while ($runlength != $i - $start);
537 * If possible, move the fully-merged run of changes
538 * back to a corresponding run in the other file.
540 while ($corresponding < $i) {
541 $changed[--$start] = 1;
544 while ($other_changed[--$j])
546 assert('$j >= 0 && !$other_changed[$j]');
549 wfProfileOut( __METHOD__
);
554 * Class representing a 'diff' between two sequences of strings.
557 * @ingroup DifferenceEngine
565 * Computes diff between sequences of strings.
567 * @param $from_lines array An array of strings.
568 * (Typically these are lines from a file.)
569 * @param $to_lines array An array of strings.
571 function __construct($from_lines, $to_lines) {
572 $eng = new _DiffEngine
;
573 $this->edits
= $eng->diff($from_lines, $to_lines);
574 //$this->_check($from_lines, $to_lines);
578 * Compute reversed Diff.
582 * $diff = new Diff($lines1, $lines2);
583 * $rev = $diff->reverse();
584 * @return object A Diff object representing the inverse of the
587 function reverse () {
589 $rev->edits
= array();
590 foreach ($this->edits
as $edit) {
591 $rev->edits
[] = $edit->reverse();
597 * Check for empty diff.
599 * @return bool True iff two sequences were identical.
601 function isEmpty () {
602 foreach ($this->edits
as $edit) {
603 if ($edit->type
!= 'copy')
610 * Compute the length of the Longest Common Subsequence (LCS).
612 * This is mostly for diagnostic purposed.
614 * @return int The length of the LCS.
618 foreach ($this->edits
as $edit) {
619 if ($edit->type
== 'copy')
620 $lcs +
= sizeof($edit->orig
);
626 * Get the original set of lines.
628 * This reconstructs the $from_lines parameter passed to the
631 * @return array The original sequence of strings.
636 foreach ($this->edits
as $edit) {
638 array_splice($lines, sizeof($lines), 0, $edit->orig
);
644 * Get the closing set of lines.
646 * This reconstructs the $to_lines parameter passed to the
649 * @return array The sequence of strings.
654 foreach ($this->edits
as $edit) {
656 array_splice($lines, sizeof($lines), 0, $edit->closing
);
662 * Check a Diff for validity.
664 * This is here only for debugging purposes.
666 function _check ($from_lines, $to_lines) {
667 wfProfileIn( __METHOD__
);
668 if (serialize($from_lines) != serialize($this->orig()))
669 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
670 if (serialize($to_lines) != serialize($this->closing()))
671 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
673 $rev = $this->reverse();
674 if (serialize($to_lines) != serialize($rev->orig()))
675 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
676 if (serialize($from_lines) != serialize($rev->closing()))
677 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
681 foreach ($this->edits
as $edit) {
682 if ( $prevtype == $edit->type
)
683 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
684 $prevtype = $edit->type
;
688 trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE
);
689 wfProfileOut( __METHOD__
);
694 * @todo document, bad name.
696 * @ingroup DifferenceEngine
698 class MappedDiff
extends Diff
703 * Computes diff between sequences of strings.
705 * This can be used to compute things like
706 * case-insensitve diffs, or diffs which ignore
707 * changes in white-space.
709 * @param $from_lines array An array of strings.
710 * (Typically these are lines from a file.)
712 * @param $to_lines array An array of strings.
714 * @param $mapped_from_lines array This array should
715 * have the same size number of elements as $from_lines.
716 * The elements in $mapped_from_lines and
717 * $mapped_to_lines are what is actually compared
718 * when computing the diff.
720 * @param $mapped_to_lines array This array should
721 * have the same number of elements as $to_lines.
723 function __construct($from_lines, $to_lines,
724 $mapped_from_lines, $mapped_to_lines) {
725 wfProfileIn( __METHOD__
);
727 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
728 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
730 parent
::__construct($mapped_from_lines, $mapped_to_lines);
733 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
734 $orig = &$this->edits
[$i]->orig
;
735 if (is_array($orig)) {
736 $orig = array_slice($from_lines, $xi, sizeof($orig));
737 $xi +
= sizeof($orig);
740 $closing = &$this->edits
[$i]->closing
;
741 if (is_array($closing)) {
742 $closing = array_slice($to_lines, $yi, sizeof($closing));
743 $yi +
= sizeof($closing);
746 wfProfileOut( __METHOD__
);
751 * A class to format Diffs
753 * This class formats the diff in classic diff format.
754 * It is intended that this class be customized via inheritance,
755 * to obtain fancier outputs.
758 * @ingroup DifferenceEngine
760 class DiffFormatter
{
762 * Number of leading context "lines" to preserve.
764 * This should be left at zero for this class, but subclasses
765 * may want to set this to other values.
767 var $leading_context_lines = 0;
770 * Number of trailing context "lines" to preserve.
772 * This should be left at zero for this class, but subclasses
773 * may want to set this to other values.
775 var $trailing_context_lines = 0;
780 * @param $diff object A Diff object.
781 * @return string The formatted output.
783 function format($diff) {
784 wfProfileIn( __METHOD__
);
790 $nlead = $this->leading_context_lines
;
791 $ntrail = $this->trailing_context_lines
;
793 $this->_start_diff();
795 foreach ($diff->edits
as $edit) {
796 if ($edit->type
== 'copy') {
797 if (is_array($block)) {
798 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
803 $context = array_slice($edit->orig
, 0, $ntrail);
804 $block[] = new _DiffOp_Copy($context);
806 $this->_block($x0, $ntrail +
$xi - $x0,
807 $y0, $ntrail +
$yi - $y0,
812 $context = $edit->orig
;
815 if (! is_array($block)) {
816 $context = array_slice($context, sizeof($context) - $nlead);
817 $x0 = $xi - sizeof($context);
818 $y0 = $yi - sizeof($context);
821 $block[] = new _DiffOp_Copy($context);
827 $xi +
= sizeof($edit->orig
);
829 $yi +
= sizeof($edit->closing
);
832 if (is_array($block))
833 $this->_block($x0, $xi - $x0,
837 $end = $this->_end_diff();
838 wfProfileOut( __METHOD__
);
842 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
843 wfProfileIn( __METHOD__
);
844 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
845 foreach ($edits as $edit) {
846 if ($edit->type
== 'copy')
847 $this->_context($edit->orig
);
848 elseif ($edit->type
== 'add')
849 $this->_added($edit->closing
);
850 elseif ($edit->type
== 'delete')
851 $this->_deleted($edit->orig
);
852 elseif ($edit->type
== 'change')
853 $this->_changed($edit->orig
, $edit->closing
);
855 trigger_error('Unknown edit type', E_USER_ERROR
);
858 wfProfileOut( __METHOD__
);
861 function _start_diff() {
865 function _end_diff() {
866 $val = ob_get_contents();
871 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
873 $xbeg .= "," . ($xbeg +
$xlen - 1);
875 $ybeg .= "," . ($ybeg +
$ylen - 1);
877 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
880 function _start_block($header) {
884 function _end_block() {
887 function _lines($lines, $prefix = ' ') {
888 foreach ($lines as $line)
889 echo "$prefix $line\n";
892 function _context($lines) {
893 $this->_lines($lines);
896 function _added($lines) {
897 $this->_lines($lines, '>');
899 function _deleted($lines) {
900 $this->_lines($lines, '<');
903 function _changed($orig, $closing) {
904 $this->_deleted($orig);
906 $this->_added($closing);
911 * A formatter that outputs unified diffs
912 * @ingroup DifferenceEngine
915 class UnifiedDiffFormatter
extends DiffFormatter
{
916 var $leading_context_lines = 2;
917 var $trailing_context_lines = 2;
919 function _added($lines) {
920 $this->_lines($lines, '+');
922 function _deleted($lines) {
923 $this->_lines($lines, '-');
925 function _changed($orig, $closing) {
926 $this->_deleted($orig);
927 $this->_added($closing);
929 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
930 return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
935 * A pseudo-formatter that just passes along the Diff::$edits array
936 * @ingroup DifferenceEngine
938 class ArrayDiffFormatter
extends DiffFormatter
{
939 function format($diff) {
943 foreach($diff->edits
as $edit)
944 switch($edit->type
) {
946 foreach($edit->closing
as $l) {
950 'newline' => $newline++
955 foreach($edit->orig
as $l) {
957 'action' => 'delete',
959 'oldline' => $oldline++
,
964 foreach($edit->orig
as $i => $l) {
966 'action' => 'change',
968 'new' => @$edit->closing
[$i],
969 'oldline' => $oldline++
,
970 'newline' => $newline++
,
975 $oldline +
= count($edit->orig
);
976 $newline +
= count($edit->orig
);
983 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
987 define('NBSP', ' '); // iso-8859-x non-breaking space.
992 * @ingroup DifferenceEngine
994 class _HWLDF_WordAccumulator
{
995 function __construct () {
996 $this->_lines
= array();
1002 function _flushGroup ($new_tag) {
1003 if ($this->_group
!== '') {
1004 if ($this->_tag
== 'ins')
1005 $this->_line
.= '<ins class="diffchange diffchange-inline">' .
1006 htmlspecialchars ( $this->_group
) . '</ins>';
1007 elseif ($this->_tag
== 'del')
1008 $this->_line
.= '<del class="diffchange diffchange-inline">' .
1009 htmlspecialchars ( $this->_group
) . '</del>';
1011 $this->_line
.= htmlspecialchars ( $this->_group
);
1014 $this->_tag
= $new_tag;
1017 function _flushLine ($new_tag) {
1018 $this->_flushGroup($new_tag);
1019 if ($this->_line
!= '')
1020 array_push ( $this->_lines
, $this->_line
);
1022 # make empty lines visible by inserting an NBSP
1023 array_push ( $this->_lines
, NBSP
);
1027 function addWords ($words, $tag = '') {
1028 if ($tag != $this->_tag
)
1029 $this->_flushGroup($tag);
1031 foreach ($words as $word) {
1032 // new-line should only come as first char of word.
1035 if ($word[0] == "\n") {
1036 $this->_flushLine($tag);
1037 $word = substr($word, 1);
1039 assert(!strstr($word, "\n"));
1040 $this->_group
.= $word;
1044 function getLines() {
1045 $this->_flushLine('~done');
1046 return $this->_lines
;
1053 * @ingroup DifferenceEngine
1055 class WordLevelDiff
extends MappedDiff
{
1056 const MAX_LINE_LENGTH
= 10000;
1058 function __construct ($orig_lines, $closing_lines) {
1059 wfProfileIn( __METHOD__
);
1061 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
1062 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
1064 parent
::__construct($orig_words, $closing_words,
1065 $orig_stripped, $closing_stripped);
1066 wfProfileOut( __METHOD__
);
1069 function _split($lines) {
1070 wfProfileIn( __METHOD__
);
1073 $stripped = array();
1075 foreach ( $lines as $line ) {
1076 # If the line is too long, just pretend the entire line is one big word
1077 # This prevents resource exhaustion problems
1084 if ( strlen( $line ) > self
::MAX_LINE_LENGTH
) {
1086 $stripped[] = $line;
1089 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
1092 $words = array_merge( $words, $m[0] );
1093 $stripped = array_merge( $stripped, $m[1] );
1097 wfProfileOut( __METHOD__
);
1098 return array($words, $stripped);
1102 wfProfileIn( __METHOD__
);
1103 $orig = new _HWLDF_WordAccumulator
;
1105 foreach ($this->edits
as $edit) {
1106 if ($edit->type
== 'copy')
1107 $orig->addWords($edit->orig
);
1108 elseif ($edit->orig
)
1109 $orig->addWords($edit->orig
, 'del');
1111 $lines = $orig->getLines();
1112 wfProfileOut( __METHOD__
);
1116 function closing () {
1117 wfProfileIn( __METHOD__
);
1118 $closing = new _HWLDF_WordAccumulator
;
1120 foreach ($this->edits
as $edit) {
1121 if ($edit->type
== 'copy')
1122 $closing->addWords($edit->closing
);
1123 elseif ($edit->closing
)
1124 $closing->addWords($edit->closing
, 'ins');
1126 $lines = $closing->getLines();
1127 wfProfileOut( __METHOD__
);
1133 * Wikipedia Table style diff formatter.
1136 * @ingroup DifferenceEngine
1138 class TableDiffFormatter
extends DiffFormatter
{
1139 function __construct() {
1140 $this->leading_context_lines
= 2;
1141 $this->trailing_context_lines
= 2;
1144 public static function escapeWhiteSpace( $msg ) {
1145 $msg = preg_replace( '/^ /m', '  ', $msg );
1146 $msg = preg_replace( '/ $/m', '  ', $msg );
1147 $msg = preg_replace( '/ /', '  ', $msg );
1151 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
1152 $r = '<tr><td colspan="2" class="diff-lineno"><!--LINE '.$xbeg."--></td>\n" .
1153 '<td colspan="2" class="diff-lineno"><!--LINE '.$ybeg."--></td></tr>\n";
1157 function _start_block( $header ) {
1161 function _end_block() {
1164 function _lines( $lines, $prefix=' ', $color='white' ) {
1167 # HTML-escape parameter before calling this
1168 function addedLine( $line ) {
1169 return $this->wrapLine( '+', 'diff-addedline', $line );
1172 # HTML-escape parameter before calling this
1173 function deletedLine( $line ) {
1174 return $this->wrapLine( '-', 'diff-deletedline', $line );
1177 # HTML-escape parameter before calling this
1178 function contextLine( $line ) {
1179 return $this->wrapLine( ' ', 'diff-context', $line );
1182 private function wrapLine( $marker, $class, $line ) {
1183 if( $line !== '' ) {
1184 // The <div> wrapper is needed for 'overflow: auto' style to scroll properly
1185 $line = Xml
::tags( 'div', null, $this->escapeWhiteSpace( $line ) );
1187 return "<td class='diff-marker'>$marker</td><td class='$class'>$line</td>";
1190 function emptyLine() {
1191 return '<td colspan="2"> </td>';
1194 function _added( $lines ) {
1195 foreach ($lines as $line) {
1196 echo '<tr>' . $this->emptyLine() .
1197 $this->addedLine( '<ins class="diffchange">' .
1198 htmlspecialchars ( $line ) . '</ins>' ) . "</tr>\n";
1202 function _deleted($lines) {
1203 foreach ($lines as $line) {
1204 echo '<tr>' . $this->deletedLine( '<del class="diffchange">' .
1205 htmlspecialchars ( $line ) . '</del>' ) .
1206 $this->emptyLine() . "</tr>\n";
1210 function _context( $lines ) {
1211 foreach ($lines as $line) {
1213 $this->contextLine( htmlspecialchars ( $line ) ) .
1214 $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n";
1218 function _changed( $orig, $closing ) {
1219 wfProfileIn( __METHOD__
);
1221 $diff = new WordLevelDiff( $orig, $closing );
1222 $del = $diff->orig();
1223 $add = $diff->closing();
1225 # Notice that WordLevelDiff returns HTML-escaped output.
1226 # Hence, we will be calling addedLine/deletedLine without HTML-escaping.
1228 while ( $line = array_shift( $del ) ) {
1229 $aline = array_shift( $add );
1230 echo '<tr>' . $this->deletedLine( $line ) .
1231 $this->addedLine( $aline ) . "</tr>\n";
1233 foreach ($add as $line) { # If any leftovers
1234 echo '<tr>' . $this->emptyLine() .
1235 $this->addedLine( $line ) . "</tr>\n";
1237 wfProfileOut( __METHOD__
);