PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Diff/Diff.php

https://github.com/mlinsey/Gitdocs
PHP | 456 lines | 231 code | 52 blank | 173 comment | 28 complexity | 2755f207054be59acd5cf0683923dab2 MD5 | raw file
  1. <?php
  2. /**
  3. http://pear.php.net/package/Text_Diff
  4. * General API for generating and formatting diffs - the differences between
  5. * two sequences of strings.
  6. *
  7. * The original PHP version of this code was written by Geoffrey T. Dairiki
  8. * <dairiki@dairiki.org>, and is used/adapted with his permission.
  9. *
  10. * $Horde: framework/Text_Diff/Diff.php,v 1.11.2.12 2009/01/06 15:23:41 jan Exp $
  11. *
  12. * Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
  13. * Copyright 2004-2009 The Horde Project (http://www.horde.org/)
  14. *
  15. * See the enclosed file COPYING for license information (LGPL). If you did
  16. * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  17. *
  18. * @package Text_Diff
  19. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  20. */
  21. class Text_Diff {
  22. /**
  23. * Array of changes.
  24. *
  25. * @var array
  26. */
  27. var $_edits;
  28. /**
  29. * Computes diffs between sequences of strings.
  30. *
  31. * @param string $engine Name of the diffing engine to use. 'auto'
  32. * will automatically select the best.
  33. * @param array $params Parameters to pass to the diffing engine.
  34. * Normally an array of two arrays, each
  35. * containing the lines from a file.
  36. */
  37. function Text_Diff($engine, $params)
  38. {
  39. // Backward compatibility workaround.
  40. if (!is_string($engine)) {
  41. $params = array($engine, $params);
  42. $engine = 'auto';
  43. }
  44. if ($engine == 'auto') {
  45. $engine = extension_loaded('xdiff') ? 'xdiff' : 'native';
  46. } else {
  47. $engine = basename($engine);
  48. }
  49. require_once 'Diff/Engine/' . $engine . '.php';
  50. $class = 'Text_Diff_Engine_' . $engine;
  51. $diff_engine = new $class();
  52. $this->_edits = call_user_func_array(array($diff_engine, 'diff'), $params);
  53. }
  54. /**
  55. * Returns the array of differences.
  56. */
  57. function getDiff()
  58. {
  59. return $this->_edits;
  60. }
  61. /**
  62. * returns the number of new (added) lines in a given diff.
  63. *
  64. * @since Text_Diff 1.1.0
  65. * @since Horde 3.2
  66. *
  67. * @return integer The number of new lines
  68. */
  69. function countAddedLines()
  70. {
  71. $count = 0;
  72. foreach ($this->_edits as $edit) {
  73. if (is_a($edit, 'Text_Diff_Op_add') ||
  74. is_a($edit, 'Text_Diff_Op_change')) {
  75. $count += $edit->nfinal();
  76. }
  77. }
  78. return $count;
  79. }
  80. /**
  81. * Returns the number of deleted (removed) lines in a given diff.
  82. *
  83. * @since Text_Diff 1.1.0
  84. * @since Horde 3.2
  85. *
  86. * @return integer The number of deleted lines
  87. */
  88. function countDeletedLines()
  89. {
  90. $count = 0;
  91. foreach ($this->_edits as $edit) {
  92. if (is_a($edit, 'Text_Diff_Op_delete') ||
  93. is_a($edit, 'Text_Diff_Op_change')) {
  94. $count += $edit->norig();
  95. }
  96. }
  97. return $count;
  98. }
  99. /**
  100. * Computes a reversed diff.
  101. *
  102. * Example:
  103. * <code>
  104. * $diff = new Text_Diff($lines1, $lines2);
  105. * $rev = $diff->reverse();
  106. * </code>
  107. *
  108. * @return Text_Diff A Diff object representing the inverse of the
  109. * original diff. Note that we purposely don't return a
  110. * reference here, since this essentially is a clone()
  111. * method.
  112. */
  113. function reverse()
  114. {
  115. if (version_compare(zend_version(), '2', '>')) {
  116. $rev = clone($this);
  117. } else {
  118. $rev = $this;
  119. }
  120. $rev->_edits = array();
  121. foreach ($this->_edits as $edit) {
  122. $rev->_edits[] = $edit->reverse();
  123. }
  124. return $rev;
  125. }
  126. /**
  127. * Checks for an empty diff.
  128. *
  129. * @return boolean True if two sequences were identical.
  130. */
  131. function isEmpty()
  132. {
  133. foreach ($this->_edits as $edit) {
  134. if (!is_a($edit, 'Text_Diff_Op_copy')) {
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. /**
  141. * Computes the length of the Longest Common Subsequence (LCS).
  142. *
  143. * This is mostly for diagnostic purposes.
  144. *
  145. * @return integer The length of the LCS.
  146. */
  147. function lcs()
  148. {
  149. $lcs = 0;
  150. foreach ($this->_edits as $edit) {
  151. if (is_a($edit, 'Text_Diff_Op_copy')) {
  152. $lcs += count($edit->orig);
  153. }
  154. }
  155. return $lcs;
  156. }
  157. /**
  158. * Gets the original set of lines.
  159. *
  160. * This reconstructs the $from_lines parameter passed to the constructor.
  161. *
  162. * @return array The original sequence of strings.
  163. */
  164. function getOriginal()
  165. {
  166. $lines = array();
  167. foreach ($this->_edits as $edit) {
  168. if ($edit->orig) {
  169. array_splice($lines, count($lines), 0, $edit->orig);
  170. }
  171. }
  172. return $lines;
  173. }
  174. /**
  175. * Gets the final set of lines.
  176. *
  177. * This reconstructs the $to_lines parameter passed to the constructor.
  178. *
  179. * @return array The sequence of strings.
  180. */
  181. function getFinal()
  182. {
  183. $lines = array();
  184. foreach ($this->_edits as $edit) {
  185. if ($edit->final) {
  186. array_splice($lines, count($lines), 0, $edit->final);
  187. }
  188. }
  189. return $lines;
  190. }
  191. /**
  192. * Removes trailing newlines from a line of text. This is meant to be used
  193. * with array_walk().
  194. *
  195. * @param string $line The line to trim.
  196. * @param integer $key The index of the line in the array. Not used.
  197. */
  198. function trimNewlines(&$line, $key)
  199. {
  200. $line = str_replace(array("\n", "\r"), '', $line);
  201. }
  202. /**
  203. * Determines the location of the system temporary directory.
  204. *
  205. * @static
  206. *
  207. * @access protected
  208. *
  209. * @return string A directory name which can be used for temp files.
  210. * Returns false if one could not be found.
  211. */
  212. function _getTempDir()
  213. {
  214. $tmp_locations = array('/tmp', '/var/tmp', 'c:\WUTemp', 'c:\temp',
  215. 'c:\windows\temp', 'c:\winnt\temp');
  216. /* Try PHP's upload_tmp_dir directive. */
  217. $tmp = ini_get('upload_tmp_dir');
  218. /* Otherwise, try to determine the TMPDIR environment variable. */
  219. if (!strlen($tmp)) {
  220. $tmp = getenv('TMPDIR');
  221. }
  222. /* If we still cannot determine a value, then cycle through a list of
  223. * preset possibilities. */
  224. while (!strlen($tmp) && count($tmp_locations)) {
  225. $tmp_check = array_shift($tmp_locations);
  226. if (@is_dir($tmp_check)) {
  227. $tmp = $tmp_check;
  228. }
  229. }
  230. /* If it is still empty, we have failed, so return false; otherwise
  231. * return the directory determined. */
  232. return strlen($tmp) ? $tmp : false;
  233. }
  234. /**
  235. * Checks a diff for validity.
  236. *
  237. * This is here only for debugging purposes.
  238. */
  239. function _check($from_lines, $to_lines)
  240. {
  241. if (serialize($from_lines) != serialize($this->getOriginal())) {
  242. trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
  243. }
  244. if (serialize($to_lines) != serialize($this->getFinal())) {
  245. trigger_error("Reconstructed final doesn't match", E_USER_ERROR);
  246. }
  247. $rev = $this->reverse();
  248. if (serialize($to_lines) != serialize($rev->getOriginal())) {
  249. trigger_error("Reversed original doesn't match", E_USER_ERROR);
  250. }
  251. if (serialize($from_lines) != serialize($rev->getFinal())) {
  252. trigger_error("Reversed final doesn't match", E_USER_ERROR);
  253. }
  254. $prevtype = null;
  255. foreach ($this->_edits as $edit) {
  256. if ($prevtype == get_class($edit)) {
  257. trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
  258. }
  259. $prevtype = get_class($edit);
  260. }
  261. return true;
  262. }
  263. }
  264. /**
  265. * @package Text_Diff
  266. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  267. */
  268. class Text_MappedDiff extends Text_Diff {
  269. /**
  270. * Computes a diff between sequences of strings.
  271. *
  272. * This can be used to compute things like case-insensitve diffs, or diffs
  273. * which ignore changes in white-space.
  274. *
  275. * @param array $from_lines An array of strings.
  276. * @param array $to_lines An array of strings.
  277. * @param array $mapped_from_lines This array should have the same size
  278. * number of elements as $from_lines. The
  279. * elements in $mapped_from_lines and
  280. * $mapped_to_lines are what is actually
  281. * compared when computing the diff.
  282. * @param array $mapped_to_lines This array should have the same number
  283. * of elements as $to_lines.
  284. */
  285. function Text_MappedDiff($from_lines, $to_lines,
  286. $mapped_from_lines, $mapped_to_lines)
  287. {
  288. assert(count($from_lines) == count($mapped_from_lines));
  289. assert(count($to_lines) == count($mapped_to_lines));
  290. parent::Text_Diff($mapped_from_lines, $mapped_to_lines);
  291. $xi = $yi = 0;
  292. for ($i = 0; $i < count($this->_edits); $i++) {
  293. $orig = &$this->_edits[$i]->orig;
  294. if (is_array($orig)) {
  295. $orig = array_slice($from_lines, $xi, count($orig));
  296. $xi += count($orig);
  297. }
  298. $final = &$this->_edits[$i]->final;
  299. if (is_array($final)) {
  300. $final = array_slice($to_lines, $yi, count($final));
  301. $yi += count($final);
  302. }
  303. }
  304. }
  305. }
  306. /**
  307. * @package Text_Diff
  308. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  309. *
  310. * @access private
  311. */
  312. class Text_Diff_Op {
  313. var $orig;
  314. var $final;
  315. function &reverse()
  316. {
  317. trigger_error('Abstract method', E_USER_ERROR);
  318. }
  319. function norig()
  320. {
  321. return $this->orig ? count($this->orig) : 0;
  322. }
  323. function nfinal()
  324. {
  325. return $this->final ? count($this->final) : 0;
  326. }
  327. }
  328. /**
  329. * @package Text_Diff
  330. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  331. *
  332. * @access private
  333. */
  334. class Text_Diff_Op_copy extends Text_Diff_Op {
  335. function Text_Diff_Op_copy($orig, $final = false)
  336. {
  337. if (!is_array($final)) {
  338. $final = $orig;
  339. }
  340. $this->orig = $orig;
  341. $this->final = $final;
  342. }
  343. function &reverse()
  344. {
  345. $reverse = &new Text_Diff_Op_copy($this->final, $this->orig);
  346. return $reverse;
  347. }
  348. }
  349. /**
  350. * @package Text_Diff
  351. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  352. *
  353. * @access private
  354. */
  355. class Text_Diff_Op_delete extends Text_Diff_Op {
  356. function Text_Diff_Op_delete($lines)
  357. {
  358. $this->orig = $lines;
  359. $this->final = false;
  360. }
  361. function &reverse()
  362. {
  363. $reverse = &new Text_Diff_Op_add($this->orig);
  364. return $reverse;
  365. }
  366. }
  367. /**
  368. * @package Text_Diff
  369. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  370. *
  371. * @access private
  372. */
  373. class Text_Diff_Op_add extends Text_Diff_Op {
  374. function Text_Diff_Op_add($lines)
  375. {
  376. $this->final = $lines;
  377. $this->orig = false;
  378. }
  379. function &reverse()
  380. {
  381. $reverse = &new Text_Diff_Op_delete($this->final);
  382. return $reverse;
  383. }
  384. }
  385. /**
  386. * @package Text_Diff
  387. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  388. *
  389. * @access private
  390. */
  391. class Text_Diff_Op_change extends Text_Diff_Op {
  392. function Text_Diff_Op_change($orig, $final)
  393. {
  394. $this->orig = $orig;
  395. $this->final = $final;
  396. }
  397. function &reverse()
  398. {
  399. $reverse = &new Text_Diff_Op_change($this->final, $this->orig);
  400. return $reverse;
  401. }
  402. }