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