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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 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. *
  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. // Does the aforementioned additional processing
  227. // *_matches tell what rows are "the same" in orig and final. Those pairs will be diffed to get word changes
  228. // match is numeric: an index in other column
  229. // match is 'X': no match. It is a new row
  230. // *_rows are column vectors for the orig column and the final column.
  231. // row >= 0: an indix of the $orig or $final array
  232. // row < 0: a blank row for that column
  233. list($orig_matches, $final_matches, $orig_rows, $final_rows) = $this->interleave_changed_lines( $orig, $final );
  234. // These will hold the word changes as determined by an inline diff
  235. $orig_diffs = array();
  236. $final_diffs = array();
  237. // Compute word diffs for each matched pair using the inline diff
  238. foreach ( $orig_matches as $o => $f ) {
  239. if ( is_numeric( $o ) && is_numeric( $f ) ) {
  240. $text_diff = new Text_Diff( 'auto', array( array( $orig[ $o ] ), array( $final[ $f ] ) ) );
  241. $renderer = new $this->inline_diff_renderer;
  242. $diff = $renderer->render( $text_diff );
  243. // If they're too different, don't include any <ins> or <dels>
  244. if ( preg_match_all( '!(<ins>.*?</ins>|<del>.*?</del>)!', $diff, $diff_matches ) ) {
  245. // length of all text between <ins> or <del>
  246. $stripped_matches = strlen( strip_tags( join( ' ', $diff_matches[0] ) ) );
  247. // since we count lengith of text between <ins> or <del> (instead of picking just one),
  248. // we double the length of chars not in those tags.
  249. $stripped_diff = strlen( strip_tags( $diff ) ) * 2 - $stripped_matches;
  250. $diff_ratio = $stripped_matches / $stripped_diff;
  251. if ( $diff_ratio > $this->_diff_threshold ) {
  252. continue; // Too different. Don't save diffs.
  253. }
  254. }
  255. // Un-inline the diffs by removing del or ins
  256. $orig_diffs[ $o ] = preg_replace( '|<ins>.*?</ins>|', '', $diff );
  257. $final_diffs[ $f ] = preg_replace( '|<del>.*?</del>|', '', $diff );
  258. }
  259. }
  260. foreach ( array_keys( $orig_rows ) as $row ) {
  261. // Both columns have blanks. Ignore them.
  262. if ( $orig_rows[ $row ] < 0 && $final_rows[ $row ] < 0 ) {
  263. continue;
  264. }
  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. }
  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. }
  280. if ( $orig_rows[ $row ] < 0 ) { // Orig is blank. This is really an added row.
  281. $r .= $this->_added( array( $final_line ), false );
  282. } elseif ( $final_rows[ $row ] < 0 ) { // Final is blank. This is really a deleted row.
  283. $r .= $this->_deleted( array( $orig_line ), false );
  284. } else { // A true changed row.
  285. if ( $this->_show_split_view ) {
  286. $r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->emptyLine() . $this->addedLine( $final_line ) . "</tr>\n";
  287. } else {
  288. $r .= '<tr>' . $this->deletedLine( $orig_line ) . '</tr><tr>' . $this->addedLine( $final_line ) . "</tr>\n";
  289. }
  290. }
  291. }
  292. return $r;
  293. }
  294. /**
  295. * Takes changed blocks and matches which rows in orig turned into which rows in final.
  296. *
  297. * @since 2.6.0
  298. *
  299. * @param array $orig Lines of the original version of the text.
  300. * @param array $final Lines of the final version of the text.
  301. * @return array {
  302. * Array containing results of comparing the original text to the final text.
  303. *
  304. * @type array $orig_matches Associative array of original matches. Index == row
  305. * number of `$orig`, value == corresponding row number
  306. * of that same line in `$final` or 'x' if there is no
  307. * corresponding row (indicating it is a deleted line).
  308. * @type array $final_matches Associative array of final matches. Index == row
  309. * number of `$final`, value == corresponding row number
  310. * of that same line in `$orig` or 'x' if there is no
  311. * corresponding row (indicating it is a new line).
  312. * @type array $orig_rows Associative array of interleaved rows of `$orig` with
  313. * blanks to keep matches aligned with side-by-side diff
  314. * of `$final`. A value >= 0 corresponds to index of `$orig`.
  315. * Value < 0 indicates a blank row.
  316. * @type array $final_rows Associative array of interleaved rows of `$final` with
  317. * blanks to keep matches aligned with side-by-side diff
  318. * of `$orig`. A value >= 0 corresponds to index of `$final`.
  319. * Value < 0 indicates a blank row.
  320. * }
  321. */
  322. public function interleave_changed_lines( $orig, $final ) {
  323. // Contains all pairwise string comparisons. Keys are such that this need only be a one dimensional array.
  324. $matches = array();
  325. foreach ( array_keys( $orig ) as $o ) {
  326. foreach ( array_keys( $final ) as $f ) {
  327. $matches[ "$o,$f" ] = $this->compute_string_distance( $orig[ $o ], $final[ $f ] );
  328. }
  329. }
  330. asort( $matches ); // Order by string distance.
  331. $orig_matches = array();
  332. $final_matches = array();
  333. foreach ( $matches as $keys => $difference ) {
  334. list($o, $f) = explode( ',', $keys );
  335. $o = (int) $o;
  336. $f = (int) $f;
  337. // Already have better matches for these guys
  338. if ( isset( $orig_matches[ $o ] ) && isset( $final_matches[ $f ] ) ) {
  339. continue;
  340. }
  341. // First match for these guys. Must be best match
  342. if ( ! isset( $orig_matches[ $o ] ) && ! isset( $final_matches[ $f ] ) ) {
  343. $orig_matches[ $o ] = $f;
  344. $final_matches[ $f ] = $o;
  345. continue;
  346. }
  347. // Best match of this final is already taken? Must mean this final is a new row.
  348. if ( isset( $orig_matches[ $o ] ) ) {
  349. $final_matches[ $f ] = 'x';
  350. } elseif ( isset( $final_matches[ $f ] ) ) {
  351. // Best match of this orig is already taken? Must mean this orig is a deleted row.
  352. $orig_matches[ $o ] = 'x';
  353. }
  354. }
  355. // We read the text in this order
  356. ksort( $orig_matches );
  357. ksort( $final_matches );
  358. // Stores rows and blanks for each column.
  359. $orig_rows = array_keys( $orig_matches );
  360. $orig_rows_copy = $orig_rows;
  361. $final_rows = array_keys( $final_matches );
  362. // Interleaves rows with blanks to keep matches aligned.
  363. // We may end up with some extraneous blank rows, but we'll just ignore them later.
  364. foreach ( $orig_rows_copy as $orig_row ) {
  365. $final_pos = array_search( $orig_matches[ $orig_row ], $final_rows, true );
  366. $orig_pos = (int) array_search( $orig_row, $orig_rows, true );
  367. if ( false === $final_pos ) { // This orig is paired with a blank final.
  368. array_splice( $final_rows, $orig_pos, 0, -1 );
  369. } elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.
  370. $diff_array = range( -1, $final_pos - $orig_pos );
  371. array_splice( $final_rows, $orig_pos, 0, $diff_array );
  372. } elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.
  373. $diff_array = range( -1, $orig_pos - $final_pos );
  374. array_splice( $orig_rows, $orig_pos, 0, $diff_array );
  375. }
  376. }
  377. // Pad the ends with blank rows if the columns aren't the same length
  378. $diff_count = count( $orig_rows ) - count( $final_rows );
  379. if ( $diff_count < 0 ) {
  380. while ( $diff_count < 0 ) {
  381. array_push( $orig_rows, $diff_count++ );
  382. }
  383. } elseif ( $diff_count > 0 ) {
  384. $diff_count = -1 * $diff_count;
  385. while ( $diff_count < 0 ) {
  386. array_push( $final_rows, $diff_count++ );
  387. }
  388. }
  389. return array( $orig_matches, $final_matches, $orig_rows, $final_rows );
  390. }
  391. /**
  392. * Computes a number that is intended to reflect the "distance" between two strings.
  393. *
  394. * @since 2.6.0
  395. *
  396. * @param string $string1
  397. * @param string $string2
  398. * @return int
  399. */
  400. public function compute_string_distance( $string1, $string2 ) {
  401. // Use an md5 hash of the strings for a count cache, as it's fast to generate, and collisions aren't a concern.
  402. $count_key1 = md5( $string1 );
  403. $count_key2 = md5( $string2 );
  404. // Cache vectors containing character frequency for all chars in each string.
  405. if ( ! isset( $this->count_cache[ $count_key1 ] ) ) {
  406. $this->count_cache[ $count_key1 ] = count_chars( $string1 );
  407. }
  408. if ( ! isset( $this->count_cache[ $count_key2 ] ) ) {
  409. $this->count_cache[ $count_key2 ] = count_chars( $string2 );
  410. }
  411. $chars1 = $this->count_cache[ $count_key1 ];
  412. $chars2 = $this->count_cache[ $count_key2 ];
  413. $difference_key = md5( implode( ',', $chars1 ) . ':' . implode( ',', $chars2 ) );
  414. if ( ! isset( $this->difference_cache[ $difference_key ] ) ) {
  415. // L1-norm of difference vector.
  416. $this->difference_cache[ $difference_key ] = array_sum( array_map( array( $this, 'difference' ), $chars1, $chars2 ) );
  417. }
  418. $difference = $this->difference_cache[ $difference_key ];
  419. // $string1 has zero length? Odd. Give huge penalty by not dividing.
  420. if ( ! $string1 ) {
  421. return $difference;
  422. }
  423. // Return distance per character (of string1).
  424. return $difference / strlen( $string1 );
  425. }
  426. /**
  427. * @ignore
  428. * @since 2.6.0
  429. *
  430. * @param int $a
  431. * @param int $b
  432. * @return int
  433. */
  434. public function difference( $a, $b ) {
  435. return abs( $a - $b );
  436. }
  437. /**
  438. * Make private properties readable for backward compatibility.
  439. *
  440. * @since 4.0.0
  441. *
  442. * @param string $name Property to get.
  443. * @return mixed Property.
  444. */
  445. public function __get( $name ) {
  446. if ( in_array( $name, $this->compat_fields ) ) {
  447. return $this->$name;
  448. }
  449. }
  450. /**
  451. * Make private properties settable for backward compatibility.
  452. *
  453. * @since 4.0.0
  454. *
  455. * @param string $name Property to check if set.
  456. * @param mixed $value Property value.
  457. * @return mixed Newly-set property.
  458. */
  459. public function __set( $name, $value ) {
  460. if ( in_array( $name, $this->compat_fields ) ) {
  461. return $this->$name = $value;
  462. }
  463. }
  464. /**
  465. * Make private properties checkable for backward compatibility.
  466. *
  467. * @since 4.0.0
  468. *
  469. * @param string $name Property to check if set.
  470. * @return bool Whether the property is set.
  471. */
  472. public function __isset( $name ) {
  473. if ( in_array( $name, $this->compat_fields ) ) {
  474. return isset( $this->$name );
  475. }
  476. }
  477. /**
  478. * Make private properties un-settable for backward compatibility.
  479. *
  480. * @since 4.0.0
  481. *
  482. * @param string $name Property to unset.
  483. */
  484. public function __unset( $name ) {
  485. if ( in_array( $name, $this->compat_fields ) ) {
  486. unset( $this->$name );
  487. }
  488. }
  489. }