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

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