PageRenderTime 24ms CodeModel.GetById 9ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/wp-diff.php

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