2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Defines classes used for plugins management
20 * This library provides a unified interface to various plugin types in
21 * Moodle. It is mainly used by the plugins management admin page and the
22 * plugins check page during the upgrade.
25 * @copyright 2011 David Mudrak <david@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
32 * Singleton class providing general plugins management functionality.
34 class core_plugin_manager {
36 /** the plugin is shipped with standard Moodle distribution */
37 const PLUGIN_SOURCE_STANDARD = 'std';
38 /** the plugin is added extension */
39 const PLUGIN_SOURCE_EXTENSION = 'ext';
41 /** the plugin uses neither database nor capabilities, no versions */
42 const PLUGIN_STATUS_NODB = 'nodb';
43 /** the plugin is up-to-date */
44 const PLUGIN_STATUS_UPTODATE = 'uptodate';
45 /** the plugin is about to be installed */
46 const PLUGIN_STATUS_NEW = 'new';
47 /** the plugin is about to be upgraded */
48 const PLUGIN_STATUS_UPGRADE = 'upgrade';
49 /** the standard plugin is about to be deleted */
50 const PLUGIN_STATUS_DELETE = 'delete';
51 /** the version at the disk is lower than the one already installed */
52 const PLUGIN_STATUS_DOWNGRADE = 'downgrade';
53 /** the plugin is installed but missing from disk */
54 const PLUGIN_STATUS_MISSING = 'missing';
56 /** @var core_plugin_manager holds the singleton instance */
57 protected static $singletoninstance;
58 /** @var array of raw plugins information */
59 protected $pluginsinfo = null;
60 /** @var array of raw subplugins information */
61 protected $subpluginsinfo = null;
62 /** @var array list of installed plugins $name=>$version */
63 protected $installedplugins = null;
64 /** @var array list of all enabled plugins $name=>$name */
65 protected $enabledplugins = null;
66 /** @var array list of all enabled plugins $name=>$diskversion */
67 protected $presentplugins = null;
68 /** @var array reordered list of plugin types */
69 protected $plugintypes = null;
72 * Direct initiation not allowed, use the factory method {@link self::instance()}
74 protected function __construct() {
78 * Sorry, this is singleton
80 protected function __clone() {
84 * Factory method for this class
86 * @return core_plugin_manager the singleton instance
88 public static function instance() {
89 if (is_null(self::$singletoninstance)) {
90 self::$singletoninstance = new self();
92 return self::$singletoninstance;
97 * @param bool $phpunitreset
99 public static function reset_caches($phpunitreset = false) {
101 self::$singletoninstance = null;
103 if (self::$singletoninstance) {
104 self::$singletoninstance->pluginsinfo = null;
105 self::$singletoninstance->subpluginsinfo = null;
106 self::$singletoninstance->installedplugins = null;
107 self::$singletoninstance->enabledplugins = null;
108 self::$singletoninstance->presentplugins = null;
109 self::$singletoninstance->plugintypes = null;
112 $cache = cache::make('core', 'plugin_manager');
117 * Returns the result of {@link core_component::get_plugin_types()} ordered for humans
119 * @see self::reorder_plugin_types()
120 * @return array (string)name => (string)location
122 public function get_plugin_types() {
123 if (func_num_args() > 0) {
124 if (!func_get_arg(0)) {
125 throw coding_exception('core_plugin_manager->get_plugin_types() does not support relative paths.');
128 if ($this->plugintypes) {
129 return $this->plugintypes;
132 $this->plugintypes = $this->reorder_plugin_types(core_component::get_plugin_types());
133 return $this->plugintypes;
137 * Load list of installed plugins,
138 * always call before using $this->installedplugins.
140 * This method is caching results for all plugins.
142 protected function load_installed_plugins() {
145 if ($this->installedplugins) {
149 if (empty($CFG->version)) {
150 // Nothing installed yet.
151 $this->installedplugins = array();
155 $cache = cache::make('core', 'plugin_manager');
156 $installed = $cache->get('installed');
158 if (is_array($installed)) {
159 $this->installedplugins = $installed;
163 $this->installedplugins = array();
165 // TODO: Delete this block once Moodle 2.6 or later becomes minimum required version to upgrade.
166 if ($CFG->version < 2013092001.02) {
167 // We did not upgrade the database yet.
168 $modules = $DB->get_records('modules', array(), 'name ASC', 'id, name, version');
169 foreach ($modules as $module) {
170 $this->installedplugins['mod'][$module->name] = $module->version;
172 $blocks = $DB->get_records('block', array(), 'name ASC', 'id, name, version');
173 foreach ($blocks as $block) {
174 $this->installedplugins['block'][$block->name] = $block->version;
178 $versions = $DB->get_records('config_plugins', array('name'=>'version'));
179 foreach ($versions as $version) {
180 $parts = explode('_', $version->plugin, 2);
181 if (!isset($parts[1])) {
182 // Invalid component, there must be at least one "_".
185 // Do not verify here if plugin type and name are valid.
186 $this->installedplugins[$parts[0]][$parts[1]] = $version->value;
189 foreach ($this->installedplugins as $key => $value) {
190 ksort($this->installedplugins[$key]);
193 $cache->set('installed', $this->installedplugins);
197 * Return list of installed plugins of given type.
198 * @param string $type
199 * @return array $name=>$version
201 public function get_installed_plugins($type) {
202 $this->load_installed_plugins();
203 if (isset($this->installedplugins[$type])) {
204 return $this->installedplugins[$type];
210 * Load list of all enabled plugins,
211 * call before using $this->enabledplugins.
213 * This method is caching results from individual plugin info classes.
215 protected function load_enabled_plugins() {
218 if ($this->enabledplugins) {
222 if (empty($CFG->version)) {
223 $this->enabledplugins = array();
227 $cache = cache::make('core', 'plugin_manager');
228 $enabled = $cache->get('enabled');
230 if (is_array($enabled)) {
231 $this->enabledplugins = $enabled;
235 $this->enabledplugins = array();
237 require_once($CFG->libdir.'/adminlib.php');
239 $plugintypes = core_component::get_plugin_types();
240 foreach ($plugintypes as $plugintype => $fulldir) {
241 $plugininfoclass = self::resolve_plugininfo_class($plugintype);
242 if (class_exists($plugininfoclass)) {
243 $enabled = $plugininfoclass::get_enabled_plugins();
244 if (!is_array($enabled)) {
247 $this->enabledplugins[$plugintype] = $enabled;
251 $cache->set('enabled', $this->enabledplugins);
255 * Get list of enabled plugins of given type,
256 * the result may contain missing plugins.
258 * @param string $type
259 * @return array|null list of enabled plugins of this type, null if unknown
261 public function get_enabled_plugins($type) {
262 $this->load_enabled_plugins();
263 if (isset($this->enabledplugins[$type])) {
264 return $this->enabledplugins[$type];
270 * Load list of all present plugins - call before using $this->presentplugins.
272 protected function load_present_plugins() {
273 if ($this->presentplugins) {
277 $cache = cache::make('core', 'plugin_manager');
278 $present = $cache->get('present');
280 if (is_array($present)) {
281 $this->presentplugins = $present;
285 $this->presentplugins = array();
287 $plugintypes = core_component::get_plugin_types();
288 foreach ($plugintypes as $type => $typedir) {
289 $plugs = core_component::get_plugin_list($type);
290 foreach ($plugs as $plug => $fullplug) {
291 $plugin = new stdClass();
292 $plugin->version = null;
294 include($fullplug.'/version.php');
295 $this->presentplugins[$type][$plug] = $plugin;
299 $cache->set('present', $this->presentplugins);
303 * Get list of present plugins of given type.
305 * @param string $type
306 * @return array|null list of presnet plugins $name=>$diskversion, null if unknown
308 public function get_present_plugins($type) {
309 $this->load_present_plugins();
310 if (isset($this->presentplugins[$type])) {
311 return $this->presentplugins[$type];
317 * Returns a tree of known plugins and information about them
319 * @return array 2D array. The first keys are plugin type names (e.g. qtype);
320 * the second keys are the plugin local name (e.g. multichoice); and
321 * the values are the corresponding objects extending {@link \core\plugininfo\base}
323 public function get_plugins() {
324 $this->init_pluginsinfo_property();
326 // Make sure all types are initialised.
327 foreach ($this->pluginsinfo as $plugintype => $list) {
328 if ($list === null) {
329 $this->get_plugins_of_type($plugintype);
333 return $this->pluginsinfo;
337 * Returns list of known plugins of the given type.
339 * This method returns the subset of the tree returned by {@link self::get_plugins()}.
340 * If the given type is not known, empty array is returned.
342 * @param string $type plugin type, e.g. 'mod' or 'workshopallocation'
343 * @return \core\plugininfo\base[] (string)plugin name (e.g. 'workshop') => corresponding subclass of {@link \core\plugininfo\base}
345 public function get_plugins_of_type($type) {
348 $this->init_pluginsinfo_property();
350 if (!array_key_exists($type, $this->pluginsinfo)) {
354 if (is_array($this->pluginsinfo[$type])) {
355 return $this->pluginsinfo[$type];
358 $types = core_component::get_plugin_types();
360 if (!isset($types[$type])) {
361 // Orphaned subplugins!
362 $plugintypeclass = self::resolve_plugininfo_class($type);
363 $this->pluginsinfo[$type] = $plugintypeclass::get_plugins($type, null, $plugintypeclass);
364 return $this->pluginsinfo[$type];
367 /** @var \core\plugininfo\base $plugintypeclass */
368 $plugintypeclass = self::resolve_plugininfo_class($type);
369 $plugins = $plugintypeclass::get_plugins($type, $types[$type], $plugintypeclass);
370 $this->pluginsinfo[$type] = $plugins;
372 if (empty($CFG->disableupdatenotifications) and !during_initial_install()) {
373 // Append the information about available updates provided by {@link \core\update\checker()}.
374 $provider = \core\update\checker::instance();
375 foreach ($plugins as $plugininfoholder) {
376 $plugininfoholder->check_available_updates($provider);
380 return $this->pluginsinfo[$type];
384 * Init placeholder array for plugin infos.
386 protected function init_pluginsinfo_property() {
387 if (is_array($this->pluginsinfo)) {
390 $this->pluginsinfo = array();
392 $plugintypes = $this->get_plugin_types();
394 foreach ($plugintypes as $plugintype => $plugintyperootdir) {
395 $this->pluginsinfo[$plugintype] = null;
398 // Add orphaned subplugin types.
399 $this->load_installed_plugins();
400 foreach ($this->installedplugins as $plugintype => $unused) {
401 if (!isset($plugintypes[$plugintype])) {
402 $this->pluginsinfo[$plugintype] = null;
408 * Find the plugin info class for given type.
410 * @param string $type
411 * @return string name of pluginfo class for give plugin type
413 public static function resolve_plugininfo_class($type) {
414 $plugintypes = core_component::get_plugin_types();
415 if (!isset($plugintypes[$type])) {
416 return '\core\plugininfo\orphaned';
419 $parent = core_component::get_subtype_parent($type);
422 $class = '\\'.$parent.'\plugininfo\\' . $type;
423 if (class_exists($class)) {
424 $plugintypeclass = $class;
426 if ($dir = core_component::get_component_directory($parent)) {
427 // BC only - use namespace instead!
428 if (file_exists("$dir/adminlib.php")) {
430 include_once("$dir/adminlib.php");
432 if (class_exists('plugininfo_' . $type)) {
433 $plugintypeclass = 'plugininfo_' . $type;
434 debugging('Class "'.$plugintypeclass.'" is deprecated, migrate to "'.$class.'"', DEBUG_DEVELOPER);
436 debugging('Subplugin type "'.$type.'" should define class "'.$class.'"', DEBUG_DEVELOPER);
437 $plugintypeclass = '\core\plugininfo\general';
440 $plugintypeclass = '\core\plugininfo\general';
444 $class = '\core\plugininfo\\' . $type;
445 if (class_exists($class)) {
446 $plugintypeclass = $class;
448 debugging('All standard types including "'.$type.'" should have plugininfo class!', DEBUG_DEVELOPER);
449 $plugintypeclass = '\core\plugininfo\general';
453 if (!in_array('core\plugininfo\base', class_parents($plugintypeclass))) {
454 throw new coding_exception('Class ' . $plugintypeclass . ' must extend \core\plugininfo\base');
457 return $plugintypeclass;
461 * Returns list of all known subplugins of the given plugin.
463 * For plugins that do not provide subplugins (i.e. there is no support for it),
464 * empty array is returned.
466 * @param string $component full component name, e.g. 'mod_workshop'
467 * @return array (string) component name (e.g. 'workshopallocation_random') => subclass of {@link \core\plugininfo\base}
469 public function get_subplugins_of_plugin($component) {
471 $pluginfo = $this->get_plugin_info($component);
473 if (is_null($pluginfo)) {
477 $subplugins = $this->get_subplugins();
479 if (!isset($subplugins[$pluginfo->component])) {
485 foreach ($subplugins[$pluginfo->component] as $subdata) {
486 foreach ($this->get_plugins_of_type($subdata->type) as $subpluginfo) {
487 $list[$subpluginfo->component] = $subpluginfo;
495 * Returns list of plugins that define their subplugins and the information
496 * about them from the db/subplugins.php file.
498 * @return array with keys like 'mod_quiz', and values the data from the
499 * corresponding db/subplugins.php file.
501 public function get_subplugins() {
503 if (is_array($this->subpluginsinfo)) {
504 return $this->subpluginsinfo;
507 $plugintypes = core_component::get_plugin_types();
509 $this->subpluginsinfo = array();
510 foreach (core_component::get_plugin_types_with_subplugins() as $type => $ignored) {
511 foreach (core_component::get_plugin_list($type) as $plugin => $componentdir) {
512 $component = $type.'_'.$plugin;
513 $subplugins = core_component::get_subplugins($component);
517 $this->subpluginsinfo[$component] = array();
518 foreach ($subplugins as $subplugintype => $ignored) {
519 $subplugin = new stdClass();
520 $subplugin->type = $subplugintype;
521 $subplugin->typerootdir = $plugintypes[$subplugintype];
522 $this->subpluginsinfo[$component][$subplugintype] = $subplugin;
526 return $this->subpluginsinfo;
530 * Returns the name of the plugin that defines the given subplugin type
532 * If the given subplugin type is not actually a subplugin, returns false.
534 * @param string $subplugintype the name of subplugin type, eg. workshopform or quiz
535 * @return false|string the name of the parent plugin, eg. mod_workshop
537 public function get_parent_of_subplugin($subplugintype) {
538 $parent = core_component::get_subtype_parent($subplugintype);
546 * Returns a localized name of a given plugin
548 * @param string $component name of the plugin, eg mod_workshop or auth_ldap
551 public function plugin_name($component) {
553 $pluginfo = $this->get_plugin_info($component);
555 if (is_null($pluginfo)) {
556 throw new moodle_exception('err_unknown_plugin', 'core_plugin', '', array('plugin' => $component));
559 return $pluginfo->displayname;
563 * Returns a localized name of a plugin typed in singular form
565 * Most plugin types define their names in core_plugin lang file. In case of subplugins,
566 * we try to ask the parent plugin for the name. In the worst case, we will return
567 * the value of the passed $type parameter.
569 * @param string $type the type of the plugin, e.g. mod or workshopform
572 public function plugintype_name($type) {
574 if (get_string_manager()->string_exists('type_' . $type, 'core_plugin')) {
575 // For most plugin types, their names are defined in core_plugin lang file.
576 return get_string('type_' . $type, 'core_plugin');
578 } else if ($parent = $this->get_parent_of_subplugin($type)) {
579 // If this is a subplugin, try to ask the parent plugin for the name.
580 if (get_string_manager()->string_exists('subplugintype_' . $type, $parent)) {
581 return $this->plugin_name($parent) . ' / ' . get_string('subplugintype_' . $type, $parent);
583 return $this->plugin_name($parent) . ' / ' . $type;
592 * Returns a localized name of a plugin type in plural form
594 * Most plugin types define their names in core_plugin lang file. In case of subplugins,
595 * we try to ask the parent plugin for the name. In the worst case, we will return
596 * the value of the passed $type parameter.
598 * @param string $type the type of the plugin, e.g. mod or workshopform
601 public function plugintype_name_plural($type) {
603 if (get_string_manager()->string_exists('type_' . $type . '_plural', 'core_plugin')) {
604 // For most plugin types, their names are defined in core_plugin lang file.
605 return get_string('type_' . $type . '_plural', 'core_plugin');
607 } else if ($parent = $this->get_parent_of_subplugin($type)) {
608 // If this is a subplugin, try to ask the parent plugin for the name.
609 if (get_string_manager()->string_exists('subplugintype_' . $type . '_plural', $parent)) {
610 return $this->plugin_name($parent) . ' / ' . get_string('subplugintype_' . $type . '_plural', $parent);
612 return $this->plugin_name($parent) . ' / ' . $type;
621 * Returns information about the known plugin, or null
623 * @param string $component frankenstyle component name.
624 * @return \core\plugininfo\base|null the corresponding plugin information.
626 public function get_plugin_info($component) {
627 list($type, $name) = core_component::normalize_component($component);
628 $plugins = $this->get_plugins_of_type($type);
629 if (isset($plugins[$name])) {
630 return $plugins[$name];
637 * Check to see if the current version of the plugin seems to be a checkout of an external repository.
639 * @see \core\update\deployer::plugin_external_source()
640 * @param string $component frankenstyle component name
641 * @return false|string
643 public function plugin_external_source($component) {
645 $plugininfo = $this->get_plugin_info($component);
647 if (is_null($plugininfo)) {
651 $pluginroot = $plugininfo->rootdir;
653 if (is_dir($pluginroot.'/.git')) {
657 if (is_file($pluginroot.'/.git')) {
658 return 'git-submodule';
661 if (is_dir($pluginroot.'/CVS')) {
665 if (is_dir($pluginroot.'/.svn')) {
669 if (is_dir($pluginroot.'/.hg')) {
677 * Get a list of any other plugins that require this one.
678 * @param string $component frankenstyle component name.
679 * @return array of frankensyle component names that require this one.
681 public function other_plugins_that_require($component) {
683 foreach ($this->get_plugins() as $type => $plugins) {
684 foreach ($plugins as $plugin) {
685 $required = $plugin->get_other_required_plugins();
686 if (isset($required[$component])) {
687 $others[] = $plugin->component;
695 * Check a dependencies list against the list of installed plugins.
696 * @param array $dependencies compenent name to required version or ANY_VERSION.
697 * @return bool true if all the dependencies are satisfied.
699 public function are_dependencies_satisfied($dependencies) {
700 foreach ($dependencies as $component => $requiredversion) {
701 $otherplugin = $this->get_plugin_info($component);
702 if (is_null($otherplugin)) {
706 if ($requiredversion != ANY_VERSION and $otherplugin->versiondisk < $requiredversion) {
715 * Checks all dependencies for all installed plugins
717 * This is used by install and upgrade. The array passed by reference as the second
718 * argument is populated with the list of plugins that have failed dependencies (note that
719 * a single plugin can appear multiple times in the $failedplugins).
721 * @param int $moodleversion the version from version.php.
722 * @param array $failedplugins to return the list of plugins with non-satisfied dependencies
723 * @return bool true if all the dependencies are satisfied for all plugins.
725 public function all_plugins_ok($moodleversion, &$failedplugins = array()) {
728 foreach ($this->get_plugins() as $type => $plugins) {
729 foreach ($plugins as $plugin) {
731 if (!$plugin->is_core_dependency_satisfied($moodleversion)) {
733 $failedplugins[] = $plugin->component;
736 if (!$this->are_dependencies_satisfied($plugin->get_other_required_plugins())) {
738 $failedplugins[] = $plugin->component;
747 * Is it possible to uninstall the given plugin?
749 * False is returned if the plugininfo subclass declares the uninstall should
750 * not be allowed via {@link \core\plugininfo\base::is_uninstall_allowed()} or if the
751 * core vetoes it (e.g. becase the plugin or some of its subplugins is required
752 * by some other installed plugin).
754 * @param string $component full frankenstyle name, e.g. mod_foobar
757 public function can_uninstall_plugin($component) {
759 $pluginfo = $this->get_plugin_info($component);
761 if (is_null($pluginfo)) {
765 if (!$this->common_uninstall_check($pluginfo)) {
769 // Verify only if something else requires the subplugins, do not verify their common_uninstall_check()!
770 $subplugins = $this->get_subplugins_of_plugin($pluginfo->component);
771 foreach ($subplugins as $subpluginfo) {
772 // Check if there are some other plugins requiring this subplugin
773 // (but the parent and siblings).
774 foreach ($this->other_plugins_that_require($subpluginfo->component) as $requiresme) {
775 $ismyparent = ($pluginfo->component === $requiresme);
776 $ismysibling = in_array($requiresme, array_keys($subplugins));
777 if (!$ismyparent and !$ismysibling) {
783 // Check if there are some other plugins requiring this plugin
784 // (but its subplugins).
785 foreach ($this->other_plugins_that_require($pluginfo->component) as $requiresme) {
786 $ismysubplugin = in_array($requiresme, array_keys($subplugins));
787 if (!$ismysubplugin) {
796 * Returns uninstall URL if exists.
798 * @param string $component
799 * @param string $return either 'overview' or 'manage'
800 * @return moodle_url uninstall URL, null if uninstall not supported
802 public function get_uninstall_url($component, $return = 'overview') {
803 if (!$this->can_uninstall_plugin($component)) {
807 $pluginfo = $this->get_plugin_info($component);
809 if (is_null($pluginfo)) {
813 if (method_exists($pluginfo, 'get_uninstall_url')) {
814 debugging('plugininfo method get_uninstall_url() is deprecated, all plugins should be uninstalled via standard URL only.');
815 return $pluginfo->get_uninstall_url($return);
818 return $pluginfo->get_default_uninstall_url($return);
822 * Uninstall the given plugin.
824 * Automatically cleans-up all remaining configuration data, log records, events,
825 * files from the file pool etc.
827 * In the future, the functionality of {@link uninstall_plugin()} function may be moved
828 * into this method and all the code should be refactored to use it. At the moment, we
829 * mimic this future behaviour by wrapping that function call.
831 * @param string $component
832 * @param progress_trace $progress traces the process
833 * @return bool true on success, false on errors/problems
835 public function uninstall_plugin($component, progress_trace $progress) {
837 $pluginfo = $this->get_plugin_info($component);
839 if (is_null($pluginfo)) {
843 // Give the pluginfo class a chance to execute some steps.
844 $result = $pluginfo->uninstall($progress);
849 // Call the legacy core function to uninstall the plugin.
851 uninstall_plugin($pluginfo->type, $pluginfo->name);
852 $progress->output(ob_get_clean());
858 * Checks if there are some plugins with a known available update
860 * @return bool true if there is at least one available update
862 public function some_plugins_updatable() {
863 foreach ($this->get_plugins() as $type => $plugins) {
864 foreach ($plugins as $plugin) {
865 if ($plugin->available_updates()) {
875 * Check to see if the given plugin folder can be removed by the web server process.
877 * @param string $component full frankenstyle component
880 public function is_plugin_folder_removable($component) {
882 $pluginfo = $this->get_plugin_info($component);
884 if (is_null($pluginfo)) {
888 // To be able to remove the plugin folder, its parent must be writable, too.
889 if (!is_writable(dirname($pluginfo->rootdir))) {
893 // Check that the folder and all its content is writable (thence removable).
894 return $this->is_directory_removable($pluginfo->rootdir);
898 * Defines a list of all plugins that were originally shipped in the standard Moodle distribution,
899 * but are not anymore and are deleted during upgrades.
901 * The main purpose of this list is to hide missing plugins during upgrade.
903 * @param string $type plugin type
904 * @param string $name plugin name
907 public static function is_deleted_standard_plugin($type, $name) {
908 // Do not include plugins that were removed during upgrades to versions that are
909 // not supported as source versions for upgrade any more. For example, at MOODLE_23_STABLE
910 // branch, listed should be no plugins that were removed at 1.9.x - 2.1.x versions as
911 // Moodle 2.3 supports upgrades from 2.2.x only.
913 'qformat' => array('blackboard', 'learnwise'),
914 'enrol' => array('authorize'),
915 'tinymce' => array('dragmath'),
916 'tool' => array('bloglevelupgrade', 'qeupgradehelper', 'timezoneimport'),
917 'theme' => array('mymobile', 'afterburner', 'anomaly', 'arialist', 'binarius', 'boxxie', 'brick', 'formal_white',
918 'formfactor', 'fusion', 'leatherbound', 'magazine', 'nimble', 'nonzero', 'overlay', 'serenity', 'sky_high',
919 'splash', 'standard', 'standardold'),
922 if (!isset($plugins[$type])) {
925 return in_array($name, $plugins[$type]);
929 * Defines a white list of all plugins shipped in the standard Moodle distribution
931 * @param string $type
932 * @return false|array array of standard plugins or false if the type is unknown
934 public static function standard_plugins_list($type) {
936 $standard_plugins = array(
939 'accessibilitychecker', 'accessibilityhelper', 'align',
940 'backcolor', 'bold', 'charmap', 'clear', 'collapse', 'emoticon',
941 'equation', 'fontcolor', 'html', 'image', 'indent', 'italic',
942 'link', 'managefiles', 'media', 'noautolink', 'orderedlist',
943 'rtl', 'strike', 'subscript', 'superscript', 'table', 'title',
944 'underline', 'undo', 'unorderedlist'
947 'assignment' => array(
948 'offline', 'online', 'upload', 'uploadsingle'
951 'assignsubmission' => array(
952 'comments', 'file', 'onlinetext'
955 'assignfeedback' => array(
956 'comments', 'file', 'offline', 'editpdf'
960 'cas', 'db', 'email', 'fc', 'imap', 'ldap', 'manual', 'mnet',
961 'nntp', 'nologin', 'none', 'pam', 'pop3', 'radius',
962 'shibboleth', 'webservice'
965 'availability' => array(
966 'completion', 'date', 'grade', 'group', 'grouping', 'profile'
970 'activity_modules', 'activity_results', 'admin_bookmarks', 'badges',
971 'blog_menu', 'blog_recent', 'blog_tags', 'calendar_month',
972 'calendar_upcoming', 'comments', 'community',
973 'completionstatus', 'course_list', 'course_overview',
974 'course_summary', 'feedback', 'glossary_random', 'html',
975 'login', 'mentees', 'messages', 'mnet_hosts', 'myprofile',
976 'navigation', 'news_items', 'online_users', 'participants',
977 'private_files', 'quiz_results', 'recent_activity',
978 'rss_client', 'search_forums', 'section_links',
979 'selfcompletion', 'settings', 'site_main_menu',
980 'social_activities', 'tag_flickr', 'tag_youtube', 'tags'
984 'exportimscp', 'importhtml', 'print'
987 'cachelock' => array(
991 'cachestore' => array(
992 'file', 'memcache', 'memcached', 'mongodb', 'session', 'static'
995 'calendartype' => array(
999 'coursereport' => array(
1003 'datafield' => array(
1004 'checkbox', 'date', 'file', 'latlong', 'menu', 'multimenu',
1005 'number', 'picture', 'radiobutton', 'text', 'textarea', 'url'
1008 'datapreset' => array(
1013 'atto', 'textarea', 'tinymce'
1017 'category', 'cohort', 'database', 'flatfile',
1018 'guest', 'imsenterprise', 'ldap', 'manual', 'meta', 'mnet',
1023 'activitynames', 'algebra', 'censor', 'emailprotect',
1024 'emoticon', 'mathjaxloader', 'mediaplugin', 'multilang', 'tex', 'tidy',
1025 'urltolink', 'data', 'glossary'
1029 'singleactivity', 'social', 'topics', 'weeks'
1032 'gradeexport' => array(
1033 'ods', 'txt', 'xls', 'xml'
1036 'gradeimport' => array(
1037 'csv', 'direct', 'xml'
1040 'gradereport' => array(
1041 'grader', 'history', 'outcomes', 'overview', 'user', 'singleview'
1044 'gradingform' => array(
1051 'logstore' => array(
1052 'database', 'legacy', 'standard',
1055 'ltiservice' => array(
1056 'profile', 'toolproxy', 'toolsettings'
1060 'airnotifier', 'email', 'jabber', 'popup'
1063 'mnetservice' => array(
1068 'assign', 'assignment', 'book', 'chat', 'choice', 'data', 'feedback', 'folder',
1069 'forum', 'glossary', 'imscp', 'label', 'lesson', 'lti', 'page',
1070 'quiz', 'resource', 'scorm', 'survey', 'url', 'wiki', 'workshop'
1073 'plagiarism' => array(
1076 'portfolio' => array(
1077 'boxnet', 'download', 'flickr', 'googledocs', 'mahara', 'picasa'
1080 'profilefield' => array(
1081 'checkbox', 'datetime', 'menu', 'text', 'textarea'
1084 'qbehaviour' => array(
1085 'adaptive', 'adaptivenopenalty', 'deferredcbm',
1086 'deferredfeedback', 'immediatecbm', 'immediatefeedback',
1087 'informationitem', 'interactive', 'interactivecountback',
1088 'manualgraded', 'missing'
1092 'aiken', 'blackboard_six', 'examview', 'gift',
1093 'missingword', 'multianswer', 'webct',
1098 'calculated', 'calculatedmulti', 'calculatedsimple',
1099 'description', 'essay', 'match', 'missingtype', 'multianswer',
1100 'multichoice', 'numerical', 'random', 'randomsamatch',
1101 'shortanswer', 'truefalse'
1105 'grading', 'overview', 'responses', 'statistics'
1108 'quizaccess' => array(
1109 'delaybetweenattempts', 'ipaddress', 'numattempts', 'openclosedate',
1110 'password', 'safebrowser', 'securewindow', 'timelimit'
1114 'backups', 'completion', 'configlog', 'courseoverview', 'eventlist',
1115 'log', 'loglive', 'outline', 'participation', 'progress', 'questioninstances', 'security', 'stats', 'performance',
1119 'repository' => array(
1120 'alfresco', 'areafiles', 'boxnet', 'coursefiles', 'dropbox', 'equella', 'filesystem',
1121 'flickr', 'flickr_public', 'googledocs', 'local', 'merlot',
1122 'picasa', 'recent', 'skydrive', 's3', 'upload', 'url', 'user', 'webdav',
1123 'wikimedia', 'youtube'
1126 'scormreport' => array(
1134 'ctrlhelp', 'managefiles', 'moodleemoticon', 'moodleimage',
1135 'moodlemedia', 'moodlenolink', 'pdw', 'spellchecker', 'wrap'
1139 'base', 'bootstrapbase', 'canvas', 'clean', 'more'
1143 'assignmentupgrade', 'availabilityconditions', 'behat', 'capability', 'customlang',
1144 'dbtransfer', 'filetypes', 'generator', 'health', 'innodb', 'installaddon',
1145 'langimport', 'log', 'messageinbound', 'multilangupgrade', 'monitor', 'phpunit', 'profiling',
1146 'replace', 'spamcleaner', 'task',
1147 'unittest', 'uploadcourse', 'uploaduser', 'unsuproles', 'xmldb'
1150 'webservice' => array(
1151 'amf', 'rest', 'soap', 'xmlrpc'
1154 'workshopallocation' => array(
1155 'manual', 'random', 'scheduled'
1158 'workshopeval' => array(
1162 'workshopform' => array(
1163 'accumulative', 'comments', 'numerrors', 'rubric'
1167 if (isset($standard_plugins[$type])) {
1168 return $standard_plugins[$type];
1175 * Reorders plugin types into a sequence to be displayed
1177 * For technical reasons, plugin types returned by {@link core_component::get_plugin_types()} are
1178 * in a certain order that does not need to fit the expected order for the display.
1179 * Particularly, activity modules should be displayed first as they represent the
1180 * real heart of Moodle. They should be followed by other plugin types that are
1181 * used to build the courses (as that is what one expects from LMS). After that,
1182 * other supportive plugin types follow.
1184 * @param array $types associative array
1185 * @return array same array with altered order of items
1187 protected function reorder_plugin_types(array $types) {
1188 $fix = array('mod' => $types['mod']);
1189 foreach (core_component::get_plugin_list('mod') as $plugin => $fulldir) {
1190 if (!$subtypes = core_component::get_subplugins('mod_'.$plugin)) {
1193 foreach ($subtypes as $subtype => $ignored) {
1194 $fix[$subtype] = $types[$subtype];
1198 $fix['mod'] = $types['mod'];
1199 $fix['block'] = $types['block'];
1200 $fix['qtype'] = $types['qtype'];
1201 $fix['qbehaviour'] = $types['qbehaviour'];
1202 $fix['qformat'] = $types['qformat'];
1203 $fix['filter'] = $types['filter'];
1205 $fix['editor'] = $types['editor'];
1206 foreach (core_component::get_plugin_list('editor') as $plugin => $fulldir) {
1207 if (!$subtypes = core_component::get_subplugins('editor_'.$plugin)) {
1210 foreach ($subtypes as $subtype => $ignored) {
1211 $fix[$subtype] = $types[$subtype];
1215 $fix['enrol'] = $types['enrol'];
1216 $fix['auth'] = $types['auth'];
1217 $fix['tool'] = $types['tool'];
1218 foreach (core_component::get_plugin_list('tool') as $plugin => $fulldir) {
1219 if (!$subtypes = core_component::get_subplugins('tool_'.$plugin)) {
1222 foreach ($subtypes as $subtype => $ignored) {
1223 $fix[$subtype] = $types[$subtype];
1227 foreach ($types as $type => $path) {
1228 if (!isset($fix[$type])) {
1229 $fix[$type] = $path;
1236 * Check if the given directory can be removed by the web server process.
1238 * This recursively checks that the given directory and all its contents
1241 * @param string $fullpath
1244 protected function is_directory_removable($fullpath) {
1246 if (!is_writable($fullpath)) {
1250 if (is_dir($fullpath)) {
1251 $handle = opendir($fullpath);
1258 while ($filename = readdir($handle)) {
1260 if ($filename === '.' or $filename === '..') {
1264 $subfilepath = $fullpath.'/'.$filename;
1266 if (is_dir($subfilepath)) {
1267 $result = $result && $this->is_directory_removable($subfilepath);
1270 $result = $result && is_writable($subfilepath);
1280 * Helper method that implements common uninstall prerequisites
1282 * @param \core\plugininfo\base $pluginfo
1285 protected function common_uninstall_check(\core\plugininfo\base $pluginfo) {
1287 if (!$pluginfo->is_uninstall_allowed()) {
1288 // The plugin's plugininfo class declares it should not be uninstalled.
1292 if ($pluginfo->get_status() === self::PLUGIN_STATUS_NEW) {
1293 // The plugin is not installed. It should be either installed or removed from the disk.
1294 // Relying on this temporary state may be tricky.
1298 if (method_exists($pluginfo, 'get_uninstall_url') and is_null($pluginfo->get_uninstall_url())) {
1299 // Backwards compatibility.
1300 debugging('\core\plugininfo\base subclasses should use is_uninstall_allowed() instead of returning null in get_uninstall_url()',