Diff.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /**
  3. * General API for generating and formatting diffs - the differences between
  4. * two sequences of strings.
  5. *
  6. * The original PHP version of this code was written by Geoffrey T. Dairiki
  7. * <dairiki@dairiki.org>, and is used/adapted with his permission.
  8. *
  9. * $Horde: framework/Text_Diff/Diff.php,v 1.11.2.12 2009/01/06 15:23:41 jan Exp $
  10. *
  11. * Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
  12. * Copyright 2004-2009 The Horde Project (http://www.horde.org/)
  13. *
  14. * See the enclosed file COPYING for license information (LGPL). If you did
  15. * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  16. *
  17. * @package Text_Diff
  18. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  19. */
  20. class Text_Diff
  21. {
  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. public function __construct($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 'Text/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. public static 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. /**
  271. * Computes a diff between sequences of strings.
  272. *
  273. * This can be used to compute things like case-insensitve diffs, or diffs
  274. * which ignore changes in white-space.
  275. *
  276. * @param array $from_lines An array of strings.
  277. * @param array $to_lines An array of strings.
  278. * @param array $mapped_from_lines This array should have the same size
  279. * number of elements as $from_lines. The
  280. * elements in $mapped_from_lines and
  281. * $mapped_to_lines are what is actually
  282. * compared when computing the diff.
  283. * @param array $mapped_to_lines This array should have the same number
  284. * of elements as $to_lines.
  285. */
  286. public function __construct($from_lines, $to_lines,
  287. $mapped_from_lines, $mapped_to_lines)
  288. {
  289. assert(count($from_lines) == count($mapped_from_lines));
  290. assert(count($to_lines) == count($mapped_to_lines));
  291. parent::__construct($mapped_from_lines, $mapped_to_lines);
  292. $xi = $yi = 0;
  293. for ($i = 0; $i < count($this->_edits); $i++) {
  294. $orig = &$this->_edits[$i]->orig;
  295. if (is_array($orig)) {
  296. $orig = array_slice($from_lines, $xi, count($orig));
  297. $xi += count($orig);
  298. }
  299. $final = &$this->_edits[$i]->final;
  300. if (is_array($final)) {
  301. $final = array_slice($to_lines, $yi, count($final));
  302. $yi += count($final);
  303. }
  304. }
  305. }
  306. }
  307. /**
  308. * @package Text_Diff
  309. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  310. *
  311. * @access private
  312. */
  313. class Text_Diff_Op {
  314. var $orig;
  315. var $final;
  316. function &reverse()
  317. {
  318. trigger_error('Abstract method', E_USER_ERROR);
  319. }
  320. function norig()
  321. {
  322. return $this->orig ? count($this->orig) : 0;
  323. }
  324. function nfinal()
  325. {
  326. return $this->final ? count($this->final) : 0;
  327. }
  328. }
  329. /**
  330. * @package Text_Diff
  331. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  332. *
  333. * @access private
  334. */
  335. class Text_Diff_Op_copy extends Text_Diff_Op
  336. {
  337. public function __construct($orig, $final = false)
  338. {
  339. if (!is_array($final)) {
  340. $final = $orig;
  341. }
  342. $this->orig = $orig;
  343. $this->final = $final;
  344. }
  345. function &reverse()
  346. {
  347. $reverse = new Text_Diff_Op_copy($this->final, $this->orig);
  348. return $reverse;
  349. }
  350. }
  351. /**
  352. * @package Text_Diff
  353. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  354. *
  355. * @access private
  356. */
  357. class Text_Diff_Op_delete extends Text_Diff_Op
  358. {
  359. public function __construct($lines)
  360. {
  361. $this->orig = $lines;
  362. $this->final = false;
  363. }
  364. function &reverse()
  365. {
  366. $reverse = new Text_Diff_Op_add($this->orig);
  367. return $reverse;
  368. }
  369. }
  370. /**
  371. * @package Text_Diff
  372. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  373. *
  374. * @access private
  375. */
  376. class Text_Diff_Op_add extends Text_Diff_Op {
  377. public function __construct($lines)
  378. {
  379. $this->final = $lines;
  380. $this->orig = false;
  381. }
  382. function &reverse()
  383. {
  384. $reverse = new Text_Diff_Op_delete($this->final);
  385. return $reverse;
  386. }
  387. }
  388. /**
  389. * @package Text_Diff
  390. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  391. *
  392. * @access private
  393. */
  394. class Text_Diff_Op_change extends Text_Diff_Op
  395. {
  396. public function __construct($orig, $final)
  397. {
  398. $this->orig = $orig;
  399. $this->final = $final;
  400. }
  401. function &reverse()
  402. {
  403. $reverse = new Text_Diff_Op_change($this->final, $this->orig);
  404. return $reverse;
  405. }
  406. }