PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/includes/diff/engine.php

https://github.com/AJenbo/ubuntudanmark.dk
PHP | 555 lines | 360 code | 70 blank | 125 comment | 103 complexity | f314246e644a57d890d5416d563a3ea2 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. /**
  14. * @ignore
  15. */
  16. if (!defined('IN_PHPBB'))
  17. {
  18. exit;
  19. }
  20. /**
  21. * Code from pear.php.net, Text_Diff-1.1.0 package
  22. * http://pear.php.net/package/Text_Diff/ (native engine)
  23. *
  24. * Modified by phpBB Limited to meet our coding standards
  25. * and being able to integrate into phpBB
  26. *
  27. * Class used internally by Text_Diff to actually compute the diffs. This
  28. * class is implemented using native PHP code.
  29. *
  30. * The algorithm used here is mostly lifted from the perl module
  31. * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
  32. * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
  33. *
  34. * More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html
  35. *
  36. * Some ideas (and a bit of code) are taken from analyze.c, of GNU
  37. * diffutils-2.7, which can be found at:
  38. * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
  39. *
  40. * Some ideas (subdivision by NCHUNKS > 2, and some optimizations) are from
  41. * Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
  42. * code was written by him, and is used/adapted with his permission.
  43. *
  44. * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
  45. *
  46. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  47. * @package diff
  48. *
  49. * @access private
  50. */
  51. class diff_engine
  52. {
  53. /**
  54. * If set to true we trim all lines before we compare them. This ensures that sole space/tab changes do not trigger diffs.
  55. */
  56. var $skip_whitespace_changes = true;
  57. function diff(&$from_lines, &$to_lines, $preserve_cr = true)
  58. {
  59. // Remove empty lines...
  60. // If preserve_cr is true, we basically only change \r\n and bare \r to \n to get the same carriage returns for both files
  61. // If it is false, we try to only use \n once per line and ommit all empty lines to be able to get a proper data diff
  62. if (is_array($from_lines))
  63. {
  64. $from_lines = implode("\n", $from_lines);
  65. }
  66. if (is_array($to_lines))
  67. {
  68. $to_lines = implode("\n", $to_lines);
  69. }
  70. if ($preserve_cr)
  71. {
  72. $from_lines = explode("\n", str_replace("\r", "\n", str_replace("\r\n", "\n", $from_lines)));
  73. $to_lines = explode("\n", str_replace("\r", "\n", str_replace("\r\n", "\n", $to_lines)));
  74. }
  75. else
  76. {
  77. $from_lines = explode("\n", preg_replace('#[\n\r]+#', "\n", $from_lines));
  78. $to_lines = explode("\n", preg_replace('#[\n\r]+#', "\n", $to_lines));
  79. }
  80. $n_from = sizeof($from_lines);
  81. $n_to = sizeof($to_lines);
  82. $this->xchanged = $this->ychanged = $this->xv = $this->yv = $this->xind = $this->yind = array();
  83. unset($this->seq, $this->in_seq, $this->lcs);
  84. // Skip leading common lines.
  85. for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++)
  86. {
  87. if (trim($from_lines[$skip]) !== trim($to_lines[$skip]))
  88. {
  89. break;
  90. }
  91. $this->xchanged[$skip] = $this->ychanged[$skip] = false;
  92. }
  93. // Skip trailing common lines.
  94. $xi = $n_from;
  95. $yi = $n_to;
  96. for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++)
  97. {
  98. if (trim($from_lines[$xi]) !== trim($to_lines[$yi]))
  99. {
  100. break;
  101. }
  102. $this->xchanged[$xi] = $this->ychanged[$yi] = false;
  103. }
  104. // Ignore lines which do not exist in both files.
  105. for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
  106. {
  107. if ($this->skip_whitespace_changes) $xhash[trim($from_lines[$xi])] = 1; else $xhash[$from_lines[$xi]] = 1;
  108. }
  109. for ($yi = $skip; $yi < $n_to - $endskip; $yi++)
  110. {
  111. $line = ($this->skip_whitespace_changes) ? trim($to_lines[$yi]) : $to_lines[$yi];
  112. if (($this->ychanged[$yi] = empty($xhash[$line])))
  113. {
  114. continue;
  115. }
  116. $yhash[$line] = 1;
  117. $this->yv[] = $line;
  118. $this->yind[] = $yi;
  119. }
  120. for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
  121. {
  122. $line = ($this->skip_whitespace_changes) ? trim($from_lines[$xi]) : $from_lines[$xi];
  123. if (($this->xchanged[$xi] = empty($yhash[$line])))
  124. {
  125. continue;
  126. }
  127. $this->xv[] = $line;
  128. $this->xind[] = $xi;
  129. }
  130. // Find the LCS.
  131. $this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv));
  132. // Merge edits when possible.
  133. if ($this->skip_whitespace_changes)
  134. {
  135. $from_lines_clean = array_map('trim', $from_lines);
  136. $to_lines_clean = array_map('trim', $to_lines);
  137. $this->_shift_boundaries($from_lines_clean, $this->xchanged, $this->ychanged);
  138. $this->_shift_boundaries($to_lines_clean, $this->ychanged, $this->xchanged);
  139. unset($from_lines_clean, $to_lines_clean);
  140. }
  141. else
  142. {
  143. $this->_shift_boundaries($from_lines, $this->xchanged, $this->ychanged);
  144. $this->_shift_boundaries($to_lines, $this->ychanged, $this->xchanged);
  145. }
  146. // Compute the edit operations.
  147. $edits = array();
  148. $xi = $yi = 0;
  149. while ($xi < $n_from || $yi < $n_to)
  150. {
  151. // Skip matching "snake".
  152. $copy = array();
  153. while ($xi < $n_from && $yi < $n_to && !$this->xchanged[$xi] && !$this->ychanged[$yi])
  154. {
  155. $copy[] = $from_lines[$xi++];
  156. $yi++;
  157. }
  158. if ($copy)
  159. {
  160. $edits[] = new diff_op_copy($copy);
  161. }
  162. // Find deletes & adds.
  163. $delete = array();
  164. while ($xi < $n_from && $this->xchanged[$xi])
  165. {
  166. $delete[] = $from_lines[$xi++];
  167. }
  168. $add = array();
  169. while ($yi < $n_to && $this->ychanged[$yi])
  170. {
  171. $add[] = $to_lines[$yi++];
  172. }
  173. if ($delete && $add)
  174. {
  175. $edits[] = new diff_op_change($delete, $add);
  176. }
  177. else if ($delete)
  178. {
  179. $edits[] = new diff_op_delete($delete);
  180. }
  181. else if ($add)
  182. {
  183. $edits[] = new diff_op_add($add);
  184. }
  185. }
  186. return $edits;
  187. }
  188. /**
  189. * Divides the Largest Common Subsequence (LCS) of the sequences (XOFF,
  190. * XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized segments.
  191. *
  192. * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an array of
  193. * NCHUNKS+1 (X, Y) indexes giving the diving points between sub
  194. * sequences. The first sub-sequence is contained in (X0, X1), (Y0, Y1),
  195. * the second in (X1, X2), (Y1, Y2) and so on. Note that (X0, Y0) ==
  196. * (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
  197. *
  198. * This function assumes that the first lines of the specified portions of
  199. * the two files do not match, and likewise that the last lines do not
  200. * match. The caller must trim matching lines from the beginning and end
  201. * of the portions it is going to specify.
  202. */
  203. function _diag($xoff, $xlim, $yoff, $ylim, $nchunks)
  204. {
  205. $flip = false;
  206. if ($xlim - $xoff > $ylim - $yoff)
  207. {
  208. // Things seems faster (I'm not sure I understand why) when the shortest sequence is in X.
  209. $flip = true;
  210. list($xoff, $xlim, $yoff, $ylim) = array($yoff, $ylim, $xoff, $xlim);
  211. }
  212. if ($flip)
  213. {
  214. for ($i = $ylim - 1; $i >= $yoff; $i--)
  215. {
  216. $ymatches[$this->xv[$i]][] = $i;
  217. }
  218. }
  219. else
  220. {
  221. for ($i = $ylim - 1; $i >= $yoff; $i--)
  222. {
  223. $ymatches[$this->yv[$i]][] = $i;
  224. }
  225. }
  226. $this->lcs = 0;
  227. $this->seq[0]= $yoff - 1;
  228. $this->in_seq = array();
  229. $ymids[0] = array();
  230. $numer = $xlim - $xoff + $nchunks - 1;
  231. $x = $xoff;
  232. for ($chunk = 0; $chunk < $nchunks; $chunk++)
  233. {
  234. if ($chunk > 0)
  235. {
  236. for ($i = 0; $i <= $this->lcs; $i++)
  237. {
  238. $ymids[$i][$chunk - 1] = $this->seq[$i];
  239. }
  240. }
  241. $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks);
  242. for (; $x < $x1; $x++)
  243. {
  244. $line = $flip ? $this->yv[$x] : $this->xv[$x];
  245. if (empty($ymatches[$line]))
  246. {
  247. continue;
  248. }
  249. $matches = $ymatches[$line];
  250. reset($matches);
  251. while (list(, $y) = each($matches))
  252. {
  253. if (empty($this->in_seq[$y]))
  254. {
  255. $k = $this->_lcs_pos($y);
  256. $ymids[$k] = $ymids[$k - 1];
  257. break;
  258. }
  259. }
  260. // no reset() here
  261. while (list(, $y) = each($matches))
  262. {
  263. if ($y > $this->seq[$k - 1])
  264. {
  265. // Optimization: this is a common case: next match is just replacing previous match.
  266. $this->in_seq[$this->seq[$k]] = false;
  267. $this->seq[$k] = $y;
  268. $this->in_seq[$y] = 1;
  269. }
  270. else if (empty($this->in_seq[$y]))
  271. {
  272. $k = $this->_lcs_pos($y);
  273. $ymids[$k] = $ymids[$k - 1];
  274. }
  275. }
  276. }
  277. }
  278. $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
  279. $ymid = $ymids[$this->lcs];
  280. for ($n = 0; $n < $nchunks - 1; $n++)
  281. {
  282. $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
  283. $y1 = $ymid[$n] + 1;
  284. $seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
  285. }
  286. $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
  287. return array($this->lcs, $seps);
  288. }
  289. function _lcs_pos($ypos)
  290. {
  291. $end = $this->lcs;
  292. if ($end == 0 || $ypos > $this->seq[$end])
  293. {
  294. $this->seq[++$this->lcs] = $ypos;
  295. $this->in_seq[$ypos] = 1;
  296. return $this->lcs;
  297. }
  298. $beg = 1;
  299. while ($beg < $end)
  300. {
  301. $mid = (int)(($beg + $end) / 2);
  302. if ($ypos > $this->seq[$mid])
  303. {
  304. $beg = $mid + 1;
  305. }
  306. else
  307. {
  308. $end = $mid;
  309. }
  310. }
  311. $this->in_seq[$this->seq[$end]] = false;
  312. $this->seq[$end] = $ypos;
  313. $this->in_seq[$ypos] = 1;
  314. return $end;
  315. }
  316. /**
  317. * Finds LCS of two sequences.
  318. *
  319. * The results are recorded in the vectors $this->{x,y}changed[], by
  320. * storing a 1 in the element for each line that is an insertion or
  321. * deletion (ie. is not in the LCS).
  322. *
  323. * The subsequence of file 0 is (XOFF, XLIM) and likewise for file 1.
  324. *
  325. * Note that XLIM, YLIM are exclusive bounds. All line numbers are
  326. * origin-0 and discarded lines are not counted.
  327. */
  328. function _compareseq($xoff, $xlim, $yoff, $ylim)
  329. {
  330. // Slide down the bottom initial diagonal.
  331. while ($xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff])
  332. {
  333. ++$xoff;
  334. ++$yoff;
  335. }
  336. // Slide up the top initial diagonal.
  337. while ($xlim > $xoff && $ylim > $yoff && $this->xv[$xlim - 1] == $this->yv[$ylim - 1])
  338. {
  339. --$xlim;
  340. --$ylim;
  341. }
  342. if ($xoff == $xlim || $yoff == $ylim)
  343. {
  344. $lcs = 0;
  345. }
  346. else
  347. {
  348. // This is ad hoc but seems to work well.
  349. // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
  350. // $nchunks = max(2,min(8,(int)$nchunks));
  351. $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
  352. list($lcs, $seps) = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks);
  353. }
  354. if ($lcs == 0)
  355. {
  356. // X and Y sequences have no common subsequence: mark all changed.
  357. while ($yoff < $ylim)
  358. {
  359. $this->ychanged[$this->yind[$yoff++]] = 1;
  360. }
  361. while ($xoff < $xlim)
  362. {
  363. $this->xchanged[$this->xind[$xoff++]] = 1;
  364. }
  365. }
  366. else
  367. {
  368. // Use the partitions to split this problem into subproblems.
  369. reset($seps);
  370. $pt1 = $seps[0];
  371. while ($pt2 = next($seps))
  372. {
  373. $this->_compareseq($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
  374. $pt1 = $pt2;
  375. }
  376. }
  377. }
  378. /**
  379. * Adjusts inserts/deletes of identical lines to join changes as much as possible.
  380. *
  381. * We do something when a run of changed lines include a line at one end
  382. * and has an excluded, identical line at the other. We are free to
  383. * choose which identical line is included. 'compareseq' usually chooses
  384. * the one at the beginning, but usually it is cleaner to consider the
  385. * following identical line to be the "change".
  386. *
  387. * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
  388. */
  389. function _shift_boundaries($lines, &$changed, $other_changed)
  390. {
  391. $i = 0;
  392. $j = 0;
  393. $len = sizeof($lines);
  394. $other_len = sizeof($other_changed);
  395. while (1)
  396. {
  397. // Scan forward to find the beginning of another run of
  398. // changes. Also keep track of the corresponding point in the other file.
  399. //
  400. // Throughout this code, $i and $j are adjusted together so that
  401. // the first $i elements of $changed and the first $j elements of
  402. // $other_changed both contain the same number of zeros (unchanged lines).
  403. //
  404. // Furthermore, $j is always kept so that $j == $other_len or $other_changed[$j] == false.
  405. while ($j < $other_len && $other_changed[$j])
  406. {
  407. $j++;
  408. }
  409. while ($i < $len && ! $changed[$i])
  410. {
  411. $i++;
  412. $j++;
  413. while ($j < $other_len && $other_changed[$j])
  414. {
  415. $j++;
  416. }
  417. }
  418. if ($i == $len)
  419. {
  420. break;
  421. }
  422. $start = $i;
  423. // Find the end of this run of changes.
  424. while (++$i < $len && $changed[$i])
  425. {
  426. continue;
  427. }
  428. do
  429. {
  430. // Record the length of this run of changes, so that we can later determine whether the run has grown.
  431. $runlength = $i - $start;
  432. // Move the changed region back, so long as the previous unchanged line matches the last changed one.
  433. // This merges with previous changed regions.
  434. while ($start > 0 && $lines[$start - 1] == $lines[$i - 1])
  435. {
  436. $changed[--$start] = 1;
  437. $changed[--$i] = false;
  438. while ($start > 0 && $changed[$start - 1])
  439. {
  440. $start--;
  441. }
  442. while ($other_changed[--$j])
  443. {
  444. continue;
  445. }
  446. }
  447. // Set CORRESPONDING to the end of the changed run, at the last point where it corresponds to a changed run in the
  448. // other file. CORRESPONDING == LEN means no such point has been found.
  449. $corresponding = $j < $other_len ? $i : $len;
  450. // Move the changed region forward, so long as the first changed line matches the following unchanged one.
  451. // This merges with following changed regions.
  452. // Do this second, so that if there are no merges, the changed region is moved forward as far as possible.
  453. while ($i < $len && $lines[$start] == $lines[$i])
  454. {
  455. $changed[$start++] = false;
  456. $changed[$i++] = 1;
  457. while ($i < $len && $changed[$i])
  458. {
  459. $i++;
  460. }
  461. $j++;
  462. if ($j < $other_len && $other_changed[$j])
  463. {
  464. $corresponding = $i;
  465. while ($j < $other_len && $other_changed[$j])
  466. {
  467. $j++;
  468. }
  469. }
  470. }
  471. }
  472. while ($runlength != $i - $start);
  473. // If possible, move the fully-merged run of changes back to a corresponding run in the other file.
  474. while ($corresponding < $i)
  475. {
  476. $changed[--$start] = 1;
  477. $changed[--$i] = 0;
  478. while ($other_changed[--$j])
  479. {
  480. continue;
  481. }
  482. }
  483. }
  484. }
  485. }