3 * A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
5 * Additions by Axel Boldt for MediaWiki
7 * @copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
8 * @license You may copy this code freely under the conditions of the GPL.
10 define('USE_ASSERTS', function_exists('assert'));
18 trigger_error("pure virtual", E_USER_ERROR
);
22 return $this->orig ?
sizeof($this->orig
) : 0;
26 return $this->closing ?
sizeof($this->closing
) : 0;
30 class _DiffOp_Copy
extends _DiffOp
{
33 function _DiffOp_Copy ($orig, $closing = false) {
34 if (!is_array($closing))
37 $this->closing
= $closing;
41 return new _DiffOp_Copy($this->closing
, $this->orig
);
45 class _DiffOp_Delete
extends _DiffOp
{
48 function _DiffOp_Delete ($lines) {
50 $this->closing
= false;
54 return new _DiffOp_Add($this->orig
);
58 class _DiffOp_Add
extends _DiffOp
{
61 function _DiffOp_Add ($lines) {
62 $this->closing
= $lines;
67 return new _DiffOp_Delete($this->closing
);
71 class _DiffOp_Change
extends _DiffOp
{
74 function _DiffOp_Change ($orig, $closing) {
76 $this->closing
= $closing;
80 return new _DiffOp_Change($this->closing
, $this->orig
);
86 * Class used internally by Diff to actually compute the diffs.
88 * The algorithm used here is mostly lifted from the perl module
89 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
90 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
92 * More ideas are taken from:
93 * http://www.ics.uci.edu/~eppstein/161/960229.html
95 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
96 * diffutils-2.7, which can be found at:
97 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
99 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
102 * @author Geoffrey T. Dairiki
107 function diff ($from_lines, $to_lines) {
108 $n_from = sizeof($from_lines);
109 $n_to = sizeof($to_lines);
111 $this->xchanged
= $this->ychanged
= array();
112 $this->xv
= $this->yv
= array();
113 $this->xind
= $this->yind
= array();
115 unset($this->in_seq
);
118 // Skip leading common lines.
119 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++
) {
120 if ($from_lines[$skip] != $to_lines[$skip])
122 $this->xchanged
[$skip] = $this->ychanged
[$skip] = false;
124 // Skip trailing common lines.
125 $xi = $n_from; $yi = $n_to;
126 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++
) {
127 if ($from_lines[$xi] != $to_lines[$yi])
129 $this->xchanged
[$xi] = $this->ychanged
[$yi] = false;
132 // Ignore lines which do not exist in both files.
133 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
)
134 $xhash[$from_lines[$xi]] = 1;
135 for ($yi = $skip; $yi < $n_to - $endskip; $yi++
) {
136 $line = $to_lines[$yi];
137 if ( ($this->ychanged
[$yi] = empty($xhash[$line])) )
143 for ($xi = $skip; $xi < $n_from - $endskip; $xi++
) {
144 $line = $from_lines[$xi];
145 if ( ($this->xchanged
[$xi] = empty($yhash[$line])) )
152 $this->_compareseq(0, sizeof($this->xv
), 0, sizeof($this->yv
));
154 // Merge edits when possible
155 $this->_shift_boundaries($from_lines, $this->xchanged
, $this->ychanged
);
156 $this->_shift_boundaries($to_lines, $this->ychanged
, $this->xchanged
);
158 // Compute the edit operations.
161 while ($xi < $n_from ||
$yi < $n_to) {
162 USE_ASSERTS
&& assert($yi < $n_to ||
$this->xchanged
[$xi]);
163 USE_ASSERTS
&& assert($xi < $n_from ||
$this->ychanged
[$yi]);
165 // Skip matching "snake".
167 while ( $xi < $n_from && $yi < $n_to
168 && !$this->xchanged
[$xi] && !$this->ychanged
[$yi]) {
169 $copy[] = $from_lines[$xi++
];
173 $edits[] = new _DiffOp_Copy($copy);
175 // Find deletes & adds.
177 while ($xi < $n_from && $this->xchanged
[$xi])
178 $delete[] = $from_lines[$xi++
];
181 while ($yi < $n_to && $this->ychanged
[$yi])
182 $add[] = $to_lines[$yi++
];
185 $edits[] = new _DiffOp_Change($delete, $add);
187 $edits[] = new _DiffOp_Delete($delete);
189 $edits[] = new _DiffOp_Add($add);
196 * Divide the Largest Common Subsequence (LCS) of the sequences
197 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
200 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
201 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
202 * sub sequences. The first sub-sequence is contained in [X0, X1),
203 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
204 * that (X0, Y0) == (XOFF, YOFF) and
205 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
207 * This function assumes that the first lines of the specified portions
208 * of the two files do not match, and likewise that the last lines do not
209 * match. The caller must trim matching lines from the beginning and end
210 * of the portions it is going to specify.
212 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
215 if ($xlim - $xoff > $ylim - $yoff) {
216 // Things seems faster (I'm not sure I understand why)
217 // when the shortest sequence in X.
219 list ($xoff, $xlim, $yoff, $ylim)
220 = array( $yoff, $ylim, $xoff, $xlim);
224 for ($i = $ylim - 1; $i >= $yoff; $i--)
225 $ymatches[$this->xv
[$i]][] = $i;
227 for ($i = $ylim - 1; $i >= $yoff; $i--)
228 $ymatches[$this->yv
[$i]][] = $i;
231 $this->seq
[0]= $yoff - 1;
232 $this->in_seq
= array();
235 $numer = $xlim - $xoff +
$nchunks - 1;
237 for ($chunk = 0; $chunk < $nchunks; $chunk++
) {
239 for ($i = 0; $i <= $this->lcs
; $i++
)
240 $ymids[$i][$chunk-1] = $this->seq
[$i];
242 $x1 = $xoff +
(int)(($numer +
($xlim-$xoff)*$chunk) / $nchunks);
243 for ( ; $x < $x1; $x++
) {
244 $line = $flip ?
$this->yv
[$x] : $this->xv
[$x];
245 if (empty($ymatches[$line]))
247 $matches = $ymatches[$line];
249 while (list ($junk, $y) = each($matches))
250 if (empty($this->in_seq
[$y])) {
251 $k = $this->_lcs_pos($y);
252 USE_ASSERTS
&& assert($k > 0);
253 $ymids[$k] = $ymids[$k-1];
256 while (list ($junk, $y) = each($matches)) {
257 if ($y > $this->seq
[$k-1]) {
258 USE_ASSERTS
&& assert($y < $this->seq
[$k]);
259 // Optimization: this is a common case:
260 // next match is just replacing previous match.
261 $this->in_seq
[$this->seq
[$k]] = false;
263 $this->in_seq
[$y] = 1;
265 else if (empty($this->in_seq
[$y])) {
266 $k = $this->_lcs_pos($y);
267 USE_ASSERTS
&& assert($k > 0);
268 $ymids[$k] = $ymids[$k-1];
274 $seps[] = $flip ?
array($yoff, $xoff) : array($xoff, $yoff);
275 $ymid = $ymids[$this->lcs
];
276 for ($n = 0; $n < $nchunks - 1; $n++
) {
277 $x1 = $xoff +
(int)(($numer +
($xlim - $xoff) * $n) / $nchunks);
279 $seps[] = $flip ?
array($y1, $x1) : array($x1, $y1);
281 $seps[] = $flip ?
array($ylim, $xlim) : array($xlim, $ylim);
283 return array($this->lcs
, $seps);
286 function _lcs_pos ($ypos) {
288 if ($end == 0 ||
$ypos > $this->seq
[$end]) {
289 $this->seq
[++
$this->lcs
] = $ypos;
290 $this->in_seq
[$ypos] = 1;
295 while ($beg < $end) {
296 $mid = (int)(($beg +
$end) / 2);
297 if ( $ypos > $this->seq
[$mid] )
303 USE_ASSERTS
&& assert($ypos != $this->seq
[$end]);
305 $this->in_seq
[$this->seq
[$end]] = false;
306 $this->seq
[$end] = $ypos;
307 $this->in_seq
[$ypos] = 1;
312 * Find LCS of two sequences.
314 * The results are recorded in the vectors $this->{x,y}changed[], by
315 * storing a 1 in the element for each line that is an insertion
316 * or deletion (ie. is not in the LCS).
318 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
320 * Note that XLIM, YLIM are exclusive bounds.
321 * All line numbers are origin-0 and discarded lines are not counted.
323 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
324 // Slide down the bottom initial diagonal.
325 while ($xoff < $xlim && $yoff < $ylim
326 && $this->xv
[$xoff] == $this->yv
[$yoff]) {
331 // Slide up the top initial diagonal.
332 while ($xlim > $xoff && $ylim > $yoff
333 && $this->xv
[$xlim - 1] == $this->yv
[$ylim - 1]) {
338 if ($xoff == $xlim ||
$yoff == $ylim)
341 // This is ad hoc but seems to work well.
342 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
343 //$nchunks = max(2,min(8,(int)$nchunks));
344 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) +
1;
346 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
350 // X and Y sequences have no common subsequence:
352 while ($yoff < $ylim)
353 $this->ychanged
[$this->yind
[$yoff++
]] = 1;
354 while ($xoff < $xlim)
355 $this->xchanged
[$this->xind
[$xoff++
]] = 1;
358 // Use the partitions to split this problem into subproblems.
361 while ($pt2 = next($seps)) {
362 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
369 * Adjust inserts/deletes of identical lines to join changes
370 * as much as possible.
372 * We do something when a run of changed lines include a
373 * line at one end and has an excluded, identical line at the other.
374 * We are free to choose which identical line is included.
375 * `compareseq' usually chooses the one at the beginning,
376 * but usually it is cleaner to consider the following identical line
377 * to be the "change".
379 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
381 function _shift_boundaries ($lines, &$changed, $other_changed) {
385 USE_ASSERTS
&& assert('sizeof($lines) == sizeof($changed)');
386 $len = sizeof($lines);
387 $other_len = sizeof($other_changed);
391 * Scan forwards to find beginning of another run of changes.
392 * Also keep track of the corresponding point in the other file.
394 * Throughout this code, $i and $j are adjusted together so that
395 * the first $i elements of $changed and the first $j elements
396 * of $other_changed both contain the same number of zeros
398 * Furthermore, $j is always kept so that $j == $other_len or
399 * $other_changed[$j] == false.
401 while ($j < $other_len && $other_changed[$j])
404 while ($i < $len && ! $changed[$i]) {
405 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
407 while ($j < $other_len && $other_changed[$j])
416 // Find the end of this run of changes.
417 while (++
$i < $len && $changed[$i])
422 * Record the length of this run of changes, so that
423 * we can later determine whether the run has grown.
425 $runlength = $i - $start;
428 * Move the changed region back, so long as the
429 * previous unchanged line matches the last changed one.
430 * This merges with previous changed regions.
432 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
433 $changed[--$start] = 1;
434 $changed[--$i] = false;
435 while ($start > 0 && $changed[$start - 1])
437 USE_ASSERTS
&& assert('$j > 0');
438 while ($other_changed[--$j])
440 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
444 * Set CORRESPONDING to the end of the changed run, at the last
445 * point where it corresponds to a changed run in the other file.
446 * CORRESPONDING == LEN means no such point has been found.
448 $corresponding = $j < $other_len ?
$i : $len;
451 * Move the changed region forward, so long as the
452 * first changed line matches the following unchanged one.
453 * This merges with following changed regions.
454 * Do this second, so that if there are no merges,
455 * the changed region is moved forward as far as possible.
457 while ($i < $len && $lines[$start] == $lines[$i]) {
458 $changed[$start++
] = false;
460 while ($i < $len && $changed[$i])
463 USE_ASSERTS
&& assert('$j < $other_len && ! $other_changed[$j]');
465 if ($j < $other_len && $other_changed[$j]) {
467 while ($j < $other_len && $other_changed[$j])
471 } while ($runlength != $i - $start);
474 * If possible, move the fully-merged run of changes
475 * back to a corresponding run in the other file.
477 while ($corresponding < $i) {
478 $changed[--$start] = 1;
480 USE_ASSERTS
&& assert('$j > 0');
481 while ($other_changed[--$j])
483 USE_ASSERTS
&& assert('$j >= 0 && !$other_changed[$j]');
490 * Class representing a 'diff' between two sequences of strings.
498 * Computes diff between sequences of strings.
500 * @param $from_lines array An array of strings.
501 * (Typically these are lines from a file.)
502 * @param $to_lines array An array of strings.
504 function Diff($from_lines, $to_lines) {
505 $eng = new _DiffEngine
;
506 $this->edits
= $eng->diff($from_lines, $to_lines);
507 //$this->_check($from_lines, $to_lines);
511 * Compute reversed Diff.
515 * $diff = new Diff($lines1, $lines2);
516 * $rev = $diff->reverse();
517 * @return object A Diff object representing the inverse of the
520 function reverse () {
522 $rev->edits
= array();
523 foreach ($this->edits
as $edit) {
524 $rev->edits
[] = $edit->reverse();
530 * Check for empty diff.
532 * @return bool True iff two sequences were identical.
534 function isEmpty () {
535 foreach ($this->edits
as $edit) {
536 if ($edit->type
!= 'copy')
543 * Compute the length of the Longest Common Subsequence (LCS).
545 * This is mostly for diagnostic purposed.
547 * @return int The length of the LCS.
551 foreach ($this->edits
as $edit) {
552 if ($edit->type
== 'copy')
553 $lcs +
= sizeof($edit->orig
);
559 * Get the original set of lines.
561 * This reconstructs the $from_lines parameter passed to the
564 * @return array The original sequence of strings.
569 foreach ($this->edits
as $edit) {
571 array_splice($lines, sizeof($lines), 0, $edit->orig
);
577 * Get the closing set of lines.
579 * This reconstructs the $to_lines parameter passed to the
582 * @return array The sequence of strings.
587 foreach ($this->edits
as $edit) {
589 array_splice($lines, sizeof($lines), 0, $edit->closing
);
595 * Check a Diff for validity.
597 * This is here only for debugging purposes.
599 function _check ($from_lines, $to_lines) {
600 if (serialize($from_lines) != serialize($this->orig()))
601 trigger_error("Reconstructed original doesn't match", E_USER_ERROR
);
602 if (serialize($to_lines) != serialize($this->closing()))
603 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR
);
605 $rev = $this->reverse();
606 if (serialize($to_lines) != serialize($rev->orig()))
607 trigger_error("Reversed original doesn't match", E_USER_ERROR
);
608 if (serialize($from_lines) != serialize($rev->closing()))
609 trigger_error("Reversed closing doesn't match", E_USER_ERROR
);
613 foreach ($this->edits
as $edit) {
614 if ( $prevtype == $edit->type
)
615 trigger_error("Edit sequence is non-optimal", E_USER_ERROR
);
616 $prevtype = $edit->type
;
620 trigger_error("Diff okay: LCS = $lcs", E_USER_NOTICE
);
633 * Computes diff between sequences of strings.
635 * This can be used to compute things like
636 * case-insensitve diffs, or diffs which ignore
637 * changes in white-space.
639 * @param $from_lines array An array of strings.
640 * (Typically these are lines from a file.)
642 * @param $to_lines array An array of strings.
644 * @param $mapped_from_lines array This array should
645 * have the same size number of elements as $from_lines.
646 * The elements in $mapped_from_lines and
647 * $mapped_to_lines are what is actually compared
648 * when computing the diff.
650 * @param $mapped_to_lines array This array should
651 * have the same number of elements as $to_lines.
653 function MappedDiff($from_lines, $to_lines,
654 $mapped_from_lines, $mapped_to_lines) {
656 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
657 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
659 $this->Diff($mapped_from_lines, $mapped_to_lines);
662 for ($i = 0; $i < sizeof($this->edits
); $i++
) {
663 $orig = &$this->edits
[$i]->orig
;
664 if (is_array($orig)) {
665 $orig = array_slice($from_lines, $xi, sizeof($orig));
666 $xi +
= sizeof($orig);
669 $closing = &$this->edits
[$i]->closing
;
670 if (is_array($closing)) {
671 $closing = array_slice($to_lines, $yi, sizeof($closing));
672 $yi +
= sizeof($closing);
679 * A class to format Diffs
681 * This class formats the diff in classic diff format.
682 * It is intended that this class be customized via inheritance,
683 * to obtain fancier outputs.
688 * Number of leading context "lines" to preserve.
690 * This should be left at zero for this class, but subclasses
691 * may want to set this to other values.
693 var $leading_context_lines = 0;
696 * Number of trailing context "lines" to preserve.
698 * This should be left at zero for this class, but subclasses
699 * may want to set this to other values.
701 var $trailing_context_lines = 0;
706 * @param $diff object A Diff object.
707 * @return string The formatted output.
709 function format($diff) {
715 $nlead = $this->leading_context_lines
;
716 $ntrail = $this->trailing_context_lines
;
718 $this->_start_diff();
720 foreach ($diff->edits
as $edit) {
721 if ($edit->type
== 'copy') {
722 if (is_array($block)) {
723 if (sizeof($edit->orig
) <= $nlead +
$ntrail) {
728 $context = array_slice($edit->orig
, 0, $ntrail);
729 $block[] = new _DiffOp_Copy($context);
731 $this->_block($x0, $ntrail +
$xi - $x0,
732 $y0, $ntrail +
$yi - $y0,
737 $context = $edit->orig
;
740 if (! is_array($block)) {
741 $context = array_slice($context, sizeof($context) - $nlead);
742 $x0 = $xi - sizeof($context);
743 $y0 = $yi - sizeof($context);
746 $block[] = new _DiffOp_Copy($context);
752 $xi +
= sizeof($edit->orig
);
754 $yi +
= sizeof($edit->closing
);
757 if (is_array($block))
758 $this->_block($x0, $xi - $x0,
762 return $this->_end_diff();
765 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
766 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
767 foreach ($edits as $edit) {
768 if ($edit->type
== 'copy')
769 $this->_context($edit->orig
);
770 elseif ($edit->type
== 'add')
771 $this->_added($edit->closing
);
772 elseif ($edit->type
== 'delete')
773 $this->_deleted($edit->orig
);
774 elseif ($edit->type
== 'change')
775 $this->_changed($edit->orig
, $edit->closing
);
777 trigger_error("Unknown edit type", E_USER_ERROR
);
782 function _start_diff() {
786 function _end_diff() {
787 $val = ob_get_contents();
792 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
794 $xbeg .= "," . ($xbeg +
$xlen - 1);
796 $ybeg .= "," . ($ybeg +
$ylen - 1);
798 return $xbeg . ($xlen ?
($ylen ?
'c' : 'd') : 'a') . $ybeg;
801 function _start_block($header) {
805 function _end_block() {
808 function _lines($lines, $prefix = ' ') {
809 foreach ($lines as $line)
810 echo "$prefix $line\n";
813 function _context($lines) {
814 $this->_lines($lines);
817 function _added($lines) {
818 $this->_lines($lines, ">");
820 function _deleted($lines) {
821 $this->_lines($lines, "<");
824 function _changed($orig, $closing) {
825 $this->_deleted($orig);
827 $this->_added($closing);
833 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
837 define('NBSP', "\xC2\xA0"); // utf-8 non-breaking space.
839 class _HWLDF_WordAccumulator
{
840 function _HWLDF_WordAccumulator () {
841 $this->_lines
= array();
847 function _flushGroup ($new_tag) {
848 if ($this->_group
!== '') {
849 if ($this->_tag
== 'mark')
850 $this->_line
.= '<strong>'.$this->_group
.'</strong>';
852 $this->_line
.= $this->_group
;
855 $this->_tag
= $new_tag;
858 function _flushLine ($new_tag) {
859 $this->_flushGroup($new_tag);
860 if ($this->_line
!= '')
861 $this->_lines
[] = $this->_line
;
865 function addWords ($words, $tag = '') {
866 if ($tag != $this->_tag
)
867 $this->_flushGroup($tag);
869 foreach ($words as $word) {
870 // new-line should only come as first char of word.
873 if ($word[0] == "\n") {
874 $this->_group
.= NBSP
;
875 $this->_flushLine($tag);
876 $word = substr($word, 1);
878 assert(!strstr($word, "\n"));
879 $this->_group
.= $word;
883 function getLines() {
884 $this->_flushLine('~done');
885 return $this->_lines
;
889 class WordLevelDiff
extends MappedDiff
891 function WordLevelDiff ($orig_lines, $closing_lines) {
892 list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
893 list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
896 $this->MappedDiff($orig_words, $closing_words,
897 $orig_stripped, $closing_stripped);
900 function _split($lines) {
901 // FIXME: fix POSIX char class.
902 # if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
903 if (!preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
904 implode("\n", $lines),
906 return array(array(''), array(''));
908 return array($m[0], $m[1]);
912 $orig = new _HWLDF_WordAccumulator
;
914 foreach ($this->edits
as $edit) {
915 if ($edit->type
== 'copy')
916 $orig->addWords($edit->orig
);
918 $orig->addWords($edit->orig
, 'mark');
920 return $orig->getLines();
923 function closing () {
924 $closing = new _HWLDF_WordAccumulator
;
926 foreach ($this->edits
as $edit) {
927 if ($edit->type
== 'copy')
928 $closing->addWords($edit->closing
);
929 elseif ($edit->closing
)
930 $closing->addWords($edit->closing
, 'mark');
932 return $closing->getLines();
937 * "Unified" diff formatter.
939 * This class formats the diff in classic "unified diff" format.
941 class UnifiedDiffFormatter
extends DiffFormatter
943 function UnifiedDiffFormatter($context_lines = 4) {
944 $this->leading_context_lines
= $context_lines;
945 $this->trailing_context_lines
= $context_lines;
948 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
950 $xbeg .= "," . $xlen;
952 $ybeg .= "," . $ylen;
953 return "@@ -$xbeg +$ybeg @@\n";
956 function _added($lines) {
957 $this->_lines($lines, "+");
959 function _deleted($lines) {
960 $this->_lines($lines, "-");
962 function _changed($orig, $final) {
963 $this->_deleted($orig);
964 $this->_added($final);
969 * Wikipedia Table style diff formatter.
972 class TableDiffFormatter
extends DiffFormatter
974 function TableDiffFormatter() {
975 $this->leading_context_lines
= 2;
976 $this->trailing_context_lines
= 2;
979 function _pre($text){
980 $text = htmlspecialchars($text);
981 $text = str_replace(' ',' ',$text);
982 if($text{0} == ' ') $text = ' '.substr($text,1);
986 function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
988 $l1 = $lang['line'].' '.$xbeg;
989 $l2 = $lang['line'].' '.$ybeg;
990 $r = '<tr><td class="diff-blockheader" colspan="2">'.$l1.":</td>\n" .
991 '<td class="diff-blockheader" colspan="2">'.$l2.":</td></tr>\n";
995 function _start_block( $header ) {
999 function _end_block() {
1002 function _lines( $lines, $prefix=' ', $color="white" ) {
1005 function addedLine( $line ) {
1006 $line = str_replace(' ',' ',$line);
1007 if($line{0} == ' ') $line = ' '.substr($line,1);
1008 return '<td>+</td><td class="diff-addedline">' .
1012 function deletedLine( $line ) {
1013 $line = str_replace(' ',' ',$line);
1014 if($line{0} == ' ') $line = ' '.substr($line,1);
1015 return '<td>-</td><td class="diff-deletedline">' .
1019 function emptyLine() {
1020 //$line = str_replace(' ',' ',$line);
1021 return '<td colspan="2"> </td>';
1024 function contextLine( $line ) {
1025 $line = str_replace(' ',' ',$line);
1026 if($line{0} == ' ') $line = ' '.substr($line,1);
1027 return '<td> </td><td class="diff-context">'.$line.'</td>';
1030 function _added($lines) {
1031 foreach ($lines as $line) {
1032 print( '<tr>' . $this->emptyLine() .
1033 $this->addedLine( $line ) . "</tr>\n" );
1037 function _deleted($lines) {
1038 foreach ($lines as $line) {
1039 print( '<tr>' . $this->deletedLine( $line ) .
1040 $this->emptyLine() . "</tr>\n" );
1044 function _context( $lines ) {
1045 foreach ($lines as $line) {
1046 print( '<tr>' . $this->contextLine( $line ) .
1047 $this->contextLine( $line ) . "</tr>\n" );
1051 function _changed( $orig, $closing ) {
1052 $diff = new WordLevelDiff( $orig, $closing );
1053 $del = $diff->orig();
1054 $add = $diff->closing();
1056 while ( $line = array_shift( $del ) ) {
1057 $aline = array_shift( $add );
1058 print( '<tr>' . $this->deletedLine( $line ) .
1059 $this->addedLine( $aline ) . "</tr>\n" );
1061 $this->_added( $add ); # If any leftovers
1066 //Setup VIM: ex: et ts=2 enc=utf-8 :