3 * WordPress Diff bastard child of old MediaWiki Diff Formatter.
5 * Basically all that remains is the table structure and some method names.
11 if ( !class_exists( 'Text_Diff' ) ) {
12 /** Text_Diff class */
13 require( dirname(__FILE__).'/Text/Diff.php' );
14 /** Text_Diff_Renderer class */
15 require( dirname(__FILE__).'/Text/Diff/Renderer.php' );
16 /** Text_Diff_Renderer_inline class */
17 require( dirname(__FILE__).'/Text/Diff/Renderer/inline.php' );
21 * Table renderer to display the diff lines.
24 * @uses Text_Diff_Renderer Extends
26 class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
29 * @see Text_Diff_Renderer::_leading_context_lines
34 var $_leading_context_lines = 10000;
37 * @see Text_Diff_Renderer::_trailing_context_lines
42 var $_trailing_context_lines = 10000;
45 * {@internal Missing Description}}
51 var $_diff_threshold = 0.6;
54 * Inline display helper object name.
60 var $inline_diff_renderer = 'WP_Text_Diff_Renderer_inline';
63 * PHP4 Constructor - Call parent constructor with params array.
65 * This will set class properties based on the key value pairs in the array.
69 * @param array $params
71 function Text_Diff_Renderer_Table( $params = array() ) {
72 $parent = get_parent_class($this);
73 $this->$parent( $params );
79 * @param string $header
82 function _startBlock( $header ) {
90 * @param string $prefix
92 function _lines( $lines, $prefix=' ' ) {
98 * @param string $line HTML-escape the value.
101 function addedLine( $line ) {
102 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
108 * @param string $line HTML-escape the value.
111 function deletedLine( $line ) {
112 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
118 * @param string $line HTML-escape the value.
121 function contextLine( $line ) {
122 return "<td> </td><td class='diff-context'>{$line}</td>";
130 function emptyLine() {
131 return '<td colspan="2"> </td>';
138 * @param array $lines
139 * @param bool $encode
142 function _added( $lines, $encode = true ) {
144 foreach ($lines as $line) {
146 $line = htmlspecialchars( $line );
147 $r .= '<tr>' . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n";
156 * @param array $lines
157 * @param bool $encode
160 function _deleted( $lines, $encode = true ) {
162 foreach ($lines as $line) {
164 $line = htmlspecialchars( $line );
165 $r .= '<tr>' . $this->deletedLine( $line ) . $this->emptyLine() . "</tr>\n";
174 * @param array $lines
175 * @param bool $encode
178 function _context( $lines, $encode = true ) {
180 foreach ($lines as $line) {
182 $line = htmlspecialchars( $line );
184 $this->contextLine( $line ) . $this->contextLine( $line ) . "</tr>\n";
190 * Process changed lines to do word-by-word diffs for extra highlighting.
192 * (TRAC style) sometimes these lines can actually be deleted or added rows.
193 * We do additional processing to figure that out
199 * @param array $final
202 function _changed( $orig, $final ) {
205 // Does the aforementioned additional processing
206 // *_matches tell what rows are "the same" in orig and final. Those pairs will be diffed to get word changes
207 // match is numeric: an index in other column
208 // match is 'X': no match. It is a new row
209 // *_rows are column vectors for the orig column and the final column.
210 // row >= 0: an indix of the $orig or $final array
211 // row < 0: a blank row for that column
212 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();
219 // Compute word diffs for each matched pair using the inline diff
220 foreach ( $orig_matches as $o => $f ) {
221 if ( is_numeric($o) && is_numeric($f) ) {
222 $text_diff = new Text_Diff( 'auto', array( array($orig[$o]), array($final[$f]) ) );
223 $renderer = new $this->inline_diff_renderer;
224 $diff = $renderer->render( $text_diff );
226 // If they're too different, don't include any <ins> or <dels>
227 if ( $diff_count = preg_match_all( '!(<ins>.*?</ins>|<del>.*?</del>)!', $diff, $diff_matches ) ) {
228 // length of all text between <ins> or <del>
229 $stripped_matches = strlen(strip_tags( join(' ', $diff_matches[0]) ));
230 // since we count lengith of text between <ins> or <del> (instead of picking just one),
231 // we double the length of chars not in those tags.
232 $stripped_diff = strlen(strip_tags( $diff )) * 2 - $stripped_matches;
233 $diff_ratio = $stripped_matches / $stripped_diff;
234 if ( $diff_ratio > $this->_diff_threshold )
235 continue; // Too different. Don't save diffs.
238 // Un-inline the diffs by removing del or ins
239 $orig_diffs[$o] = preg_replace( '|<ins>.*?</ins>|', '', $diff );
240 $final_diffs[$f] = preg_replace( '|<del>.*?</del>|', '', $diff );
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 )
249 // If we have a word based diff, use it. Otherwise, use the normal line.
250 if ( isset( $orig_diffs[$orig_rows[$row]] ) )
251 $orig_line = $orig_diffs[$orig_rows[$row]];
252 elseif ( isset( $orig[$orig_rows[$row]] ) )
253 $orig_line = htmlspecialchars($orig[$orig_rows[$row]]);
257 if ( isset( $final_diffs[$final_rows[$row]] ) )
258 $final_line = $final_diffs[$final_rows[$row]];
259 elseif ( isset( $final[$final_rows[$row]] ) )
260 $final_line = htmlspecialchars($final[$final_rows[$row]]);
264 if ( $orig_rows[$row] < 0 ) { // Orig is blank. This is really an added row.
265 $r .= $this->_added( array($final_line), false );
266 } elseif ( $final_rows[$row] < 0 ) { // Final is blank. This is really a deleted row.
267 $r .= $this->_deleted( array($orig_line), false );
268 } else { // A true changed row.
269 $r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->addedLine( $final_line ) . "</tr>\n";
277 * Takes changed blocks and matches which rows in orig turned into which rows in final.
280 * *_matches ( which rows match with which )
281 * *_rows ( order of rows in each column interleaved with blank rows as
286 * @param unknown_type $orig
287 * @param unknown_type $final
290 function interleave_changed_lines( $orig, $final ) {
292 // Contains all pairwise string comparisons. Keys are such that this need only be a one dimensional array.
294 foreach ( array_keys($orig) as $o ) {
295 foreach ( array_keys($final) as $f ) {
296 $matches["$o,$f"] = $this->compute_string_distance( $orig[$o], $final[$f] );
299 asort($matches); // Order by string distance.
301 $orig_matches = array();
302 $final_matches = array();
304 foreach ( $matches as $keys => $difference ) {
305 list($o, $f) = explode(',', $keys);
309 // Already have better matches for these guys
310 if ( isset($orig_matches[$o]) && isset($final_matches[$f]) )
313 // First match for these guys. Must be best match
314 if ( !isset($orig_matches[$o]) && !isset($final_matches[$f]) ) {
315 $orig_matches[$o] = $f;
316 $final_matches[$f] = $o;
320 // Best match of this final is already taken? Must mean this final is a new row.
321 if ( isset($orig_matches[$o]) )
322 $final_matches[$f] = 'x';
324 // Best match of this orig is already taken? Must mean this orig is a deleted row.
325 elseif ( isset($final_matches[$f]) )
326 $orig_matches[$o] = 'x';
329 // We read the text in this order
330 ksort($orig_matches);
331 ksort($final_matches);
334 // Stores rows and blanks for each column.
335 $orig_rows = $orig_rows_copy = array_keys($orig_matches);
336 $final_rows = array_keys($final_matches);
338 // Interleaves rows with blanks to keep matches aligned.
339 // We may end up with some extraneous blank rows, but we'll just ignore them later.
340 foreach ( $orig_rows_copy as $orig_row ) {
341 $final_pos = array_search($orig_matches[$orig_row], $final_rows, true);
342 $orig_pos = (int) array_search($orig_row, $orig_rows, true);
344 if ( false === $final_pos ) { // This orig is paired with a blank final.
345 array_splice( $final_rows, $orig_pos, 0, -1 );
346 } elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.
347 $diff_pos = $final_pos - $orig_pos;
348 while ( $diff_pos < 0 )
349 array_splice( $final_rows, $orig_pos, 0, $diff_pos++ );
350 } elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.
351 $diff_pos = $orig_pos - $final_pos;
352 while ( $diff_pos < 0 )
353 array_splice( $orig_rows, $orig_pos, 0, $diff_pos++ );
358 // Pad the ends with blank rows if the columns aren't the same length
359 $diff_count = count($orig_rows) - count($final_rows);
360 if ( $diff_count < 0 ) {
361 while ( $diff_count < 0 )
362 array_push($orig_rows, $diff_count++);
363 } elseif ( $diff_count > 0 ) {
364 $diff_count = -1 * $diff_count;
365 while ( $diff_count < 0 )
366 array_push($final_rows, $diff_count++);
369 return array($orig_matches, $final_matches, $orig_rows, $final_rows);
375 echo "-- DEBUG Matches: Orig -> Final --";
377 foreach ( $orig_matches as $o => $f ) {
379 echo "ORIG: $o, FINAL: $f\n";
380 var_dump($orig[$o],$final[$f]);
384 echo "-- DEBUG Matches: Final -> Orig --";
386 foreach ( $final_matches as $f => $o ) {
388 echo "FINAL: $f, ORIG: $o\n";
389 var_dump($final[$f],$orig[$o]);
393 echo "-- DEBUG Rows: Orig -- Final --";
396 foreach ( $orig_rows as $row => $o ) {
399 $f = $final_rows[$row];
406 echo "-- END DEBUG --";
410 return array($orig_matches, $final_matches, $orig_rows, $final_rows);
415 * Computes a number that is intended to reflect the "distance" between two strings.
419 * @param string $string1
420 * @param string $string2
423 function compute_string_distance( $string1, $string2 ) {
424 // Vectors containing character frequency for all chars in each string
425 $chars1 = count_chars($string1);
426 $chars2 = count_chars($string2);
428 // L1-norm of difference vector.
429 $difference = array_sum( array_map( array(&$this, 'difference'), $chars1, $chars2 ) );
431 // $string1 has zero length? Odd. Give huge penalty by not dividing.
435 // Return distance per charcter (of string1)
436 return $difference / strlen($string1);
447 function difference( $a, $b ) {
448 return abs( $a - $b );
454 * Better word splitting than the PEAR package provides.
457 * @uses Text_Diff_Renderer_inline Extends
459 class WP_Text_Diff_Renderer_inline extends Text_Diff_Renderer_inline {
465 * @param string $string
466 * @param string $newlineEscape
469 function _splitOnWords($string, $newlineEscape = "\n") {
470 $string = str_replace("\0", '', $string);
471 $words = preg_split( '/([^\w])/u', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
472 $words = str_replace( "\n", $newlineEscape, $words );