mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 00:12:56 +02:00
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:
parent
95190fda69
commit
5f85073525
3
cache/classes/helper.php
vendored
3
cache/classes/helper.php
vendored
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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');
|
||||
|
Loading…
x
Reference in New Issue
Block a user