PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/wp-diff.php

https://github.com/shlomsky/lim
PHP | 478 lines | 183 code | 55 blank | 240 comment | 26 complexity | fc3630e68303082d4a83965d4c508e15 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Diff bastard child of old MediaWiki Diff Formatter.
  4. *
  5. * Basically all that remains is the table structure and some method names.
  6. *
  7. * @package WordPress
  8. * @subpackage Diff
  9. */
  10. if ( !class_exists( 'Text_Diff' ) ) {
  11. /** Text_Diff class */
  12. require( dirname(__FILE__).'/Text/Diff.php' );
  13. /** Text_Diff_Renderer class */
  14. require( dirname(__FILE__).'/Text/Diff/Renderer.php' );
  15. /** Text_Diff_Renderer_inline class */
  16. require( dirname(__FILE__).'/Text/Diff/Renderer/inline.php' );
  17. }
  18. /**
  19. * Table renderer to display the diff lines.
  20. *
  21. * @since 2.6.0
  22. * @uses Text_Diff_Renderer Extends
  23. */
  24. class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
  25. /**
  26. * @see Text_Diff_Renderer::_leading_context_lines
  27. * @var int
  28. * @access protected
  29. * @since 2.6.0
  30. */
  31. var $_leading_context_lines = 10000;
  32. /**
  33. * @see Text_Diff_Renderer::_trailing_context_lines
  34. * @var int
  35. * @access protected
  36. * @since 2.6.0
  37. */
  38. var $_trailing_context_lines = 10000;
  39. /**
  40. * {@internal Missing Description}}
  41. *
  42. * @var float
  43. * @access protected
  44. * @since 2.6.0
  45. */
  46. var $_diff_threshold = 0.6;
  47. /**
  48. * Inline display helper object name.
  49. *
  50. * @var string
  51. * @access protected
  52. * @since 2.6.0
  53. */
  54. var $inline_diff_renderer = 'WP_Text_Diff_Renderer_inline';
  55. /**
  56. * PHP4 Constructor - Call parent constructor with params array.
  57. *
  58. * This will set class properties based on the key value pairs in the array.
  59. *
  60. * @since unknown
  61. *
  62. * @param array $params
  63. */
  64. function Text_Diff_Renderer_Table( $params = array() ) {
  65. $parent = get_parent_class($this);
  66. $this->$parent( $params );
  67. }
  68. /**
  69. * @ignore
  70. *
  71. * @param string $header
  72. * @return string
  73. */
  74. function _startBlock( $header ) {
  75. return '';
  76. }
  77. /**
  78. * @ignore
  79. *
  80. * @param array $lines
  81. * @param string $prefix
  82. */
  83. function _lines( $lines, $prefix=' ' ) {
  84. }
  85. /**
  86. * @ignore
  87. *
  88. * @param string $line HTML-escape the value.
  89. * @return string
  90. */
  91. function addedLine( $line ) {
  92. return "<td>+</td><td class='diff-addedline'>{$line}</td>";
  93. }
  94. /**
  95. * @ignore
  96. *
  97. * @param string $line HTML-escape the value.
  98. * @return string
  99. */
  100. function deletedLine( $line ) {
  101. return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
  102. }
  103. /**
  104. * @ignore
  105. *
  106. * @param string $line HTML-escape the value.
  107. * @return string
  108. */
  109. function contextLine( $line ) {
  110. return "<td> </td><td class='diff-context'>{$line}</td>";
  111. }
  112. /**
  113. * @ignore
  114. *
  115. * @return string
  116. */
  117. function emptyLine() {
  118. return '<td colspan="2">&nbsp;</td>';
  119. }
  120. /**
  121. * @ignore
  122. * @access private
  123. *
  124. * @param array $lines
  125. * @param bool $encode
  126. * @return string
  127. */
  128. function _added( $lines, $encode = true ) {
  129. $r = '';
  130. foreach ($lines as $line) {
  131. if ( $encode )
  132. $line = htmlspecialchars( $line );
  133. $r .= '<tr>' . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n";
  134. }
  135. return $r;
  136. }
  137. /**
  138. * @ignore
  139. * @access private
  140. *
  141. * @param array $lines
  142. * @param bool $encode
  143. * @return string
  144. */
  145. function _deleted( $lines, $encode = true ) {
  146. $r = '';
  147. foreach ($lines as $line) {
  148. if ( $encode )
  149. $line = htmlspecialchars( $line );
  150. $r .= '<tr>' . $this->deletedLine( $line ) . $this->emptyLine() . "</tr>\n";
  151. }
  152. return $r;
  153. }
  154. /**
  155. * @ignore
  156. * @access private
  157. *
  158. * @param array $lines
  159. * @param bool $encode
  160. * @return string
  161. */
  162. function _context( $lines, $encode = true ) {
  163. $r = '';
  164. foreach ($lines as $line) {
  165. if ( $encode )
  166. $line = htmlspecialchars( $line );
  167. $r .= '<tr>' .
  168. $this->contextLine( $line ) . $this->contextLine( $line ) . "</tr>\n";
  169. }
  170. return $r;
  171. }
  172. /**
  173. * Process changed lines to do word-by-word diffs for extra highlighting.
  174. *
  175. * (TRAC style) sometimes these lines can actually be deleted or added rows.
  176. * We do additional processing to figure that out
  177. *
  178. * @access private
  179. * @since 2.6.0
  180. *
  181. * @param array $orig
  182. * @param array $final
  183. * @return string
  184. */
  185. function _changed( $orig, $final ) {
  186. $r = '';
  187. // Does the aforementioned additional processing
  188. // *_matches tell what rows are "the same" in orig and final. Those pairs will be diffed to get word changes
  189. // match is numeric: an index in other column
  190. // match is 'X': no match. It is a new row
  191. // *_rows are column vectors for the orig column and the final column.
  192. // row >= 0: an indix of the $orig or $final array
  193. // row < 0: a blank row for that column
  194. list($orig_matches, $final_matches, $orig_rows, $final_rows) = $this->interleave_changed_lines( $orig, $final );
  195. // These will hold the word changes as determined by an inline diff
  196. $orig_diffs = array();
  197. $final_diffs = array();
  198. // Compute word diffs for each matched pair using the inline diff
  199. foreach ( $orig_matches as $o => $f ) {
  200. if ( is_numeric($o) && is_numeric($f) ) {
  201. $text_diff = new Text_Diff( 'auto', array( array($orig[$o]), array($final[$f]) ) );
  202. $renderer = new $this->inline_diff_renderer;
  203. $diff = $renderer->render( $text_diff );
  204. // If they're too different, don't include any <ins> or <dels>
  205. if ( $diff_count = preg_match_all( '!(<ins>.*?</ins>|<del>.*?</del>)!', $diff, $diff_matches ) ) {
  206. // length of all text between <ins> or <del>
  207. $stripped_matches = strlen(strip_tags( join(' ', $diff_matches[0]) ));
  208. // since we count lengith of text between <ins> or <del> (instead of picking just one),
  209. // we double the length of chars not in those tags.
  210. $stripped_diff = strlen(strip_tags( $diff )) * 2 - $stripped_matches;
  211. $diff_ratio = $stripped_matches / $stripped_diff;
  212. if ( $diff_ratio > $this->_diff_threshold )
  213. continue; // Too different. Don't save diffs.
  214. }
  215. // Un-inline the diffs by removing del or ins
  216. $orig_diffs[$o] = preg_replace( '|<ins>.*?</ins>|', '', $diff );
  217. $final_diffs[$f] = preg_replace( '|<del>.*?</del>|', '', $diff );
  218. }
  219. }
  220. foreach ( array_keys($orig_rows) as $row ) {
  221. // Both columns have blanks. Ignore them.
  222. if ( $orig_rows[$row] < 0 && $final_rows[$row] < 0 )
  223. continue;
  224. // If we have a word based diff, use it. Otherwise, use the normal line.
  225. if ( isset( $orig_diffs[$orig_rows[$row]] ) )
  226. $orig_line = $orig_diffs[$orig_rows[$row]];
  227. elseif ( isset( $orig[$orig_rows[$row]] ) )
  228. $orig_line = htmlspecialchars($orig[$orig_rows[$row]]);
  229. else
  230. $orig_line = '';
  231. if ( isset( $final_diffs[$final_rows[$row]] ) )
  232. $final_line = $final_diffs[$final_rows[$row]];
  233. elseif ( isset( $final[$final_rows[$row]] ) )
  234. $final_line = htmlspecialchars($final[$final_rows[$row]]);
  235. else
  236. $final_line = '';
  237. if ( $orig_rows[$row] < 0 ) { // Orig is blank. This is really an added row.
  238. $r .= $this->_added( array($final_line), false );
  239. } elseif ( $final_rows[$row] < 0 ) { // Final is blank. This is really a deleted row.
  240. $r .= $this->_deleted( array($orig_line), false );
  241. } else { // A true changed row.
  242. $r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->addedLine( $final_line ) . "</tr>\n";
  243. }
  244. }
  245. return $r;
  246. }
  247. /**
  248. * Takes changed blocks and matches which rows in orig turned into which rows in final.
  249. *
  250. * Returns
  251. * *_matches ( which rows match with which )
  252. * *_rows ( order of rows in each column interleaved with blank rows as
  253. * necessary )
  254. *
  255. * @since 2.6.0
  256. *
  257. * @param unknown_type $orig
  258. * @param unknown_type $final
  259. * @return unknown
  260. */
  261. function interleave_changed_lines( $orig, $final ) {
  262. // Contains all pairwise string comparisons. Keys are such that this need only be a one dimensional array.
  263. $matches = array();
  264. foreach ( array_keys($orig) as $o ) {
  265. foreach ( array_keys($final) as $f ) {
  266. $matches["$o,$f"] = $this->compute_string_distance( $orig[$o], $final[$f] );
  267. }
  268. }
  269. asort($matches); // Order by string distance.
  270. $orig_matches = array();
  271. $final_matches = array();
  272. foreach ( $matches as $keys => $difference ) {
  273. list($o, $f) = explode(',', $keys);
  274. $o = (int) $o;
  275. $f = (int) $f;
  276. // Already have better matches for these guys
  277. if ( isset($orig_matches[$o]) && isset($final_matches[$f]) )
  278. continue;
  279. // First match for these guys. Must be best match
  280. if ( !isset($orig_matches[$o]) && !isset($final_matches[$f]) ) {
  281. $orig_matches[$o] = $f;
  282. $final_matches[$f] = $o;
  283. continue;
  284. }
  285. // Best match of this final is already taken? Must mean this final is a new row.
  286. if ( isset($orig_matches[$o]) )
  287. $final_matches[$f] = 'x';
  288. // Best match of this orig is already taken? Must mean this orig is a deleted row.
  289. elseif ( isset($final_matches[$f]) )
  290. $orig_matches[$o] = 'x';
  291. }
  292. // We read the text in this order
  293. ksort($orig_matches);
  294. ksort($final_matches);
  295. // Stores rows and blanks for each column.
  296. $orig_rows = $orig_rows_copy = array_keys($orig_matches);
  297. $final_rows = array_keys($final_matches);
  298. // Interleaves rows with blanks to keep matches aligned.
  299. // We may end up with some extraneous blank rows, but we'll just ignore them later.
  300. foreach ( $orig_rows_copy as $orig_row ) {
  301. $final_pos = array_search($orig_matches[$orig_row], $final_rows, true);
  302. $orig_pos = (int) array_search($orig_row, $orig_rows, true);
  303. if ( false === $final_pos ) { // This orig is paired with a blank final.
  304. array_splice( $final_rows, $orig_pos, 0, -1 );
  305. } elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.
  306. $diff_pos = $final_pos - $orig_pos;
  307. while ( $diff_pos < 0 )
  308. array_splice( $final_rows, $orig_pos, 0, $diff_pos++ );
  309. } elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.
  310. $diff_pos = $orig_pos - $final_pos;
  311. while ( $diff_pos < 0 )
  312. array_splice( $orig_rows, $orig_pos, 0, $diff_pos++ );
  313. }
  314. }
  315. // Pad the ends with blank rows if the columns aren't the same length
  316. $diff_count = count($orig_rows) - count($final_rows);
  317. if ( $diff_count < 0 ) {
  318. while ( $diff_count < 0 )
  319. array_push($orig_rows, $diff_count++);
  320. } elseif ( $diff_count > 0 ) {
  321. $diff_count = -1 * $diff_count;
  322. while ( $diff_count < 0 )
  323. array_push($final_rows, $diff_count++);
  324. }
  325. return array($orig_matches, $final_matches, $orig_rows, $final_rows);
  326. /*
  327. // Debug
  328. echo "\n\n\n\n\n";
  329. echo "-- DEBUG Matches: Orig -> Final --";
  330. foreach ( $orig_matches as $o => $f ) {
  331. echo "\n\n\n\n\n";
  332. echo "ORIG: $o, FINAL: $f\n";
  333. var_dump($orig[$o],$final[$f]);
  334. }
  335. echo "\n\n\n\n\n";
  336. echo "-- DEBUG Matches: Final -> Orig --";
  337. foreach ( $final_matches as $f => $o ) {
  338. echo "\n\n\n\n\n";
  339. echo "FINAL: $f, ORIG: $o\n";
  340. var_dump($final[$f],$orig[$o]);
  341. }
  342. echo "\n\n\n\n\n";
  343. echo "-- DEBUG Rows: Orig -- Final --";
  344. echo "\n\n\n\n\n";
  345. foreach ( $orig_rows as $row => $o ) {
  346. if ( $o < 0 )
  347. $o = 'X';
  348. $f = $final_rows[$row];
  349. if ( $f < 0 )
  350. $f = 'X';
  351. echo "$o -- $f\n";
  352. }
  353. echo "\n\n\n\n\n";
  354. echo "-- END DEBUG --";
  355. echo "\n\n\n\n\n";
  356. return array($orig_matches, $final_matches, $orig_rows, $final_rows);
  357. */
  358. }
  359. /**
  360. * Computes a number that is intended to reflect the "distance" between two strings.
  361. *
  362. * @since 2.6.0
  363. *
  364. * @param string $string1
  365. * @param string $string2
  366. * @return int
  367. */
  368. function compute_string_distance( $string1, $string2 ) {
  369. // Vectors containing character frequency for all chars in each string
  370. $chars1 = count_chars($string1);
  371. $chars2 = count_chars($string2);
  372. // L1-norm of difference vector.
  373. $difference = array_sum( array_map( array(&$this, 'difference'), $chars1, $chars2 ) );
  374. // $string1 has zero length? Odd. Give huge penalty by not dividing.
  375. if ( !$string1 )
  376. return $difference;
  377. // Return distance per charcter (of string1)
  378. return $difference / strlen($string1);
  379. }
  380. /**
  381. * @ignore
  382. * @since 2.6.0
  383. *
  384. * @param int $a
  385. * @param int $b
  386. * @return int
  387. */
  388. function difference( $a, $b ) {
  389. return abs( $a - $b );
  390. }
  391. }
  392. /**
  393. * Better word splitting than the PEAR package provides.
  394. *
  395. * @since 2.6.0
  396. * @uses Text_Diff_Renderer_inline Extends
  397. */
  398. class WP_Text_Diff_Renderer_inline extends Text_Diff_Renderer_inline {
  399. /**
  400. * @ignore
  401. * @since 2.6.0
  402. *
  403. * @param string $string
  404. * @param string $newlineEscape
  405. * @return string
  406. */
  407. function _splitOnWords($string, $newlineEscape = "\n") {
  408. $string = str_replace("\0", '', $string);
  409. $words = preg_split( '/([^\w])/u', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
  410. $words = str_replace( "\n", $newlineEscape, $words );
  411. return $words;
  412. }
  413. }
  414. ?>