Diff.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. * Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
  10. * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
  11. *
  12. * See the enclosed file COPYING for license information (LGPL). If you did
  13. * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  14. *
  15. * @package Text_Diff
  16. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  17. */
  18. class Text_Diff {
  19. /**
  20. * Array of changes.
  21. *
  22. * @var array
  23. */
  24. var $_edits;
  25. /**
  26. * Computes diffs between sequences of strings.
  27. *
  28. * @param string $engine Name of the diffing engine to use. 'auto'
  29. * will automatically select the best.
  30. * @param array $params Parameters to pass to the diffing engine.
  31. * Normally an array of two arrays, each
  32. * containing the lines from a file.
  33. */
  34. function __construct( $engine, $params )
  35. {
  36. // Backward compatibility workaround.
  37. if (!is_string($engine)) {
  38. $params = array($engine, $params);
  39. $engine = 'auto';
  40. }
  41. if ($engine == 'auto') {
  42. $engine = extension_loaded('xdiff') ? 'xdiff' : 'native';
  43. } else {
  44. $engine = basename($engine);
  45. }
  46. // WP #7391
  47. require_once dirname(__FILE__).'/Diff/Engine/' . $engine . '.php';
  48. $class = 'Text_Diff_Engine_' . $engine;
  49. $diff_engine = new $class();
  50. $this->_edits = call_user_func_array(array($diff_engine, 'diff'), $params);
  51. }
  52. /**
  53. * PHP4 constructor.
  54. */
  55. public function Text_Diff( $engine, $params ) {
  56. self::__construct( $engine, $params );
  57. }
  58. /**
  59. * Returns the array of differences.
  60. */
  61. function getDiff()
  62. {
  63. return $this->_edits;
  64. }
  65. /**
  66. * returns the number of new (added) lines in a given diff.
  67. *
  68. * @since Text_Diff 1.1.0
  69. *
  70. * @return int The number of new lines
  71. */
  72. function countAddedLines()
  73. {
  74. $count = 0;
  75. foreach ($this->_edits as $edit) {
  76. if (is_a($edit, 'Text_Diff_Op_add') ||
  77. is_a($edit, 'Text_Diff_Op_change')) {
  78. $count += $edit->nfinal();
  79. }
  80. }
  81. return $count;
  82. }
  83. /**
  84. * Returns the number of deleted (removed) lines in a given diff.
  85. *
  86. * @since Text_Diff 1.1.0
  87. *
  88. * @return int The number of deleted lines
  89. */
  90. function countDeletedLines()
  91. {
  92. $count = 0;
  93. foreach ($this->_edits as $edit) {
  94. if (is_a($edit, 'Text_Diff_Op_delete') ||
  95. is_a($edit, 'Text_Diff_Op_change')) {
  96. $count += $edit->norig();
  97. }
  98. }
  99. return $count;
  100. }
  101. /**
  102. * Computes a reversed diff.
  103. *
  104. * Example:
  105. * <code>
  106. * $diff = new Text_Diff($lines1, $lines2);
  107. * $rev = $diff->reverse();
  108. * </code>
  109. *
  110. * @return Text_Diff A Diff object representing the inverse of the
  111. * original diff. Note that we purposely don't return a
  112. * reference here, since this essentially is a clone()
  113. * method.
  114. */
  115. function reverse()
  116. {
  117. if (version_compare(zend_version(), '2', '>')) {
  118. $rev = clone($this);
  119. } else {
  120. $rev = $this;
  121. }
  122. $rev->_edits = array();
  123. foreach ($this->_edits as $edit) {
  124. $rev->_edits[] = $edit->reverse();
  125. }
  126. return $rev;
  127. }
  128. /**
  129. * Checks for an empty diff.
  130. *
  131. * @return bool True if two sequences were identical.
  132. */
  133. function isEmpty()
  134. {
  135. foreach ($this->_edits as $edit) {
  136. if (!is_a($edit, 'Text_Diff_Op_copy')) {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142. /**
  143. * Computes the length of the Longest Common Subsequence (LCS).
  144. *
  145. * This is mostly for diagnostic purposes.
  146. *
  147. * @return int The length of the LCS.
  148. */
  149. function lcs()
  150. {
  151. $lcs = 0;
  152. foreach ($this->_edits as $edit) {
  153. if (is_a($edit, 'Text_Diff_Op_copy')) {
  154. $lcs += count($edit->orig);
  155. }
  156. }
  157. return $lcs;
  158. }
  159. /**
  160. * Gets the original set of lines.
  161. *
  162. * This reconstructs the $from_lines parameter passed to the constructor.
  163. *
  164. * @return array The original sequence of strings.
  165. */
  166. function getOriginal()
  167. {
  168. $lines = array();
  169. foreach ($this->_edits as $edit) {
  170. if ($edit->orig) {
  171. array_splice($lines, count($lines), 0, $edit->orig);
  172. }
  173. }
  174. return $lines;
  175. }
  176. /**
  177. * Gets the final set of lines.
  178. *
  179. * This reconstructs the $to_lines parameter passed to the constructor.
  180. *
  181. * @return array The sequence of strings.
  182. */
  183. function getFinal()
  184. {
  185. $lines = array();
  186. foreach ($this->_edits as $edit) {
  187. if ($edit->final) {
  188. array_splice($lines, count($lines), 0, $edit->final);
  189. }
  190. }
  191. return $lines;
  192. }
  193. /**
  194. * Removes trailing newlines from a line of text. This is meant to be used
  195. * with array_walk().
  196. *
  197. * @param string $line The line to trim.
  198. * @param int $key The index of the line in the array. Not used.
  199. */
  200. static function trimNewlines(&$line, $key)
  201. {
  202. $line = str_replace(array("\n", "\r"), '', $line);
  203. }
  204. /**
  205. * Determines the location of the system temporary directory.
  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. static 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 does not match", E_USER_ERROR);
  243. }
  244. if (serialize($to_lines) != serialize($this->getFinal())) {
  245. trigger_error("Reconstructed final does not match", E_USER_ERROR);
  246. }
  247. $rev = $this->reverse();
  248. if (serialize($to_lines) != serialize($rev->getOriginal())) {
  249. trigger_error("Reversed original does not match", E_USER_ERROR);
  250. }
  251. if (serialize($from_lines) != serialize($rev->getFinal())) {
  252. trigger_error("Reversed final does not match", E_USER_ERROR);
  253. }
  254. $prevtype = null;
  255. foreach ($this->_edits as $edit) {
  256. if ($edit instanceof $prevtype) {
  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 __construct($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. * PHP4 constructor.
  307. */
  308. public function Text_MappedDiff( $from_lines, $to_lines,
  309. $mapped_from_lines, $mapped_to_lines ) {
  310. self::__construct( $from_lines, $to_lines,
  311. $mapped_from_lines, $mapped_to_lines );
  312. }
  313. }
  314. /**
  315. * @package Text_Diff
  316. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  317. *
  318. * @access private
  319. */
  320. class Text_Diff_Op {
  321. var $orig;
  322. var $final;
  323. function &reverse()
  324. {
  325. trigger_error('Abstract method', E_USER_ERROR);
  326. }
  327. function norig()
  328. {
  329. return $this->orig ? count($this->orig) : 0;
  330. }
  331. function nfinal()
  332. {
  333. return $this->final ? count($this->final) : 0;
  334. }
  335. }
  336. /**
  337. * @package Text_Diff
  338. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  339. *
  340. * @access private
  341. */
  342. class Text_Diff_Op_copy extends Text_Diff_Op {
  343. /**
  344. * PHP5 constructor.
  345. */
  346. function __construct( $orig, $final = false )
  347. {
  348. if (!is_array($final)) {
  349. $final = $orig;
  350. }
  351. $this->orig = $orig;
  352. $this->final = $final;
  353. }
  354. /**
  355. * PHP4 constructor.
  356. */
  357. public function Text_Diff_Op_copy( $orig, $final = false ) {
  358. self::__construct( $orig, $final );
  359. }
  360. function &reverse()
  361. {
  362. $reverse = new Text_Diff_Op_copy($this->final, $this->orig);
  363. return $reverse;
  364. }
  365. }
  366. /**
  367. * @package Text_Diff
  368. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  369. *
  370. * @access private
  371. */
  372. class Text_Diff_Op_delete extends Text_Diff_Op {
  373. /**
  374. * PHP5 constructor.
  375. */
  376. function __construct( $lines )
  377. {
  378. $this->orig = $lines;
  379. $this->final = false;
  380. }
  381. /**
  382. * PHP4 constructor.
  383. */
  384. public function Text_Diff_Op_delete( $lines ) {
  385. self::__construct( $lines );
  386. }
  387. function &reverse()
  388. {
  389. $reverse = new Text_Diff_Op_add($this->orig);
  390. return $reverse;
  391. }
  392. }
  393. /**
  394. * @package Text_Diff
  395. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  396. *
  397. * @access private
  398. */
  399. class Text_Diff_Op_add extends Text_Diff_Op {
  400. /**
  401. * PHP5 constructor.
  402. */
  403. function __construct( $lines )
  404. {
  405. $this->final = $lines;
  406. $this->orig = false;
  407. }
  408. /**
  409. * PHP4 constructor.
  410. */
  411. public function Text_Diff_Op_add( $lines ) {
  412. self::__construct( $lines );
  413. }
  414. function &reverse()
  415. {
  416. $reverse = new Text_Diff_Op_delete($this->final);
  417. return $reverse;
  418. }
  419. }
  420. /**
  421. * @package Text_Diff
  422. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  423. *
  424. * @access private
  425. */
  426. class Text_Diff_Op_change extends Text_Diff_Op {
  427. /**
  428. * PHP5 constructor.
  429. */
  430. function __construct( $orig, $final )
  431. {
  432. $this->orig = $orig;
  433. $this->final = $final;
  434. }
  435. /**
  436. * PHP4 constructor.
  437. */
  438. public function Text_Diff_Op_change( $orig, $final ) {
  439. self::__construct( $orig, $final );
  440. }
  441. function &reverse()
  442. {
  443. $reverse = new Text_Diff_Op_change($this->final, $this->orig);
  444. return $reverse;
  445. }
  446. }