class-wp-text-diff-renderer-table.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. #[AllowDynamicProperties]
  16. class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
  17. /**
  18. * @see Text_Diff_Renderer::_leading_context_lines
  19. * @var int
  20. * @since 2.6.0
  21. */
  22. public $_leading_context_lines = 10000;
  23. /**
  24. * @see Text_Diff_Renderer::_trailing_context_lines
  25. * @var int
  26. * @since 2.6.0
  27. */
  28. public $_trailing_context_lines = 10000;
  29. /**
  30. * Threshold for when a diff should be saved or omitted.
  31. *
  32. * @var float
  33. * @since 2.6.0
  34. */
  35. protected $_diff_threshold = 0.6;
  36. /**
  37. * Inline display helper object name.
  38. *
  39. * @var string
  40. * @since 2.6.0
  41. */
  42. protected $inline_diff_renderer = 'WP_Text_Diff_Renderer_inline';
  43. /**
  44. * Should we show the split view or not
  45. *
  46. * @var string
  47. * @since 3.6.0
  48. */
  49. protected $_show_split_view = true;
  50. protected $compat_fields = array( '_show_split_view', 'inline_diff_renderer', '_diff_threshold' );
  51. /**
  52. * Caches the output of count_chars() in compute_string_distance()
  53. *
  54. * @var array
  55. * @since 5.0.0
  56. */
  57. protected $count_cache = array();
  58. /**
  59. * Caches the difference calculation in compute_string_distance()
  60. *
  61. * @var array
  62. * @since 5.0.0
  63. */
  64. protected $difference_cache = array();
  65. /**
  66. * Constructor - Call parent constructor with params array.
  67. *
  68. * This will set class properties based on the key value pairs in the array.
  69. *
  70. * @since 2.6.0
  71. *
  72. * @param array $params
  73. */
  74. public function __construct( $params = array() ) {
  75. parent::__construct( $params );
  76. if ( isset( $params['show_split_view'] ) ) {
  77. $this->_show_split_view = $params['show_split_view'];
  78. }
  79. }
  80. /**
  81. * @ignore
  82. *
  83. * @param string $header
  84. * @return string
  85. */
  86. public function _startBlock( $header ) {
  87. return '';
  88. }
  89. /**
  90. * @ignore
  91. *
  92. * @param array $lines
  93. * @param string $prefix
  94. */
  95. public function _lines( $lines, $prefix = ' ' ) {
  96. }
  97. /**
  98. * @ignore
  99. *
  100. * @param string $line HTML-escape the value.
  101. * @return string
  102. */
  103. public function addedLine( $line ) {
  104. return "<td class='diff-addedline'><span aria-hidden='true' class='dashicons dashicons-plus'></span><span class='screen-reader-text'>" . __( 'Added:' ) . " </span>{$line}</td>";
  105. }
  106. /**
  107. * @ignore
  108. *
  109. * @param string $line HTML-escape the value.
  110. * @return string
  111. */
  112. public function deletedLine( $line ) {
  113. return "<td class='diff-deletedline'><span aria-hidden='true' class='dashicons dashicons-minus'></span><span class='screen-reader-text'>" . __( 'Deleted:' ) . " </span>{$line}</td>";
  114. }
  115. /**
  116. * @ignore
  117. *
  118. * @param string $line HTML-escape the value.
  119. * @return string
  120. */
  121. public function contextLine( $line ) {
  122. return "<td class='diff-context'><span class='screen-reader-text'>" . __( 'Unchanged:' ) . " </span>{$line}</td>";
  123. }
  124. /**
  125. * @ignore
  126. *
  127. * @return string
  128. */
  129. public function emptyLine() {
  130. return '<td>&nbsp;</td>';
  131. }
  132. /**
  133. * @ignore
  134. *
  135. * @param array $lines
  136. * @param bool $encode
  137. * @return string
  138. */
  139. public function _added( $lines, $encode = true ) {
  140. $r = '';
  141. foreach ( $lines as $line ) {
  142. if ( $encode ) {
  143. $processed_line = htmlspecialchars( $line );
  144. /**
  145. * Contextually filters a diffed line.
  146. *
  147. * Filters TextDiff processing of diffed line. By default, diffs are processed with
  148. * htmlspecialchars. Use this filter to remove or change the processing. Passes a context
  149. * indicating if the line is added, deleted or unchanged.
  150. *
  151. * @since 4.1.0
  152. *
  153. * @param string $processed_line The processed diffed line.
  154. * @param string $line The unprocessed diffed line.
  155. * @param string $context The line context. Values are 'added', 'deleted' or 'unchanged'.
  156. */
  157. $line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'added' );
  158. }
  159. if ( $this->_show_split_view ) {
  160. $r .= '<tr>' . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n";
  161. } else {
  162. $r .= '<tr>' . $this->addedLine( $line ) . "</tr>\n";
  163. }
  164. }
  165. return $r;
  166. }
  167. /**
  168. * @ignore
  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() . "</tr>\n";
  184. } else {
  185. $r .= '<tr>' . $this->deletedLine( $line ) . "</tr>\n";
  186. }
  187. }
  188. return $r;
  189. }
  190. /**
  191. * @ignore
  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->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. * @since 2.6.0
  220. *
  221. * @param array $orig
  222. * @param array $final
  223. * @return string
  224. */
  225. public function _changed( $orig, $final ) {
  226. $r = '';
  227. /*
  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 index of the $orig or $final array.
  234. * - row < 0: a blank row for that column.
  235. */
  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 <del>'s.
  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( implode( ' ', $diff_matches[0] ) ) );
  250. // Since we count length 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. }
  258. // Un-inline the diffs by removing <del> or <ins>.
  259. $orig_diffs[ $o ] = preg_replace( '|<ins>.*?</ins>|', '', $diff );
  260. $final_diffs[ $f ] = preg_replace( '|<del>.*?</del>|', '', $diff );
  261. }
  262. }
  263. foreach ( array_keys( $orig_rows ) as $row ) {
  264. // Both columns have blanks. Ignore them.
  265. if ( $orig_rows[ $row ] < 0 && $final_rows[ $row ] < 0 ) {
  266. continue;
  267. }
  268. // If we have a word based diff, use it. Otherwise, use the normal line.
  269. if ( isset( $orig_diffs[ $orig_rows[ $row ] ] ) ) {
  270. $orig_line = $orig_diffs[ $orig_rows[ $row ] ];
  271. } elseif ( isset( $orig[ $orig_rows[ $row ] ] ) ) {
  272. $orig_line = htmlspecialchars( $orig[ $orig_rows[ $row ] ] );
  273. } else {
  274. $orig_line = '';
  275. }
  276. if ( isset( $final_diffs[ $final_rows[ $row ] ] ) ) {
  277. $final_line = $final_diffs[ $final_rows[ $row ] ];
  278. } elseif ( isset( $final[ $final_rows[ $row ] ] ) ) {
  279. $final_line = htmlspecialchars( $final[ $final_rows[ $row ] ] );
  280. } else {
  281. $final_line = '';
  282. }
  283. if ( $orig_rows[ $row ] < 0 ) { // Orig is blank. This is really an added row.
  284. $r .= $this->_added( array( $final_line ), false );
  285. } elseif ( $final_rows[ $row ] < 0 ) { // Final is blank. This is really a deleted row.
  286. $r .= $this->_deleted( array( $orig_line ), false );
  287. } else { // A true changed row.
  288. if ( $this->_show_split_view ) {
  289. $r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->addedLine( $final_line ) . "</tr>\n";
  290. } else {
  291. $r .= '<tr>' . $this->deletedLine( $orig_line ) . '</tr><tr>' . $this->addedLine( $final_line ) . "</tr>\n";
  292. }
  293. }
  294. }
  295. return $r;
  296. }
  297. /**
  298. * Takes changed blocks and matches which rows in orig turned into which rows in final.
  299. *
  300. * @since 2.6.0
  301. *
  302. * @param array $orig Lines of the original version of the text.
  303. * @param array $final Lines of the final version of the text.
  304. * @return array {
  305. * Array containing results of comparing the original text to the final text.
  306. *
  307. * @type array $orig_matches Associative array of original matches. Index == row
  308. * number of `$orig`, value == corresponding row number
  309. * of that same line in `$final` or 'x' if there is no
  310. * corresponding row (indicating it is a deleted line).
  311. * @type array $final_matches Associative array of final matches. Index == row
  312. * number of `$final`, value == corresponding row number
  313. * of that same line in `$orig` or 'x' if there is no
  314. * corresponding row (indicating it is a new line).
  315. * @type array $orig_rows Associative array of interleaved rows of `$orig` with
  316. * blanks to keep matches aligned with side-by-side diff
  317. * of `$final`. A value >= 0 corresponds to index of `$orig`.
  318. * Value < 0 indicates a blank row.
  319. * @type array $final_rows Associative array of interleaved rows of `$final` with
  320. * blanks to keep matches aligned with side-by-side diff
  321. * of `$orig`. A value >= 0 corresponds to index of `$final`.
  322. * Value < 0 indicates a blank row.
  323. * }
  324. */
  325. public function interleave_changed_lines( $orig, $final ) {
  326. // Contains all pairwise string comparisons. Keys are such that this need only be a one dimensional array.
  327. $matches = array();
  328. foreach ( array_keys( $orig ) as $o ) {
  329. foreach ( array_keys( $final ) as $f ) {
  330. $matches[ "$o,$f" ] = $this->compute_string_distance( $orig[ $o ], $final[ $f ] );
  331. }
  332. }
  333. asort( $matches ); // Order by string distance.
  334. $orig_matches = array();
  335. $final_matches = array();
  336. foreach ( $matches as $keys => $difference ) {
  337. list($o, $f) = explode( ',', $keys );
  338. $o = (int) $o;
  339. $f = (int) $f;
  340. // Already have better matches for these guys.
  341. if ( isset( $orig_matches[ $o ] ) && isset( $final_matches[ $f ] ) ) {
  342. continue;
  343. }
  344. // First match for these guys. Must be best match.
  345. if ( ! isset( $orig_matches[ $o ] ) && ! isset( $final_matches[ $f ] ) ) {
  346. $orig_matches[ $o ] = $f;
  347. $final_matches[ $f ] = $o;
  348. continue;
  349. }
  350. // Best match of this final is already taken? Must mean this final is a new row.
  351. if ( isset( $orig_matches[ $o ] ) ) {
  352. $final_matches[ $f ] = 'x';
  353. } elseif ( isset( $final_matches[ $f ] ) ) {
  354. // Best match of this orig is already taken? Must mean this orig is a deleted row.
  355. $orig_matches[ $o ] = 'x';
  356. }
  357. }
  358. // We read the text in this order.
  359. ksort( $orig_matches );
  360. ksort( $final_matches );
  361. // Stores rows and blanks for each column.
  362. $orig_rows = array_keys( $orig_matches );
  363. $orig_rows_copy = $orig_rows;
  364. $final_rows = array_keys( $final_matches );
  365. // Interleaves rows with blanks to keep matches aligned.
  366. // We may end up with some extraneous blank rows, but we'll just ignore them later.
  367. foreach ( $orig_rows_copy as $orig_row ) {
  368. $final_pos = array_search( $orig_matches[ $orig_row ], $final_rows, true );
  369. $orig_pos = (int) array_search( $orig_row, $orig_rows, true );
  370. if ( false === $final_pos ) { // This orig is paired with a blank final.
  371. array_splice( $final_rows, $orig_pos, 0, -1 );
  372. } elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.
  373. $diff_array = range( -1, $final_pos - $orig_pos );
  374. array_splice( $final_rows, $orig_pos, 0, $diff_array );
  375. } elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.
  376. $diff_array = range( -1, $orig_pos - $final_pos );
  377. array_splice( $orig_rows, $orig_pos, 0, $diff_array );
  378. }
  379. }
  380. // Pad the ends with blank rows if the columns aren't the same length.
  381. $diff_count = count( $orig_rows ) - count( $final_rows );
  382. if ( $diff_count < 0 ) {
  383. while ( $diff_count < 0 ) {
  384. array_push( $orig_rows, $diff_count++ );
  385. }
  386. } elseif ( $diff_count > 0 ) {
  387. $diff_count = -1 * $diff_count;
  388. while ( $diff_count < 0 ) {
  389. array_push( $final_rows, $diff_count++ );
  390. }
  391. }
  392. return array( $orig_matches, $final_matches, $orig_rows, $final_rows );
  393. }
  394. /**
  395. * Computes a number that is intended to reflect the "distance" between two strings.
  396. *
  397. * @since 2.6.0
  398. *
  399. * @param string $string1
  400. * @param string $string2
  401. * @return int
  402. */
  403. public function compute_string_distance( $string1, $string2 ) {
  404. // Use an md5 hash of the strings for a count cache, as it's fast to generate, and collisions aren't a concern.
  405. $count_key1 = md5( $string1 );
  406. $count_key2 = md5( $string2 );
  407. // Cache vectors containing character frequency for all chars in each string.
  408. if ( ! isset( $this->count_cache[ $count_key1 ] ) ) {
  409. $this->count_cache[ $count_key1 ] = count_chars( $string1 );
  410. }
  411. if ( ! isset( $this->count_cache[ $count_key2 ] ) ) {
  412. $this->count_cache[ $count_key2 ] = count_chars( $string2 );
  413. }
  414. $chars1 = $this->count_cache[ $count_key1 ];
  415. $chars2 = $this->count_cache[ $count_key2 ];
  416. $difference_key = md5( implode( ',', $chars1 ) . ':' . implode( ',', $chars2 ) );
  417. if ( ! isset( $this->difference_cache[ $difference_key ] ) ) {
  418. // L1-norm of difference vector.
  419. $this->difference_cache[ $difference_key ] = array_sum( array_map( array( $this, 'difference' ), $chars1, $chars2 ) );
  420. }
  421. $difference = $this->difference_cache[ $difference_key ];
  422. // $string1 has zero length? Odd. Give huge penalty by not dividing.
  423. if ( ! $string1 ) {
  424. return $difference;
  425. }
  426. // Return distance per character (of string1).
  427. return $difference / strlen( $string1 );
  428. }
  429. /**
  430. * @ignore
  431. * @since 2.6.0
  432. *
  433. * @param int $a
  434. * @param int $b
  435. * @return int
  436. */
  437. public function difference( $a, $b ) {
  438. return abs( $a - $b );
  439. }
  440. /**
  441. * Make private properties readable for backward compatibility.
  442. *
  443. * @since 4.0.0
  444. *
  445. * @param string $name Property to get.
  446. * @return mixed Property.
  447. */
  448. public function __get( $name ) {
  449. if ( in_array( $name, $this->compat_fields, true ) ) {
  450. return $this->$name;
  451. }
  452. }
  453. /**
  454. * Make private properties settable for backward compatibility.
  455. *
  456. * @since 4.0.0
  457. *
  458. * @param string $name Property to check if set.
  459. * @param mixed $value Property value.
  460. * @return mixed Newly-set property.
  461. */
  462. public function __set( $name, $value ) {
  463. if ( in_array( $name, $this->compat_fields, true ) ) {
  464. return $this->$name = $value;
  465. }
  466. }
  467. /**
  468. * Make private properties checkable for backward compatibility.
  469. *
  470. * @since 4.0.0
  471. *
  472. * @param string $name Property to check if set.
  473. * @return bool Whether the property is set.
  474. */
  475. public function __isset( $name ) {
  476. if ( in_array( $name, $this->compat_fields, true ) ) {
  477. return isset( $this->$name );
  478. }
  479. }
  480. /**
  481. * Make private properties un-settable for backward compatibility.
  482. *
  483. * @since 4.0.0
  484. *
  485. * @param string $name Property to unset.
  486. */
  487. public function __unset( $name ) {
  488. if ( in_array( $name, $this->compat_fields, true ) ) {
  489. unset( $this->$name );
  490. }
  491. }
  492. }