MDL-39445 get_plugin_list clean_param use is slow

get_plugin_list was calling clean_param($pluginname, PARAM_PLUGIN) a
lot (600+ times per page), and that is much slower than you would guess.
A specific function for this case, (which we then also use from
clean_param) is a performance win.
This commit is contained in:
Tim Hunt 2013-05-01 11:35:14 +01:00
parent 95190fda69
commit 5f85073525
3 changed files with 28 additions and 9 deletions

View File

@ -192,8 +192,7 @@ class cache_helper {
if (in_array($pluginname, $ignored)) {
continue;
}
$pluginname = clean_param($pluginname, PARAM_PLUGIN);
if (empty($pluginname)) {
if (!is_valid_plugin_name($pluginname)) {
// Better ignore plugins with problematic names here.
continue;
}

View File

@ -912,10 +912,7 @@ function clean_param($param, $type) {
case PARAM_PLUGIN:
case PARAM_AREA:
// we do not want any guessing here, either the name is correct or not
if (!preg_match('/^[a-z][a-z0-9_]*[a-z0-9]$/', $param)) {
return '';
}
if (strpos($param, '__') !== false) {
if (!is_valid_plugin_name($param)) {
return '';
}
return $param;
@ -8286,6 +8283,15 @@ function get_plugin_types($fullpaths=true) {
}
}
/**
* This method validates a plug name. It is much faster than calling clean_param.
* @param string $name a string that might be a plugin name.
* @return bool if this string is a valid plugin name.
*/
function is_valid_plugin_name($name) {
return (bool) preg_match('/^[a-z](?:[a-z0-9_](?!__))*[a-z0-9]$/', $name);
}
/**
* Simplified version of get_list_of_plugins()
* @param string $plugintype type of plugin
@ -8358,9 +8364,8 @@ function get_plugin_list($plugintype) {
if (in_array($pluginname, $ignored)) {
continue;
}
$pluginname = clean_param($pluginname, PARAM_PLUGIN);
if (empty($pluginname)) {
// better ignore plugins with problematic names here
if (!is_valid_plugin_name($pluginname)) {
// Better ignore plugins with problematic names here.
continue;
}
$result[$pluginname] = $fulldir.'/'.$pluginname;

View File

@ -797,6 +797,21 @@ class moodlelib_testcase extends advanced_testcase {
$this->assertSame(clean_param('user_', PARAM_COMPONENT), '');
}
function test_is_valid_plugin_name() {
$this->assertTrue(is_valid_plugin_name('forum'));
$this->assertTrue(is_valid_plugin_name('forum2'));
$this->assertTrue(is_valid_plugin_name('online_users'));
$this->assertTrue(is_valid_plugin_name('blond_online_users'));
$this->assertFalse(is_valid_plugin_name('online__users'));
$this->assertFalse(is_valid_plugin_name('forum '));
$this->assertFalse(is_valid_plugin_name('forum.old'));
$this->assertFalse(is_valid_plugin_name('xx-yy'));
$this->assertFalse(is_valid_plugin_name('2xx'));
$this->assertFalse(is_valid_plugin_name('Xx'));
$this->assertFalse(is_valid_plugin_name('_xx'));
$this->assertFalse(is_valid_plugin_name('xx_'));
}
function test_clean_param_plugin() {
// please note the cleaning of plugin names is very strict, no guessing here
$this->assertSame(clean_param('forum', PARAM_PLUGIN), 'forum');