-
+ E43707995404FA59779B0DE4F34AF7CF73914E21ACABECC24A971EC3D467EDC8D395247954D1D3F1A59A23A4F5B351994A18B2162E1ADA0B4094DE91EB27F978
mp-wp/wp-includes/wp-diff.php
(0 . 0)(1 . 471)
159135 <?php
159136 /**
159137 * WordPress Diff bastard child of old MediaWiki Diff Formatter.
159138 *
159139 * Basically all that remains is the table structure and some method names.
159140 *
159141 * @package WordPress
159142 * @subpackage Diff
159143 */
159144
159145 if ( !class_exists( 'Text_Diff' ) ) {
159146 /** Text_Diff class */
159147 require( dirname(__FILE__).'/Text/Diff.php' );
159148 /** Text_Diff_Renderer class */
159149 require( dirname(__FILE__).'/Text/Diff/Renderer.php' );
159150 /** Text_Diff_Renderer_inline class */
159151 require( dirname(__FILE__).'/Text/Diff/Renderer/inline.php' );
159152 }
159153
159154 /**
159155 * Table renderer to display the diff lines.
159156 *
159157 * @since 2.6.0
159158 * @uses Text_Diff_Renderer Extends
159159 */
159160 class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
159161
159162 /**
159163 * @see Text_Diff_Renderer::_leading_context_lines
159164 * @var int
159165 * @access protected
159166 * @since 2.6.0
159167 */
159168 var $_leading_context_lines = 10000;
159169
159170 /**
159171 * @see Text_Diff_Renderer::_trailing_context_lines
159172 * @var int
159173 * @access protected
159174 * @since 2.6.0
159175 */
159176 var $_trailing_context_lines = 10000;
159177
159178 /**
159179 * {@internal Missing Description}}
159180 *
159181 * @var float
159182 * @access protected
159183 * @since 2.6.0
159184 */
159185 var $_diff_threshold = 0.6;
159186
159187 /**
159188 * Inline display helper object name.
159189 *
159190 * @var string
159191 * @access protected
159192 * @since 2.6.0
159193 */
159194 var $inline_diff_renderer = 'WP_Text_Diff_Renderer_inline';
159195
159196 /**
159197 * PHP4 Constructor - Call parent constructor with params array.
159198 *
159199 * This will set class properties based on the key value pairs in the array.
159200 *
159201 * @since unknown
159202 *
159203 * @param array $params
159204 */
159205 function Text_Diff_Renderer_Table( $params = array() ) {
159206 $parent = get_parent_class($this);
159207 $this->$parent( $params );
159208 }
159209
159210 /**
159211 * @ignore
159212 *
159213 * @param string $header
159214 * @return string
159215 */
159216 function _startBlock( $header ) {
159217 return '';
159218 }
159219
159220 /**
159221 * @ignore
159222 *
159223 * @param array $lines
159224 * @param string $prefix
159225 */
159226 function _lines( $lines, $prefix=' ' ) {
159227 }
159228
159229 /**
159230 * @ignore
159231 *
159232 * @param string $line HTML-escape the value.
159233 * @return string
159234 */
159235 function addedLine( $line ) {
159236 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
159237 }
159238
159239 /**
159240 * @ignore
159241 *
159242 * @param string $line HTML-escape the value.
159243 * @return string
159244 */
159245 function deletedLine( $line ) {
159246 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
159247 }
159248
159249 /**
159250 * @ignore
159251 *
159252 * @param string $line HTML-escape the value.
159253 * @return string
159254 */
159255 function contextLine( $line ) {
159256 return "<td> </td><td class='diff-context'>{$line}</td>";
159257 }
159258
159259 /**
159260 * @ignore
159261 *
159262 * @return string
159263 */
159264 function emptyLine() {
159265 return '<td colspan="2"> </td>';
159266 }
159267
159268 /**
159269 * @ignore
159270 * @access private
159271 *
159272 * @param array $lines
159273 * @param bool $encode
159274 * @return string
159275 */
159276 function _added( $lines, $encode = true ) {
159277 $r = '';
159278 foreach ($lines as $line) {
159279 if ( $encode )
159280 $line = htmlspecialchars( $line );
159281 $r .= '<tr>' . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n";
159282 }
159283 return $r;
159284 }
159285
159286 /**
159287 * @ignore
159288 * @access private
159289 *
159290 * @param array $lines
159291 * @param bool $encode
159292 * @return string
159293 */
159294 function _deleted( $lines, $encode = true ) {
159295 $r = '';
159296 foreach ($lines as $line) {
159297 if ( $encode )
159298 $line = htmlspecialchars( $line );
159299 $r .= '<tr>' . $this->deletedLine( $line ) . $this->emptyLine() . "</tr>\n";
159300 }
159301 return $r;
159302 }
159303
159304 /**
159305 * @ignore
159306 * @access private
159307 *
159308 * @param array $lines
159309 * @param bool $encode
159310 * @return string
159311 */
159312 function _context( $lines, $encode = true ) {
159313 $r = '';
159314 foreach ($lines as $line) {
159315 if ( $encode )
159316 $line = htmlspecialchars( $line );
159317 $r .= '<tr>' .
159318 $this->contextLine( $line ) . $this->contextLine( $line ) . "</tr>\n";
159319 }
159320 return $r;
159321 }
159322
159323 /**
159324 * Process changed lines to do word-by-word diffs for extra highlighting.
159325 *
159326 * (TRAC style) sometimes these lines can actually be deleted or added rows.
159327 * We do additional processing to figure that out
159328 *
159329 * @access private
159330 * @since 2.6.0
159331 *
159332 * @param array $orig
159333 * @param array $final
159334 * @return string
159335 */
159336 function _changed( $orig, $final ) {
159337 $r = '';
159338
159339 // Does the aforementioned additional processing
159340 // *_matches tell what rows are "the same" in orig and final. Those pairs will be diffed to get word changes
159341 // match is numeric: an index in other column
159342 // match is 'X': no match. It is a new row
159343 // *_rows are column vectors for the orig column and the final column.
159344 // row >= 0: an indix of the $orig or $final array
159345 // row < 0: a blank row for that column
159346 list($orig_matches, $final_matches, $orig_rows, $final_rows) = $this->interleave_changed_lines( $orig, $final );
159347
159348
159349 // These will hold the word changes as determined by an inline diff
159350 $orig_diffs = array();
159351 $final_diffs = array();
159352
159353 // Compute word diffs for each matched pair using the inline diff
159354 foreach ( $orig_matches as $o => $f ) {
159355 if ( is_numeric($o) && is_numeric($f) ) {
159356 $text_diff = new Text_Diff( 'auto', array( array($orig[$o]), array($final[$f]) ) );
159357 $renderer = new $this->inline_diff_renderer;
159358 $diff = $renderer->render( $text_diff );
159359
159360 // If they're too different, don't include any <ins> or <dels>
159361 if ( $diff_count = preg_match_all( '!(<ins>.*?</ins>|<del>.*?</del>)!', $diff, $diff_matches ) ) {
159362 // length of all text between <ins> or <del>
159363 $stripped_matches = strlen(strip_tags( join(' ', $diff_matches[0]) ));
159364 // since we count lengith of text between <ins> or <del> (instead of picking just one),
159365 // we double the length of chars not in those tags.
159366 $stripped_diff = strlen(strip_tags( $diff )) * 2 - $stripped_matches;
159367 $diff_ratio = $stripped_matches / $stripped_diff;
159368 if ( $diff_ratio > $this->_diff_threshold )
159369 continue; // Too different. Don't save diffs.
159370 }
159371
159372 // Un-inline the diffs by removing del or ins
159373 $orig_diffs[$o] = preg_replace( '|<ins>.*?</ins>|', '', $diff );
159374 $final_diffs[$f] = preg_replace( '|<del>.*?</del>|', '', $diff );
159375 }
159376 }
159377
159378 foreach ( array_keys($orig_rows) as $row ) {
159379 // Both columns have blanks. Ignore them.
159380 if ( $orig_rows[$row] < 0 && $final_rows[$row] < 0 )
159381 continue;
159382
159383 // If we have a word based diff, use it. Otherwise, use the normal line.
159384 $orig_line = isset($orig_diffs[$orig_rows[$row]])
159385 ? $orig_diffs[$orig_rows[$row]]
159386 : htmlspecialchars($orig[$orig_rows[$row]]);
159387 $final_line = isset($final_diffs[$final_rows[$row]])
159388 ? $final_diffs[$final_rows[$row]]
159389 : htmlspecialchars($final[$final_rows[$row]]);
159390
159391 if ( $orig_rows[$row] < 0 ) { // Orig is blank. This is really an added row.
159392 $r .= $this->_added( array($final_line), false );
159393 } elseif ( $final_rows[$row] < 0 ) { // Final is blank. This is really a deleted row.
159394 $r .= $this->_deleted( array($orig_line), false );
159395 } else { // A true changed row.
159396 $r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->addedLine( $final_line ) . "</tr>\n";
159397 }
159398 }
159399
159400 return $r;
159401 }
159402
159403 /**
159404 * Takes changed blocks and matches which rows in orig turned into which rows in final.
159405 *
159406 * Returns
159407 * *_matches ( which rows match with which )
159408 * *_rows ( order of rows in each column interleaved with blank rows as
159409 * necessary )
159410 *
159411 * @since 2.6.0
159412 *
159413 * @param unknown_type $orig
159414 * @param unknown_type $final
159415 * @return unknown
159416 */
159417 function interleave_changed_lines( $orig, $final ) {
159418
159419 // Contains all pairwise string comparisons. Keys are such that this need only be a one dimensional array.
159420 $matches = array();
159421 foreach ( array_keys($orig) as $o ) {
159422 foreach ( array_keys($final) as $f ) {
159423 $matches["$o,$f"] = $this->compute_string_distance( $orig[$o], $final[$f] );
159424 }
159425 }
159426 asort($matches); // Order by string distance.
159427
159428 $orig_matches = array();
159429 $final_matches = array();
159430
159431 foreach ( $matches as $keys => $difference ) {
159432 list($o, $f) = explode(',', $keys);
159433 $o = (int) $o;
159434 $f = (int) $f;
159435
159436 // Already have better matches for these guys
159437 if ( isset($orig_matches[$o]) && isset($final_matches[$f]) )
159438 continue;
159439
159440 // First match for these guys. Must be best match
159441 if ( !isset($orig_matches[$o]) && !isset($final_matches[$f]) ) {
159442 $orig_matches[$o] = $f;
159443 $final_matches[$f] = $o;
159444 continue;
159445 }
159446
159447 // Best match of this final is already taken? Must mean this final is a new row.
159448 if ( isset($orig_matches[$o]) )
159449 $final_matches[$f] = 'x';
159450
159451 // Best match of this orig is already taken? Must mean this orig is a deleted row.
159452 elseif ( isset($final_matches[$f]) )
159453 $orig_matches[$o] = 'x';
159454 }
159455
159456 // We read the text in this order
159457 ksort($orig_matches);
159458 ksort($final_matches);
159459
159460
159461 // Stores rows and blanks for each column.
159462 $orig_rows = $orig_rows_copy = array_keys($orig_matches);
159463 $final_rows = array_keys($final_matches);
159464
159465 // Interleaves rows with blanks to keep matches aligned.
159466 // We may end up with some extraneous blank rows, but we'll just ignore them later.
159467 foreach ( $orig_rows_copy as $orig_row ) {
159468 $final_pos = array_search($orig_matches[$orig_row], $final_rows, true);
159469 $orig_pos = (int) array_search($orig_row, $orig_rows, true);
159470
159471 if ( false === $final_pos ) { // This orig is paired with a blank final.
159472 array_splice( $final_rows, $orig_pos, 0, -1 );
159473 } elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.
159474 $diff_pos = $final_pos - $orig_pos;
159475 while ( $diff_pos < 0 )
159476 array_splice( $final_rows, $orig_pos, 0, $diff_pos++ );
159477 } elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.
159478 $diff_pos = $orig_pos - $final_pos;
159479 while ( $diff_pos < 0 )
159480 array_splice( $orig_rows, $orig_pos, 0, $diff_pos++ );
159481 }
159482 }
159483
159484
159485 // Pad the ends with blank rows if the columns aren't the same length
159486 $diff_count = count($orig_rows) - count($final_rows);
159487 if ( $diff_count < 0 ) {
159488 while ( $diff_count < 0 )
159489 array_push($orig_rows, $diff_count++);
159490 } elseif ( $diff_count > 0 ) {
159491 $diff_count = -1 * $diff_count;
159492 while ( $diff_count < 0 )
159493 array_push($final_rows, $diff_count++);
159494 }
159495
159496 return array($orig_matches, $final_matches, $orig_rows, $final_rows);
159497
159498 /*
159499 // Debug
159500 echo "\n\n\n\n\n";
159501
159502 echo "-- DEBUG Matches: Orig -> Final --";
159503
159504 foreach ( $orig_matches as $o => $f ) {
159505 echo "\n\n\n\n\n";
159506 echo "ORIG: $o, FINAL: $f\n";
159507 var_dump($orig[$o],$final[$f]);
159508 }
159509 echo "\n\n\n\n\n";
159510
159511 echo "-- DEBUG Matches: Final -> Orig --";
159512
159513 foreach ( $final_matches as $f => $o ) {
159514 echo "\n\n\n\n\n";
159515 echo "FINAL: $f, ORIG: $o\n";
159516 var_dump($final[$f],$orig[$o]);
159517 }
159518 echo "\n\n\n\n\n";
159519
159520 echo "-- DEBUG Rows: Orig -- Final --";
159521
159522 echo "\n\n\n\n\n";
159523 foreach ( $orig_rows as $row => $o ) {
159524 if ( $o < 0 )
159525 $o = 'X';
159526 $f = $final_rows[$row];
159527 if ( $f < 0 )
159528 $f = 'X';
159529 echo "$o -- $f\n";
159530 }
159531 echo "\n\n\n\n\n";
159532
159533 echo "-- END DEBUG --";
159534
159535 echo "\n\n\n\n\n";
159536
159537 return array($orig_matches, $final_matches, $orig_rows, $final_rows);
159538 */
159539 }
159540
159541 /**
159542 * Computes a number that is intended to reflect the "distance" between two strings.
159543 *
159544 * @since 2.6.0
159545 *
159546 * @param string $string1
159547 * @param string $string2
159548 * @return int
159549 */
159550 function compute_string_distance( $string1, $string2 ) {
159551 // Vectors containing character frequency for all chars in each string
159552 $chars1 = count_chars($string1);
159553 $chars2 = count_chars($string2);
159554
159555 // L1-norm of difference vector.
159556 $difference = array_sum( array_map( array(&$this, 'difference'), $chars1, $chars2 ) );
159557
159558 // $string1 has zero length? Odd. Give huge penalty by not dividing.
159559 if ( !$string1 )
159560 return $difference;
159561
159562 // Return distance per charcter (of string1)
159563 return $difference / strlen($string1);
159564 }
159565
159566 /**
159567 * @ignore
159568 * @since 2.6.0
159569 *
159570 * @param int $a
159571 * @param int $b
159572 * @return int
159573 */
159574 function difference( $a, $b ) {
159575 return abs( $a - $b );
159576 }
159577
159578 }
159579
159580 /**
159581 * Better word splitting than the PEAR package provides.
159582 *
159583 * @since 2.6.0
159584 * @uses Text_Diff_Renderer_inline Extends
159585 */
159586 class WP_Text_Diff_Renderer_inline extends Text_Diff_Renderer_inline {
159587
159588 /**
159589 * @ignore
159590 * @since 2.6.0
159591 *
159592 * @param string $string
159593 * @param string $newlineEscape
159594 * @return string
159595 */
159596 function _splitOnWords($string, $newlineEscape = "\n") {
159597 $string = str_replace("\0", '', $string);
159598 $words = preg_split( '/([^\w])/u', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
159599 $words = str_replace( "\n", $newlineEscape, $words );
159600 return $words;
159601 }
159602
159603 }
159604
159605 ?>