You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Diff.php 13 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 integer 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 integer 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 boolean 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 integer 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 integer $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. * @static
  208. *
  209. * @access protected
  210. *
  211. * @return string A directory name which can be used for temp files.
  212. * Returns false if one could not be found.
  213. */
  214. function _getTempDir()
  215. {
  216. $tmp_locations = array('/tmp', '/var/tmp', 'c:\WUTemp', 'c:\temp',
  217. 'c:\windows\temp', 'c:\winnt\temp');
  218. /* Try PHP's upload_tmp_dir directive. */
  219. $tmp = ini_get('upload_tmp_dir');
  220. /* Otherwise, try to determine the TMPDIR environment variable. */
  221. if (!strlen($tmp)) {
  222. $tmp = getenv('TMPDIR');
  223. }
  224. /* If we still cannot determine a value, then cycle through a list of
  225. * preset possibilities. */
  226. while (!strlen($tmp) && count($tmp_locations)) {
  227. $tmp_check = array_shift($tmp_locations);
  228. if (@is_dir($tmp_check)) {
  229. $tmp = $tmp_check;
  230. }
  231. }
  232. /* If it is still empty, we have failed, so return false; otherwise
  233. * return the directory determined. */
  234. return strlen($tmp) ? $tmp : false;
  235. }
  236. /**
  237. * Checks a diff for validity.
  238. *
  239. * This is here only for debugging purposes.
  240. */
  241. function _check($from_lines, $to_lines)
  242. {
  243. if (serialize($from_lines) != serialize($this->getOriginal())) {
  244. trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
  245. }
  246. if (serialize($to_lines) != serialize($this->getFinal())) {
  247. trigger_error("Reconstructed final doesn't match", E_USER_ERROR);
  248. }
  249. $rev = $this->reverse();
  250. if (serialize($to_lines) != serialize($rev->getOriginal())) {
  251. trigger_error("Reversed original doesn't match", E_USER_ERROR);
  252. }
  253. if (serialize($from_lines) != serialize($rev->getFinal())) {
  254. trigger_error("Reversed final doesn't match", E_USER_ERROR);
  255. }
  256. $prevtype = null;
  257. foreach ($this->_edits as $edit) {
  258. if ($prevtype == get_class($edit)) {
  259. trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
  260. }
  261. $prevtype = get_class($edit);
  262. }
  263. return true;
  264. }
  265. }
  266. /**
  267. * @package Text_Diff
  268. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  269. */
  270. class Text_MappedDiff extends Text_Diff {
  271. /**
  272. * Computes a diff between sequences of strings.
  273. *
  274. * This can be used to compute things like case-insensitve diffs, or diffs
  275. * which ignore changes in white-space.
  276. *
  277. * @param array $from_lines An array of strings.
  278. * @param array $to_lines An array of strings.
  279. * @param array $mapped_from_lines This array should have the same size
  280. * number of elements as $from_lines. The
  281. * elements in $mapped_from_lines and
  282. * $mapped_to_lines are what is actually
  283. * compared when computing the diff.
  284. * @param array $mapped_to_lines This array should have the same number
  285. * of elements as $to_lines.
  286. */
  287. function __construct($from_lines, $to_lines,
  288. $mapped_from_lines, $mapped_to_lines)
  289. {
  290. assert(count($from_lines) == count($mapped_from_lines));
  291. assert(count($to_lines) == count($mapped_to_lines));
  292. parent::Text_Diff($mapped_from_lines, $mapped_to_lines);
  293. $xi = $yi = 0;
  294. for ($i = 0; $i < count($this->_edits); $i++) {
  295. $orig = &$this->_edits[$i]->orig;
  296. if (is_array($orig)) {
  297. $orig = array_slice($from_lines, $xi, count($orig));
  298. $xi += count($orig);
  299. }
  300. $final = &$this->_edits[$i]->final;
  301. if (is_array($final)) {
  302. $final = array_slice($to_lines, $yi, count($final));
  303. $yi += count($final);
  304. }
  305. }
  306. }
  307. /**
  308. * PHP4 constructor.
  309. */
  310. public function Text_MappedDiff( $from_lines, $to_lines,
  311. $mapped_from_lines, $mapped_to_lines ) {
  312. self::__construct( $from_lines, $to_lines,
  313. $mapped_from_lines, $mapped_to_lines );
  314. }
  315. }
  316. /**
  317. * @package Text_Diff
  318. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  319. *
  320. * @access private
  321. */
  322. class Text_Diff_Op {
  323. var $orig;
  324. var $final;
  325. function &reverse()
  326. {
  327. trigger_error('Abstract method', E_USER_ERROR);
  328. }
  329. function norig()
  330. {
  331. return $this->orig ? count($this->orig) : 0;
  332. }
  333. function nfinal()
  334. {
  335. return $this->final ? count($this->final) : 0;
  336. }
  337. }
  338. /**
  339. * @package Text_Diff
  340. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  341. *
  342. * @access private
  343. */
  344. class Text_Diff_Op_copy extends Text_Diff_Op {
  345. /**
  346. * PHP5 constructor.
  347. */
  348. function __construct( $orig, $final = false )
  349. {
  350. if (!is_array($final)) {
  351. $final = $orig;
  352. }
  353. $this->orig = $orig;
  354. $this->final = $final;
  355. }
  356. /**
  357. * PHP4 constructor.
  358. */
  359. public function Text_Diff_Op_copy( $orig, $final = false ) {
  360. self::__construct( $orig, $final );
  361. }
  362. function &reverse()
  363. {
  364. $reverse = new Text_Diff_Op_copy($this->final, $this->orig);
  365. return $reverse;
  366. }
  367. }
  368. /**
  369. * @package Text_Diff
  370. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  371. *
  372. * @access private
  373. */
  374. class Text_Diff_Op_delete extends Text_Diff_Op {
  375. /**
  376. * PHP5 constructor.
  377. */
  378. function __construct( $lines )
  379. {
  380. $this->orig = $lines;
  381. $this->final = false;
  382. }
  383. /**
  384. * PHP4 constructor.
  385. */
  386. public function Text_Diff_Op_delete( $lines ) {
  387. self::__construct( $lines );
  388. }
  389. function &reverse()
  390. {
  391. $reverse = new Text_Diff_Op_add($this->orig);
  392. return $reverse;
  393. }
  394. }
  395. /**
  396. * @package Text_Diff
  397. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  398. *
  399. * @access private
  400. */
  401. class Text_Diff_Op_add extends Text_Diff_Op {
  402. /**
  403. * PHP5 constructor.
  404. */
  405. function __construct( $lines )
  406. {
  407. $this->final = $lines;
  408. $this->orig = false;
  409. }
  410. /**
  411. * PHP4 constructor.
  412. */
  413. public function Text_Diff_Op_add( $lines ) {
  414. self::__construct( $lines );
  415. }
  416. function &reverse()
  417. {
  418. $reverse = new Text_Diff_Op_delete($this->final);
  419. return $reverse;
  420. }
  421. }
  422. /**
  423. * @package Text_Diff
  424. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  425. *
  426. * @access private
  427. */
  428. class Text_Diff_Op_change extends Text_Diff_Op {
  429. /**
  430. * PHP5 constructor.
  431. */
  432. function __construct( $orig, $final )
  433. {
  434. $this->orig = $orig;
  435. $this->final = $final;
  436. }
  437. /**
  438. * PHP4 constructor.
  439. */
  440. public function Text_Diff_Op_change( $orig, $final ) {
  441. self::__construct( $orig, $final );
  442. }
  443. function &reverse()
  444. {
  445. $reverse = new Text_Diff_Op_change($this->final, $this->orig);
  446. return $reverse;
  447. }
  448. }