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

/wp-includes/wp-diff.php

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