author | Dan |
Tue, 12 Aug 2008 00:05:09 -0400 | |
changeset 687 | ea43ac1ff2ee |
parent 685 | 17ebe24cdf85 |
child 801 | eb8b23f11744 |
permissions | -rw-r--r-- |
1 | 1 |
<?php |
2 |
||
536 | 3 |
/* |
4 |
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between |
|
685
17ebe24cdf85
Rebranded as 1.1.5 (Caoineag alpha 5) and fixed a couple bugs related to CDN support in template_nodb and installerUI. Updated readme.
Dan
parents:
536
diff
changeset
|
5 |
* Version 1.1.5 (Caoineag alpha 5) |
536 | 6 |
* Copyright (C) 2006-2008 Dan Fuhry |
7 |
* |
|
8 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
9 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
12 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
13 |
*/ |
|
14 |
||
1 | 15 |
// A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3) |
16 |
// |
|
17 |
// Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org> |
|
18 |
// You may copy this code freely under the conditions of the GPL. |
|
19 |
// |
|
20 |
||
21 |
define('USE_ASSERTS', function_exists('assert')); |
|
22 |
||
23 |
/** |
|
24 |
* @todo document |
|
25 |
* @package Enano |
|
26 |
* @subpackage DifferenceEngine |
|
27 |
*/ |
|
28 |
class _DiffOp { |
|
29 |
var $type; |
|
30 |
var $orig; |
|
31 |
var $closing; |
|
32 |
||
33 |
function reverse() { |
|
34 |
trigger_error('pure virtual', E_USER_ERROR); |
|
35 |
} |
|
36 |
||
37 |
function norig() { |
|
38 |
return $this->orig ? sizeof($this->orig) : 0; |
|
39 |
} |
|
40 |
||
41 |
function nclosing() { |
|
42 |
return $this->closing ? sizeof($this->closing) : 0; |
|
43 |
} |
|
44 |
} |
|
45 |
||
46 |
/** |
|
47 |
* @todo document |
|
48 |
* @package Enano |
|
49 |
* @subpackage DifferenceEngine |
|
50 |
*/ |
|
51 |
class _DiffOp_Copy extends _DiffOp { |
|
52 |
var $type = 'copy'; |
|
53 |
||
54 |
function _DiffOp_Copy ($orig, $closing = false) { |
|
55 |
if (!is_array($closing)) |
|
56 |
$closing = $orig; |
|
57 |
$this->orig = $orig; |
|
58 |
$this->closing = $closing; |
|
59 |
} |
|
60 |
||
61 |
function reverse() { |
|
62 |
return new _DiffOp_Copy($this->closing, $this->orig); |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* @todo document |
|
68 |
* @package Enano |
|
69 |
* @subpackage DifferenceEngine |
|
70 |
*/ |
|
71 |
class _DiffOp_Delete extends _DiffOp { |
|
72 |
var $type = 'delete'; |
|
73 |
||
74 |
function _DiffOp_Delete ($lines) { |
|
75 |
$this->orig = $lines; |
|
76 |
$this->closing = false; |
|
77 |
} |
|
78 |
||
79 |
function reverse() { |
|
80 |
return new _DiffOp_Add($this->orig); |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* @todo document |
|
86 |
* @package Enano |
|
87 |
* @subpackage DifferenceEngine |
|
88 |
*/ |
|
89 |
class _DiffOp_Add extends _DiffOp { |
|
90 |
var $type = 'add'; |
|
91 |
||
92 |
function _DiffOp_Add ($lines) { |
|
93 |
$this->closing = $lines; |
|
94 |
$this->orig = false; |
|
95 |
} |
|
96 |
||
97 |
function reverse() { |
|
98 |
return new _DiffOp_Delete($this->closing); |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* @todo document |
|
104 |
* @package Enano |
|
105 |
* @subpackage DifferenceEngine |
|
106 |
*/ |
|
107 |
class _DiffOp_Change extends _DiffOp { |
|
108 |
var $type = 'change'; |
|
109 |
||
110 |
function _DiffOp_Change ($orig, $closing) { |
|
111 |
$this->orig = $orig; |
|
112 |
$this->closing = $closing; |
|
113 |
} |
|
114 |
||
115 |
function reverse() { |
|
116 |
return new _DiffOp_Change($this->closing, $this->orig); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
||
121 |
/** |
|
122 |
* Class used internally by Diff to actually compute the diffs. |
|
123 |
* |
|
124 |
* The algorithm used here is mostly lifted from the perl module |
|
125 |
* Algorithm::Diff (version 1.06) by Ned Konz, which is available at: |
|
126 |
* http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip |
|
127 |
* |
|
128 |
* More ideas are taken from: |
|
129 |
* http://www.ics.uci.edu/~eppstein/161/960229.html |
|
130 |
* |
|
131 |
* Some ideas are (and a bit of code) are from from analyze.c, from GNU |
|
132 |
* diffutils-2.7, which can be found at: |
|
133 |
* ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz |
|
134 |
* |
|
135 |
* closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations) |
|
136 |
* are my own. |
|
137 |
* |
|
138 |
* Line length limits for robustness added by Tim Starling, 2005-08-31 |
|
139 |
* |
|
140 |
* @author Geoffrey T. Dairiki, Tim Starling |
|
141 |
* @package Enano |
|
142 |
* @subpackage DifferenceEngine |
|
143 |
*/ |
|
144 |
define('MAX_XREF_LENGTH', 10000); |
|
145 |
class _DiffEngine |
|
146 |
{ |
|
147 |
function diff ($from_lines, $to_lines) { |
|
148 |
$fname = '_DiffEngine::diff'; |
|
149 |
// wfProfileIn( $fname ); |
|
150 |
||
151 |
$n_from = sizeof($from_lines); |
|
152 |
$n_to = sizeof($to_lines); |
|
153 |
||
154 |
$this->xchanged = $this->ychanged = array(); |
|
155 |
$this->xv = $this->yv = array(); |
|
156 |
$this->xind = $this->yind = array(); |
|
157 |
unset($this->seq); |
|
158 |
unset($this->in_seq); |
|
159 |
unset($this->lcs); |
|
160 |
||
161 |
// Skip leading common lines. |
|
162 |
for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) { |
|
163 |
if ($from_lines[$skip] !== $to_lines[$skip]) |
|
164 |
break; |
|
165 |
$this->xchanged[$skip] = $this->ychanged[$skip] = false; |
|
166 |
} |
|
167 |
// Skip trailing common lines. |
|
168 |
$xi = $n_from; $yi = $n_to; |
|
169 |
for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) { |
|
170 |
if ($from_lines[$xi] !== $to_lines[$yi]) |
|
171 |
break; |
|
172 |
$this->xchanged[$xi] = $this->ychanged[$yi] = false; |
|
173 |
} |
|
174 |
||
175 |
// Ignore lines which do not exist in both files. |
|
176 |
for ($xi = $skip; $xi < $n_from - $endskip; $xi++) { |
|
177 |
$xhash[$this->_line_hash($from_lines[$xi])] = 1; |
|
178 |
} |
|
179 |
||
180 |
for ($yi = $skip; $yi < $n_to - $endskip; $yi++) { |
|
181 |
$line = $to_lines[$yi]; |
|
182 |
if ( ($this->ychanged[$yi] = empty($xhash[$this->_line_hash($line)])) ) |
|
183 |
continue; |
|
184 |
$yhash[$this->_line_hash($line)] = 1; |
|
185 |
$this->yv[] = $line; |
|
186 |
$this->yind[] = $yi; |
|
187 |
} |
|
188 |
for ($xi = $skip; $xi < $n_from - $endskip; $xi++) { |
|
189 |
$line = $from_lines[$xi]; |
|
190 |
if ( ($this->xchanged[$xi] = empty($yhash[$this->_line_hash($line)])) ) |
|
191 |
continue; |
|
192 |
$this->xv[] = $line; |
|
193 |
$this->xind[] = $xi; |
|
194 |
} |
|
195 |
||
196 |
// Find the LCS. |
|
197 |
$this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv)); |
|
198 |
||
199 |
// Merge edits when possible |
|
200 |
$this->_shift_boundaries($from_lines, $this->xchanged, $this->ychanged); |
|
201 |
$this->_shift_boundaries($to_lines, $this->ychanged, $this->xchanged); |
|
202 |
||
203 |
// Compute the edit operations. |
|
204 |
$edits = array(); |
|
205 |
$xi = $yi = 0; |
|
206 |
while ($xi < $n_from || $yi < $n_to) { |
|
207 |
USE_ASSERTS && assert($yi < $n_to || $this->xchanged[$xi]); |
|
208 |
USE_ASSERTS && assert($xi < $n_from || $this->ychanged[$yi]); |
|
209 |
||
210 |
// Skip matching "snake". |
|
211 |
$copy = array(); |
|
212 |
while ( $xi < $n_from && $yi < $n_to |
|
213 |
&& !$this->xchanged[$xi] && !$this->ychanged[$yi]) { |
|
214 |
$copy[] = $from_lines[$xi++]; |
|
215 |
++$yi; |
|
216 |
} |
|
217 |
if ($copy) |
|
218 |
$edits[] = new _DiffOp_Copy($copy); |
|
219 |
||
220 |
// Find deletes & adds. |
|
221 |
$delete = array(); |
|
222 |
while ($xi < $n_from && $this->xchanged[$xi]) |
|
223 |
$delete[] = $from_lines[$xi++]; |
|
224 |
||
225 |
$add = array(); |
|
226 |
while ($yi < $n_to && $this->ychanged[$yi]) |
|
227 |
$add[] = $to_lines[$yi++]; |
|
228 |
||
229 |
if ($delete && $add) |
|
230 |
$edits[] = new _DiffOp_Change($delete, $add); |
|
231 |
elseif ($delete) |
|
232 |
$edits[] = new _DiffOp_Delete($delete); |
|
233 |
elseif ($add) |
|
234 |
$edits[] = new _DiffOp_Add($add); |
|
235 |
} |
|
236 |
// wfProfileOut( $fname ); |
|
237 |
return $edits; |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Returns the whole line if it's small enough, or the MD5 hash otherwise |
|
242 |
*/ |
|
243 |
function _line_hash( $line ) { |
|
244 |
if ( strlen( $line ) > MAX_XREF_LENGTH ) { |
|
245 |
return md5( $line ); |
|
246 |
} else { |
|
247 |
return $line; |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
||
252 |
/* Divide the Largest Common Subsequence (LCS) of the sequences |
|
253 |
* [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally |
|
254 |
* sized segments. |
|
255 |
* |
|
256 |
* Returns (LCS, PTS). LCS is the length of the LCS. PTS is an |
|
257 |
* array of NCHUNKS+1 (X, Y) indexes giving the diving points between |
|
258 |
* sub sequences. The first sub-sequence is contained in [X0, X1), |
|
259 |
* [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note |
|
260 |
* that (X0, Y0) == (XOFF, YOFF) and |
|
261 |
* (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM). |
|
262 |
* |
|
263 |
* This function assumes that the first lines of the specified portions |
|
264 |
* of the two files do not match, and likewise that the last lines do not |
|
265 |
* match. The caller must trim matching lines from the beginning and end |
|
266 |
* of the portions it is going to specify. |
|
267 |
*/ |
|
268 |
function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) { |
|
269 |
$fname = '_DiffEngine::_diag'; |
|
270 |
// wfProfileIn( $fname ); |
|
271 |
$flip = false; |
|
272 |
||
273 |
if ($xlim - $xoff > $ylim - $yoff) { |
|
274 |
// Things seems faster (I'm not sure I understand why) |
|
275 |
// when the shortest sequence in X. |
|
276 |
$flip = true; |
|
277 |
list ($xoff, $xlim, $yoff, $ylim) |
|
278 |
= array( $yoff, $ylim, $xoff, $xlim); |
|
279 |
} |
|
280 |
||
281 |
if ($flip) |
|
282 |
for ($i = $ylim - 1; $i >= $yoff; $i--) |
|
283 |
$ymatches[$this->xv[$i]][] = $i; |
|
284 |
else |
|
285 |
for ($i = $ylim - 1; $i >= $yoff; $i--) |
|
286 |
$ymatches[$this->yv[$i]][] = $i; |
|
287 |
||
288 |
$this->lcs = 0; |
|
289 |
$this->seq[0]= $yoff - 1; |
|
290 |
$this->in_seq = array(); |
|
291 |
$ymids[0] = array(); |
|
292 |
||
293 |
$numer = $xlim - $xoff + $nchunks - 1; |
|
294 |
$x = $xoff; |
|
295 |
for ($chunk = 0; $chunk < $nchunks; $chunk++) { |
|
296 |
// wfProfileIn( "$fname-chunk" ); |
|
297 |
if ($chunk > 0) |
|
298 |
for ($i = 0; $i <= $this->lcs; $i++) |
|
299 |
$ymids[$i][$chunk-1] = $this->seq[$i]; |
|
300 |
||
301 |
$x1 = $xoff + (int)(($numer + ($xlim-$xoff)*$chunk) / $nchunks); |
|
302 |
for ( ; $x < $x1; $x++) { |
|
303 |
$line = $flip ? $this->yv[$x] : $this->xv[$x]; |
|
304 |
if (empty($ymatches[$line])) |
|
305 |
continue; |
|
306 |
$matches = $ymatches[$line]; |
|
307 |
reset($matches); |
|
308 |
while (list ($junk, $y) = each($matches)) |
|
309 |
if (empty($this->in_seq[$y])) { |
|
310 |
$k = $this->_lcs_pos($y); |
|
311 |
USE_ASSERTS && assert($k > 0); |
|
312 |
$ymids[$k] = $ymids[$k-1]; |
|
313 |
break; |
|
314 |
} |
|
315 |
while (list ($junk, $y) = each($matches)) { |
|
316 |
if ($y > $this->seq[$k-1]) { |
|
317 |
USE_ASSERTS && assert($y < $this->seq[$k]); |
|
318 |
// Optimization: this is a common case: |
|
319 |
// next match is just replacing previous match. |
|
320 |
$this->in_seq[$this->seq[$k]] = false; |
|
321 |
$this->seq[$k] = $y; |
|
322 |
$this->in_seq[$y] = 1; |
|
323 |
} else if (empty($this->in_seq[$y])) { |
|
324 |
$k = $this->_lcs_pos($y); |
|
325 |
USE_ASSERTS && assert($k > 0); |
|
326 |
$ymids[$k] = $ymids[$k-1]; |
|
327 |
} |
|
328 |
} |
|
329 |
} |
|
330 |
// wfProfileOut( "$fname-chunk" ); |
|
331 |
} |
|
332 |
||
333 |
$seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff); |
|
334 |
$ymid = $ymids[$this->lcs]; |
|
335 |
for ($n = 0; $n < $nchunks - 1; $n++) { |
|
336 |
$x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks); |
|
337 |
$y1 = $ymid[$n] + 1; |
|
338 |
$seps[] = $flip ? array($y1, $x1) : array($x1, $y1); |
|
339 |
} |
|
340 |
$seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim); |
|
341 |
||
342 |
// wfProfileOut( $fname ); |
|
343 |
return array($this->lcs, $seps); |
|
344 |
} |
|
345 |
||
346 |
function _lcs_pos ($ypos) { |
|
347 |
$fname = '_DiffEngine::_lcs_pos'; |
|
348 |
// wfProfileIn( $fname ); |
|
349 |
||
350 |
$end = $this->lcs; |
|
351 |
if ($end == 0 || $ypos > $this->seq[$end]) { |
|
352 |
$this->seq[++$this->lcs] = $ypos; |
|
353 |
$this->in_seq[$ypos] = 1; |
|
354 |
// wfProfileOut( $fname ); |
|
355 |
return $this->lcs; |
|
356 |
} |
|
357 |
||
358 |
$beg = 1; |
|
359 |
while ($beg < $end) { |
|
360 |
$mid = (int)(($beg + $end) / 2); |
|
361 |
if ( $ypos > $this->seq[$mid] ) |
|
362 |
$beg = $mid + 1; |
|
363 |
else |
|
364 |
$end = $mid; |
|
365 |
} |
|
366 |
||
367 |
USE_ASSERTS && assert($ypos != $this->seq[$end]); |
|
368 |
||
369 |
$this->in_seq[$this->seq[$end]] = false; |
|
370 |
$this->seq[$end] = $ypos; |
|
371 |
$this->in_seq[$ypos] = 1; |
|
372 |
// wfProfileOut( $fname ); |
|
373 |
return $end; |
|
374 |
} |
|
375 |
||
376 |
/* Find LCS of two sequences. |
|
377 |
* |
|
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). |
|
381 |
* |
|
382 |
* The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1. |
|
383 |
* |
|
384 |
* Note that XLIM, YLIM are exclusive bounds. |
|
385 |
* All line numbers are origin-0 and discarded lines are not counted. |
|
386 |
*/ |
|
387 |
function _compareseq ($xoff, $xlim, $yoff, $ylim) { |
|
388 |
$fname = '_DiffEngine::_compareseq'; |
|
389 |
// wfProfileIn( $fname ); |
|
390 |
||
391 |
// Slide down the bottom initial diagonal. |
|
392 |
while ($xoff < $xlim && $yoff < $ylim |
|
393 |
&& $this->xv[$xoff] == $this->yv[$yoff]) { |
|
394 |
++$xoff; |
|
395 |
++$yoff; |
|
396 |
} |
|
397 |
||
398 |
// Slide up the top initial diagonal. |
|
399 |
while ($xlim > $xoff && $ylim > $yoff |
|
400 |
&& $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) { |
|
401 |
--$xlim; |
|
402 |
--$ylim; |
|
403 |
} |
|
404 |
||
405 |
if ($xoff == $xlim || $yoff == $ylim) |
|
406 |
$lcs = 0; |
|
407 |
else { |
|
408 |
// This is ad hoc but seems to work well. |
|
409 |
//$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5); |
|
410 |
//$nchunks = max(2,min(8,(int)$nchunks)); |
|
411 |
$nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1; |
|
412 |
list ($lcs, $seps) |
|
413 |
= $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks); |
|
414 |
} |
|
415 |
||
416 |
if ($lcs == 0) { |
|
417 |
// X and Y sequences have no common subsequence: |
|
418 |
// mark all changed. |
|
419 |
while ($yoff < $ylim) |
|
420 |
$this->ychanged[$this->yind[$yoff++]] = 1; |
|
421 |
while ($xoff < $xlim) |
|
422 |
$this->xchanged[$this->xind[$xoff++]] = 1; |
|
423 |
} else { |
|
424 |
// Use the partitions to split this problem into subproblems. |
|
425 |
reset($seps); |
|
426 |
$pt1 = $seps[0]; |
|
427 |
while ($pt2 = next($seps)) { |
|
428 |
$this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]); |
|
429 |
$pt1 = $pt2; |
|
430 |
} |
|
431 |
} |
|
432 |
// wfProfileOut( $fname ); |
|
433 |
} |
|
434 |
||
435 |
/* Adjust inserts/deletes of identical lines to join changes |
|
436 |
* as much as possible. |
|
437 |
* |
|
438 |
* We do something when a run of changed lines include a |
|
439 |
* line at one end and has an excluded, identical line at the other. |
|
440 |
* We are free to choose which identical line is included. |
|
441 |
* `compareseq' usually chooses the one at the beginning, |
|
442 |
* but usually it is cleaner to consider the following identical line |
|
443 |
* to be the "change". |
|
444 |
* |
|
445 |
* This is extracted verbatim from analyze.c (GNU diffutils-2.7). |
|
446 |
*/ |
|
447 |
function _shift_boundaries ($lines, &$changed, $other_changed) { |
|
448 |
$fname = '_DiffEngine::_shift_boundaries'; |
|
449 |
// wfProfileIn( $fname ); |
|
450 |
$i = 0; |
|
451 |
$j = 0; |
|
452 |
||
453 |
USE_ASSERTS && assert('sizeof($lines) == sizeof($changed)'); |
|
454 |
$len = sizeof($lines); |
|
455 |
$other_len = sizeof($other_changed); |
|
456 |
||
457 |
while (1) { |
|
458 |
/* |
|
459 |
* Scan forwards to find beginning of another run of changes. |
|
460 |
* Also keep track of the corresponding point in the other file. |
|
461 |
* |
|
462 |
* Throughout this code, $i and $j are adjusted together so that |
|
463 |
* the first $i elements of $changed and the first $j elements |
|
464 |
* of $other_changed both contain the same number of zeros |
|
465 |
* (unchanged lines). |
|
466 |
* Furthermore, $j is always kept so that $j == $other_len or |
|
467 |
* $other_changed[$j] == false. |
|
468 |
*/ |
|
469 |
while ($j < $other_len && $other_changed[$j]) |
|
470 |
$j++; |
|
471 |
||
472 |
while ($i < $len && ! $changed[$i]) { |
|
473 |
USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]'); |
|
474 |
$i++; $j++; |
|
475 |
while ($j < $other_len && $other_changed[$j]) |
|
476 |
$j++; |
|
477 |
} |
|
478 |
||
479 |
if ($i == $len) |
|
480 |
break; |
|
481 |
||
482 |
$start = $i; |
|
483 |
||
484 |
// Find the end of this run of changes. |
|
485 |
while (++$i < $len && $changed[$i]) |
|
486 |
continue; |
|
487 |
||
488 |
do { |
|
489 |
/* |
|
490 |
* Record the length of this run of changes, so that |
|
491 |
* we can later determine whether the run has grown. |
|
492 |
*/ |
|
493 |
$runlength = $i - $start; |
|
494 |
||
495 |
/* |
|
496 |
* Move the changed region back, so long as the |
|
497 |
* previous unchanged line matches the last changed one. |
|
498 |
* This merges with previous changed regions. |
|
499 |
*/ |
|
500 |
while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) { |
|
501 |
$changed[--$start] = 1; |
|
502 |
$changed[--$i] = false; |
|
503 |
while ($start > 0 && $changed[$start - 1]) |
|
504 |
$start--; |
|
505 |
USE_ASSERTS && assert('$j > 0'); |
|
506 |
while ($other_changed[--$j]) |
|
507 |
continue; |
|
508 |
USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]'); |
|
509 |
} |
|
510 |
||
511 |
/* |
|
512 |
* Set CORRESPONDING to the end of the changed run, at the last |
|
513 |
* point where it corresponds to a changed run in the other file. |
|
514 |
* CORRESPONDING == LEN means no such point has been found. |
|
515 |
*/ |
|
516 |
$corresponding = $j < $other_len ? $i : $len; |
|
517 |
||
518 |
/* |
|
519 |
* Move the changed region forward, so long as the |
|
520 |
* first changed line matches the following unchanged one. |
|
521 |
* This merges with following changed regions. |
|
522 |
* Do this second, so that if there are no merges, |
|
523 |
* the changed region is moved forward as far as possible. |
|
524 |
*/ |
|
525 |
while ($i < $len && $lines[$start] == $lines[$i]) { |
|
526 |
$changed[$start++] = false; |
|
527 |
$changed[$i++] = 1; |
|
528 |
while ($i < $len && $changed[$i]) |
|
529 |
$i++; |
|
530 |
||
531 |
USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]'); |
|
532 |
$j++; |
|
533 |
if ($j < $other_len && $other_changed[$j]) { |
|
534 |
$corresponding = $i; |
|
535 |
while ($j < $other_len && $other_changed[$j]) |
|
536 |
$j++; |
|
537 |
} |
|
538 |
} |
|
539 |
} while ($runlength != $i - $start); |
|
540 |
||
541 |
/* |
|
542 |
* If possible, move the fully-merged run of changes |
|
543 |
* back to a corresponding run in the other file. |
|
544 |
*/ |
|
545 |
while ($corresponding < $i) { |
|
546 |
$changed[--$start] = 1; |
|
547 |
$changed[--$i] = 0; |
|
548 |
USE_ASSERTS && assert('$j > 0'); |
|
549 |
while ($other_changed[--$j]) |
|
550 |
continue; |
|
551 |
USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]'); |
|
552 |
} |
|
553 |
} |
|
554 |
// wfProfileOut( $fname ); |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
/** |
|
559 |
* Class representing a 'diff' between two sequences of strings. |
|
560 |
* @todo document |
|
561 |
* @package Enano |
|
562 |
* @subpackage DifferenceEngine |
|
563 |
*/ |
|
564 |
class Diff |
|
565 |
{ |
|
566 |
var $edits; |
|
567 |
||
568 |
/** |
|
569 |
* Constructor. |
|
570 |
* Computes diff between sequences of strings. |
|
571 |
* |
|
572 |
* @param $from_lines array An array of strings. |
|
573 |
* (Typically these are lines from a file.) |
|
574 |
* @param $to_lines array An array of strings. |
|
575 |
*/ |
|
576 |
function Diff($from_lines, $to_lines) { |
|
577 |
$eng = new _DiffEngine; |
|
578 |
$this->edits = $eng->diff($from_lines, $to_lines); |
|
579 |
//$this->_check($from_lines, $to_lines); |
|
580 |
} |
|
581 |
||
582 |
/** |
|
583 |
* Compute reversed Diff. |
|
584 |
* |
|
585 |
* SYNOPSIS: |
|
586 |
* |
|
587 |
* $diff = new Diff($lines1, $lines2); |
|
588 |
* $rev = $diff->reverse(); |
|
589 |
* @return object A Diff object representing the inverse of the |
|
590 |
* original diff. |
|
591 |
*/ |
|
592 |
function reverse () { |
|
593 |
$rev = $this; |
|
594 |
$rev->edits = array(); |
|
595 |
foreach ($this->edits as $edit) { |
|
596 |
$rev->edits[] = $edit->reverse(); |
|
597 |
} |
|
598 |
return $rev; |
|
599 |
} |
|
600 |
||
601 |
/** |
|
602 |
* Check for empty diff. |
|
603 |
* |
|
604 |
* @return bool True iff two sequences were identical. |
|
605 |
*/ |
|
606 |
function isEmpty () { |
|
607 |
foreach ($this->edits as $edit) { |
|
608 |
if ($edit->type != 'copy') |
|
609 |
return false; |
|
610 |
} |
|
611 |
return true; |
|
612 |
} |
|
613 |
||
614 |
/** |
|
615 |
* Compute the length of the Longest Common Subsequence (LCS). |
|
616 |
* |
|
617 |
* This is mostly for diagnostic purposes. |
|
618 |
* |
|
619 |
* @return int The length of the LCS. |
|
620 |
*/ |
|
621 |
function lcs () { |
|
622 |
$lcs = 0; |
|
623 |
foreach ($this->edits as $edit) { |
|
624 |
if ($edit->type == 'copy') |
|
625 |
$lcs += sizeof($edit->orig); |
|
626 |
} |
|
627 |
return $lcs; |
|
628 |
} |
|
629 |
||
630 |
/** |
|
631 |
* Get the original set of lines. |
|
632 |
* |
|
633 |
* This reconstructs the $from_lines parameter passed to the |
|
634 |
* constructor. |
|
635 |
* |
|
636 |
* @return array The original sequence of strings. |
|
637 |
*/ |
|
638 |
function orig() { |
|
639 |
$lines = array(); |
|
640 |
||
641 |
foreach ($this->edits as $edit) { |
|
642 |
if ($edit->orig) |
|
643 |
array_splice($lines, sizeof($lines), 0, $edit->orig); |
|
644 |
} |
|
645 |
return $lines; |
|
646 |
} |
|
647 |
||
648 |
/** |
|
649 |
* Get the closing set of lines. |
|
650 |
* |
|
651 |
* This reconstructs the $to_lines parameter passed to the |
|
652 |
* constructor. |
|
653 |
* |
|
654 |
* @return array The sequence of strings. |
|
655 |
*/ |
|
656 |
function closing() { |
|
657 |
$lines = array(); |
|
658 |
||
659 |
foreach ($this->edits as $edit) { |
|
660 |
if ($edit->closing) |
|
661 |
array_splice($lines, sizeof($lines), 0, $edit->closing); |
|
662 |
} |
|
663 |
return $lines; |
|
664 |
} |
|
665 |
||
666 |
/** |
|
667 |
* Check a Diff for validity. |
|
668 |
* |
|
669 |
* This is here only for debugging purposes. |
|
670 |
*/ |
|
671 |
function _check ($from_lines, $to_lines) { |
|
672 |
$fname = 'Diff::_check'; |
|
673 |
// wfProfileIn( $fname ); |
|
674 |
if (serialize($from_lines) != serialize($this->orig())) |
|
675 |
trigger_error("Reconstructed original doesn't match", E_USER_ERROR); |
|
676 |
if (serialize($to_lines) != serialize($this->closing())) |
|
677 |
trigger_error("Reconstructed closing doesn't match", E_USER_ERROR); |
|
678 |
||
679 |
$rev = $this->reverse(); |
|
680 |
if (serialize($to_lines) != serialize($rev->orig())) |
|
681 |
trigger_error("Reversed original doesn't match", E_USER_ERROR); |
|
682 |
if (serialize($from_lines) != serialize($rev->closing())) |
|
683 |
trigger_error("Reversed closing doesn't match", E_USER_ERROR); |
|
684 |
||
685 |
||
686 |
$prevtype = 'none'; |
|
687 |
foreach ($this->edits as $edit) { |
|
688 |
if ( $prevtype == $edit->type ) |
|
689 |
trigger_error("Edit sequence is non-optimal", E_USER_ERROR); |
|
690 |
$prevtype = $edit->type; |
|
691 |
} |
|
692 |
||
693 |
$lcs = $this->lcs(); |
|
694 |
trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE); |
|
695 |
// wfProfileOut( $fname ); |
|
696 |
} |
|
697 |
} |
|
698 |
||
699 |
/** |
|
700 |
* FIXME: bad name. |
|
701 |
* @todo document |
|
702 |
* @package Enano |
|
703 |
* @subpackage DifferenceEngine |
|
704 |
*/ |
|
705 |
class MappedDiff extends Diff |
|
706 |
{ |
|
707 |
/** |
|
708 |
* Constructor. |
|
709 |
* |
|
710 |
* Computes diff between sequences of strings. |
|
711 |
* |
|
712 |
* This can be used to compute things like |
|
713 |
* case-insensitve diffs, or diffs which ignore |
|
714 |
* changes in white-space. |
|
715 |
* |
|
716 |
* @param $from_lines array An array of strings. |
|
717 |
* (Typically these are lines from a file.) |
|
718 |
* |
|
719 |
* @param $to_lines array An array of strings. |
|
720 |
* |
|
721 |
* @param $mapped_from_lines array This array should |
|
722 |
* have the same size number of elements as $from_lines. |
|
723 |
* The elements in $mapped_from_lines and |
|
724 |
* $mapped_to_lines are what is actually compared |
|
725 |
* when computing the diff. |
|
726 |
* |
|
727 |
* @param $mapped_to_lines array This array should |
|
728 |
* have the same number of elements as $to_lines. |
|
729 |
*/ |
|
730 |
function MappedDiff($from_lines, $to_lines, |
|
731 |
$mapped_from_lines, $mapped_to_lines) { |
|
732 |
$fname = 'MappedDiff::MappedDiff'; |
|
733 |
// wfProfileIn( $fname ); |
|
734 |
||
735 |
assert(sizeof($from_lines) == sizeof($mapped_from_lines)); |
|
736 |
assert(sizeof($to_lines) == sizeof($mapped_to_lines)); |
|
737 |
||
738 |
$this->Diff($mapped_from_lines, $mapped_to_lines); |
|
739 |
||
740 |
$xi = $yi = 0; |
|
741 |
for ($i = 0; $i < sizeof($this->edits); $i++) { |
|
742 |
$orig = &$this->edits[$i]->orig; |
|
743 |
if (is_array($orig)) { |
|
744 |
$orig = array_slice($from_lines, $xi, sizeof($orig)); |
|
745 |
$xi += sizeof($orig); |
|
746 |
} |
|
747 |
||
748 |
$closing = &$this->edits[$i]->closing; |
|
749 |
if (is_array($closing)) { |
|
750 |
$closing = array_slice($to_lines, $yi, sizeof($closing)); |
|
751 |
$yi += sizeof($closing); |
|
752 |
} |
|
753 |
} |
|
754 |
// wfProfileOut( $fname ); |
|
755 |
} |
|
756 |
} |
|
757 |
||
758 |
/** |
|
759 |
* A class to format Diffs |
|
760 |
* |
|
761 |
* This class formats the diff in classic diff format. |
|
762 |
* It is intended that this class be customized via inheritance, |
|
763 |
* to obtain fancier outputs. |
|
764 |
* @todo document |
|
765 |
* @package Enano |
|
766 |
* @subpackage DifferenceEngine |
|
767 |
*/ |
|
768 |
class DiffFormatter |
|
769 |
{ |
|
770 |
/** |
|
771 |
* Number of leading context "lines" to preserve. |
|
772 |
* |
|
773 |
* This should be left at zero for this class, but subclasses |
|
774 |
* may want to set this to other values. |
|
775 |
*/ |
|
776 |
var $leading_context_lines = 0; |
|
777 |
||
778 |
/** |
|
779 |
* Number of trailing context "lines" to preserve. |
|
780 |
* |
|
781 |
* This should be left at zero for this class, but subclasses |
|
782 |
* may want to set this to other values. |
|
783 |
*/ |
|
784 |
var $trailing_context_lines = 0; |
|
785 |
||
786 |
/** |
|
787 |
* Format a diff. |
|
788 |
* |
|
789 |
* @param $diff object A Diff object. |
|
790 |
* @return string The formatted output. |
|
791 |
*/ |
|
792 |
function format($diff) { |
|
793 |
$fname = 'DiffFormatter::format'; |
|
794 |
// wfProfileIn( $fname ); |
|
795 |
||
796 |
$xi = $yi = 1; |
|
797 |
$block = false; |
|
798 |
$context = array(); |
|
799 |
||
800 |
$nlead = $this->leading_context_lines; |
|
801 |
$ntrail = $this->trailing_context_lines; |
|
802 |
||
803 |
$this->_start_diff(); |
|
804 |
||
805 |
foreach ($diff->edits as $edit) { |
|
806 |
if ($edit->type == 'copy') { |
|
807 |
if (is_array($block)) { |
|
808 |
if (sizeof($edit->orig) <= $nlead + $ntrail) { |
|
809 |
$block[] = $edit; |
|
810 |
} |
|
811 |
else{ |
|
812 |
if ($ntrail) { |
|
813 |
$context = array_slice($edit->orig, 0, $ntrail); |
|
814 |
$block[] = new _DiffOp_Copy($context); |
|
815 |
} |
|
816 |
$this->_block($x0, $ntrail + $xi - $x0, |
|
817 |
$y0, $ntrail + $yi - $y0, |
|
818 |
$block); |
|
819 |
$block = false; |
|
820 |
} |
|
821 |
} |
|
822 |
$context = $edit->orig; |
|
823 |
} |
|
824 |
else { |
|
825 |
if (! is_array($block)) { |
|
826 |
$context = array_slice($context, sizeof($context) - $nlead); |
|
827 |
$x0 = $xi - sizeof($context); |
|
828 |
$y0 = $yi - sizeof($context); |
|
829 |
$block = array(); |
|
830 |
if ($context) |
|
831 |
$block[] = new _DiffOp_Copy($context); |
|
832 |
} |
|
833 |
$block[] = $edit; |
|
834 |
} |
|
835 |
||
836 |
if ($edit->orig) |
|
837 |
$xi += sizeof($edit->orig); |
|
838 |
if ($edit->closing) |
|
839 |
$yi += sizeof($edit->closing); |
|
840 |
} |
|
841 |
||
842 |
if (is_array($block)) |
|
843 |
$this->_block($x0, $xi - $x0, |
|
844 |
$y0, $yi - $y0, |
|
845 |
$block); |
|
846 |
||
847 |
$end = $this->_end_diff(); |
|
848 |
// wfProfileOut( $fname ); |
|
849 |
return $end; |
|
850 |
} |
|
851 |
||
852 |
function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) { |
|
853 |
$fname = 'DiffFormatter::_block'; |
|
854 |
// wfProfileIn( $fname ); |
|
855 |
$this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen)); |
|
856 |
foreach ($edits as $edit) { |
|
857 |
if ($edit->type == 'copy') |
|
858 |
$this->_context($edit->orig); |
|
859 |
elseif ($edit->type == 'add') |
|
860 |
$this->_added($edit->closing); |
|
861 |
elseif ($edit->type == 'delete') |
|
862 |
$this->_deleted($edit->orig); |
|
863 |
elseif ($edit->type == 'change') |
|
864 |
$this->_changed($edit->orig, $edit->closing); |
|
865 |
else |
|
866 |
trigger_error('Unknown edit type', E_USER_ERROR); |
|
867 |
} |
|
868 |
$this->_end_block(); |
|
869 |
// wfProfileOut( $fname ); |
|
870 |
} |
|
871 |
||
872 |
function _start_diff() { |
|
873 |
ob_start(); |
|
874 |
} |
|
875 |
||
876 |
function _end_diff() { |
|
877 |
$val = ob_get_contents(); |
|
878 |
ob_end_clean(); |
|
879 |
return $val; |
|
880 |
} |
|
881 |
||
882 |
function _block_header($xbeg, $xlen, $ybeg, $ylen) { |
|
883 |
if ($xlen > 1) |
|
884 |
$xbeg .= "," . ($xbeg + $xlen - 1); |
|
885 |
if ($ylen > 1) |
|
886 |
$ybeg .= "," . ($ybeg + $ylen - 1); |
|
887 |
||
888 |
return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg; |
|
889 |
} |
|
890 |
||
891 |
function _start_block($header) { |
|
892 |
echo $header; |
|
893 |
} |
|
894 |
||
895 |
function _end_block() { |
|
896 |
} |
|
897 |
||
898 |
function _lines($lines, $prefix = ' ') { |
|
899 |
foreach ($lines as $line) |
|
900 |
echo "$prefix $line\n"; |
|
901 |
} |
|
902 |
||
903 |
function _context($lines) { |
|
904 |
$this->_lines($lines); |
|
905 |
} |
|
906 |
||
907 |
function _added($lines) { |
|
908 |
$this->_lines($lines, '>'); |
|
909 |
} |
|
910 |
function _deleted($lines) { |
|
911 |
$this->_lines($lines, '<'); |
|
912 |
} |
|
913 |
||
914 |
function _changed($orig, $closing) { |
|
915 |
$this->_deleted($orig); |
|
916 |
echo "---\n"; |
|
917 |
$this->_added($closing); |
|
918 |
} |
|
919 |
} |
|
920 |
||
921 |
||
922 |
/** |
|
923 |
* Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3 |
|
924 |
* |
|
925 |
*/ |
|
926 |
||
927 |
define('NBSP', ' '); // iso-8859-x non-breaking space. |
|
928 |
||
929 |
/** |
|
930 |
* @todo document |
|
931 |
* @package Enano |
|
932 |
* @subpackage DifferenceEngine |
|
933 |
*/ |
|
934 |
class _HWLDF_WordAccumulator { |
|
935 |
function _HWLDF_WordAccumulator () { |
|
936 |
$this->_lines = array(); |
|
937 |
$this->_line = ''; |
|
938 |
$this->_group = ''; |
|
939 |
$this->_tag = ''; |
|
940 |
} |
|
941 |
||
942 |
function _flushGroup ($new_tag) { |
|
943 |
if ($this->_group !== '') { |
|
944 |
if ($this->_tag == 'mark') |
|
945 |
$this->_line .= '<span class="diffchange">' . |
|
946 |
htmlspecialchars ( $this->_group ) . '</span>'; |
|
947 |
else |
|
948 |
$this->_line .= htmlspecialchars ( $this->_group ); |
|
949 |
} |
|
950 |
$this->_group = ''; |
|
951 |
$this->_tag = $new_tag; |
|
952 |
} |
|
953 |
||
954 |
function _flushLine ($new_tag) { |
|
955 |
$this->_flushGroup($new_tag); |
|
956 |
if ($this->_line != '') |
|
957 |
array_push ( $this->_lines, $this->_line ); |
|
958 |
else |
|
959 |
# make empty lines visible by inserting an NBSP |
|
960 |
array_push ( $this->_lines, NBSP ); |
|
961 |
$this->_line = ''; |
|
962 |
} |
|
963 |
||
964 |
function addWords ($words, $tag = '') { |
|
965 |
if ($tag != $this->_tag) |
|
966 |
$this->_flushGroup($tag); |
|
967 |
||
968 |
foreach ($words as $word) { |
|
969 |
// new-line should only come as first char of word. |
|
970 |
if ($word == '') |
|
971 |
continue; |
|
972 |
if ($word[0] == "\n") { |
|
973 |
$this->_flushLine($tag); |
|
974 |
$word = substr($word, 1); |
|
975 |
} |
|
976 |
assert(!strstr($word, "\n")); |
|
977 |
$this->_group .= $word; |
|
978 |
} |
|
979 |
} |
|
980 |
||
981 |
function getLines() { |
|
982 |
$this->_flushLine('~done'); |
|
983 |
return $this->_lines; |
|
984 |
} |
|
985 |
} |
|
986 |
||
987 |
/** |
|
988 |
* @todo document |
|
989 |
* @package Enano |
|
990 |
* @subpackage DifferenceEngine |
|
991 |
*/ |
|
992 |
define('MAX_LINE_LENGTH', 10000); |
|
993 |
class WordLevelDiff extends MappedDiff |
|
994 |
{ |
|
995 |
function WordLevelDiff ($orig_lines, $closing_lines) { |
|
996 |
$fname = 'WordLevelDiff::WordLevelDiff'; |
|
997 |
// wfProfileIn( $fname ); |
|
998 |
||
999 |
list ($orig_words, $orig_stripped) = $this->_split($orig_lines); |
|
1000 |
list ($closing_words, $closing_stripped) = $this->_split($closing_lines); |
|
1001 |
||
1002 |
$this->MappedDiff($orig_words, $closing_words, |
|
1003 |
$orig_stripped, $closing_stripped); |
|
1004 |
// wfProfileOut( $fname ); |
|
1005 |
} |
|
1006 |
||
1007 |
function _split($lines) { |
|
1008 |
$fname = 'WordLevelDiff::_split'; |
|
1009 |
// wfProfileIn( $fname ); |
|
1010 |
||
1011 |
$words = array(); |
|
1012 |
$stripped = array(); |
|
1013 |
$first = true; |
|
1014 |
foreach ( $lines as $line ) { |
|
1015 |
# If the line is too long, just pretend the entire line is one big word |
|
1016 |
# This prevents resource exhaustion problems |
|
1017 |
if ( $first ) { |
|
1018 |
$first = false; |
|
1019 |
} else { |
|
1020 |
$words[] = "\n"; |
|
1021 |
$stripped[] = "\n"; |
|
1022 |
} |
|
1023 |
if ( strlen( $line ) > MAX_LINE_LENGTH ) { |
|
1024 |
$words[] = $line; |
|
1025 |
$stripped[] = $line; |
|
1026 |
} else { |
|
1027 |
if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs', |
|
1028 |
$line, $m)) |
|
1029 |
{ |
|
1030 |
$words = array_merge( $words, $m[0] ); |
|
1031 |
$stripped = array_merge( $stripped, $m[1] ); |
|
1032 |
} |
|
1033 |
} |
|
1034 |
} |
|
1035 |
// wfProfileOut( $fname ); |
|
1036 |
return array($words, $stripped); |
|
1037 |
} |
|
1038 |
||
1039 |
function orig () { |
|
1040 |
$fname = 'WordLevelDiff::orig'; |
|
1041 |
// wfProfileIn( $fname ); |
|
1042 |
$orig = new _HWLDF_WordAccumulator; |
|
1043 |
||
1044 |
foreach ($this->edits as $edit) { |
|
1045 |
if ($edit->type == 'copy') |
|
1046 |
$orig->addWords($edit->orig); |
|
1047 |
elseif ($edit->orig) |
|
1048 |
$orig->addWords($edit->orig, 'mark'); |
|
1049 |
} |
|
1050 |
$lines = $orig->getLines(); |
|
1051 |
// wfProfileOut( $fname ); |
|
1052 |
return $lines; |
|
1053 |
} |
|
1054 |
||
1055 |
function closing () { |
|
1056 |
$fname = 'WordLevelDiff::closing'; |
|
1057 |
// wfProfileIn( $fname ); |
|
1058 |
$closing = new _HWLDF_WordAccumulator; |
|
1059 |
||
1060 |
foreach ($this->edits as $edit) { |
|
1061 |
if ($edit->type == 'copy') |
|
1062 |
$closing->addWords($edit->closing); |
|
1063 |
elseif ($edit->closing) |
|
1064 |
$closing->addWords($edit->closing, 'mark'); |
|
1065 |
} |
|
1066 |
$lines = $closing->getLines(); |
|
1067 |
// wfProfileOut( $fname ); |
|
1068 |
return $lines; |
|
1069 |
} |
|
1070 |
} |
|
1071 |
||
1072 |
/** |
|
1073 |
* Wikipedia Table style diff formatter. |
|
1074 |
* @todo document |
|
1075 |
* @package Enano |
|
1076 |
* @subpackage DifferenceEngine |
|
1077 |
*/ |
|
1078 |
class TableDiffFormatter extends DiffFormatter |
|
1079 |
{ |
|
1080 |
function TableDiffFormatter() { |
|
1081 |
$this->leading_context_lines = 2; |
|
1082 |
$this->trailing_context_lines = 2; |
|
1083 |
} |
|
1084 |
||
1085 |
function _block_header( $xbeg, $xlen, $ybeg, $ylen ) { |
|
1086 |
$r = '<tr><td colspan="2" align="left"><strong><!--LINE '.$xbeg."--></strong></td>\n" . |
|
1087 |
'<td colspan="2" align="left"><strong><!--LINE '.$ybeg."--></strong></td></tr>\n"; |
|
1088 |
return $r; |
|
1089 |
} |
|
1090 |
||
1091 |
function _start_block( $header ) { |
|
1092 |
echo $header; |
|
1093 |
} |
|
1094 |
||
1095 |
function _end_block() { |
|
1096 |
} |
|
1097 |
||
1098 |
function _lines( $lines, $prefix=' ', $color='white' ) { |
|
1099 |
} |
|
1100 |
||
1101 |
# HTML-escape parameter before calling this |
|
1102 |
function addedLine( $line ) { |
|
1103 |
return "<td>+</td><td class='diff-addedline'>{$line}</td>"; |
|
1104 |
} |
|
1105 |
||
1106 |
# HTML-escape parameter before calling this |
|
1107 |
function deletedLine( $line ) { |
|
1108 |
return "<td>-</td><td class='diff-deletedline'>{$line}</td>"; |
|
1109 |
} |
|
1110 |
||
1111 |
# HTML-escape parameter before calling this |
|
1112 |
function contextLine( $line ) { |
|
1113 |
return "<td> </td><td class='diff-context'>{$line}</td>"; |
|
1114 |
} |
|
1115 |
||
1116 |
function emptyLine() { |
|
1117 |
return '<td colspan="2"> </td>'; |
|
1118 |
} |
|
1119 |
||
1120 |
function _added( $lines ) { |
|
1121 |
foreach ($lines as $line) { |
|
1122 |
echo '<tr>' . $this->emptyLine() . |
|
1123 |
$this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n"; |
|
1124 |
} |
|
1125 |
} |
|
1126 |
||
1127 |
function _deleted($lines) { |
|
1128 |
foreach ($lines as $line) { |
|
1129 |
echo '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) . |
|
1130 |
$this->emptyLine() . "</tr>\n"; |
|
1131 |
} |
|
1132 |
} |
|
1133 |
||
1134 |
function _context( $lines ) { |
|
1135 |
foreach ($lines as $line) { |
|
1136 |
echo '<tr>' . |
|
1137 |
$this->contextLine( htmlspecialchars ( $line ) ) . |
|
1138 |
$this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n"; |
|
1139 |
} |
|
1140 |
} |
|
1141 |
||
1142 |
function _changed( $orig, $closing ) { |
|
1143 |
$fname = 'TableDiffFormatter::_changed'; |
|
1144 |
// wfProfileIn( $fname ); |
|
1145 |
||
1146 |
$diff = new WordLevelDiff( $orig, $closing ); |
|
1147 |
$del = $diff->orig(); |
|
1148 |
$add = $diff->closing(); |
|
1149 |
||
1150 |
# Notice that WordLevelDiff returns HTML-escaped output. |
|
1151 |
# Hence, we will be calling addedLine/deletedLine without HTML-escaping. |
|
1152 |
||
1153 |
while ( $line = array_shift( $del ) ) { |
|
1154 |
$aline = array_shift( $add ); |
|
1155 |
echo '<tr>' . $this->deletedLine( $line ) . |
|
1156 |
$this->addedLine( $aline ) . "</tr>\n"; |
|
1157 |
} |
|
1158 |
foreach ($add as $line) { # If any leftovers |
|
1159 |
echo '<tr>' . $this->emptyLine() . |
|
1160 |
$this->addedLine( $line ) . "</tr>\n"; |
|
1161 |
} |
|
1162 |
// wfProfileOut( $fname ); |
|
1163 |
} |
|
1164 |
} |
|
1165 |