Merge "Document the wikipage.content hook"
[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 abstract public 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 DiffOpCopy 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 DiffOpCopy
73 */
74 function reverse() {
75 return new DiffOpCopy( $this->closing, $this->orig );
76 }
77 }
78
79 /**
80 * @todo document
81 * @private
82 * @ingroup DifferenceEngine
83 */
84 class DiffOpDelete extends DiffOp {
85 public $type = 'delete';
86
87 function __construct( $lines ) {
88 $this->orig = $lines;
89 $this->closing = false;
90 }
91
92 /**
93 * @return DiffOpAdd
94 */
95 function reverse() {
96 return new DiffOpAdd( $this->orig );
97 }
98 }
99
100 /**
101 * @todo document
102 * @private
103 * @ingroup DifferenceEngine
104 */
105 class DiffOpAdd extends DiffOp {
106 public $type = 'add';
107
108 function __construct( $lines ) {
109 $this->closing = $lines;
110 $this->orig = false;
111 }
112
113 /**
114 * @return DiffOpDelete
115 */
116 function reverse() {
117 return new DiffOpDelete( $this->closing );
118 }
119 }
120
121 /**
122 * @todo document
123 * @private
124 * @ingroup DifferenceEngine
125 */
126 class DiffOpChange 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 DiffOpChange
136 */
137 function reverse() {
138 return new DiffOpChange( $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 DiffOpCopy( $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 DiffOpChange( $delete, $add );
228 } elseif ( $delete ) {
229 $edits[] = new DiffOpDelete( $delete );
230 } elseif ( $add ) {
231 $edits[] = new DiffOpAdd( $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
388 $k = 0;
389 $matches = $ymatches[$line];
390 reset( $matches );
391 while ( list( , $y ) = each( $matches ) ) {
392 if ( empty( $this->in_seq[$y] ) ) {
393 $k = $this->lcsPos( $y );
394 assert( '$k > 0' );
395 $ymids[$k] = $ymids[$k - 1];
396 break;
397 }
398 }
399
400 while ( list( , $y ) = each( $matches ) ) {
401 if ( $y > $this->seq[$k - 1] ) {
402 assert( '$y < $this->seq[$k]' );
403 // Optimization: this is a common case:
404 // next match is just replacing previous match.
405 $this->in_seq[$this->seq[$k]] = false;
406 $this->seq[$k] = $y;
407 $this->in_seq[$y] = 1;
408 } elseif ( empty( $this->in_seq[$y] ) ) {
409 $k = $this->lcsPos( $y );
410 assert( '$k > 0' );
411 $ymids[$k] = $ymids[$k - 1];
412 }
413 }
414 }
415 }
416
417 $seps[] = $flip ? array( $yoff, $xoff ) : array( $xoff, $yoff );
418 $ymid = $ymids[$this->lcs];
419 for ( $n = 0; $n < $nchunks - 1; $n++ ) {
420 $x1 = $xoff + (int)( ( $numer + ( $xlim - $xoff ) * $n ) / $nchunks );
421 $y1 = $ymid[$n] + 1;
422 $seps[] = $flip ? array( $y1, $x1 ) : array( $x1, $y1 );
423 }
424 $seps[] = $flip ? array( $ylim, $xlim ) : array( $xlim, $ylim );
425
426 return array( $this->lcs, $seps );
427 }
428
429 /**
430 * @param $ypos
431 * @return int
432 */
433 private function lcsPos( $ypos ) {
434 $end = $this->lcs;
435 if ( $end == 0 || $ypos > $this->seq[$end] ) {
436 $this->seq[++$this->lcs] = $ypos;
437 $this->in_seq[$ypos] = 1;
438
439 return $this->lcs;
440 }
441
442 $beg = 1;
443 while ( $beg < $end ) {
444 $mid = (int)( ( $beg + $end ) / 2 );
445 if ( $ypos > $this->seq[$mid] ) {
446 $beg = $mid + 1;
447 } else {
448 $end = $mid;
449 }
450 }
451
452 assert( '$ypos != $this->seq[$end]' );
453
454 $this->in_seq[$this->seq[$end]] = false;
455 $this->seq[$end] = $ypos;
456 $this->in_seq[$ypos] = 1;
457
458 return $end;
459 }
460
461 /**
462 * Find LCS of two sequences.
463 *
464 * The results are recorded in the vectors $this->{x,y}changed[], by
465 * storing a 1 in the element for each line that is an insertion
466 * or deletion (ie. is not in the LCS).
467 *
468 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
469 *
470 * Note that XLIM, YLIM are exclusive bounds.
471 * All line numbers are origin-0 and discarded lines are not counted.
472 * @param $xoff
473 * @param $xlim
474 * @param $yoff
475 * @param $ylim
476 */
477 private function compareSeq( $xoff, $xlim, $yoff, $ylim ) {
478 // Slide down the bottom initial diagonal.
479 while ( $xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff] ) {
480 ++$xoff;
481 ++$yoff;
482 }
483
484 // Slide up the top initial diagonal.
485 while ( $xlim > $xoff && $ylim > $yoff
486 && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]
487 ) {
488 --$xlim;
489 --$ylim;
490 }
491
492 if ( $xoff == $xlim || $yoff == $ylim ) {
493 $lcs = 0;
494 } else {
495 // This is ad hoc but seems to work well.
496 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
497 // $nchunks = max(2,min(8,(int)$nchunks));
498 $nchunks = min( 7, $xlim - $xoff, $ylim - $yoff ) + 1;
499 list( $lcs, $seps ) = $this->diag( $xoff, $xlim, $yoff, $ylim, $nchunks );
500 }
501
502 if ( $lcs == 0 ) {
503 // X and Y sequences have no common subsequence:
504 // mark all changed.
505 while ( $yoff < $ylim ) {
506 $this->ychanged[$this->yind[$yoff++]] = 1;
507 }
508 while ( $xoff < $xlim ) {
509 $this->xchanged[$this->xind[$xoff++]] = 1;
510 }
511 } else {
512 // Use the partitions to split this problem into subproblems.
513 reset( $seps );
514 $pt1 = $seps[0];
515 while ( $pt2 = next( $seps ) ) {
516 $this->compareSeq( $pt1[0], $pt2[0], $pt1[1], $pt2[1] );
517 $pt1 = $pt2;
518 }
519 }
520 }
521
522 /**
523 * Adjust inserts/deletes of identical lines to join changes
524 * as much as possible.
525 *
526 * We do something when a run of changed lines include a
527 * line at one end and has an excluded, identical line at the other.
528 * We are free to choose which identical line is included.
529 * `compareseq' usually chooses the one at the beginning,
530 * but usually it is cleaner to consider the following identical line
531 * to be the "change".
532 *
533 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
534 */
535 private function shiftBoundaries( $lines, &$changed, $other_changed ) {
536 wfProfileIn( __METHOD__ );
537 $i = 0;
538 $j = 0;
539
540 assert( 'count($lines) == count($changed)' );
541 $len = count( $lines );
542 $other_len = count( $other_changed );
543
544 while ( 1 ) {
545 /*
546 * Scan forwards to find beginning of another run of changes.
547 * Also keep track of the corresponding point in the other file.
548 *
549 * Throughout this code, $i and $j are adjusted together so that
550 * the first $i elements of $changed and the first $j elements
551 * of $other_changed both contain the same number of zeros
552 * (unchanged lines).
553 * Furthermore, $j is always kept so that $j == $other_len or
554 * $other_changed[$j] == false.
555 */
556 while ( $j < $other_len && $other_changed[$j] ) {
557 $j++;
558 }
559
560 while ( $i < $len && !$changed[$i] ) {
561 assert( '$j < $other_len && ! $other_changed[$j]' );
562 $i++;
563 $j++;
564 while ( $j < $other_len && $other_changed[$j] ) {
565 $j++;
566 }
567 }
568
569 if ( $i == $len ) {
570 break;
571 }
572
573 $start = $i;
574
575 // Find the end of this run of changes.
576 while ( ++$i < $len && $changed[$i] ) {
577 continue;
578 }
579
580 do {
581 /*
582 * Record the length of this run of changes, so that
583 * we can later determine whether the run has grown.
584 */
585 $runlength = $i - $start;
586
587 /*
588 * Move the changed region back, so long as the
589 * previous unchanged line matches the last changed one.
590 * This merges with previous changed regions.
591 */
592 while ( $start > 0 && $lines[$start - 1] == $lines[$i - 1] ) {
593 $changed[--$start] = 1;
594 $changed[--$i] = false;
595 while ( $start > 0 && $changed[$start - 1] ) {
596 $start--;
597 }
598 assert( '$j > 0' );
599 while ( $other_changed[--$j] ) {
600 continue;
601 }
602 assert( '$j >= 0 && !$other_changed[$j]' );
603 }
604
605 /*
606 * Set CORRESPONDING to the end of the changed run, at the last
607 * point where it corresponds to a changed run in the other file.
608 * CORRESPONDING == LEN means no such point has been found.
609 */
610 $corresponding = $j < $other_len ? $i : $len;
611
612 /*
613 * Move the changed region forward, so long as the
614 * first changed line matches the following unchanged one.
615 * This merges with following changed regions.
616 * Do this second, so that if there are no merges,
617 * the changed region is moved forward as far as possible.
618 */
619 while ( $i < $len && $lines[$start] == $lines[$i] ) {
620 $changed[$start++] = false;
621 $changed[$i++] = 1;
622 while ( $i < $len && $changed[$i] ) {
623 $i++;
624 }
625
626 assert( '$j < $other_len && ! $other_changed[$j]' );
627 $j++;
628 if ( $j < $other_len && $other_changed[$j] ) {
629 $corresponding = $i;
630 while ( $j < $other_len && $other_changed[$j] ) {
631 $j++;
632 }
633 }
634 }
635 } while ( $runlength != $i - $start );
636
637 /*
638 * If possible, move the fully-merged run of changes
639 * back to a corresponding run in the other file.
640 */
641 while ( $corresponding < $i ) {
642 $changed[--$start] = 1;
643 $changed[--$i] = 0;
644 assert( '$j > 0' );
645 while ( $other_changed[--$j] ) {
646 continue;
647 }
648 assert( '$j >= 0 && !$other_changed[$j]' );
649 }
650 }
651 wfProfileOut( __METHOD__ );
652 }
653 }
654
655 /**
656 * Class representing a 'diff' between two sequences of strings.
657 * @todo document
658 * @private
659 * @ingroup DifferenceEngine
660 */
661 class Diff {
662 public $edits;
663
664 /**
665 * Constructor.
666 * Computes diff between sequences of strings.
667 *
668 * @param $from_lines array An array of strings.
669 * Typically these are lines from a file.
670 * @param $to_lines array An array of strings.
671 */
672 function __construct( $from_lines, $to_lines ) {
673 $eng = new DiffEngine;
674 $this->edits = $eng->diff( $from_lines, $to_lines );
675 }
676
677 /**
678 * Compute reversed Diff.
679 *
680 * SYNOPSIS:
681 *
682 * $diff = new Diff($lines1, $lines2);
683 * $rev = $diff->reverse();
684 * @return Object A Diff object representing the inverse of the
685 * original diff.
686 */
687 function reverse() {
688 $rev = $this;
689 $rev->edits = array();
690 /** @var DiffOp $edit */
691 foreach ( $this->edits as $edit ) {
692 $rev->edits[] = $edit->reverse();
693 }
694
695 return $rev;
696 }
697
698 /**
699 * Check for empty diff.
700 *
701 * @return bool True if two sequences were identical.
702 */
703 function isEmpty() {
704 foreach ( $this->edits as $edit ) {
705 if ( $edit->type != 'copy' ) {
706 return false;
707 }
708 }
709
710 return true;
711 }
712
713 /**
714 * Compute the length of the Longest Common Subsequence (LCS).
715 *
716 * This is mostly for diagnostic purposed.
717 *
718 * @return int The length of the LCS.
719 */
720 function lcs() {
721 $lcs = 0;
722 foreach ( $this->edits as $edit ) {
723 if ( $edit->type == 'copy' ) {
724 $lcs += count( $edit->orig );
725 }
726 }
727
728 return $lcs;
729 }
730
731 /**
732 * Get the original set of lines.
733 *
734 * This reconstructs the $from_lines parameter passed to the
735 * constructor.
736 *
737 * @return array The original sequence of strings.
738 */
739 function orig() {
740 $lines = array();
741
742 foreach ( $this->edits as $edit ) {
743 if ( $edit->orig ) {
744 array_splice( $lines, count( $lines ), 0, $edit->orig );
745 }
746 }
747
748 return $lines;
749 }
750
751 /**
752 * Get the closing set of lines.
753 *
754 * This reconstructs the $to_lines parameter passed to the
755 * constructor.
756 *
757 * @return array The sequence of strings.
758 */
759 function closing() {
760 $lines = array();
761
762 foreach ( $this->edits as $edit ) {
763 if ( $edit->closing ) {
764 array_splice( $lines, count( $lines ), 0, $edit->closing );
765 }
766 }
767
768 return $lines;
769 }
770 }
771
772 /**
773 * @todo document, bad name.
774 * @private
775 * @ingroup DifferenceEngine
776 */
777 class MappedDiff extends Diff {
778 /**
779 * Constructor.
780 *
781 * Computes diff between sequences of strings.
782 *
783 * This can be used to compute things like
784 * case-insensitve diffs, or diffs which ignore
785 * changes in white-space.
786 *
787 * @param $from_lines array An array of strings.
788 * Typically these are lines from a file.
789 *
790 * @param $to_lines array An array of strings.
791 *
792 * @param $mapped_from_lines array This array should
793 * have the same size number of elements as $from_lines.
794 * The elements in $mapped_from_lines and
795 * $mapped_to_lines are what is actually compared
796 * when computing the diff.
797 *
798 * @param $mapped_to_lines array This array should
799 * have the same number of elements as $to_lines.
800 */
801 function __construct( $from_lines, $to_lines,
802 $mapped_from_lines, $mapped_to_lines ) {
803 wfProfileIn( __METHOD__ );
804
805 assert( 'count( $from_lines ) == count( $mapped_from_lines )' );
806 assert( 'count( $to_lines ) == count( $mapped_to_lines )' );
807
808 parent::__construct( $mapped_from_lines, $mapped_to_lines );
809
810 $xi = $yi = 0;
811 $editCount = count( $this->edits );
812 for ( $i = 0; $i < $editCount; $i++ ) {
813 $orig = &$this->edits[$i]->orig;
814 if ( is_array( $orig ) ) {
815 $orig = array_slice( $from_lines, $xi, count( $orig ) );
816 $xi += count( $orig );
817 }
818
819 $closing = &$this->edits[$i]->closing;
820 if ( is_array( $closing ) ) {
821 $closing = array_slice( $to_lines, $yi, count( $closing ) );
822 $yi += count( $closing );
823 }
824 }
825 wfProfileOut( __METHOD__ );
826 }
827 }
828
829 /**
830 * Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
831 */
832
833 /**
834 * @todo document
835 * @private
836 * @ingroup DifferenceEngine
837 */
838 class HWLDFWordAccumulator {
839 public $insClass = ' class="diffchange diffchange-inline"';
840 public $delClass = ' class="diffchange diffchange-inline"';
841
842 private $lines = array();
843 private $line = '';
844 private $group = '';
845 private $tag = '';
846
847 /**
848 * @param $new_tag
849 */
850 private function flushGroup( $new_tag ) {
851 if ( $this->group !== '' ) {
852 if ( $this->tag == 'ins' ) {
853 $this->line .= "<ins{$this->insClass}>" .
854 htmlspecialchars( $this->group ) . '</ins>';
855 } elseif ( $this->tag == 'del' ) {
856 $this->line .= "<del{$this->delClass}>" .
857 htmlspecialchars( $this->group ) . '</del>';
858 } else {
859 $this->line .= htmlspecialchars( $this->group );
860 }
861 }
862 $this->group = '';
863 $this->tag = $new_tag;
864 }
865
866 /**
867 * @param $new_tag
868 */
869 private function flushLine( $new_tag ) {
870 $this->flushGroup( $new_tag );
871 if ( $this->line != '' ) {
872 array_push( $this->lines, $this->line );
873 } else {
874 # make empty lines visible by inserting an NBSP
875 array_push( $this->lines, '&#160;' );
876 }
877 $this->line = '';
878 }
879
880 /**
881 * @param $words
882 * @param $tag string
883 */
884 public function addWords( $words, $tag = '' ) {
885 if ( $tag != $this->tag ) {
886 $this->flushGroup( $tag );
887 }
888
889 foreach ( $words as $word ) {
890 // new-line should only come as first char of word.
891 if ( $word == '' ) {
892 continue;
893 }
894 if ( $word[0] == "\n" ) {
895 $this->flushLine( $tag );
896 $word = substr( $word, 1 );
897 }
898 assert( '!strstr( $word, "\n" )' );
899 $this->group .= $word;
900 }
901 }
902
903 /**
904 * @return array
905 */
906 public function getLines() {
907 $this->flushLine( '~done' );
908
909 return $this->lines;
910 }
911 }
912
913 /**
914 * @todo document
915 * @private
916 * @ingroup DifferenceEngine
917 */
918 class WordLevelDiff extends MappedDiff {
919 const MAX_LINE_LENGTH = 10000;
920
921 /**
922 * @param $orig_lines
923 * @param $closing_lines
924 */
925 function __construct( $orig_lines, $closing_lines ) {
926 wfProfileIn( __METHOD__ );
927
928 list( $orig_words, $orig_stripped ) = $this->split( $orig_lines );
929 list( $closing_words, $closing_stripped ) = $this->split( $closing_lines );
930
931 parent::__construct( $orig_words, $closing_words,
932 $orig_stripped, $closing_stripped );
933 wfProfileOut( __METHOD__ );
934 }
935
936 /**
937 * @param $lines
938 * @return array
939 */
940 private function split( $lines ) {
941 wfProfileIn( __METHOD__ );
942
943 $words = array();
944 $stripped = array();
945 $first = true;
946 foreach ( $lines as $line ) {
947 # If the line is too long, just pretend the entire line is one big word
948 # This prevents resource exhaustion problems
949 if ( $first ) {
950 $first = false;
951 } else {
952 $words[] = "\n";
953 $stripped[] = "\n";
954 }
955 if ( strlen( $line ) > self::MAX_LINE_LENGTH ) {
956 $words[] = $line;
957 $stripped[] = $line;
958 } else {
959 $m = array();
960 if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
961 $line, $m )
962 ) {
963 foreach ( $m[0] as $word ) {
964 $words[] = $word;
965 }
966 foreach ( $m[1] as $stripped_word ) {
967 $stripped[] = $stripped_word;
968 }
969 }
970 }
971 }
972 wfProfileOut( __METHOD__ );
973
974 return array( $words, $stripped );
975 }
976
977 /**
978 * @return array
979 */
980 public function orig() {
981 wfProfileIn( __METHOD__ );
982 $orig = new HWLDFWordAccumulator;
983
984 foreach ( $this->edits as $edit ) {
985 if ( $edit->type == 'copy' ) {
986 $orig->addWords( $edit->orig );
987 } elseif ( $edit->orig ) {
988 $orig->addWords( $edit->orig, 'del' );
989 }
990 }
991 $lines = $orig->getLines();
992 wfProfileOut( __METHOD__ );
993
994 return $lines;
995 }
996
997 /**
998 * @return array
999 */
1000 public function closing() {
1001 wfProfileIn( __METHOD__ );
1002 $closing = new HWLDFWordAccumulator;
1003
1004 foreach ( $this->edits as $edit ) {
1005 if ( $edit->type == 'copy' ) {
1006 $closing->addWords( $edit->closing );
1007 } elseif ( $edit->closing ) {
1008 $closing->addWords( $edit->closing, 'ins' );
1009 }
1010 }
1011 $lines = $closing->getLines();
1012 wfProfileOut( __METHOD__ );
1013
1014 return $lines;
1015 }
1016 }