Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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.
  • Loading branch information...
commit 2ec2d00dc8f1de785b6476a94ddc3fd9d2c11782 1 parent 43ded67
Tim Hunt timhunt authored
3  cache/classes/helper.php
@@ -192,8 +192,7 @@ public static function early_get_cache_plugins() {
192 192 if (in_array($pluginname, $ignored)) {
193 193 continue;
194 194 }
195   - $pluginname = clean_param($pluginname, PARAM_PLUGIN);
196   - if (empty($pluginname)) {
  195 + if (!is_valid_plugin_name($pluginname)) {
197 196 // Better ignore plugins with problematic names here.
198 197 continue;
199 198 }
19 lib/moodlelib.php
@@ -907,10 +907,7 @@ function clean_param($param, $type) {
907 907 case PARAM_PLUGIN:
908 908 case PARAM_AREA:
909 909 // we do not want any guessing here, either the name is correct or not
910   - if (!preg_match('/^[a-z][a-z0-9_]*[a-z0-9]$/', $param)) {
911   - return '';
912   - }
913   - if (strpos($param, '__') !== false) {
  910 + if (!is_valid_plugin_name($param)) {
914 911 return '';
915 912 }
916 913 return $param;
@@ -8093,6 +8090,15 @@ function get_plugin_types($fullpaths=true) {
8093 8090 }
8094 8091
8095 8092 /**
  8093 + * This method validates a plug name. It is much faster than calling clean_param.
  8094 + * @param string $name a string that might be a plugin name.
  8095 + * @return bool if this string is a valid plugin name.
  8096 + */
  8097 +function is_valid_plugin_name($name) {
  8098 + return (bool) preg_match('/^[a-z](?:[a-z0-9_](?!__))*[a-z0-9]$/', $name);
  8099 +}
  8100 +
  8101 +/**
8096 8102 * Simplified version of get_list_of_plugins()
8097 8103 * @param string $plugintype type of plugin
8098 8104 * @return array name=>fulllocation pairs of plugins of given type
@@ -8156,9 +8162,8 @@ function get_plugin_list($plugintype) {
8156 8162 if (in_array($pluginname, $ignored)) {
8157 8163 continue;
8158 8164 }
8159   - $pluginname = clean_param($pluginname, PARAM_PLUGIN);
8160   - if (empty($pluginname)) {
8161   - // better ignore plugins with problematic names here
  8165 + if (!is_valid_plugin_name($pluginname)) {
  8166 + // Better ignore plugins with problematic names here.
8162 8167 continue;
8163 8168 }
8164 8169 $result[$pluginname] = $fulldir.'/'.$pluginname;
15 lib/tests/moodlelib_test.php
@@ -797,6 +797,21 @@ function test_clean_param_component() {
797 797 $this->assertSame(clean_param('user_', PARAM_COMPONENT), '');
798 798 }
799 799
  800 + function test_is_valid_plugin_name() {
  801 + $this->assertTrue(is_valid_plugin_name('forum'));
  802 + $this->assertTrue(is_valid_plugin_name('forum2'));
  803 + $this->assertTrue(is_valid_plugin_name('online_users'));
  804 + $this->assertTrue(is_valid_plugin_name('blond_online_users'));
  805 + $this->assertFalse(is_valid_plugin_name('online__users'));
  806 + $this->assertFalse(is_valid_plugin_name('forum '));
  807 + $this->assertFalse(is_valid_plugin_name('forum.old'));
  808 + $this->assertFalse(is_valid_plugin_name('xx-yy'));
  809 + $this->assertFalse(is_valid_plugin_name('2xx'));
  810 + $this->assertFalse(is_valid_plugin_name('Xx'));
  811 + $this->assertFalse(is_valid_plugin_name('_xx'));
  812 + $this->assertFalse(is_valid_plugin_name('xx_'));
  813 + }
  814 +
800 815 function test_clean_param_plugin() {
801 816 // please note the cleaning of plugin names is very strict, no guessing here
802 817 $this->assertSame(clean_param('forum', PARAM_PLUGIN), 'forum');

0 comments on commit 2ec2d00

Please sign in to comment.
Something went wrong with that request. Please try again.