Merge branch 'MDL-62945-master' of https://github.com/HuongNV13/moodle
[moodle.git] / lib / classes / component.php
blobf7debb8ed816cdc3713e76c0e9638638b0271ec1
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Components (core subsystems + plugins) related code.
20 * @package core
21 * @copyright 2013 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 // Constants used in version.php files, these must exist when core_component executes.
29 /** Software maturity level - internals can be tested using white box techniques. */
30 define('MATURITY_ALPHA', 50);
31 /** Software maturity level - feature complete, ready for preview and testing. */
32 define('MATURITY_BETA', 100);
33 /** Software maturity level - tested, will be released unless there are fatal bugs. */
34 define('MATURITY_RC', 150);
35 /** Software maturity level - ready for production deployment. */
36 define('MATURITY_STABLE', 200);
37 /** Any version - special value that can be used in $plugin->dependencies in version.php files. */
38 define('ANY_VERSION', 'any');
41 /**
42 * Collection of components related methods.
44 class core_component {
45 /** @var array list of ignored directories - watch out for auth/db exception */
46 protected static $ignoreddirs = array('CVS'=>true, '_vti_cnf'=>true, 'simpletest'=>true, 'db'=>true, 'yui'=>true, 'tests'=>true, 'classes'=>true, 'fonts'=>true);
47 /** @var array list plugin types that support subplugins, do not add more here unless absolutely necessary */
48 protected static $supportsubplugins = array('mod', 'editor', 'tool', 'local');
50 /** @var array cache of plugin types */
51 protected static $plugintypes = null;
52 /** @var array cache of plugin locations */
53 protected static $plugins = null;
54 /** @var array cache of core subsystems */
55 protected static $subsystems = null;
56 /** @var array subplugin type parents */
57 protected static $parents = null;
58 /** @var array subplugins */
59 protected static $subplugins = null;
60 /** @var array list of all known classes that can be autoloaded */
61 protected static $classmap = null;
62 /** @var array list of all classes that have been renamed to be autoloaded */
63 protected static $classmaprenames = null;
64 /** @var array list of some known files that can be included. */
65 protected static $filemap = null;
66 /** @var int|float core version. */
67 protected static $version = null;
68 /** @var array list of the files to map. */
69 protected static $filestomap = array('lib.php', 'settings.php');
70 /** @var array associative array of PSR-0 namespaces and corresponding paths. */
71 protected static $psr0namespaces = array(
72 'Horde' => 'lib/horde/framework/Horde',
73 'Mustache' => 'lib/mustache/src/Mustache',
75 /** @var array associative array of PRS-4 namespaces and corresponding paths. */
76 protected static $psr4namespaces = array(
77 'MaxMind' => 'lib/maxmind/MaxMind',
78 'GeoIp2' => 'lib/maxmind/GeoIp2',
79 'Sabberworm\\CSS' => 'lib/php-css-parser',
80 'MoodleHQ\\RTLCSS' => 'lib/rtlcss',
81 'Leafo\\ScssPhp' => 'lib/scssphp',
82 'Box\\Spout' => 'lib/spout/src/Spout',
83 'MatthiasMullie\\Minify' => 'lib/minify/matthiasmullie-minify/src/',
84 'MatthiasMullie\\PathConverter' => 'lib/minify/matthiasmullie-pathconverter/src/',
85 'IMSGlobal\LTI' => 'lib/ltiprovider/src',
86 'Phpml' => 'lib/mlbackend/php/phpml/src/Phpml',
87 'PHPMailer\\PHPMailer' => 'lib/phpmailer/src',
88 'RedeyeVentures\\GeoPattern' => 'lib/geopattern-php/GeoPattern',
91 /**
92 * Class loader for Frankenstyle named classes in standard locations.
93 * Frankenstyle namespaces are supported.
95 * The expected location for core classes is:
96 * 1/ core_xx_yy_zz ---> lib/classes/xx_yy_zz.php
97 * 2/ \core\xx_yy_zz ---> lib/classes/xx_yy_zz.php
98 * 3/ \core\xx\yy_zz ---> lib/classes/xx/yy_zz.php
100 * The expected location for plugin classes is:
101 * 1/ mod_name_xx_yy_zz ---> mod/name/classes/xx_yy_zz.php
102 * 2/ \mod_name\xx_yy_zz ---> mod/name/classes/xx_yy_zz.php
103 * 3/ \mod_name\xx\yy_zz ---> mod/name/classes/xx/yy_zz.php
105 * @param string $classname
107 public static function classloader($classname) {
108 self::init();
110 if (isset(self::$classmap[$classname])) {
111 // Global $CFG is expected in included scripts.
112 global $CFG;
113 // Function include would be faster, but for BC it is better to include only once.
114 include_once(self::$classmap[$classname]);
115 return;
117 if (isset(self::$classmaprenames[$classname]) && isset(self::$classmap[self::$classmaprenames[$classname]])) {
118 $newclassname = self::$classmaprenames[$classname];
119 $debugging = "Class '%s' has been renamed for the autoloader and is now deprecated. Please use '%s' instead.";
120 debugging(sprintf($debugging, $classname, $newclassname), DEBUG_DEVELOPER);
121 if (PHP_VERSION_ID >= 70000 && preg_match('#\\\null(\\\|$)#', $classname)) {
122 throw new \coding_exception("Cannot alias $classname to $newclassname");
124 class_alias($newclassname, $classname);
125 return;
128 $file = self::psr_classloader($classname);
129 // If the file is found, require it.
130 if (!empty($file)) {
131 require($file);
132 return;
137 * Return the path to a class from our defined PSR-0 or PSR-4 standard namespaces on
138 * demand. Only returns paths to files that exist.
140 * Adapated from http://www.php-fig.org/psr/psr-4/examples/ and made PSR-0
141 * compatible.
143 * @param string $class the name of the class.
144 * @return string|bool The full path to the file defining the class. Or false if it could not be resolved or does not exist.
146 protected static function psr_classloader($class) {
147 // Iterate through each PSR-4 namespace prefix.
148 foreach (self::$psr4namespaces as $prefix => $path) {
149 $file = self::get_class_file($class, $prefix, $path, array('\\'));
150 if (!empty($file) && file_exists($file)) {
151 return $file;
155 // Iterate through each PSR-0 namespace prefix.
156 foreach (self::$psr0namespaces as $prefix => $path) {
157 $file = self::get_class_file($class, $prefix, $path, array('\\', '_'));
158 if (!empty($file) && file_exists($file)) {
159 return $file;
163 return false;
167 * Return the path to the class based on the given namespace prefix and path it corresponds to.
169 * Will return the path even if the file does not exist. Check the file esists before requiring.
171 * @param string $class the name of the class.
172 * @param string $prefix The namespace prefix used to identify the base directory of the source files.
173 * @param string $path The relative path to the base directory of the source files.
174 * @param string[] $separators The characters that should be used for separating.
175 * @return string|bool The full path to the file defining the class. Or false if it could not be resolved.
177 protected static function get_class_file($class, $prefix, $path, $separators) {
178 global $CFG;
180 // Does the class use the namespace prefix?
181 $len = strlen($prefix);
182 if (strncmp($prefix, $class, $len) !== 0) {
183 // No, move to the next prefix.
184 return false;
186 $path = $CFG->dirroot . '/' . $path;
188 // Get the relative class name.
189 $relativeclass = substr($class, $len);
191 // Replace the namespace prefix with the base directory, replace namespace
192 // separators with directory separators in the relative class name, append
193 // with .php.
194 $file = $path . str_replace($separators, '/', $relativeclass) . '.php';
196 return $file;
201 * Initialise caches, always call before accessing self:: caches.
203 protected static function init() {
204 global $CFG;
206 // Init only once per request/CLI execution, we ignore changes done afterwards.
207 if (isset(self::$plugintypes)) {
208 return;
211 if (defined('IGNORE_COMPONENT_CACHE') and IGNORE_COMPONENT_CACHE) {
212 self::fill_all_caches();
213 return;
216 if (!empty($CFG->alternative_component_cache)) {
217 // Hack for heavily clustered sites that want to manage component cache invalidation manually.
218 $cachefile = $CFG->alternative_component_cache;
220 if (file_exists($cachefile)) {
221 if (CACHE_DISABLE_ALL) {
222 // Verify the cache state only on upgrade pages.
223 $content = self::get_cache_content();
224 if (sha1_file($cachefile) !== sha1($content)) {
225 die('Outdated component cache file defined in $CFG->alternative_component_cache, can not continue');
227 return;
229 $cache = array();
230 include($cachefile);
231 self::$plugintypes = $cache['plugintypes'];
232 self::$plugins = $cache['plugins'];
233 self::$subsystems = $cache['subsystems'];
234 self::$parents = $cache['parents'];
235 self::$subplugins = $cache['subplugins'];
236 self::$classmap = $cache['classmap'];
237 self::$classmaprenames = $cache['classmaprenames'];
238 self::$filemap = $cache['filemap'];
239 return;
242 if (!is_writable(dirname($cachefile))) {
243 die('Can not create alternative component cache file defined in $CFG->alternative_component_cache, can not continue');
246 // Lets try to create the file, it might be in some writable directory or a local cache dir.
248 } else {
249 // Note: $CFG->cachedir MUST be shared by all servers in a cluster,
250 // use $CFG->alternative_component_cache if you do not like it.
251 $cachefile = "$CFG->cachedir/core_component.php";
254 if (!CACHE_DISABLE_ALL and !self::is_developer()) {
255 // 1/ Use the cache only outside of install and upgrade.
256 // 2/ Let developers add/remove classes in developer mode.
257 if (is_readable($cachefile)) {
258 $cache = false;
259 include($cachefile);
260 if (!is_array($cache)) {
261 // Something is very wrong.
262 } else if (!isset($cache['version'])) {
263 // Something is very wrong.
264 } else if ((float) $cache['version'] !== (float) self::fetch_core_version()) {
265 // Outdated cache. We trigger an error log to track an eventual repetitive failure of float comparison.
266 error_log('Resetting core_component cache after core upgrade to version ' . self::fetch_core_version());
267 } else if ($cache['plugintypes']['mod'] !== "$CFG->dirroot/mod") {
268 // $CFG->dirroot was changed.
269 } else {
270 // The cache looks ok, let's use it.
271 self::$plugintypes = $cache['plugintypes'];
272 self::$plugins = $cache['plugins'];
273 self::$subsystems = $cache['subsystems'];
274 self::$parents = $cache['parents'];
275 self::$subplugins = $cache['subplugins'];
276 self::$classmap = $cache['classmap'];
277 self::$classmaprenames = $cache['classmaprenames'];
278 self::$filemap = $cache['filemap'];
279 return;
281 // Note: we do not verify $CFG->admin here intentionally,
282 // they must visit admin/index.php after any change.
286 if (!isset(self::$plugintypes)) {
287 // This needs to be atomic and self-fixing as much as possible.
289 $content = self::get_cache_content();
290 if (file_exists($cachefile)) {
291 if (sha1_file($cachefile) === sha1($content)) {
292 return;
294 // Stale cache detected!
295 unlink($cachefile);
298 // Permissions might not be setup properly in installers.
299 $dirpermissions = !isset($CFG->directorypermissions) ? 02777 : $CFG->directorypermissions;
300 $filepermissions = !isset($CFG->filepermissions) ? ($dirpermissions & 0666) : $CFG->filepermissions;
302 clearstatcache();
303 $cachedir = dirname($cachefile);
304 if (!is_dir($cachedir)) {
305 mkdir($cachedir, $dirpermissions, true);
308 if ($fp = @fopen($cachefile.'.tmp', 'xb')) {
309 fwrite($fp, $content);
310 fclose($fp);
311 @rename($cachefile.'.tmp', $cachefile);
312 @chmod($cachefile, $filepermissions);
314 @unlink($cachefile.'.tmp'); // Just in case anything fails (race condition).
315 self::invalidate_opcode_php_cache($cachefile);
320 * Are we in developer debug mode?
322 * Note: You need to set "$CFG->debug = (E_ALL | E_STRICT);" in config.php,
323 * the reason is we need to use this before we setup DB connection or caches for CFG.
325 * @return bool
327 protected static function is_developer() {
328 global $CFG;
330 // Note we can not rely on $CFG->debug here because DB is not initialised yet.
331 if (isset($CFG->config_php_settings['debug'])) {
332 $debug = (int)$CFG->config_php_settings['debug'];
333 } else {
334 return false;
337 if ($debug & E_ALL and $debug & E_STRICT) {
338 return true;
341 return false;
345 * Create cache file content.
347 * @private this is intended for $CFG->alternative_component_cache only.
349 * @return string
351 public static function get_cache_content() {
352 if (!isset(self::$plugintypes)) {
353 self::fill_all_caches();
356 $cache = array(
357 'subsystems' => self::$subsystems,
358 'plugintypes' => self::$plugintypes,
359 'plugins' => self::$plugins,
360 'parents' => self::$parents,
361 'subplugins' => self::$subplugins,
362 'classmap' => self::$classmap,
363 'classmaprenames' => self::$classmaprenames,
364 'filemap' => self::$filemap,
365 'version' => self::$version,
368 return '<?php
369 $cache = '.var_export($cache, true).';
374 * Fill all caches.
376 protected static function fill_all_caches() {
377 self::$subsystems = self::fetch_subsystems();
379 list(self::$plugintypes, self::$parents, self::$subplugins) = self::fetch_plugintypes();
381 self::$plugins = array();
382 foreach (self::$plugintypes as $type => $fulldir) {
383 self::$plugins[$type] = self::fetch_plugins($type, $fulldir);
386 self::fill_classmap_cache();
387 self::fill_classmap_renames_cache();
388 self::fill_filemap_cache();
389 self::fetch_core_version();
393 * Get the core version.
395 * In order for this to work properly, opcache should be reset beforehand.
397 * @return float core version.
399 protected static function fetch_core_version() {
400 global $CFG;
401 if (self::$version === null) {
402 $version = null; // Prevent IDE complaints.
403 require($CFG->dirroot . '/version.php');
404 self::$version = $version;
406 return self::$version;
410 * Returns list of core subsystems.
411 * @return array
413 protected static function fetch_subsystems() {
414 global $CFG;
416 // NOTE: Any additions here must be verified to not collide with existing add-on modules and subplugins!!!
418 $info = array(
419 'access' => null,
420 'admin' => $CFG->dirroot.'/'.$CFG->admin,
421 'analytics' => $CFG->dirroot . '/analytics',
422 'antivirus' => $CFG->dirroot . '/lib/antivirus',
423 'auth' => $CFG->dirroot.'/auth',
424 'availability' => $CFG->dirroot . '/availability',
425 'backup' => $CFG->dirroot.'/backup/util/ui',
426 'badges' => $CFG->dirroot.'/badges',
427 'block' => $CFG->dirroot.'/blocks',
428 'blog' => $CFG->dirroot.'/blog',
429 'bulkusers' => null,
430 'cache' => $CFG->dirroot.'/cache',
431 'calendar' => $CFG->dirroot.'/calendar',
432 'cohort' => $CFG->dirroot.'/cohort',
433 'comment' => $CFG->dirroot.'/comment',
434 'competency' => $CFG->dirroot.'/competency',
435 'completion' => $CFG->dirroot.'/completion',
436 'countries' => null,
437 'course' => $CFG->dirroot.'/course',
438 'currencies' => null,
439 'dbtransfer' => null,
440 'debug' => null,
441 'editor' => $CFG->dirroot.'/lib/editor',
442 'edufields' => null,
443 'enrol' => $CFG->dirroot.'/enrol',
444 'error' => null,
445 'filepicker' => null,
446 'fileconverter' => $CFG->dirroot.'/files/converter',
447 'files' => $CFG->dirroot.'/files',
448 'filters' => $CFG->dirroot.'/filter',
449 //'fonts' => null, // Bogus.
450 'form' => $CFG->dirroot.'/lib/form',
451 'grades' => $CFG->dirroot.'/grade',
452 'grading' => $CFG->dirroot.'/grade/grading',
453 'group' => $CFG->dirroot.'/group',
454 'help' => null,
455 'hub' => null,
456 'imscc' => null,
457 'install' => null,
458 'iso6392' => null,
459 'langconfig' => null,
460 'license' => null,
461 'mathslib' => null,
462 'media' => $CFG->dirroot.'/media',
463 'message' => $CFG->dirroot.'/message',
464 'mimetypes' => null,
465 'mnet' => $CFG->dirroot.'/mnet',
466 //'moodle.org' => null, // Not used any more.
467 'my' => $CFG->dirroot.'/my',
468 'notes' => $CFG->dirroot.'/notes',
469 'pagetype' => null,
470 'pix' => null,
471 'plagiarism' => $CFG->dirroot.'/plagiarism',
472 'plugin' => null,
473 'portfolio' => $CFG->dirroot.'/portfolio',
474 'privacy' => $CFG->dirroot . '/privacy',
475 'question' => $CFG->dirroot.'/question',
476 'rating' => $CFG->dirroot.'/rating',
477 'repository' => $CFG->dirroot.'/repository',
478 'rss' => $CFG->dirroot.'/rss',
479 'role' => $CFG->dirroot.'/'.$CFG->admin.'/roles',
480 'search' => $CFG->dirroot.'/search',
481 'table' => null,
482 'tag' => $CFG->dirroot.'/tag',
483 'timezones' => null,
484 'user' => $CFG->dirroot.'/user',
485 'userkey' => $CFG->dirroot.'/lib/userkey',
486 'webservice' => $CFG->dirroot.'/webservice',
489 return $info;
493 * Returns list of known plugin types.
494 * @return array
496 protected static function fetch_plugintypes() {
497 global $CFG;
499 $types = array(
500 'antivirus' => $CFG->dirroot . '/lib/antivirus',
501 'availability' => $CFG->dirroot . '/availability/condition',
502 'qtype' => $CFG->dirroot.'/question/type',
503 'mod' => $CFG->dirroot.'/mod',
504 'auth' => $CFG->dirroot.'/auth',
505 'calendartype' => $CFG->dirroot.'/calendar/type',
506 'enrol' => $CFG->dirroot.'/enrol',
507 'message' => $CFG->dirroot.'/message/output',
508 'block' => $CFG->dirroot.'/blocks',
509 'media' => $CFG->dirroot.'/media/player',
510 'filter' => $CFG->dirroot.'/filter',
511 'editor' => $CFG->dirroot.'/lib/editor',
512 'format' => $CFG->dirroot.'/course/format',
513 'dataformat' => $CFG->dirroot.'/dataformat',
514 'profilefield' => $CFG->dirroot.'/user/profile/field',
515 'report' => $CFG->dirroot.'/report',
516 'coursereport' => $CFG->dirroot.'/course/report', // Must be after system reports.
517 'gradeexport' => $CFG->dirroot.'/grade/export',
518 'gradeimport' => $CFG->dirroot.'/grade/import',
519 'gradereport' => $CFG->dirroot.'/grade/report',
520 'gradingform' => $CFG->dirroot.'/grade/grading/form',
521 'mlbackend' => $CFG->dirroot.'/lib/mlbackend',
522 'mnetservice' => $CFG->dirroot.'/mnet/service',
523 'webservice' => $CFG->dirroot.'/webservice',
524 'repository' => $CFG->dirroot.'/repository',
525 'portfolio' => $CFG->dirroot.'/portfolio',
526 'search' => $CFG->dirroot.'/search/engine',
527 'qbehaviour' => $CFG->dirroot.'/question/behaviour',
528 'qformat' => $CFG->dirroot.'/question/format',
529 'plagiarism' => $CFG->dirroot.'/plagiarism',
530 'tool' => $CFG->dirroot.'/'.$CFG->admin.'/tool',
531 'cachestore' => $CFG->dirroot.'/cache/stores',
532 'cachelock' => $CFG->dirroot.'/cache/locks',
533 'fileconverter' => $CFG->dirroot.'/files/converter',
535 $parents = array();
536 $subplugins = array();
538 if (!empty($CFG->themedir) and is_dir($CFG->themedir) ) {
539 $types['theme'] = $CFG->themedir;
540 } else {
541 $types['theme'] = $CFG->dirroot.'/theme';
544 foreach (self::$supportsubplugins as $type) {
545 if ($type === 'local') {
546 // Local subplugins must be after local plugins.
547 continue;
549 $plugins = self::fetch_plugins($type, $types[$type]);
550 foreach ($plugins as $plugin => $fulldir) {
551 $subtypes = self::fetch_subtypes($fulldir);
552 if (!$subtypes) {
553 continue;
555 $subplugins[$type.'_'.$plugin] = array();
556 foreach($subtypes as $subtype => $subdir) {
557 if (isset($types[$subtype])) {
558 error_log("Invalid subtype '$subtype', duplicate detected.");
559 continue;
561 $types[$subtype] = $subdir;
562 $parents[$subtype] = $type.'_'.$plugin;
563 $subplugins[$type.'_'.$plugin][$subtype] = array_keys(self::fetch_plugins($subtype, $subdir));
567 // Local is always last!
568 $types['local'] = $CFG->dirroot.'/local';
570 if (in_array('local', self::$supportsubplugins)) {
571 $type = 'local';
572 $plugins = self::fetch_plugins($type, $types[$type]);
573 foreach ($plugins as $plugin => $fulldir) {
574 $subtypes = self::fetch_subtypes($fulldir);
575 if (!$subtypes) {
576 continue;
578 $subplugins[$type.'_'.$plugin] = array();
579 foreach($subtypes as $subtype => $subdir) {
580 if (isset($types[$subtype])) {
581 error_log("Invalid subtype '$subtype', duplicate detected.");
582 continue;
584 $types[$subtype] = $subdir;
585 $parents[$subtype] = $type.'_'.$plugin;
586 $subplugins[$type.'_'.$plugin][$subtype] = array_keys(self::fetch_plugins($subtype, $subdir));
591 return array($types, $parents, $subplugins);
595 * Returns list of subtypes.
596 * @param string $ownerdir
597 * @return array
599 protected static function fetch_subtypes($ownerdir) {
600 global $CFG;
602 $types = array();
603 if (file_exists("$ownerdir/db/subplugins.php")) {
604 $subplugins = array();
605 include("$ownerdir/db/subplugins.php");
606 foreach ($subplugins as $subtype => $dir) {
607 if (!preg_match('/^[a-z][a-z0-9]*$/', $subtype)) {
608 error_log("Invalid subtype '$subtype'' detected in '$ownerdir', invalid characters present.");
609 continue;
611 if (isset(self::$subsystems[$subtype])) {
612 error_log("Invalid subtype '$subtype'' detected in '$ownerdir', duplicates core subsystem.");
613 continue;
615 if ($CFG->admin !== 'admin' and strpos($dir, 'admin/') === 0) {
616 $dir = preg_replace('|^admin/|', "$CFG->admin/", $dir);
618 if (!is_dir("$CFG->dirroot/$dir")) {
619 error_log("Invalid subtype directory '$dir' detected in '$ownerdir'.");
620 continue;
622 $types[$subtype] = "$CFG->dirroot/$dir";
625 return $types;
629 * Returns list of plugins of given type in given directory.
630 * @param string $plugintype
631 * @param string $fulldir
632 * @return array
634 protected static function fetch_plugins($plugintype, $fulldir) {
635 global $CFG;
637 $fulldirs = (array)$fulldir;
638 if ($plugintype === 'theme') {
639 if (realpath($fulldir) !== realpath($CFG->dirroot.'/theme')) {
640 // Include themes in standard location too.
641 array_unshift($fulldirs, $CFG->dirroot.'/theme');
645 $result = array();
647 foreach ($fulldirs as $fulldir) {
648 if (!is_dir($fulldir)) {
649 continue;
651 $items = new \DirectoryIterator($fulldir);
652 foreach ($items as $item) {
653 if ($item->isDot() or !$item->isDir()) {
654 continue;
656 $pluginname = $item->getFilename();
657 if ($plugintype === 'auth' and $pluginname === 'db') {
658 // Special exception for this wrong plugin name.
659 } else if (isset(self::$ignoreddirs[$pluginname])) {
660 continue;
662 if (!self::is_valid_plugin_name($plugintype, $pluginname)) {
663 // Always ignore plugins with problematic names here.
664 continue;
666 $result[$pluginname] = $fulldir.'/'.$pluginname;
667 unset($item);
669 unset($items);
672 ksort($result);
673 return $result;
677 * Find all classes that can be autoloaded including frankenstyle namespaces.
679 protected static function fill_classmap_cache() {
680 global $CFG;
682 self::$classmap = array();
684 self::load_classes('core', "$CFG->dirroot/lib/classes");
686 foreach (self::$subsystems as $subsystem => $fulldir) {
687 if (!$fulldir) {
688 continue;
690 self::load_classes('core_'.$subsystem, "$fulldir/classes");
693 foreach (self::$plugins as $plugintype => $plugins) {
694 foreach ($plugins as $pluginname => $fulldir) {
695 self::load_classes($plugintype.'_'.$pluginname, "$fulldir/classes");
698 ksort(self::$classmap);
702 * Fills up the cache defining what plugins have certain files.
704 * @see self::get_plugin_list_with_file
705 * @return void
707 protected static function fill_filemap_cache() {
708 global $CFG;
710 self::$filemap = array();
712 foreach (self::$filestomap as $file) {
713 if (!isset(self::$filemap[$file])) {
714 self::$filemap[$file] = array();
716 foreach (self::$plugins as $plugintype => $plugins) {
717 if (!isset(self::$filemap[$file][$plugintype])) {
718 self::$filemap[$file][$plugintype] = array();
720 foreach ($plugins as $pluginname => $fulldir) {
721 if (file_exists("$fulldir/$file")) {
722 self::$filemap[$file][$plugintype][$pluginname] = "$fulldir/$file";
730 * Find classes in directory and recurse to subdirs.
731 * @param string $component
732 * @param string $fulldir
733 * @param string $namespace
735 protected static function load_classes($component, $fulldir, $namespace = '') {
736 if (!is_dir($fulldir)) {
737 return;
740 if (!is_readable($fulldir)) {
741 // TODO: MDL-51711 We should generate some diagnostic debugging information in this case
742 // because its pretty likely to lead to a missing class error further down the line.
743 // But our early setup code can't handle errors this early at the moment.
744 return;
747 $items = new \DirectoryIterator($fulldir);
748 foreach ($items as $item) {
749 if ($item->isDot()) {
750 continue;
752 if ($item->isDir()) {
753 $dirname = $item->getFilename();
754 self::load_classes($component, "$fulldir/$dirname", $namespace.'\\'.$dirname);
755 continue;
758 $filename = $item->getFilename();
759 $classname = preg_replace('/\.php$/', '', $filename);
761 if ($filename === $classname) {
762 // Not a php file.
763 continue;
765 if ($namespace === '') {
766 // Legacy long frankenstyle class name.
767 self::$classmap[$component.'_'.$classname] = "$fulldir/$filename";
769 // New namespaced classes.
770 self::$classmap[$component.$namespace.'\\'.$classname] = "$fulldir/$filename";
772 unset($item);
773 unset($items);
778 * List all core subsystems and their location
780 * This is a whitelist of components that are part of the core and their
781 * language strings are defined in /lang/en/<<subsystem>>.php. If a given
782 * plugin is not listed here and it does not have proper plugintype prefix,
783 * then it is considered as course activity module.
785 * The location is absolute file path to dir. NULL means there is no special
786 * directory for this subsystem. If the location is set, the subsystem's
787 * renderer.php is expected to be there.
789 * @return array of (string)name => (string|null)full dir location
791 public static function get_core_subsystems() {
792 self::init();
793 return self::$subsystems;
797 * Get list of available plugin types together with their location.
799 * @return array as (string)plugintype => (string)fulldir
801 public static function get_plugin_types() {
802 self::init();
803 return self::$plugintypes;
807 * Get list of plugins of given type.
809 * @param string $plugintype
810 * @return array as (string)pluginname => (string)fulldir
812 public static function get_plugin_list($plugintype) {
813 self::init();
815 if (!isset(self::$plugins[$plugintype])) {
816 return array();
818 return self::$plugins[$plugintype];
822 * Get a list of all the plugins of a given type that define a certain class
823 * in a certain file. The plugin component names and class names are returned.
825 * @param string $plugintype the type of plugin, e.g. 'mod' or 'report'.
826 * @param string $class the part of the name of the class after the
827 * frankenstyle prefix. e.g 'thing' if you are looking for classes with
828 * names like report_courselist_thing. If you are looking for classes with
829 * the same name as the plugin name (e.g. qtype_multichoice) then pass ''.
830 * Frankenstyle namespaces are also supported.
831 * @param string $file the name of file within the plugin that defines the class.
832 * @return array with frankenstyle plugin names as keys (e.g. 'report_courselist', 'mod_forum')
833 * and the class names as values (e.g. 'report_courselist_thing', 'qtype_multichoice').
835 public static function get_plugin_list_with_class($plugintype, $class, $file = null) {
836 global $CFG; // Necessary in case it is referenced by included PHP scripts.
838 if ($class) {
839 $suffix = '_' . $class;
840 } else {
841 $suffix = '';
844 $pluginclasses = array();
845 $plugins = self::get_plugin_list($plugintype);
846 foreach ($plugins as $plugin => $fulldir) {
847 // Try class in frankenstyle namespace.
848 if ($class) {
849 $classname = '\\' . $plugintype . '_' . $plugin . '\\' . $class;
850 if (class_exists($classname, true)) {
851 $pluginclasses[$plugintype . '_' . $plugin] = $classname;
852 continue;
856 // Try autoloading of class with frankenstyle prefix.
857 $classname = $plugintype . '_' . $plugin . $suffix;
858 if (class_exists($classname, true)) {
859 $pluginclasses[$plugintype . '_' . $plugin] = $classname;
860 continue;
863 // Fall back to old file location and class name.
864 if ($file and file_exists("$fulldir/$file")) {
865 include_once("$fulldir/$file");
866 if (class_exists($classname, false)) {
867 $pluginclasses[$plugintype . '_' . $plugin] = $classname;
868 continue;
873 return $pluginclasses;
877 * Get a list of all the plugins of a given type that contain a particular file.
879 * @param string $plugintype the type of plugin, e.g. 'mod' or 'report'.
880 * @param string $file the name of file that must be present in the plugin.
881 * (e.g. 'view.php', 'db/install.xml').
882 * @param bool $include if true (default false), the file will be include_once-ed if found.
883 * @return array with plugin name as keys (e.g. 'forum', 'courselist') and the path
884 * to the file relative to dirroot as value (e.g. "$CFG->dirroot/mod/forum/view.php").
886 public static function get_plugin_list_with_file($plugintype, $file, $include = false) {
887 global $CFG; // Necessary in case it is referenced by included PHP scripts.
888 $pluginfiles = array();
890 if (isset(self::$filemap[$file])) {
891 // If the file was supposed to be mapped, then it should have been set in the array.
892 if (isset(self::$filemap[$file][$plugintype])) {
893 $pluginfiles = self::$filemap[$file][$plugintype];
895 } else {
896 // Old-style search for non-cached files.
897 $plugins = self::get_plugin_list($plugintype);
898 foreach ($plugins as $plugin => $fulldir) {
899 $path = $fulldir . '/' . $file;
900 if (file_exists($path)) {
901 $pluginfiles[$plugin] = $path;
906 if ($include) {
907 foreach ($pluginfiles as $path) {
908 include_once($path);
912 return $pluginfiles;
916 * Returns all classes in a component matching the provided namespace.
918 * It checks that the class exists.
920 * e.g. get_component_classes_in_namespace('mod_forum', 'event')
922 * @param string $component A valid moodle component (frankenstyle)
923 * @param string $namespace Namespace from the component name or empty if all $component namespace classes.
924 * @return array The full class name as key and the class path as value.
926 public static function get_component_classes_in_namespace($component, $namespace = '') {
928 $component = self::normalize_componentname($component);
930 if ($namespace) {
932 // We will add them later.
933 $namespace = trim($namespace, '\\');
935 // We need add double backslashes as it is how classes are stored into self::$classmap.
936 $namespace = implode('\\\\', explode('\\', $namespace));
937 $namespace = $namespace . '\\\\';
940 $regex = '|^' . $component . '\\\\' . $namespace . '|';
941 $it = new RegexIterator(new ArrayIterator(self::$classmap), $regex, RegexIterator::GET_MATCH, RegexIterator::USE_KEY);
943 // We want to be sure that they exist.
944 $classes = array();
945 foreach ($it as $classname => $classpath) {
946 if (class_exists($classname)) {
947 $classes[$classname] = $classpath;
951 return $classes;
955 * Returns the exact absolute path to plugin directory.
957 * @param string $plugintype type of plugin
958 * @param string $pluginname name of the plugin
959 * @return string full path to plugin directory; null if not found
961 public static function get_plugin_directory($plugintype, $pluginname) {
962 if (empty($pluginname)) {
963 // Invalid plugin name, sorry.
964 return null;
967 self::init();
969 if (!isset(self::$plugins[$plugintype][$pluginname])) {
970 return null;
972 return self::$plugins[$plugintype][$pluginname];
976 * Returns the exact absolute path to plugin directory.
978 * @param string $subsystem type of core subsystem
979 * @return string full path to subsystem directory; null if not found
981 public static function get_subsystem_directory($subsystem) {
982 self::init();
984 if (!isset(self::$subsystems[$subsystem])) {
985 return null;
987 return self::$subsystems[$subsystem];
991 * This method validates a plug name. It is much faster than calling clean_param.
993 * @param string $plugintype type of plugin
994 * @param string $pluginname a string that might be a plugin name.
995 * @return bool if this string is a valid plugin name.
997 public static function is_valid_plugin_name($plugintype, $pluginname) {
998 if ($plugintype === 'mod') {
999 // Modules must not have the same name as core subsystems.
1000 if (!isset(self::$subsystems)) {
1001 // Watch out, this is called from init!
1002 self::init();
1004 if (isset(self::$subsystems[$pluginname])) {
1005 return false;
1007 // Modules MUST NOT have any underscores,
1008 // component normalisation would break very badly otherwise!
1009 return (bool)preg_match('/^[a-z][a-z0-9]*$/', $pluginname);
1011 } else {
1012 return (bool)preg_match('/^[a-z](?:[a-z0-9_](?!__))*[a-z0-9]+$/', $pluginname);
1017 * Normalize the component name.
1019 * Note: this does not verify the validity of the plugin or component.
1021 * @param string $component
1022 * @return string
1024 public static function normalize_componentname($componentname) {
1025 list($plugintype, $pluginname) = self::normalize_component($componentname);
1026 if ($plugintype === 'core' && is_null($pluginname)) {
1027 return $plugintype;
1029 return $plugintype . '_' . $pluginname;
1033 * Normalize the component name using the "frankenstyle" rules.
1035 * Note: this does not verify the validity of plugin or type names.
1037 * @param string $component
1038 * @return array two-items list of [(string)type, (string|null)name]
1040 public static function normalize_component($component) {
1041 if ($component === 'moodle' or $component === 'core' or $component === '') {
1042 return array('core', null);
1045 if (strpos($component, '_') === false) {
1046 self::init();
1047 if (array_key_exists($component, self::$subsystems)) {
1048 $type = 'core';
1049 $plugin = $component;
1050 } else {
1051 // Everything else without underscore is a module.
1052 $type = 'mod';
1053 $plugin = $component;
1056 } else {
1057 list($type, $plugin) = explode('_', $component, 2);
1058 if ($type === 'moodle') {
1059 $type = 'core';
1061 // Any unknown type must be a subplugin.
1064 return array($type, $plugin);
1068 * Return exact absolute path to a plugin directory.
1070 * @param string $component name such as 'moodle', 'mod_forum'
1071 * @return string full path to component directory; NULL if not found
1073 public static function get_component_directory($component) {
1074 global $CFG;
1076 list($type, $plugin) = self::normalize_component($component);
1078 if ($type === 'core') {
1079 if ($plugin === null) {
1080 return $path = $CFG->libdir;
1082 return self::get_subsystem_directory($plugin);
1085 return self::get_plugin_directory($type, $plugin);
1089 * Returns list of plugin types that allow subplugins.
1090 * @return array as (string)plugintype => (string)fulldir
1092 public static function get_plugin_types_with_subplugins() {
1093 self::init();
1095 $return = array();
1096 foreach (self::$supportsubplugins as $type) {
1097 $return[$type] = self::$plugintypes[$type];
1099 return $return;
1103 * Returns parent of this subplugin type.
1105 * @param string $type
1106 * @return string parent component or null
1108 public static function get_subtype_parent($type) {
1109 self::init();
1111 if (isset(self::$parents[$type])) {
1112 return self::$parents[$type];
1115 return null;
1119 * Return all subplugins of this component.
1120 * @param string $component.
1121 * @return array $subtype=>array($component, ..), null if no subtypes defined
1123 public static function get_subplugins($component) {
1124 self::init();
1126 if (isset(self::$subplugins[$component])) {
1127 return self::$subplugins[$component];
1130 return null;
1134 * Returns hash of all versions including core and all plugins.
1136 * This is relatively slow and not fully cached, use with care!
1138 * @return string sha1 hash
1140 public static function get_all_versions_hash() {
1141 global $CFG;
1143 self::init();
1145 $versions = array();
1147 // Main version first.
1148 $versions['core'] = self::fetch_core_version();
1150 // The problem here is tha the component cache might be stable,
1151 // we want this to work also on frontpage without resetting the component cache.
1152 $usecache = false;
1153 if (CACHE_DISABLE_ALL or (defined('IGNORE_COMPONENT_CACHE') and IGNORE_COMPONENT_CACHE)) {
1154 $usecache = true;
1157 // Now all plugins.
1158 $plugintypes = core_component::get_plugin_types();
1159 foreach ($plugintypes as $type => $typedir) {
1160 if ($usecache) {
1161 $plugs = core_component::get_plugin_list($type);
1162 } else {
1163 $plugs = self::fetch_plugins($type, $typedir);
1165 foreach ($plugs as $plug => $fullplug) {
1166 $plugin = new stdClass();
1167 $plugin->version = null;
1168 $module = $plugin;
1169 include($fullplug.'/version.php');
1170 $versions[$type.'_'.$plug] = $plugin->version;
1174 return sha1(serialize($versions));
1178 * Invalidate opcode cache for given file, this is intended for
1179 * php files that are stored in dataroot.
1181 * Note: we need it here because this class must be self-contained.
1183 * @param string $file
1185 public static function invalidate_opcode_php_cache($file) {
1186 if (function_exists('opcache_invalidate')) {
1187 if (!file_exists($file)) {
1188 return;
1190 opcache_invalidate($file, true);
1195 * Return true if subsystemname is core subsystem.
1197 * @param string $subsystemname name of the subsystem.
1198 * @return bool true if core subsystem.
1200 public static function is_core_subsystem($subsystemname) {
1201 return isset(self::$subsystems[$subsystemname]);
1205 * Records all class renames that have been made to facilitate autoloading.
1207 protected static function fill_classmap_renames_cache() {
1208 global $CFG;
1210 self::$classmaprenames = array();
1212 self::load_renamed_classes("$CFG->dirroot/lib/");
1214 foreach (self::$subsystems as $subsystem => $fulldir) {
1215 self::load_renamed_classes($fulldir);
1218 foreach (self::$plugins as $plugintype => $plugins) {
1219 foreach ($plugins as $pluginname => $fulldir) {
1220 self::load_renamed_classes($fulldir);
1226 * Loads the db/renamedclasses.php file from the given directory.
1228 * The renamedclasses.php should contain a key => value array ($renamedclasses) where the key is old class name,
1229 * and the value is the new class name.
1230 * It is only included when we are populating the component cache. After that is not needed.
1232 * @param string $fulldir
1234 protected static function load_renamed_classes($fulldir) {
1235 $file = $fulldir . '/db/renamedclasses.php';
1236 if (is_readable($file)) {
1237 $renamedclasses = null;
1238 require($file);
1239 if (is_array($renamedclasses)) {
1240 foreach ($renamedclasses as $oldclass => $newclass) {
1241 self::$classmaprenames[(string)$oldclass] = (string)$newclass;
1248 * Returns a list of frankenstyle component names and their paths, for all components (plugins and subsystems).
1250 * E.g.
1252 * 'mod' => [
1253 * 'mod_forum' => FORUM_PLUGIN_PATH,
1254 * ...
1255 * ],
1256 * ...
1257 * 'core' => [
1258 * 'core_comment' => COMMENT_SUBSYSTEM_PATH,
1259 * ...
1263 * @return array an associative array of components and their corresponding paths.
1265 public static function get_component_list() : array {
1266 $components = [];
1267 // Get all plugins.
1268 foreach (self::get_plugin_types() as $plugintype => $typedir) {
1269 $components[$plugintype] = [];
1270 foreach (self::get_plugin_list($plugintype) as $pluginname => $plugindir) {
1271 $components[$plugintype][$plugintype . '_' . $pluginname] = $plugindir;
1274 // Get all subsystems.
1275 foreach (self::get_core_subsystems() as $subsystemname => $subsystempath) {
1276 $components['core']['core_' . $subsystemname] = $subsystempath;
1278 return $components;