PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/diff/DairikiDiff.php

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