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

/wp-includes/wp-diff.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 548 lines | 220 code | 61 blank | 267 comment | 35 complexity | 3576ccddba4c3f91d3dd15747f9d2b14 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  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 public
  29. * @since 2.6.0
  30. */
  31. public $_leading_context_lines = 10000;
  32. /**
  33. * @see Text_Diff_Renderer::_trailing_context_lines
  34. * @var int
  35. * @access public
  36. * @since 2.6.0
  37. */
  38. public $_trailing_context_lines = 10000;
  39. /**
  40. * {@internal Missing Description}}
  41. *
  42. * @var float
  43. * @access protected
  44. * @since 2.6.0
  45. */
  46. protected $_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. protected $inline_diff_renderer = 'WP_Text_Diff_Renderer_inline';
  55. /**
  56. * Should we show the split view or not
  57. *
  58. * @var string
  59. * @access protected
  60. * @since 3.6.0
  61. */
  62. protected $_show_split_view = true;
  63. /**
  64. * Constructor - Call parent constructor with params array.
  65. *
  66. * This will set class properties based on the key value pairs in the array.
  67. *
  68. * @since 2.6.0
  69. *
  70. * @param array $params
  71. */
  72. public function __construct( $params = array() ) {
  73. parent::__construct( $params );
  74. if ( isset( $params[ 'show_split_view' ] ) )
  75. $this->_show_split_view = $params[ 'show_split_view' ];
  76. }
  77. /**
  78. * @ignore
  79. *
  80. * @param string $header
  81. * @return string
  82. */
  83. public function _startBlock( $header ) {
  84. return '';
  85. }
  86. /**
  87. * @ignore
  88. *
  89. * @param array $lines
  90. * @param string $prefix
  91. */
  92. public function _lines( $lines, $prefix=' ' ) {
  93. }
  94. /**
  95. * @ignore
  96. *
  97. * @param string $line HTML-escape the value.
  98. * @return string
  99. */
  100. public function addedLine( $line ) {
  101. return "<td class='diff-addedline'>{$line}</td>";
  102. }
  103. /**
  104. * @ignore
  105. *
  106. * @param string $line HTML-escape the value.
  107. * @return string
  108. */
  109. public function deletedLine( $line ) {
  110. return "<td class='diff-deletedline'>{$line}</td>";
  111. }
  112. /**
  113. * @ignore
  114. *
  115. * @param string $line HTML-escape the value.
  116. * @return string
  117. */
  118. public function contextLine( $line ) {
  119. return "<td class='diff-context'>{$line}</td>";
  120. }
  121. /**
  122. * @ignore
  123. *
  124. * @return string
  125. */
  126. public function emptyLine() {
  127. return '<td>&nbsp;</td>';
  128. }
  129. /**
  130. * @ignore
  131. * @access public
  132. *
  133. * @param array $lines
  134. * @param bool $encode
  135. * @return string
  136. */
  137. public function _added( $lines, $encode = true ) {
  138. $r = '';
  139. foreach ($lines as $line) {
  140. if ( $encode ) {
  141. $processed_line = htmlspecialchars( $line );
  142. /**
  143. * Contextually filter a diffed line.
  144. *
  145. * Filters TextDiff processing of diffed line. By default, diffs are processed with
  146. * htmlspecialchars. Use this filter to remove or change the processing. Passes a context
  147. * indicating if the line is added, deleted or unchanged.
  148. *
  149. * @since 4.1.0
  150. *
  151. * @param String $processed_line The processed diffed line.
  152. * @param String $line The unprocessed diffed line.
  153. * @param string null The line context. Values are 'added', 'deleted' or 'unchanged'.
  154. */
  155. $line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'added' );
  156. }
  157. if ( $this->_show_split_view ) {
  158. $r .= '<tr>' . $this->emptyLine() . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n";
  159. } else {
  160. $r .= '<tr>' . $this->addedLine( $line ) . "</tr>\n";
  161. }
  162. }
  163. return $r;
  164. }
  165. /**
  166. * @ignore
  167. * @access public
  168. *
  169. * @param array $lines
  170. * @param bool $encode
  171. * @return string
  172. */
  173. public function _deleted( $lines, $encode = true ) {
  174. $r = '';
  175. foreach ($lines as $line) {
  176. if ( $encode ) {
  177. $processed_line = htmlspecialchars( $line );
  178. /** This filter is documented in wp-includes/wp-diff.php */
  179. $line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'deleted' );
  180. }
  181. if ( $this->_show_split_view ) {
  182. $r .= '<tr>' . $this->deletedLine( $line ) . $this->emptyLine() . $this->emptyLine() . "</tr>\n";
  183. } else {
  184. $r .= '<tr>' . $this->deletedLine( $line ) . "</tr>\n";
  185. }
  186. }
  187. return $r;
  188. }
  189. /**
  190. * @ignore
  191. * @access public
  192. *
  193. * @param array $lines
  194. * @param bool $encode
  195. * @return string
  196. */
  197. public function _context( $lines, $encode = true ) {
  198. $r = '';
  199. foreach ($lines as $line) {
  200. if ( $encode ) {
  201. $processed_line = htmlspecialchars( $line );
  202. /** This filter is documented in wp-includes/wp-diff.php */
  203. $line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'unchanged' );
  204. }
  205. if ( $this->_show_split_view ) {
  206. $r .= '<tr>' . $this->contextLine( $line ) . $this->emptyLine() . $this->contextLine( $line ) . "</tr>\n";
  207. } else {
  208. $r .= '<tr>' . $this->contextLine( $line ) . "</tr>\n";
  209. }
  210. }
  211. return $r;
  212. }
  213. /**
  214. * Process changed lines to do word-by-word diffs for extra highlighting.
  215. *
  216. * (TRAC style) sometimes these lines can actually be deleted or added rows.
  217. * We do additional processing to figure that out
  218. *
  219. * @access public
  220. * @since 2.6.0
  221. *
  222. * @param array $orig
  223. * @param array $final
  224. * @return string
  225. */
  226. public function _changed( $orig, $final ) {
  227. $r = '';
  228. // Does the aforementioned additional processing
  229. // *_matches tell what rows are "the same" in orig and final. Those pairs will be diffed to get word changes
  230. // match is numeric: an index in other column
  231. // match is 'X': no match. It is a new row
  232. // *_rows are column vectors for the orig column and the final column.
  233. // row >= 0: an indix of the $orig or $final array
  234. // row < 0: a blank row for that column
  235. list($orig_matches, $final_matches, $orig_rows, $final_rows) = $this->interleave_changed_lines( $orig, $final );
  236. // These will hold the word changes as determined by an inline diff
  237. $orig_diffs = array();
  238. $final_diffs = array();
  239. // Compute word diffs for each matched pair using the inline diff
  240. foreach ( $orig_matches as $o => $f ) {
  241. if ( is_numeric($o) && is_numeric($f) ) {
  242. $text_diff = new Text_Diff( 'auto', array( array($orig[$o]), array($final[$f]) ) );
  243. $renderer = new $this->inline_diff_renderer;
  244. $diff = $renderer->render( $text_diff );
  245. // If they're too different, don't include any <ins> or <dels>
  246. if ( $diff_count = preg_match_all( '!(<ins>.*?</ins>|<del>.*?</del>)!', $diff, $diff_matches ) ) {
  247. // length of all text between <ins> or <del>
  248. $stripped_matches = strlen(strip_tags( join(' ', $diff_matches[0]) ));
  249. // since we count lengith of text between <ins> or <del> (instead of picking just one),
  250. // we double the length of chars not in those tags.
  251. $stripped_diff = strlen(strip_tags( $diff )) * 2 - $stripped_matches;
  252. $diff_ratio = $stripped_matches / $stripped_diff;
  253. if ( $diff_ratio > $this->_diff_threshold )
  254. continue; // Too different. Don't save diffs.
  255. }
  256. // Un-inline the diffs by removing del or ins
  257. $orig_diffs[$o] = preg_replace( '|<ins>.*?</ins>|', '', $diff );
  258. $final_diffs[$f] = preg_replace( '|<del>.*?</del>|', '', $diff );
  259. }
  260. }
  261. foreach ( array_keys($orig_rows) as $row ) {
  262. // Both columns have blanks. Ignore them.
  263. if ( $orig_rows[$row] < 0 && $final_rows[$row] < 0 )
  264. continue;
  265. // If we have a word based diff, use it. Otherwise, use the normal line.
  266. if ( isset( $orig_diffs[$orig_rows[$row]] ) )
  267. $orig_line = $orig_diffs[$orig_rows[$row]];
  268. elseif ( isset( $orig[$orig_rows[$row]] ) )
  269. $orig_line = htmlspecialchars($orig[$orig_rows[$row]]);
  270. else
  271. $orig_line = '';
  272. if ( isset( $final_diffs[$final_rows[$row]] ) )
  273. $final_line = $final_diffs[$final_rows[$row]];
  274. elseif ( isset( $final[$final_rows[$row]] ) )
  275. $final_line = htmlspecialchars($final[$final_rows[$row]]);
  276. else
  277. $final_line = '';
  278. if ( $orig_rows[$row] < 0 ) { // Orig is blank. This is really an added row.
  279. $r .= $this->_added( array($final_line), false );
  280. } elseif ( $final_rows[$row] < 0 ) { // Final is blank. This is really a deleted row.
  281. $r .= $this->_deleted( array($orig_line), false );
  282. } else { // A true changed row.
  283. if ( $this->_show_split_view ) {
  284. $r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->emptyLine() . $this->addedLine( $final_line ) . "</tr>\n";
  285. } else {
  286. $r .= '<tr>' . $this->deletedLine( $orig_line ) . "</tr><tr>" . $this->addedLine( $final_line ) . "</tr>\n";
  287. }
  288. }
  289. }
  290. return $r;
  291. }
  292. /**
  293. * Takes changed blocks and matches which rows in orig turned into which rows in final.
  294. *
  295. * Returns
  296. * *_matches ( which rows match with which )
  297. * *_rows ( order of rows in each column interleaved with blank rows as
  298. * necessary )
  299. *
  300. * @since 2.6.0
  301. *
  302. * @param array $orig
  303. * @param array $final
  304. * @return array
  305. */
  306. public function interleave_changed_lines( $orig, $final ) {
  307. // Contains all pairwise string comparisons. Keys are such that this need only be a one dimensional array.
  308. $matches = array();
  309. foreach ( array_keys($orig) as $o ) {
  310. foreach ( array_keys($final) as $f ) {
  311. $matches["$o,$f"] = $this->compute_string_distance( $orig[$o], $final[$f] );
  312. }
  313. }
  314. asort($matches); // Order by string distance.
  315. $orig_matches = array();
  316. $final_matches = array();
  317. foreach ( $matches as $keys => $difference ) {
  318. list($o, $f) = explode(',', $keys);
  319. $o = (int) $o;
  320. $f = (int) $f;
  321. // Already have better matches for these guys
  322. if ( isset($orig_matches[$o]) && isset($final_matches[$f]) )
  323. continue;
  324. // First match for these guys. Must be best match
  325. if ( !isset($orig_matches[$o]) && !isset($final_matches[$f]) ) {
  326. $orig_matches[$o] = $f;
  327. $final_matches[$f] = $o;
  328. continue;
  329. }
  330. // Best match of this final is already taken? Must mean this final is a new row.
  331. if ( isset($orig_matches[$o]) )
  332. $final_matches[$f] = 'x';
  333. // Best match of this orig is already taken? Must mean this orig is a deleted row.
  334. elseif ( isset($final_matches[$f]) )
  335. $orig_matches[$o] = 'x';
  336. }
  337. // We read the text in this order
  338. ksort($orig_matches);
  339. ksort($final_matches);
  340. // Stores rows and blanks for each column.
  341. $orig_rows = $orig_rows_copy = array_keys($orig_matches);
  342. $final_rows = array_keys($final_matches);
  343. // Interleaves rows with blanks to keep matches aligned.
  344. // We may end up with some extraneous blank rows, but we'll just ignore them later.
  345. foreach ( $orig_rows_copy as $orig_row ) {
  346. $final_pos = array_search($orig_matches[$orig_row], $final_rows, true);
  347. $orig_pos = (int) array_search($orig_row, $orig_rows, true);
  348. if ( false === $final_pos ) { // This orig is paired with a blank final.
  349. array_splice( $final_rows, $orig_pos, 0, -1 );
  350. } elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.
  351. $diff_pos = $final_pos - $orig_pos;
  352. while ( $diff_pos < 0 )
  353. array_splice( $final_rows, $orig_pos, 0, $diff_pos++ );
  354. } elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.
  355. $diff_pos = $orig_pos - $final_pos;
  356. while ( $diff_pos < 0 )
  357. array_splice( $orig_rows, $orig_pos, 0, $diff_pos++ );
  358. }
  359. }
  360. // Pad the ends with blank rows if the columns aren't the same length
  361. $diff_count = count($orig_rows) - count($final_rows);
  362. if ( $diff_count < 0 ) {
  363. while ( $diff_count < 0 )
  364. array_push($orig_rows, $diff_count++);
  365. } elseif ( $diff_count > 0 ) {
  366. $diff_count = -1 * $diff_count;
  367. while ( $diff_count < 0 )
  368. array_push($final_rows, $diff_count++);
  369. }
  370. return array($orig_matches, $final_matches, $orig_rows, $final_rows);
  371. }
  372. /**
  373. * Computes a number that is intended to reflect the "distance" between two strings.
  374. *
  375. * @since 2.6.0
  376. *
  377. * @param string $string1
  378. * @param string $string2
  379. * @return int
  380. */
  381. public function compute_string_distance( $string1, $string2 ) {
  382. // Vectors containing character frequency for all chars in each string
  383. $chars1 = count_chars($string1);
  384. $chars2 = count_chars($string2);
  385. // L1-norm of difference vector.
  386. $difference = array_sum( array_map( array($this, 'difference'), $chars1, $chars2 ) );
  387. // $string1 has zero length? Odd. Give huge penalty by not dividing.
  388. if ( !$string1 )
  389. return $difference;
  390. // Return distance per character (of string1).
  391. return $difference / strlen($string1);
  392. }
  393. /**
  394. * @ignore
  395. * @since 2.6.0
  396. *
  397. * @param int $a
  398. * @param int $b
  399. * @return int
  400. */
  401. public function difference( $a, $b ) {
  402. return abs( $a - $b );
  403. }
  404. /**
  405. * Make private properties readable for backwards compatibility.
  406. *
  407. * @since 4.0.0
  408. * @access public
  409. *
  410. * @param string $name Property to get.
  411. * @return mixed Property.
  412. */
  413. public function __get( $name ) {
  414. return $this->$name;
  415. }
  416. /**
  417. * Make private properties settable for backwards compatibility.
  418. *
  419. * @since 4.0.0
  420. * @access public
  421. *
  422. * @param string $name Property to check if set.
  423. * @param mixed $value Property value.
  424. * @return mixed Newly-set property.
  425. */
  426. public function __set( $name, $value ) {
  427. return $this->$name = $value;
  428. }
  429. /**
  430. * Make private properties checkable for backwards compatibility.
  431. *
  432. * @since 4.0.0
  433. * @access public
  434. *
  435. * @param string $name Property to check if set.
  436. * @return bool Whether the property is set.
  437. */
  438. public function __isset( $name ) {
  439. return isset( $this->$name );
  440. }
  441. /**
  442. * Make private properties un-settable for backwards compatibility.
  443. *
  444. * @since 4.0.0
  445. * @access public
  446. *
  447. * @param string $name Property to unset.
  448. */
  449. public function __unset( $name ) {
  450. unset( $this->$name );
  451. }
  452. /**
  453. * Make private/protected methods readable for backwards compatibility.
  454. *
  455. * @since 4.0.0
  456. * @access public
  457. *
  458. * @param callable $name Method to call.
  459. * @param array $arguments Arguments to pass when calling.
  460. * @return mixed|bool Return value of the callback, false otherwise.
  461. */
  462. public function __call( $name, $arguments ) {
  463. return call_user_func_array( array( $this, $name ), $arguments );
  464. }
  465. }
  466. /**
  467. * Better word splitting than the PEAR package provides.
  468. *
  469. * @since 2.6.0
  470. * @uses Text_Diff_Renderer_inline Extends
  471. */
  472. class WP_Text_Diff_Renderer_inline extends Text_Diff_Renderer_inline {
  473. /**
  474. * @ignore
  475. * @since 2.6.0
  476. *
  477. * @param string $string
  478. * @param string $newlineEscape
  479. * @return string
  480. */
  481. public function _splitOnWords($string, $newlineEscape = "\n") {
  482. $string = str_replace("\0", '', $string);
  483. $words = preg_split( '/([^\w])/u', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
  484. $words = str_replace( "\n", $newlineEscape, $words );
  485. return $words;
  486. }
  487. }