PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/class-wp-text-diff-renderer-table.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 551 lines | 236 code | 60 blank | 255 comment | 41 complexity | c8784e36dbe400e709811bbda4ecc16d MD5 | raw file
  1. <?php
  2. /**
  3. * Diff API: WP_Text_Diff_Renderer_Table class
  4. *
  5. * @package WordPress
  6. * @subpackage Diff
  7. * @since 4.7.0
  8. */
  9. /**
  10. * Table renderer to display the diff lines.
  11. *
  12. * @since 2.6.0
  13. * @uses Text_Diff_Renderer Extends
  14. */
  15. class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
  16. /**
  17. * @see Text_Diff_Renderer::_leading_context_lines
  18. * @var int
  19. * @since 2.6.0
  20. */
  21. public $_leading_context_lines = 10000;
  22. /**
  23. * @see Text_Diff_Renderer::_trailing_context_lines
  24. * @var int
  25. * @since 2.6.0
  26. */
  27. public $_trailing_context_lines = 10000;
  28. /**
  29. * Threshold for when a diff should be saved or omitted.
  30. *
  31. * @var float
  32. * @since 2.6.0
  33. */
  34. protected $_diff_threshold = 0.6;
  35. /**
  36. * Inline display helper object name.
  37. *
  38. * @var string
  39. * @since 2.6.0
  40. */
  41. protected $inline_diff_renderer = 'WP_Text_Diff_Renderer_inline';
  42. /**
  43. * Should we show the split view or not
  44. *
  45. * @var string
  46. * @since 3.6.0
  47. */
  48. protected $_show_split_view = true;
  49. protected $compat_fields = array( '_show_split_view', 'inline_diff_renderer', '_diff_threshold' );
  50. /**
  51. * Caches the output of count_chars() in compute_string_distance()
  52. *
  53. * @var array
  54. * @since 5.0.0
  55. */
  56. protected $count_cache = array();
  57. /**
  58. * Caches the difference calculation in compute_string_distance()
  59. *
  60. * @var array
  61. * @since 5.0.0
  62. */
  63. protected $difference_cache = array();
  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. /**
  80. * @ignore
  81. *
  82. * @param string $header
  83. * @return string
  84. */
  85. public function _startBlock( $header ) {
  86. return '';
  87. }
  88. /**
  89. * @ignore
  90. *
  91. * @param array $lines
  92. * @param string $prefix
  93. */
  94. public function _lines( $lines, $prefix = ' ' ) {
  95. }
  96. /**
  97. * @ignore
  98. *
  99. * @param string $line HTML-escape the value.
  100. * @return string
  101. */
  102. public function addedLine( $line ) {
  103. return "<td class='diff-addedline'><span aria-hidden='true' class='dashicons dashicons-plus'></span><span class='screen-reader-text'>" . __( 'Added:' ) . " </span>{$line}</td>";
  104. }
  105. /**
  106. * @ignore
  107. *
  108. * @param string $line HTML-escape the value.
  109. * @return string
  110. */
  111. public function deletedLine( $line ) {
  112. return "<td class='diff-deletedline'><span aria-hidden='true' class='dashicons dashicons-minus'></span><span class='screen-reader-text'>" . __( 'Deleted:' ) . " </span>{$line}</td>";
  113. }
  114. /**
  115. * @ignore
  116. *
  117. * @param string $line HTML-escape the value.
  118. * @return string
  119. */
  120. public function contextLine( $line ) {
  121. return "<td class='diff-context'><span class='screen-reader-text'>" . __( 'Unchanged:' ) . " </span>{$line}</td>";
  122. }
  123. /**
  124. * @ignore
  125. *
  126. * @return string
  127. */
  128. public function emptyLine() {
  129. return '<td>&nbsp;</td>';
  130. }
  131. /**
  132. * @ignore
  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 filters 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 $context 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. *
  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. *
  192. * @param array $lines
  193. * @param bool $encode
  194. * @return string
  195. */
  196. public function _context( $lines, $encode = true ) {
  197. $r = '';
  198. foreach ( $lines as $line ) {
  199. if ( $encode ) {
  200. $processed_line = htmlspecialchars( $line );
  201. /** This filter is documented in wp-includes/wp-diff.php */
  202. $line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'unchanged' );
  203. }
  204. if ( $this->_show_split_view ) {
  205. $r .= '<tr>' . $this->contextLine( $line ) . $this->emptyLine() . $this->contextLine( $line ) . "</tr>\n";
  206. } else {
  207. $r .= '<tr>' . $this->contextLine( $line ) . "</tr>\n";
  208. }
  209. }
  210. return $r;
  211. }
  212. /**
  213. * Process changed lines to do word-by-word diffs for extra highlighting.
  214. *
  215. * (TRAC style) sometimes these lines can actually be deleted or added rows.
  216. * We do additional processing to figure that out
  217. *
  218. * @since 2.6.0
  219. *
  220. * @param array $orig
  221. * @param array $final
  222. * @return string
  223. */
  224. public function _changed( $orig, $final ) {
  225. $r = '';
  226. /*
  227. * Does the aforementioned additional processing:
  228. * *_matches tell what rows are "the same" in orig and final. Those pairs will be diffed to get word changes.
  229. * - match is numeric: an index in other column.
  230. * - match is 'X': no match. It is a new row.
  231. * *_rows are column vectors for the orig column and the final column.
  232. * - row >= 0: an indix of the $orig or $final array.
  233. * - row < 0: a blank row for that column.
  234. */
  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 <del>'s.
  246. if ( 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 length 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. }
  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. }
  267. // If we have a word based diff, use it. Otherwise, use the normal line.
  268. if ( isset( $orig_diffs[ $orig_rows[ $row ] ] ) ) {
  269. $orig_line = $orig_diffs[ $orig_rows[ $row ] ];
  270. } elseif ( isset( $orig[ $orig_rows[ $row ] ] ) ) {
  271. $orig_line = htmlspecialchars( $orig[ $orig_rows[ $row ] ] );
  272. } else {
  273. $orig_line = '';
  274. }
  275. if ( isset( $final_diffs[ $final_rows[ $row ] ] ) ) {
  276. $final_line = $final_diffs[ $final_rows[ $row ] ];
  277. } elseif ( isset( $final[ $final_rows[ $row ] ] ) ) {
  278. $final_line = htmlspecialchars( $final[ $final_rows[ $row ] ] );
  279. } else {
  280. $final_line = '';
  281. }
  282. if ( $orig_rows[ $row ] < 0 ) { // Orig is blank. This is really an added row.
  283. $r .= $this->_added( array( $final_line ), false );
  284. } elseif ( $final_rows[ $row ] < 0 ) { // Final is blank. This is really a deleted row.
  285. $r .= $this->_deleted( array( $orig_line ), false );
  286. } else { // A true changed row.
  287. if ( $this->_show_split_view ) {
  288. $r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->emptyLine() . $this->addedLine( $final_line ) . "</tr>\n";
  289. } else {
  290. $r .= '<tr>' . $this->deletedLine( $orig_line ) . '</tr><tr>' . $this->addedLine( $final_line ) . "</tr>\n";
  291. }
  292. }
  293. }
  294. return $r;
  295. }
  296. /**
  297. * Takes changed blocks and matches which rows in orig turned into which rows in final.
  298. *
  299. * @since 2.6.0
  300. *
  301. * @param array $orig Lines of the original version of the text.
  302. * @param array $final Lines of the final version of the text.
  303. * @return array {
  304. * Array containing results of comparing the original text to the final text.
  305. *
  306. * @type array $orig_matches Associative array of original matches. Index == row
  307. * number of `$orig`, value == corresponding row number
  308. * of that same line in `$final` or 'x' if there is no
  309. * corresponding row (indicating it is a deleted line).
  310. * @type array $final_matches Associative array of final matches. Index == row
  311. * number of `$final`, value == corresponding row number
  312. * of that same line in `$orig` or 'x' if there is no
  313. * corresponding row (indicating it is a new line).
  314. * @type array $orig_rows Associative array of interleaved rows of `$orig` with
  315. * blanks to keep matches aligned with side-by-side diff
  316. * of `$final`. A value >= 0 corresponds to index of `$orig`.
  317. * Value < 0 indicates a blank row.
  318. * @type array $final_rows Associative array of interleaved rows of `$final` with
  319. * blanks to keep matches aligned with side-by-side diff
  320. * of `$orig`. A value >= 0 corresponds to index of `$final`.
  321. * Value < 0 indicates a blank row.
  322. * }
  323. */
  324. public function interleave_changed_lines( $orig, $final ) {
  325. // Contains all pairwise string comparisons. Keys are such that this need only be a one dimensional array.
  326. $matches = array();
  327. foreach ( array_keys( $orig ) as $o ) {
  328. foreach ( array_keys( $final ) as $f ) {
  329. $matches[ "$o,$f" ] = $this->compute_string_distance( $orig[ $o ], $final[ $f ] );
  330. }
  331. }
  332. asort( $matches ); // Order by string distance.
  333. $orig_matches = array();
  334. $final_matches = array();
  335. foreach ( $matches as $keys => $difference ) {
  336. list($o, $f) = explode( ',', $keys );
  337. $o = (int) $o;
  338. $f = (int) $f;
  339. // Already have better matches for these guys.
  340. if ( isset( $orig_matches[ $o ] ) && isset( $final_matches[ $f ] ) ) {
  341. continue;
  342. }
  343. // First match for these guys. Must be best match.
  344. if ( ! isset( $orig_matches[ $o ] ) && ! isset( $final_matches[ $f ] ) ) {
  345. $orig_matches[ $o ] = $f;
  346. $final_matches[ $f ] = $o;
  347. continue;
  348. }
  349. // Best match of this final is already taken? Must mean this final is a new row.
  350. if ( isset( $orig_matches[ $o ] ) ) {
  351. $final_matches[ $f ] = 'x';
  352. } elseif ( isset( $final_matches[ $f ] ) ) {
  353. // Best match of this orig is already taken? Must mean this orig is a deleted row.
  354. $orig_matches[ $o ] = 'x';
  355. }
  356. }
  357. // We read the text in this order.
  358. ksort( $orig_matches );
  359. ksort( $final_matches );
  360. // Stores rows and blanks for each column.
  361. $orig_rows = array_keys( $orig_matches );
  362. $orig_rows_copy = $orig_rows;
  363. $final_rows = array_keys( $final_matches );
  364. // Interleaves rows with blanks to keep matches aligned.
  365. // We may end up with some extraneous blank rows, but we'll just ignore them later.
  366. foreach ( $orig_rows_copy as $orig_row ) {
  367. $final_pos = array_search( $orig_matches[ $orig_row ], $final_rows, true );
  368. $orig_pos = (int) array_search( $orig_row, $orig_rows, true );
  369. if ( false === $final_pos ) { // This orig is paired with a blank final.
  370. array_splice( $final_rows, $orig_pos, 0, -1 );
  371. } elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.
  372. $diff_array = range( -1, $final_pos - $orig_pos );
  373. array_splice( $final_rows, $orig_pos, 0, $diff_array );
  374. } elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.
  375. $diff_array = range( -1, $orig_pos - $final_pos );
  376. array_splice( $orig_rows, $orig_pos, 0, $diff_array );
  377. }
  378. }
  379. // Pad the ends with blank rows if the columns aren't the same length.
  380. $diff_count = count( $orig_rows ) - count( $final_rows );
  381. if ( $diff_count < 0 ) {
  382. while ( $diff_count < 0 ) {
  383. array_push( $orig_rows, $diff_count++ );
  384. }
  385. } elseif ( $diff_count > 0 ) {
  386. $diff_count = -1 * $diff_count;
  387. while ( $diff_count < 0 ) {
  388. array_push( $final_rows, $diff_count++ );
  389. }
  390. }
  391. return array( $orig_matches, $final_matches, $orig_rows, $final_rows );
  392. }
  393. /**
  394. * Computes a number that is intended to reflect the "distance" between two strings.
  395. *
  396. * @since 2.6.0
  397. *
  398. * @param string $string1
  399. * @param string $string2
  400. * @return int
  401. */
  402. public function compute_string_distance( $string1, $string2 ) {
  403. // Use an md5 hash of the strings for a count cache, as it's fast to generate, and collisions aren't a concern.
  404. $count_key1 = md5( $string1 );
  405. $count_key2 = md5( $string2 );
  406. // Cache vectors containing character frequency for all chars in each string.
  407. if ( ! isset( $this->count_cache[ $count_key1 ] ) ) {
  408. $this->count_cache[ $count_key1 ] = count_chars( $string1 );
  409. }
  410. if ( ! isset( $this->count_cache[ $count_key2 ] ) ) {
  411. $this->count_cache[ $count_key2 ] = count_chars( $string2 );
  412. }
  413. $chars1 = $this->count_cache[ $count_key1 ];
  414. $chars2 = $this->count_cache[ $count_key2 ];
  415. $difference_key = md5( implode( ',', $chars1 ) . ':' . implode( ',', $chars2 ) );
  416. if ( ! isset( $this->difference_cache[ $difference_key ] ) ) {
  417. // L1-norm of difference vector.
  418. $this->difference_cache[ $difference_key ] = array_sum( array_map( array( $this, 'difference' ), $chars1, $chars2 ) );
  419. }
  420. $difference = $this->difference_cache[ $difference_key ];
  421. // $string1 has zero length? Odd. Give huge penalty by not dividing.
  422. if ( ! $string1 ) {
  423. return $difference;
  424. }
  425. // Return distance per character (of string1).
  426. return $difference / strlen( $string1 );
  427. }
  428. /**
  429. * @ignore
  430. * @since 2.6.0
  431. *
  432. * @param int $a
  433. * @param int $b
  434. * @return int
  435. */
  436. public function difference( $a, $b ) {
  437. return abs( $a - $b );
  438. }
  439. /**
  440. * Make private properties readable for backward compatibility.
  441. *
  442. * @since 4.0.0
  443. *
  444. * @param string $name Property to get.
  445. * @return mixed Property.
  446. */
  447. public function __get( $name ) {
  448. if ( in_array( $name, $this->compat_fields ) ) {
  449. return $this->$name;
  450. }
  451. }
  452. /**
  453. * Make private properties settable for backward compatibility.
  454. *
  455. * @since 4.0.0
  456. *
  457. * @param string $name Property to check if set.
  458. * @param mixed $value Property value.
  459. * @return mixed Newly-set property.
  460. */
  461. public function __set( $name, $value ) {
  462. if ( in_array( $name, $this->compat_fields ) ) {
  463. return $this->$name = $value;
  464. }
  465. }
  466. /**
  467. * Make private properties checkable for backward compatibility.
  468. *
  469. * @since 4.0.0
  470. *
  471. * @param string $name Property to check if set.
  472. * @return bool Whether the property is set.
  473. */
  474. public function __isset( $name ) {
  475. if ( in_array( $name, $this->compat_fields ) ) {
  476. return isset( $this->$name );
  477. }
  478. }
  479. /**
  480. * Make private properties un-settable for backward compatibility.
  481. *
  482. * @since 4.0.0
  483. *
  484. * @param string $name Property to unset.
  485. */
  486. public function __unset( $name ) {
  487. if ( in_array( $name, $this->compat_fields ) ) {
  488. unset( $this->$name );
  489. }
  490. }
  491. }