Update formatting for includes/diff
[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 abstract class DiffOp {
34 public $type;
35 public $orig;
36 public $closing;
37
38 public abstract function reverse();
39
40 /**
41 * @return int
42 */
43 function norig() {
44 return $this->orig ? count( $this->orig ) : 0;
45 }
46
47 /**
48 * @return int
49 */
50 function nclosing() {
51 return $this->closing ? count( $this->closing ) : 0;
52 }
53 }
54
55 /**
56 * @todo document
57 * @private
58 * @ingroup DifferenceEngine
59 */
60 class DiffOp_Copy extends DiffOp {
61 public $type = 'copy';
62
63 function __construct( $orig, $closing = false ) {
64 if ( !is_array( $closing ) ) {
65 $closing = $orig;
66 }
67 $this->orig = $orig;
68 $this->closing = $closing;
69 }
70
71 /**
72 * @return DiffOp_Copy
73 */
74 function reverse() {
75 return new DiffOp_Copy( $this->closing, $this->orig );
76 }
77 }
78
79 /**
80 * @todo document
81 * @private
82 * @ingroup DifferenceEngine
83 */
84 class DiffOp_Delete extends DiffOp {
85 public $type = 'delete';
86
87 function __construct( $lines ) {
88 $this->orig = $lines;
89 $this->closing = false;
90 }
91
92 /**
93 * @return DiffOp_Add
94 */
95 function reverse() {
96 return new DiffOp_Add( $this->orig );
97 }
98 }
99
100 /**
101 * @todo document
102 * @private
103 * @ingroup DifferenceEngine
104 */
105 class DiffOp_Add extends DiffOp {
106 public $type = 'add';
107
108 function __construct( $lines ) {
109 $this->closing = $lines;
110 $this->orig = false;
111 }
112
113 /**
114 * @return DiffOp_Delete
115 */
116 function reverse() {
117 return new DiffOp_Delete( $this->closing );
118 }
119 }
120
121 /**
122 * @todo document
123 * @private
124 * @ingroup DifferenceEngine
125 */
126 class DiffOp_Change extends DiffOp {
127 public $type = 'change';
128
129 function __construct( $orig, $closing ) {
130 $this->orig = $orig;
131 $this->closing = $closing;
132 }
133
134 /**
135 * @return DiffOp_Change
136 */
137 function reverse() {
138 return new DiffOp_Change( $this->closing, $this->orig );
139 }
140 }
141
142 /**
143 * Class used internally by Diff to actually compute the diffs.
144 *
145 * The algorithm used here is mostly lifted from the perl module
146 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
147 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
148 *
149 * More ideas are taken from:
150 * http://www.ics.uci.edu/~eppstein/161/960229.html
151 *
152 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
153 * diffutils-2.7, which can be found at:
154 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
155 *
156 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
157 * are my own.
158 *
159 * Line length limits for robustness added by Tim Starling, 2005-08-31
160 * Alternative implementation added by Guy Van den Broeck, 2008-07-30
161 *
162 * @author Geoffrey T. Dairiki, Tim Starling, Guy Van den Broeck
163 * @private
164 * @ingroup DifferenceEngine
165 */
166 class DiffEngine {
167 const MAX_XREF_LENGTH = 10000;
168
169 protected $xchanged, $ychanged;
170
171 protected $xv = array(), $yv = array();
172 protected $xind = array(), $yind = array();
173
174 protected $seq = array(), $in_seq = array();
175
176 protected $lcs = 0;
177
178 /**
179 * @param $from_lines
180 * @param $to_lines
181 * @return array
182 */
183 function diff( $from_lines, $to_lines ) {
184 wfProfileIn( __METHOD__ );
185
186 // Diff and store locally
187 $this->diffLocal( $from_lines, $to_lines );
188
189 // Merge edits when possible
190 $this->shiftBoundaries( $from_lines, $this->xchanged, $this->ychanged );
191 $this->shiftBoundaries( $to_lines, $this->ychanged, $this->xchanged );
192
193 // Compute the edit operations.
194 $n_from = count( $from_lines );
195 $n_to = count( $to_lines );
196
197 $edits = array();
198 $xi = $yi = 0;
199 while ( $xi < $n_from || $yi < $n_to ) {
200 assert( '$yi < $n_to || $this->xchanged[$xi]' );
201 assert( '$xi < $n_from || $this->ychanged[$yi]' );
202
203 // Skip matching "snake".
204 $copy = array();
205 while ( $xi < $n_from && $yi < $n_to
206 && !$this->xchanged[$xi] && !$this->ychanged[$yi]
207 ) {
208 $copy[] = $from_lines[$xi++];
209 ++$yi;
210 }
211 if ( $copy ) {
212 $edits[] = new DiffOp_Copy( $copy );
213 }
214
215 // Find deletes & adds.
216 $delete = array();
217 while ( $xi < $n_from && $this->xchanged[$xi] ) {
218 $delete[] = $from_lines[$xi++];
219 }
220
221 $add = array();
222 while ( $yi < $n_to && $this->ychanged[$yi] ) {
223 $add[] = $to_lines[$yi++];
224 }
225
226 if ( $delete && $add ) {
227 $edits[] = new DiffOp_Change( $delete, $add );
228 } elseif ( $delete ) {
229 $edits[] = new DiffOp_Delete( $delete );
230 } elseif ( $add ) {
231 $edits[] = new DiffOp_Add( $add );
232 }
233 }
234 wfProfileOut( __METHOD__ );
235
236 return $edits;
237 }
238
239 /**
240 * @param $from_lines
241 * @param $to_lines
242 */
243 private function diffLocal( $from_lines, $to_lines ) {
244 global $wgExternalDiffEngine;
245 wfProfileIn( __METHOD__ );
246
247 if ( $wgExternalDiffEngine == 'wikidiff3' ) {
248 // wikidiff3
249 $wikidiff3 = new WikiDiff3();
250 $wikidiff3->diff( $from_lines, $to_lines );
251 $this->xchanged = $wikidiff3->removed;
252 $this->ychanged = $wikidiff3->added;
253 unset( $wikidiff3 );
254 } else {
255 // old diff
256 $n_from = count( $from_lines );
257 $n_to = count( $to_lines );
258 $this->xchanged = $this->ychanged = array();
259 $this->xv = $this->yv = array();
260 $this->xind = $this->yind = array();
261 unset( $this->seq );
262 unset( $this->in_seq );
263 unset( $this->lcs );
264
265 // Skip leading common lines.
266 for ( $skip = 0; $skip < $n_from && $skip < $n_to; $skip++ ) {
267 if ( $from_lines[$skip] !== $to_lines[$skip] ) {
268 break;
269 }
270 $this->xchanged[$skip] = $this->ychanged[$skip] = false;
271 }
272 // Skip trailing common lines.
273 $xi = $n_from;
274 $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->lineHash( $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->lineHash( $line )] ) ) ) {
290 continue;
291 }
292 $yhash[$this->lineHash( $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->lineHash( $line )] ) ) ) {
299 continue;
300 }
301 $this->xv[] = $line;
302 $this->xind[] = $xi;
303 }
304
305 // Find the LCS.
306 $this->compareSeq( 0, count( $this->xv ), 0, count( $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 private function lineHash( $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 private 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->lcsPos( $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->lcsPos( $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 private function lcsPos( $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
436 return $this->lcs;
437 }
438
439 $beg = 1;
440 while ( $beg < $end ) {
441 $mid = (int)( ( $beg + $end ) / 2 );
442 if ( $ypos > $this->seq[$mid] ) {
443 $beg = $mid + 1;
444 } else {
445 $end = $mid;
446 }
447 }
448
449 assert( '$ypos != $this->seq[$end]' );
450
451 $this->in_seq[$this->seq[$end]] = false;
452 $this->seq[$end] = $ypos;
453 $this->in_seq[$ypos] = 1;
454
455 return $end;
456 }
457
458 /**
459 * Find LCS of two sequences.
460 *
461 * The results are recorded in the vectors $this->{x,y}changed[], by
462 * storing a 1 in the element for each line that is an insertion
463 * or deletion (ie. is not in the LCS).
464 *
465 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
466 *
467 * Note that XLIM, YLIM are exclusive bounds.
468 * All line numbers are origin-0 and discarded lines are not counted.
469 * @param $xoff
470 * @param $xlim
471 * @param $yoff
472 * @param $ylim
473 */
474 private function compareSeq( $xoff, $xlim, $yoff, $ylim ) {
475 // Slide down the bottom initial diagonal.
476 while ( $xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff] ) {
477 ++$xoff;
478 ++$yoff;
479 }
480
481 // Slide up the top initial diagonal.
482 while ( $xlim > $xoff && $ylim > $yoff
483 && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]
484 ) {
485 --$xlim;
486 --$ylim;
487 }
488
489 if ( $xoff == $xlim || $yoff == $ylim ) {
490 $lcs = 0;
491 } else {
492 // This is ad hoc but seems to work well.
493 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
494 // $nchunks = max(2,min(8,(int)$nchunks));
495 $nchunks = min( 7, $xlim - $xoff, $ylim - $yoff ) + 1;
496 list( $lcs, $seps ) = $this->diag( $xoff, $xlim, $yoff, $ylim, $nchunks );
497 }
498
499 if ( $lcs == 0 ) {
500 // X and Y sequences have no common subsequence:
501 // mark all changed.
502 while ( $yoff < $ylim ) {
503 $this->ychanged[$this->yind[$yoff++]] = 1;
504 }
505 while ( $xoff < $xlim ) {
506 $this->xchanged[$this->xind[$xoff++]] = 1;
507 }
508 } else {
509 // Use the partitions to split this problem into subproblems.
510 reset( $seps );
511 $pt1 = $seps[0];
512 while ( $pt2 = next( $seps ) ) {
513 $this->compareSeq( $pt1[0], $pt2[0], $pt1[1], $pt2[1] );
514 $pt1 = $pt2;
515 }
516 }
517 }
518
519 /**
520 * Adjust inserts/deletes of identical lines to join changes
521 * as much as possible.
522 *
523 * We do something when a run of changed lines include a
524 * line at one end and has an excluded, identical line at the other.
525 * We are free to choose which identical line is included.
526 * `compareseq' usually chooses the one at the beginning,
527 * but usually it is cleaner to consider the following identical line
528 * to be the "change".
529 *
530 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
531 */
532 private function shiftBoundaries( $lines, &$changed, $other_changed ) {
533 wfProfileIn( __METHOD__ );
534 $i = 0;
535 $j = 0;
536
537 assert( 'count($lines) == count($changed)' );
538 $len = count( $lines );
539 $other_len = count( $other_changed );
540
541 while ( 1 ) {
542 /*
543 * Scan forwards to find beginning of another run of changes.
544 * Also keep track of the corresponding point in the other file.
545 *
546 * Throughout this code, $i and $j are adjusted together so that
547 * the first $i elements of $changed and the first $j elements
548 * of $other_changed both contain the same number of zeros
549 * (unchanged lines).
550 * Furthermore, $j is always kept so that $j == $other_len or
551 * $other_changed[$j] == false.
552 */
553 while ( $j < $other_len && $other_changed[$j] ) {
554 $j++;
555 }
556
557 while ( $i < $len && !$changed[$i] ) {
558 assert( '$j < $other_len && ! $other_changed[$j]' );
559 $i++;
560 $j++;
561 while ( $j < $other_len && $other_changed[$j] ) {
562 $j++;
563 }
564 }
565
566 if ( $i == $len ) {
567 break;
568 }
569
570 $start = $i;
571
572 // Find the end of this run of changes.
573 while ( ++$i < $len && $changed[$i] ) {
574 continue;
575 }
576
577 do {
578 /*
579 * Record the length of this run of changes, so that
580 * we can later determine whether the run has grown.
581 */
582 $runlength = $i - $start;
583
584 /*
585 * Move the changed region back, so long as the
586 * previous unchanged line matches the last changed one.
587 * This merges with previous changed regions.
588 */
589 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
590 $changed[--$start] = 1;
591 $changed[--$i] = false;
592 while ( $start > 0 && $changed[$start - 1] ) {
593 $start--;
594 }
595 assert( '$j > 0' );
596 while ( $other_changed[--$j] ) {
597 continue;
598 }
599 assert( '$j >= 0 && !$other_changed[$j]' );
600 }
601
602 /*
603 * Set CORRESPONDING to the end of the changed run, at the last
604 * point where it corresponds to a changed run in the other file.
605 * CORRESPONDING == LEN means no such point has been found.
606 */
607 $corresponding = $j < $other_len ? $i : $len;
608
609 /*
610 * Move the changed region forward, so long as the
611 * first changed line matches the following unchanged one.
612 * This merges with following changed regions.
613 * Do this second, so that if there are no merges,
614 * the changed region is moved forward as far as possible.
615 */
616 while ( $i < $len && $lines[$start] == $lines[$i] ) {
617 $changed[$start++] = false;
618 $changed[$i++] = 1;
619 while ( $i < $len && $changed[$i] ) {
620 $i++;
621 }
622
623 assert( '$j < $other_len && ! $other_changed[$j]' );
624 $j++;
625 if ( $j < $other_len && $other_changed[$j] ) {
626 $corresponding = $i;
627 while ( $j < $other_len && $other_changed[$j] ) {
628 $j++;
629 }
630 }
631 }
632 } while ( $runlength != $i - $start );
633
634 /*
635 * If possible, move the fully-merged run of changes
636 * back to a corresponding run in the other file.
637 */
638 while ( $corresponding < $i ) {
639 $changed[--$start] = 1;
640 $changed[--$i] = 0;
641 assert( '$j > 0' );
642 while ( $other_changed[--$j] ) {
643 continue;
644 }
645 assert( '$j >= 0 && !$other_changed[$j]' );
646 }
647 }
648 wfProfileOut( __METHOD__ );
649 }
650 }
651
652 /**
653 * Class representing a 'diff' between two sequences of strings.
654 * @todo document
655 * @private
656 * @ingroup DifferenceEngine
657 */
658 class Diff {
659 public $edits;
660
661 /**
662 * Constructor.
663 * Computes diff between sequences of strings.
664 *
665 * @param $from_lines array An array of strings.
666 * Typically these are lines from a file.
667 * @param $to_lines array An array of strings.
668 */
669 function __construct( $from_lines, $to_lines ) {
670 $eng = new DiffEngine;
671 $this->edits = $eng->diff( $from_lines, $to_lines );
672 // $this->check($from_lines, $to_lines);
673 }
674
675 /**
676 * Compute reversed Diff.
677 *
678 * SYNOPSIS:
679 *
680 * $diff = new Diff($lines1, $lines2);
681 * $rev = $diff->reverse();
682 * @return Object A Diff object representing the inverse of the
683 * original diff.
684 */
685 function reverse() {
686 $rev = $this;
687 $rev->edits = array();
688 /** @var DiffOp $edit */
689 foreach ( $this->edits as $edit ) {
690 $rev->edits[] = $edit->reverse();
691 }
692
693 return $rev;
694 }
695
696 /**
697 * Check for empty diff.
698 *
699 * @return bool True if two sequences were identical.
700 */
701 function isEmpty() {
702 foreach ( $this->edits as $edit ) {
703 if ( $edit->type != 'copy' ) {
704 return false;
705 }
706 }
707
708 return true;
709 }
710
711 /**
712 * Compute the length of the Longest Common Subsequence (LCS).
713 *
714 * This is mostly for diagnostic purposed.
715 *
716 * @return int The length of the LCS.
717 */
718 function lcs() {
719 $lcs = 0;
720 foreach ( $this->edits as $edit ) {
721 if ( $edit->type == 'copy' ) {
722 $lcs += count( $edit->orig );
723 }
724 }
725
726 return $lcs;
727 }
728
729 /**
730 * Get the original set of lines.
731 *
732 * This reconstructs the $from_lines parameter passed to the
733 * constructor.
734 *
735 * @return array The original sequence of strings.
736 */
737 function orig() {
738 $lines = array();
739
740 foreach ( $this->edits as $edit ) {
741 if ( $edit->orig ) {
742 array_splice( $lines, count( $lines ), 0, $edit->orig );
743 }
744 }
745
746 return $lines;
747 }
748
749 /**
750 * Get the closing set of lines.
751 *
752 * This reconstructs the $to_lines parameter passed to the
753 * constructor.
754 *
755 * @return array The sequence of strings.
756 */
757 function closing() {
758 $lines = array();
759
760 foreach ( $this->edits as $edit ) {
761 if ( $edit->closing ) {
762 array_splice( $lines, count( $lines ), 0, $edit->closing );
763 }
764 }
765
766 return $lines;
767 }
768
769 /**
770 * Check a Diff for validity.
771 *
772 * This is here only for debugging purposes.
773 * @param $from_lines
774 * @param $to_lines
775 */
776 private function check( $from_lines, $to_lines ) {
777 wfProfileIn( __METHOD__ );
778 if ( serialize( $from_lines ) != serialize( $this->orig() ) ) {
779 trigger_error( "Reconstructed original doesn't match", E_USER_ERROR );
780 }
781 if ( serialize( $to_lines ) != serialize( $this->closing() ) ) {
782 trigger_error( "Reconstructed closing doesn't match", E_USER_ERROR );
783 }
784
785 $rev = $this->reverse();
786 if ( serialize( $to_lines ) != serialize( $rev->orig() ) ) {
787 trigger_error( "Reversed original doesn't match", E_USER_ERROR );
788 }
789 if ( serialize( $from_lines ) != serialize( $rev->closing() ) ) {
790 trigger_error( "Reversed closing doesn't match", E_USER_ERROR );
791 }
792
793 $prevtype = 'none';
794 foreach ( $this->edits as $edit ) {
795 if ( $prevtype == $edit->type ) {
796 trigger_error( 'Edit sequence is non-optimal', E_USER_ERROR );
797 }
798 $prevtype = $edit->type;
799 }
800
801 $lcs = $this->lcs();
802 trigger_error( 'Diff okay: LCS = ' . $lcs, E_USER_NOTICE );
803 wfProfileOut( __METHOD__ );
804 }
805 }
806
807 /**
808 * @todo document, bad name.
809 * @private
810 * @ingroup DifferenceEngine
811 */
812 class MappedDiff extends Diff {
813 /**
814 * Constructor.
815 *
816 * Computes diff between sequences of strings.
817 *
818 * This can be used to compute things like
819 * case-insensitve diffs, or diffs which ignore
820 * changes in white-space.
821 *
822 * @param $from_lines array An array of strings.
823 * Typically these are lines from a file.
824 *
825 * @param $to_lines array An array of strings.
826 *
827 * @param $mapped_from_lines array This array should
828 * have the same size number of elements as $from_lines.
829 * The elements in $mapped_from_lines and
830 * $mapped_to_lines are what is actually compared
831 * when computing the diff.
832 *
833 * @param $mapped_to_lines array This array should
834 * have the same number of elements as $to_lines.
835 */
836 function __construct( $from_lines, $to_lines,
837 $mapped_from_lines, $mapped_to_lines ) {
838 wfProfileIn( __METHOD__ );
839
840 assert( 'count( $from_lines ) == count( $mapped_from_lines )' );
841 assert( 'count( $to_lines ) == count( $mapped_to_lines )' );
842
843 parent::__construct( $mapped_from_lines, $mapped_to_lines );
844
845 $xi = $yi = 0;
846 for ( $i = 0; $i < count( $this->edits ); $i++ ) {
847 $orig = &$this->edits[$i]->orig;
848 if ( is_array( $orig ) ) {
849 $orig = array_slice( $from_lines, $xi, count( $orig ) );
850 $xi += count( $orig );
851 }
852
853 $closing = &$this->edits[$i]->closing;
854 if ( is_array( $closing ) ) {
855 $closing = array_slice( $to_lines, $yi, count( $closing ) );
856 $yi += count( $closing );
857 }
858 }
859 wfProfileOut( __METHOD__ );
860 }
861 }
862
863 /**
864 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
865 */
866
867 /**
868 * @todo document
869 * @private
870 * @ingroup DifferenceEngine
871 */
872 class HWLDF_WordAccumulator {
873 private $lines = array();
874 private $line = '';
875 private $group = '';
876 private $tag = '';
877
878 /**
879 * @param $new_tag
880 */
881 private function flushGroup( $new_tag ) {
882 if ( $this->group !== '' ) {
883 if ( $this->tag == 'ins' ) {
884 $this->line .= '<ins class="diffchange diffchange-inline">' .
885 htmlspecialchars( $this->group ) . '</ins>';
886 } elseif ( $this->tag == 'del' ) {
887 $this->line .= '<del class="diffchange diffchange-inline">' .
888 htmlspecialchars( $this->group ) . '</del>';
889 } else {
890 $this->line .= htmlspecialchars( $this->group );
891 }
892 }
893 $this->group = '';
894 $this->tag = $new_tag;
895 }
896
897 /**
898 * @param $new_tag
899 */
900 private function flushLine( $new_tag ) {
901 $this->flushGroup( $new_tag );
902 if ( $this->line != '' ) {
903 array_push( $this->lines, $this->line );
904 } else {
905 # make empty lines visible by inserting an NBSP
906 array_push( $this->lines, '&#160;' );
907 }
908 $this->line = '';
909 }
910
911 /**
912 * @param $words
913 * @param $tag string
914 */
915 public function addWords( $words, $tag = '' ) {
916 if ( $tag != $this->tag ) {
917 $this->flushGroup( $tag );
918 }
919
920 foreach ( $words as $word ) {
921 // new-line should only come as first char of word.
922 if ( $word == '' ) {
923 continue;
924 }
925 if ( $word[0] == "\n" ) {
926 $this->flushLine( $tag );
927 $word = substr( $word, 1 );
928 }
929 assert( '!strstr( $word, "\n" )' );
930 $this->group .= $word;
931 }
932 }
933
934 /**
935 * @return array
936 */
937 public function getLines() {
938 $this->flushLine( '~done' );
939
940 return $this->lines;
941 }
942 }
943
944 /**
945 * @todo document
946 * @private
947 * @ingroup DifferenceEngine
948 */
949 class WordLevelDiff extends MappedDiff {
950 const MAX_LINE_LENGTH = 10000;
951
952 /**
953 * @param $orig_lines
954 * @param $closing_lines
955 */
956 function __construct( $orig_lines, $closing_lines ) {
957 wfProfileIn( __METHOD__ );
958
959 list( $orig_words, $orig_stripped ) = $this->split( $orig_lines );
960 list( $closing_words, $closing_stripped ) = $this->split( $closing_lines );
961
962 parent::__construct( $orig_words, $closing_words,
963 $orig_stripped, $closing_stripped );
964 wfProfileOut( __METHOD__ );
965 }
966
967 /**
968 * @param $lines
969 * @return array
970 */
971 private function split( $lines ) {
972 wfProfileIn( __METHOD__ );
973
974 $words = array();
975 $stripped = array();
976 $first = true;
977 foreach ( $lines as $line ) {
978 # If the line is too long, just pretend the entire line is one big word
979 # This prevents resource exhaustion problems
980 if ( $first ) {
981 $first = false;
982 } else {
983 $words[] = "\n";
984 $stripped[] = "\n";
985 }
986 if ( strlen( $line ) > self::MAX_LINE_LENGTH ) {
987 $words[] = $line;
988 $stripped[] = $line;
989 } else {
990 $m = array();
991 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
992 $line, $m )
993 ) {
994 foreach ( $m[0] as $word ) {
995 $words[] = $word;
996 }
997 foreach ( $m[1] as $stripped_word ) {
998 $stripped[] = $stripped_word;
999 }
1000 }
1001 }
1002 }
1003 wfProfileOut( __METHOD__ );
1004
1005 return array( $words, $stripped );
1006 }
1007
1008 /**
1009 * @return array
1010 */
1011 public function orig() {
1012 wfProfileIn( __METHOD__ );
1013 $orig = new HWLDF_WordAccumulator;
1014
1015 foreach ( $this->edits as $edit ) {
1016 if ( $edit->type == 'copy' ) {
1017 $orig->addWords( $edit->orig );
1018 } elseif ( $edit->orig ) {
1019 $orig->addWords( $edit->orig, 'del' );
1020 }
1021 }
1022 $lines = $orig->getLines();
1023 wfProfileOut( __METHOD__ );
1024
1025 return $lines;
1026 }
1027
1028 /**
1029 * @return array
1030 */
1031 public function closing() {
1032 wfProfileIn( __METHOD__ );
1033 $closing = new HWLDF_WordAccumulator;
1034
1035 foreach ( $this->edits as $edit ) {
1036 if ( $edit->type == 'copy' ) {
1037 $closing->addWords( $edit->closing );
1038 } elseif ( $edit->closing ) {
1039 $closing->addWords( $edit->closing, 'ins' );
1040 }
1041 }
1042 $lines = $closing->getLines();
1043 wfProfileOut( __METHOD__ );
1044
1045 return $lines;
1046 }
1047 }