]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/wp-diff.php
WordPress 3.4.1-scripts
[autoinstalls/wordpress.git] / wp-includes / wp-diff.php
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
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' );
18 }
19
20 /**
21  * Table renderer to display the diff lines.
22  *
23  * @since 2.6.0
24  * @uses Text_Diff_Renderer Extends
25  */
26 class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
27
28         /**
29          * @see Text_Diff_Renderer::_leading_context_lines
30          * @var int
31          * @access protected
32          * @since 2.6.0
33          */
34         var $_leading_context_lines  = 10000;
35
36         /**
37          * @see Text_Diff_Renderer::_trailing_context_lines
38          * @var int
39          * @access protected
40          * @since 2.6.0
41          */
42         var $_trailing_context_lines = 10000;
43
44         /**
45          * {@internal Missing Description}}
46          *
47          * @var float
48          * @access protected
49          * @since 2.6.0
50          */
51         var $_diff_threshold = 0.6;
52
53         /**
54          * Inline display helper object name.
55          *
56          * @var string
57          * @access protected
58          * @since 2.6.0
59          */
60         var $inline_diff_renderer = 'WP_Text_Diff_Renderer_inline';
61
62         /**
63          * Constructor - Call parent constructor with params array.
64          *
65          * This will set class properties based on the key value pairs in the array.
66          *
67          * @since 2.6.0
68          *
69          * @param array $params
70          */
71         function __construct( $params = array() ) {
72                 parent::__construct( $params );
73         }
74
75         /**
76          * @ignore
77          *
78          * @param string $header
79          * @return string
80          */
81         function _startBlock( $header ) {
82                 return '';
83         }
84
85         /**
86          * @ignore
87          *
88          * @param array $lines
89          * @param string $prefix
90          */
91         function _lines( $lines, $prefix=' ' ) {
92         }
93
94         /**
95          * @ignore
96          *
97          * @param string $line HTML-escape the value.
98          * @return string
99          */
100         function addedLine( $line ) {
101                 return "<td>+</td><td class='diff-addedline'>{$line}</td>";
102         }
103
104         /**
105          * @ignore
106          *
107          * @param string $line HTML-escape the value.
108          * @return string
109          */
110         function deletedLine( $line ) {
111                 return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
112         }
113
114         /**
115          * @ignore
116          *
117          * @param string $line HTML-escape the value.
118          * @return string
119          */
120         function contextLine( $line ) {
121                 return "<td> </td><td class='diff-context'>{$line}</td>";
122         }
123
124         /**
125          * @ignore
126          *
127          * @return string
128          */
129         function emptyLine() {
130                 return '<td colspan="2">&nbsp;</td>';
131         }
132
133         /**
134          * @ignore
135          * @access private
136          *
137          * @param array $lines
138          * @param bool $encode
139          * @return string
140          */
141         function _added( $lines, $encode = true ) {
142                 $r = '';
143                 foreach ($lines as $line) {
144                         if ( $encode )
145                                 $line = htmlspecialchars( $line );
146                         $r .= '<tr>' . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n";
147                 }
148                 return $r;
149         }
150
151         /**
152          * @ignore
153          * @access private
154          *
155          * @param array $lines
156          * @param bool $encode
157          * @return string
158          */
159         function _deleted( $lines, $encode = true ) {
160                 $r = '';
161                 foreach ($lines as $line) {
162                         if ( $encode )
163                                 $line = htmlspecialchars( $line );
164                         $r .= '<tr>' . $this->deletedLine( $line ) . $this->emptyLine() . "</tr>\n";
165                 }
166                 return $r;
167         }
168
169         /**
170          * @ignore
171          * @access private
172          *
173          * @param array $lines
174          * @param bool $encode
175          * @return string
176          */
177         function _context( $lines, $encode = true ) {
178                 $r = '';
179                 foreach ($lines as $line) {
180                         if ( $encode )
181                                 $line = htmlspecialchars( $line );
182                         $r .= '<tr>' .
183                                 $this->contextLine( $line ) . $this->contextLine( $line ) . "</tr>\n";
184                 }
185                 return $r;
186         }
187
188         /**
189          * Process changed lines to do word-by-word diffs for extra highlighting.
190          *
191          * (TRAC style) sometimes these lines can actually be deleted or added rows.
192          * We do additional processing to figure that out
193          *
194          * @access private
195          * @since 2.6.0
196          *
197          * @param array $orig
198          * @param array $final
199          * @return string
200          */
201         function _changed( $orig, $final ) {
202                 $r = '';
203
204                 // Does the aforementioned additional processing
205                 // *_matches tell what rows are "the same" in orig and final. Those pairs will be diffed to get word changes
206                 //      match is numeric: an index in other column
207                 //      match is 'X': no match. It is a new row
208                 // *_rows are column vectors for the orig column and the final column.
209                 //      row >= 0: an indix of the $orig or $final array
210                 //      row  < 0: a blank row for that column
211                 list($orig_matches, $final_matches, $orig_rows, $final_rows) = $this->interleave_changed_lines( $orig, $final );
212
213                 // These will hold the word changes as determined by an inline diff
214                 $orig_diffs  = array();
215                 $final_diffs = array();
216
217                 // Compute word diffs for each matched pair using the inline diff
218                 foreach ( $orig_matches as $o => $f ) {
219                         if ( is_numeric($o) && is_numeric($f) ) {
220                                 $text_diff = new Text_Diff( 'auto', array( array($orig[$o]), array($final[$f]) ) );
221                                 $renderer = new $this->inline_diff_renderer;
222                                 $diff = $renderer->render( $text_diff );
223
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
236                                 // Un-inline the diffs by removing del or ins
237                                 $orig_diffs[$o]  = preg_replace( '|<ins>.*?</ins>|', '', $diff );
238                                 $final_diffs[$f] = preg_replace( '|<del>.*?</del>|', '', $diff );
239                         }
240                 }
241
242                 foreach ( array_keys($orig_rows) as $row ) {
243                         // Both columns have blanks. Ignore them.
244                         if ( $orig_rows[$row] < 0 && $final_rows[$row] < 0 )
245                                 continue;
246
247                         // If we have a word based diff, use it. Otherwise, use the normal line.
248                         if ( isset( $orig_diffs[$orig_rows[$row]] ) )
249                                 $orig_line = $orig_diffs[$orig_rows[$row]];
250                         elseif ( isset( $orig[$orig_rows[$row]] ) )
251                                 $orig_line = htmlspecialchars($orig[$orig_rows[$row]]);
252                         else
253                                 $orig_line = '';
254
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
262                         if ( $orig_rows[$row] < 0 ) { // Orig is blank. This is really an added row.
263                                 $r .= $this->_added( array($final_line), false );
264                         } elseif ( $final_rows[$row] < 0 ) { // Final is blank. This is really a deleted row.
265                                 $r .= $this->_deleted( array($orig_line), false );
266                         } else { // A true changed row.
267                                 $r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->addedLine( $final_line ) . "</tr>\n";
268                         }
269                 }
270
271                 return $r;
272         }
273
274         /**
275          * Takes changed blocks and matches which rows in orig turned into which rows in final.
276          *
277          * Returns
278          *      *_matches ( which rows match with which )
279          *      *_rows ( order of rows in each column interleaved with blank rows as
280          *              necessary )
281          *
282          * @since 2.6.0
283          *
284          * @param unknown_type $orig
285          * @param unknown_type $final
286          * @return unknown
287          */
288         function interleave_changed_lines( $orig, $final ) {
289
290                 // Contains all pairwise string comparisons. Keys are such that this need only be a one dimensional array.
291                 $matches = array();
292                 foreach ( array_keys($orig) as $o ) {
293                         foreach ( array_keys($final) as $f ) {
294                                 $matches["$o,$f"] = $this->compute_string_distance( $orig[$o], $final[$f] );
295                         }
296                 }
297                 asort($matches); // Order by string distance.
298
299                 $orig_matches  = array();
300                 $final_matches = array();
301
302                 foreach ( $matches as $keys => $difference ) {
303                         list($o, $f) = explode(',', $keys);
304                         $o = (int) $o;
305                         $f = (int) $f;
306
307                         // Already have better matches for these guys
308                         if ( isset($orig_matches[$o]) && isset($final_matches[$f]) )
309                                 continue;
310
311                         // First match for these guys. Must be best match
312                         if ( !isset($orig_matches[$o]) && !isset($final_matches[$f]) ) {
313                                 $orig_matches[$o] = $f;
314                                 $final_matches[$f] = $o;
315                                 continue;
316                         }
317
318                         // Best match of this final is already taken?  Must mean this final is a new row.
319                         if ( isset($orig_matches[$o]) )
320                                 $final_matches[$f] = 'x';
321
322                         // Best match of this orig is already taken?  Must mean this orig is a deleted row.
323                         elseif ( isset($final_matches[$f]) )
324                                 $orig_matches[$o] = 'x';
325                 }
326
327                 // We read the text in this order
328                 ksort($orig_matches);
329                 ksort($final_matches);
330
331                 // Stores rows and blanks for each column.
332                 $orig_rows = $orig_rows_copy = array_keys($orig_matches);
333                 $final_rows = array_keys($final_matches);
334
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
341                         if ( false === $final_pos ) { // This orig is paired with a blank final.
342                                 array_splice( $final_rows, $orig_pos, 0, -1 );
343                         } elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.
344                                 $diff_pos = $final_pos - $orig_pos;
345                                 while ( $diff_pos < 0 )
346                                         array_splice( $final_rows, $orig_pos, 0, $diff_pos++ );
347                         } elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.
348                                 $diff_pos = $orig_pos - $final_pos;
349                                 while ( $diff_pos < 0 )
350                                         array_splice( $orig_rows, $orig_pos, 0, $diff_pos++ );
351                         }
352                 }
353
354                 // Pad the ends with blank rows if the columns aren't the same length
355                 $diff_count = count($orig_rows) - count($final_rows);
356                 if ( $diff_count < 0 ) {
357                         while ( $diff_count < 0 )
358                                 array_push($orig_rows, $diff_count++);
359                 } elseif ( $diff_count > 0 ) {
360                         $diff_count = -1 * $diff_count;
361                         while ( $diff_count < 0 )
362                                 array_push($final_rows, $diff_count++);
363                 }
364
365                 return array($orig_matches, $final_matches, $orig_rows, $final_rows);
366
367 /*
368                 // Debug
369                 echo "\n\n\n\n\n";
370
371                 echo "-- DEBUG Matches: Orig -> Final --";
372
373                 foreach ( $orig_matches as $o => $f ) {
374                         echo "\n\n\n\n\n";
375                         echo "ORIG: $o, FINAL: $f\n";
376                         var_dump($orig[$o],$final[$f]);
377                 }
378                 echo "\n\n\n\n\n";
379
380                 echo "-- DEBUG Matches: Final -> Orig --";
381
382                 foreach ( $final_matches as $f => $o ) {
383                         echo "\n\n\n\n\n";
384                         echo "FINAL: $f, ORIG: $o\n";
385                         var_dump($final[$f],$orig[$o]);
386                 }
387                 echo "\n\n\n\n\n";
388
389                 echo "-- DEBUG Rows: Orig -- Final --";
390
391                 echo "\n\n\n\n\n";
392                 foreach ( $orig_rows as $row => $o ) {
393                         if ( $o < 0 )
394                                 $o = 'X';
395                         $f = $final_rows[$row];
396                         if ( $f < 0 )
397                                 $f = 'X';
398                         echo "$o -- $f\n";
399                 }
400                 echo "\n\n\n\n\n";
401
402                 echo "-- END DEBUG --";
403
404                 echo "\n\n\n\n\n";
405
406                 return array($orig_matches, $final_matches, $orig_rows, $final_rows);
407 */
408         }
409
410         /**
411          * Computes a number that is intended to reflect the "distance" between two strings.
412          *
413          * @since 2.6.0
414          *
415          * @param string $string1
416          * @param string $string2
417          * @return int
418          */
419         function compute_string_distance( $string1, $string2 ) {
420                 // Vectors containing character frequency for all chars in each string
421                 $chars1 = count_chars($string1);
422                 $chars2 = count_chars($string2);
423
424                 // L1-norm of difference vector.
425                 $difference = array_sum( array_map( array(&$this, 'difference'), $chars1, $chars2 ) );
426
427                 // $string1 has zero length? Odd. Give huge penalty by not dividing.
428                 if ( !$string1 )
429                         return $difference;
430
431                 // Return distance per charcter (of string1)
432                 return $difference / strlen($string1);
433         }
434
435         /**
436          * @ignore
437          * @since 2.6.0
438          *
439          * @param int $a
440          * @param int $b
441          * @return int
442          */
443         function difference( $a, $b ) {
444                 return abs( $a - $b );
445         }
446
447 }
448
449 /**
450  * Better word splitting than the PEAR package provides.
451  *
452  * @since 2.6.0
453  * @uses Text_Diff_Renderer_inline Extends
454  */
455 class WP_Text_Diff_Renderer_inline extends Text_Diff_Renderer_inline {
456
457         /**
458          * @ignore
459          * @since 2.6.0
460          *
461          * @param string $string
462          * @param string $newlineEscape
463          * @return string
464          */
465         function _splitOnWords($string, $newlineEscape = "\n") {
466                 $string = str_replace("\0", '', $string);
467                 $words  = preg_split( '/([^\w])/u', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
468                 $words  = str_replace( "\n", $newlineEscape, $words );
469                 return $words;
470         }
471
472 }