PageRenderTime 34ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/wp-diff.php

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