選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

class-wp-text-diff-renderer-table.php 14 KiB

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