mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 21:00:21 +00:00
Shortcodes/Formatting: Add PCRE Performance Testing
* Move pattern from `wptexturize()` into a separate function. * Move pattern from `wp_html_split()` into a separate function. * Beautify code for `wp_html_split()`. * Remove unnecessary instances of `/s` modifier in patterns that don't use dots. * Add `tests/phpunit/data/formatting/whole-posts.php` for testing larger strings. * Add function `benchmark_pcre_backtracking()`. * Add tests for `wp_html_split()`. * Add tests for `wptexturize()`. * Add tests for `get_shortcode_regex()`. Props miqrogroove. Fixes #34121. git-svn-id: https://develop.svn.wordpress.org/trunk@34761 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -390,3 +390,59 @@ class wpdb_exposed_methods_for_testing extends wpdb {
|
||||
return call_user_func_array( array( $this, $name ), $arguments );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine approximate backtrack count when running PCRE.
|
||||
*
|
||||
* @return int The backtrack count.
|
||||
*/
|
||||
function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) {
|
||||
$saved_config = ini_get( 'pcre.backtrack_limit' );
|
||||
|
||||
// Attempt to prevent PHP crashes. Adjust these lower when needed.
|
||||
if ( version_compare( phpversion(), '5.4.8', '>' ) ) {
|
||||
$limit = 1000000;
|
||||
} else {
|
||||
$limit = 20000; // 20,000 is a reasonable upper limit, but see also https://core.trac.wordpress.org/ticket/29557#comment:10
|
||||
}
|
||||
|
||||
// Start with small numbers, so if a crash is encountered at higher numbers we can still debug the problem.
|
||||
for( $i = 4; $i <= $limit; $i *= 2 ) {
|
||||
|
||||
ini_set( 'pcre.backtrack_limit', $i );
|
||||
|
||||
switch( $strategy ) {
|
||||
case 'split':
|
||||
preg_split( $pattern, $subject );
|
||||
break;
|
||||
case 'match':
|
||||
preg_match( $pattern, $subject );
|
||||
break;
|
||||
case 'match_all':
|
||||
preg_match_all( $pattern, $subject );
|
||||
break;
|
||||
}
|
||||
|
||||
ini_set( 'pcre.backtrack_limit', $saved_config );
|
||||
|
||||
switch( preg_last_error() ) {
|
||||
case PREG_NO_ERROR:
|
||||
return $i;
|
||||
case PREG_BACKTRACK_LIMIT_ERROR:
|
||||
continue;
|
||||
case PREG_RECURSION_LIMIT_ERROR:
|
||||
trigger_error('PCRE recursion limit encountered before backtrack limit.');
|
||||
break;
|
||||
case PREG_BAD_UTF8_ERROR:
|
||||
trigger_error('UTF-8 error during PCRE benchmark.');
|
||||
break;
|
||||
case PREG_INTERNAL_ERROR:
|
||||
trigger_error('Internal error during PCRE benchmark.');
|
||||
break;
|
||||
default:
|
||||
trigger_error('Unexpected error during PCRE benchmark.');
|
||||
}
|
||||
}
|
||||
|
||||
return $i;
|
||||
}
|
||||
Reference in New Issue
Block a user