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