MDL-42078 multiple uninstall improvements and cleanup
[moodle.git] / lib / classes / plugininfo / base.php
CommitLineData
e87214bd
PS
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.
13//
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/>.
16
17/**
18 * Defines classes used for plugin info.
19 *
20 * @package core
21 * @copyright 2011 David Mudrak <david@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core\plugininfo;
25
26use core_component, core_plugin_manager, moodle_url, coding_exception;
27
28defined('MOODLE_INTERNAL') || die();
29
30
31/**
32 * Base class providing access to the information about a plugin
33 *
34 * @property-read string component the component name, type_name
35 */
36abstract class base {
37
38 /** @var string the plugintype name, eg. mod, auth or workshopform */
39 public $type;
40 /** @var string full path to the location of all the plugins of this type */
41 public $typerootdir;
42 /** @var string the plugin name, eg. assignment, ldap */
43 public $name;
44 /** @var string the localized plugin name */
45 public $displayname;
46 /** @var string the plugin source, one of core_plugin_manager::PLUGIN_SOURCE_xxx constants */
47 public $source;
48 /** @var string fullpath to the location of this plugin */
49 public $rootdir;
50 /** @var int|string the version of the plugin's source code */
51 public $versiondisk;
52 /** @var int|string the version of the installed plugin */
53 public $versiondb;
54 /** @var int|float|string required version of Moodle core */
55 public $versionrequires;
56 /** @var array other plugins that this one depends on, lazy-loaded by {@link get_other_required_plugins()} */
57 public $dependencies;
58 /** @var int number of instances of the plugin - not supported yet */
59 public $instances;
60 /** @var int order of the plugin among other plugins of the same type - not supported yet */
61 public $sortorder;
62 /** @var array|null array of {@link \core\update\info} for this plugin */
63 public $availableupdates;
64
65 /**
66 * Finds all enabled plugins, the result may include missing plugins.
67 * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
68 */
69 public static function get_enabled_plugins() {
70 return null;
71 }
72
73 /**
74 * Gathers and returns the information about all plugins of the given type,
75 * either on disk or previously installed.
76 *
77 * @param string $type the name of the plugintype, eg. mod, auth or workshopform
78 * @param string $typerootdir full path to the location of the plugin dir
79 * @param string $typeclass the name of the actually called class
80 * @return array of plugintype classes, indexed by the plugin name
81 */
82 public static function get_plugins($type, $typerootdir, $typeclass) {
83 // Get the information about plugins at the disk.
84 $plugins = core_component::get_plugin_list($type);
85 $return = array();
86 foreach ($plugins as $pluginname => $pluginrootdir) {
87 $return[$pluginname] = self::make_plugin_instance($type, $typerootdir,
88 $pluginname, $pluginrootdir, $typeclass);
89 }
90
91 // Fetch missing incorrectly uninstalled plugins.
92 $manager = core_plugin_manager::instance();
93 $plugins = $manager->get_installed_plugins($type);
94
95 foreach ($plugins as $name => $version) {
96 if (isset($return[$name])) {
97 continue;
98 }
99 $plugin = new $typeclass();
100 $plugin->type = $type;
101 $plugin->typerootdir = $typerootdir;
102 $plugin->name = $name;
103 $plugin->rootdir = null;
104 $plugin->displayname = $name;
105 $plugin->versiondb = $version;
106 $plugin->init_is_standard();
107
108 $return[$name] = $plugin;
109 }
110
111 return $return;
112 }
113
114 /**
115 * Makes a new instance of the plugininfo class
116 *
117 * @param string $type the plugin type, eg. 'mod'
118 * @param string $typerootdir full path to the location of all the plugins of this type
119 * @param string $name the plugin name, eg. 'workshop'
120 * @param string $namerootdir full path to the location of the plugin
121 * @param string $typeclass the name of class that holds the info about the plugin
122 * @return base the instance of $typeclass
123 */
124 protected static function make_plugin_instance($type, $typerootdir, $name, $namerootdir, $typeclass) {
125 $plugin = new $typeclass();
126 $plugin->type = $type;
127 $plugin->typerootdir = $typerootdir;
128 $plugin->name = $name;
129 $plugin->rootdir = $namerootdir;
130
131 $plugin->init_display_name();
132 $plugin->load_disk_version();
133 $plugin->load_db_version();
134 $plugin->init_is_standard();
135
136 return $plugin;
137 }
138
139 /**
140 * Is this plugin already installed and updated?
141 * @return bool true if plugin installed and upgraded.
142 */
143 public function is_installed_and_upgraded() {
144 if (!$this->rootdir) {
145 return false;
146 }
147 if ($this->versiondb === null and $this->versiondisk === null) {
148 // There is no version.php or version info inside,
149 // for now let's pretend it is ok.
150 // TODO: return false once we require version in each plugin.
151 return true;
152 }
153
154 return ((float)$this->versiondb === (float)$this->versiondisk);
155 }
156
157 /**
158 * Sets {@link $displayname} property to a localized name of the plugin
159 */
160 public function init_display_name() {
161 if (!get_string_manager()->string_exists('pluginname', $this->component)) {
162 $this->displayname = '[pluginname,' . $this->component . ']';
163 } else {
164 $this->displayname = get_string('pluginname', $this->component);
165 }
166 }
167
168 /**
169 * Magic method getter, redirects to read only values.
170 *
171 * @param string $name
172 * @return mixed
173 */
174 public function __get($name) {
175 switch ($name) {
176 case 'component': return $this->type . '_' . $this->name;
177
178 default:
179 debugging('Invalid plugin property accessed! '.$name);
180 return null;
181 }
182 }
183
184 /**
185 * Return the full path name of a file within the plugin.
186 *
187 * No check is made to see if the file exists.
188 *
189 * @param string $relativepath e.g. 'version.php'.
190 * @return string e.g. $CFG->dirroot . '/mod/quiz/version.php'.
191 */
192 public function full_path($relativepath) {
193 if (empty($this->rootdir)) {
194 return '';
195 }
196 return $this->rootdir . '/' . $relativepath;
197 }
198
199 /**
200 * Sets {@link $versiondisk} property to a numerical value representing the
201 * version of the plugin's source code.
202 *
203 * If the value is null after calling this method, either the plugin
204 * does not use versioning (typically does not have any database
205 * data) or is missing from disk.
206 */
207 public function load_disk_version() {
208 $versions = core_plugin_manager::instance()->get_present_plugins($this->type);
209
210 $this->versiondisk = null;
211 $this->versionrequires = null;
212 $this->dependencies = array();
213
214 if (!isset($versions[$this->name])) {
215 return;
216 }
217
218 $plugin = $versions[$this->name];
219
220 if (isset($plugin->version)) {
221 $this->versiondisk = $plugin->version;
222 }
223 if (isset($plugin->requires)) {
224 $this->versionrequires = $plugin->requires;
225 }
226 if (isset($plugin->dependencies)) {
227 $this->dependencies = $plugin->dependencies;
228 }
229 }
230
231 /**
232 * Get the list of other plugins that this plugin requires to be installed.
233 *
234 * @return array with keys the frankenstyle plugin name, and values either
235 * a version string (like '2011101700') or the constant ANY_VERSION.
236 */
237 public function get_other_required_plugins() {
238 if (is_null($this->dependencies)) {
239 $this->load_disk_version();
240 }
241 return $this->dependencies;
242 }
243
244 /**
245 * Is this is a subplugin?
246 *
247 * @return boolean
248 */
249 public function is_subplugin() {
250 return ($this->get_parent_plugin() !== false);
251 }
252
253 /**
254 * If I am a subplugin, return the name of my parent plugin.
255 *
256 * @return string|bool false if not a subplugin, name of the parent otherwise
257 */
258 public function get_parent_plugin() {
259 return $this->get_plugin_manager()->get_parent_of_subplugin($this->type);
260 }
261
262 /**
263 * Sets {@link $versiondb} property to a numerical value representing the
264 * currently installed version of the plugin.
265 *
266 * If the value is null after calling this method, either the plugin
267 * does not use versioning (typically does not have any database
268 * data) or has not been installed yet.
269 */
270 public function load_db_version() {
271 $versions = core_plugin_manager::instance()->get_installed_plugins($this->type);
272
273 if (isset($versions[$this->name])) {
274 $this->versiondb = $versions[$this->name];
275 } else {
276 $this->versiondb = null;
277 }
278 }
279
280 /**
281 * Sets {@link $source} property to one of core_plugin_manager::PLUGIN_SOURCE_xxx
282 * constants.
283 *
284 * If the property's value is null after calling this method, then
285 * the type of the plugin has not been recognized and you should throw
286 * an exception.
287 */
288 public function init_is_standard() {
289
290 $standard = core_plugin_manager::standard_plugins_list($this->type);
291
292 if ($standard !== false) {
293 $standard = array_flip($standard);
294 if (isset($standard[$this->name])) {
295 $this->source = core_plugin_manager::PLUGIN_SOURCE_STANDARD;
296 } else if (!is_null($this->versiondb) and is_null($this->versiondisk)
297 and core_plugin_manager::is_deleted_standard_plugin($this->type, $this->name)) {
298 $this->source = core_plugin_manager::PLUGIN_SOURCE_STANDARD; // To be deleted.
299 } else {
300 $this->source = core_plugin_manager::PLUGIN_SOURCE_EXTENSION;
301 }
302 }
303 }
304
305 /**
306 * Returns true if the plugin is shipped with the official distribution
307 * of the current Moodle version, false otherwise.
308 *
309 * @return bool
310 */
311 public function is_standard() {
312 return $this->source === core_plugin_manager::PLUGIN_SOURCE_STANDARD;
313 }
314
315 /**
316 * Returns true if the the given Moodle version is enough to run this plugin
317 *
318 * @param string|int|double $moodleversion
319 * @return bool
320 */
321 public function is_core_dependency_satisfied($moodleversion) {
322
323 if (empty($this->versionrequires)) {
324 return true;
325
326 } else {
327 return (double)$this->versionrequires <= (double)$moodleversion;
328 }
329 }
330
331 /**
332 * Returns the status of the plugin
333 *
334 * @return string one of core_plugin_manager::PLUGIN_STATUS_xxx constants
335 */
336 public function get_status() {
337
338 if (is_null($this->versiondb) and is_null($this->versiondisk)) {
339 return core_plugin_manager::PLUGIN_STATUS_NODB;
340
341 } else if (is_null($this->versiondb) and !is_null($this->versiondisk)) {
342 return core_plugin_manager::PLUGIN_STATUS_NEW;
343
344 } else if (!is_null($this->versiondb) and is_null($this->versiondisk)) {
345 if (core_plugin_manager::is_deleted_standard_plugin($this->type, $this->name)) {
346 return core_plugin_manager::PLUGIN_STATUS_DELETE;
347 } else {
348 return core_plugin_manager::PLUGIN_STATUS_MISSING;
349 }
350
351 } else if ((float)$this->versiondb === (float)$this->versiondisk) {
352 // Note: the float comparison should work fine here
353 // because there are no arithmetic operations with the numbers.
354 return core_plugin_manager::PLUGIN_STATUS_UPTODATE;
355
356 } else if ($this->versiondb < $this->versiondisk) {
357 return core_plugin_manager::PLUGIN_STATUS_UPGRADE;
358
359 } else if ($this->versiondb > $this->versiondisk) {
360 return core_plugin_manager::PLUGIN_STATUS_DOWNGRADE;
361
362 } else {
363 // $version = pi(); and similar funny jokes - hopefully Donald E. Knuth will never contribute to Moodle ;-)
364 throw new coding_exception('Unable to determine plugin state, check the plugin versions');
365 }
366 }
367
368 /**
369 * Returns the information about plugin availability
370 *
371 * True means that the plugin is enabled. False means that the plugin is
372 * disabled. Null means that the information is not available, or the
373 * plugin does not support configurable availability or the availability
374 * can not be changed.
375 *
376 * @return null|bool
377 */
378 public function is_enabled() {
379 if (!$this->rootdir) {
380 // Plugin missing.
381 return false;
382 }
383
384 $enabled = core_plugin_manager::instance()->get_enabled_plugins($this->type);
385
386 if (!is_array($enabled)) {
387 return null;
388 }
389
390 return isset($enabled[$this->name]);
391 }
392
393 /**
394 * Populates the property {@link $availableupdates} with the information provided by
395 * available update checker
396 *
397 * @param \core\update\checker $provider the class providing the available update info
398 */
399 public function check_available_updates(\core\update\checker $provider) {
400 global $CFG;
401
402 if (isset($CFG->updateminmaturity)) {
403 $minmaturity = $CFG->updateminmaturity;
404 } else {
405 // This can happen during the very first upgrade to 2.3 .
406 $minmaturity = MATURITY_STABLE;
407 }
408
409 $this->availableupdates = $provider->get_update_info($this->component,
410 array('minmaturity' => $minmaturity));
411 }
412
413 /**
414 * If there are updates for this plugin available, returns them.
415 *
416 * Returns array of {@link \core\update\info} objects, if some update
417 * is available. Returns null if there is no update available or if the update
418 * availability is unknown.
419 *
420 * @return array|null
421 */
422 public function available_updates() {
423
424 if (empty($this->availableupdates) or !is_array($this->availableupdates)) {
425 return null;
426 }
427
428 $updates = array();
429
430 foreach ($this->availableupdates as $availableupdate) {
431 if ($availableupdate->version > $this->versiondisk) {
432 $updates[] = $availableupdate;
433 }
434 }
435
436 if (empty($updates)) {
437 return null;
438 }
439
440 return $updates;
441 }
442
443 /**
444 * Returns the node name used in admin settings menu for this plugin settings (if applicable)
445 *
446 * @return null|string node name or null if plugin does not create settings node (default)
447 */
448 public function get_settings_section_name() {
449 return null;
450 }
451
452 /**
453 * Returns the URL of the plugin settings screen
454 *
455 * Null value means that the plugin either does not have the settings screen
456 * or its location is not available via this library.
457 *
458 * @return null|moodle_url
459 */
460 public function get_settings_url() {
461 $section = $this->get_settings_section_name();
462 if ($section === null) {
463 return null;
464 }
465 $settings = admin_get_root()->locate($section);
466 if ($settings && $settings instanceof \admin_settingpage) {
467 return new moodle_url('/admin/settings.php', array('section' => $section));
468 } else if ($settings && $settings instanceof \admin_externalpage) {
469 return new moodle_url($settings->url);
470 } else {
471 return null;
472 }
473 }
474
475 /**
476 * Loads plugin settings to the settings tree
477 *
478 * This function usually includes settings.php file in plugins folder.
479 * Alternatively it can create a link to some settings page (instance of admin_externalpage)
480 *
481 * @param \part_of_admin_tree $adminroot
482 * @param string $parentnodename
483 * @param bool $hassiteconfig whether the current user has moodle/site:config capability
484 */
485 public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
486 }
487
488 /**
489 * Should there be a way to uninstall the plugin via the administration UI.
490 *
491 * By default uninstallation is not allowed, plugin developers must enable it explicitly!
492 *
493 * @return bool
494 */
495 public function is_uninstall_allowed() {
496 return false;
497 }
498
499 /**
500 * Optional extra warning before uninstallation, for example number of uses in courses.
501 *
502 * @return string
503 */
504 public function get_uninstall_extra_warning() {
505 return '';
506 }
507
508 /**
509 * Pre-uninstall hook.
510 *
511 * This is intended for disabling of plugin, some DB table purging, etc.
512 *
513 * NOTE: to be called from uninstall_plugin() only.
514 * @private
515 */
516 public function uninstall_cleanup() {
517 // Override when extending class,
518 // do not forget to call parent::pre_uninstall_cleanup() at the end.
519 }
520
521 /**
522 * Returns relative directory of the plugin with heading '/'
523 *
524 * @return string
525 */
526 public function get_dir() {
527 global $CFG;
528
529 return substr($this->rootdir, strlen($CFG->dirroot));
530 }
531
532 /**
533 * Hook method to implement certain steps when uninstalling the plugin.
534 *
535 * This hook is called by {@link core_plugin_manager::uninstall_plugin()} so
536 * it is basically usable only for those plugin types that use the default
537 * uninstall tool provided by {@link self::get_default_uninstall_url()}.
538 *
539 * @param \progress_trace $progress traces the process
540 * @return bool true on success, false on failure
541 */
542 public function uninstall(\progress_trace $progress) {
543 return true;
544 }
545
546 /**
547 * Where should we return after plugin of this type is uninstalled?
548 * @param string $return
549 * @return moodle_url
550 */
551 public function get_return_url_after_uninstall($return) {
552 if ($return === 'manage') {
553 if ($url = $this->get_manage_url()) {
554 return $url;
555 }
556 }
557 return new moodle_url('/admin/plugins.php#plugin_type_cell_'.$this->type);
558 }
559
560 /**
561 * Return URL used for management of plugins of this type.
562 * @return moodle_url
563 */
564 public static function get_manage_url() {
565 return null;
566 }
567
568 /**
569 * Returns URL to a script that handles common plugin uninstall procedure.
570 *
571 * This URL is intended for all plugin uninstallations.
572 *
573 * @param string $return either 'overview' or 'manage'
574 * @return moodle_url
575 */
576 public final function get_default_uninstall_url($return = 'overview') {
577 return new moodle_url('/admin/plugins.php', array(
578 'sesskey' => sesskey(),
579 'uninstall' => $this->component,
580 'confirm' => 0,
581 'return' => $return,
582 ));
583 }
584
585 /**
586 * Provides access to the core_plugin_manager singleton.
587 *
588 * @return core_plugin_manager
589 */
590 protected function get_plugin_manager() {
591 return core_plugin_manager::instance();
592 }
593}