MDL-49329 admin: Support plugins installation during upgrade
[moodle.git] / admin / plugins.php
CommitLineData
b9934a17
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * UI for general plugins management
20 *
c2d2001a
DM
21 * Supported HTTP parameters:
22 *
23 * ?fetchremote=1 - check for available updates
24 * ?updatesonly=1 - display plugins with available update only
25 * ?contribonly=1 - display non-standard add-ons only
26 * ?uninstall=foo_bar - uninstall the given plugin
27 * ?delete=foo_bar - delete the plugin folder (it must not be installed)
28 * &confirm=1 - confirm the uninstall or delete action
29 *
b9934a17
DM
30 * @package core
31 * @subpackage admin
32 * @copyright 2011 David Mudrak <david@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35
36require_once(dirname(dirname(__FILE__)) . '/config.php');
37require_once($CFG->libdir . '/adminlib.php');
436d9447 38require_once($CFG->libdir . '/filelib.php');
b9934a17 39
3204daea 40$fetchremote = optional_param('fetchremote', false, PARAM_BOOL);
4df8bced
DM
41$updatesonly = optional_param('updatesonly', false, PARAM_BOOL);
42$contribonly = optional_param('contribonly', false, PARAM_BOOL);
e87214bd
PS
43$uninstall = optional_param('uninstall', '', PARAM_COMPONENT);
44$delete = optional_param('delete', '', PARAM_COMPONENT);
45$confirmed = optional_param('confirm', false, PARAM_BOOL);
46$return = optional_param('return', 'overview', PARAM_ALPHA);
436d9447 47
a3cbc198
PS
48// NOTE: do not use admin_externalpage_setup() here because it loads
49// full admin tree which is not possible during uninstallation.
50
51require_login();
52$syscontext = context_system::instance();
53require_capability('moodle/site:config', $syscontext);
3204daea 54
e87214bd 55$pluginman = core_plugin_manager::instance();
436d9447
DM
56
57if ($uninstall) {
58 require_sesskey();
a3cbc198
PS
59
60 if (!$confirmed) {
61 admin_externalpage_setup('pluginsoverview');
62 } else {
63 $PAGE->set_url('/admin/plugins.php');
64 $PAGE->set_context($syscontext);
65 $PAGE->set_pagelayout('maintenance');
66 $PAGE->set_popup_notification_allowed(false);
67 }
68
69 /** @var core_admin_renderer $output */
70 $output = $PAGE->get_renderer('core', 'admin');
71
436d9447
DM
72 $pluginfo = $pluginman->get_plugin_info($uninstall);
73
c2d2001a 74 // Make sure we know the plugin.
436d9447
DM
75 if (is_null($pluginfo)) {
76 throw new moodle_exception('err_uninstalling_unknown_plugin', 'core_plugin', '', array('plugin' => $uninstall),
e87214bd 77 'core_plugin_manager::get_plugin_info() returned null for the plugin to be uninstalled');
436d9447
DM
78 }
79
c2d2001a
DM
80 $pluginname = $pluginman->plugin_name($pluginfo->component);
81 $PAGE->set_title($pluginname);
82 $PAGE->navbar->add(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
83
84 if (!$pluginman->can_uninstall_plugin($pluginfo->component)) {
85 throw new moodle_exception('err_cannot_uninstall_plugin', 'core_plugin', '',
86 array('plugin' => $pluginfo->component),
e87214bd 87 'core_plugin_manager::can_uninstall_plugin() returned false');
436d9447
DM
88 }
89
90 if (!$confirmed) {
e87214bd
PS
91 $continueurl = new moodle_url($PAGE->url, array('uninstall' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1, 'return'=>$return));
92 $cancelurl = $pluginfo->get_return_url_after_uninstall($return);
93 echo $output->plugin_uninstall_confirm_page($pluginman, $pluginfo, $continueurl, $cancelurl);
436d9447
DM
94 exit();
95
96 } else {
e87214bd 97 $SESSION->pluginuninstallreturn = $pluginfo->get_return_url_after_uninstall($return);
3ca1b546
DM
98 $progress = new progress_trace_buffer(new text_progress_trace(), false);
99 $pluginman->uninstall_plugin($pluginfo->component, $progress);
100 $progress->finished();
436d9447
DM
101
102 if ($pluginman->is_plugin_folder_removable($pluginfo->component)) {
103 $continueurl = new moodle_url($PAGE->url, array('delete' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1));
3ca1b546 104 echo $output->plugin_uninstall_results_removable_page($pluginman, $pluginfo, $progress, $continueurl);
c05a5099
PS
105 // Reset op code caches.
106 if (function_exists('opcache_reset')) {
107 opcache_reset();
108 }
436d9447
DM
109 exit();
110
111 } else {
3ca1b546 112 echo $output->plugin_uninstall_results_page($pluginman, $pluginfo, $progress);
c05a5099
PS
113 // Reset op code caches.
114 if (function_exists('opcache_reset')) {
115 opcache_reset();
116 }
436d9447
DM
117 exit();
118 }
119 }
120}
121
122if ($delete and $confirmed) {
123 require_sesskey();
a3cbc198
PS
124
125 $PAGE->set_url('/admin/plugins.php');
126 $PAGE->set_context($syscontext);
127 $PAGE->set_pagelayout('maintenance');
128 $PAGE->set_popup_notification_allowed(false);
129
130 /** @var core_admin_renderer $output */
131 $output = $PAGE->get_renderer('core', 'admin');
132
436d9447
DM
133 $pluginfo = $pluginman->get_plugin_info($delete);
134
135 // Make sure we know the plugin.
136 if (is_null($pluginfo)) {
137 throw new moodle_exception('err_removing_unknown_plugin', 'core_plugin', '', array('plugin' => $delete),
e87214bd 138 'core_plugin_manager::get_plugin_info() returned null for the plugin to be deleted');
436d9447
DM
139 }
140
c2d2001a
DM
141 $pluginname = $pluginman->plugin_name($pluginfo->component);
142 $PAGE->set_title($pluginname);
143 $PAGE->navbar->add(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
144
436d9447
DM
145 // Make sure it is not installed.
146 if (!is_null($pluginfo->versiondb)) {
147 throw new moodle_exception('err_removing_installed_plugin', 'core_plugin', '',
148 array('plugin' => $pluginfo->component, 'versiondb' => $pluginfo->versiondb),
e87214bd 149 'core_plugin_manager::get_plugin_info() returned not-null versiondb for the plugin to be deleted');
436d9447
DM
150 }
151
152 // Make sure the folder is removable.
153 if (!$pluginman->is_plugin_folder_removable($pluginfo->component)) {
154 throw new moodle_exception('err_removing_unremovable_folder', 'core_plugin', '',
155 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir),
156 'plugin root folder is not removable as expected');
157 }
158
159 // Make sure the folder is within Moodle installation tree.
160 if (strpos($pluginfo->rootdir, $CFG->dirroot) !== 0) {
161 throw new moodle_exception('err_unexpected_plugin_rootdir', 'core_plugin', '',
162 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir, 'dirroot' => $CFG->dirroot),
163 'plugin root folder not in the moodle dirroot');
164 }
165
166 // So long, and thanks for all the bugs.
167 fulldelete($pluginfo->rootdir);
c05a5099
PS
168 // Reset op code caches.
169 if (function_exists('opcache_reset')) {
170 opcache_reset();
171 }
82b1cf00
PS
172 // We need to execute upgrade to make sure everything including caches is up to date.
173 redirect(new moodle_url('/admin/index.php'));
436d9447
DM
174}
175
a3cbc198
PS
176admin_externalpage_setup('pluginsoverview');
177
178/** @var core_admin_renderer $output */
179$output = $PAGE->get_renderer('core', 'admin');
180
e87214bd 181$checker = \core\update\checker::instance();
3204daea 182
4df8bced
DM
183// Filtering options.
184$options = array(
185 'updatesonly' => $updatesonly,
186 'contribonly' => $contribonly,
187);
188
3204daea 189if ($fetchremote) {
00ea11f8
DM
190 require_sesskey();
191 $checker->fetch();
4df8bced 192 redirect(new moodle_url($PAGE->url, $options));
3204daea
DM
193}
194
e87214bd 195$deployer = \core\update\deployer::instance();
bcc8397a
DM
196if ($deployer->enabled()) {
197 $myurl = new moodle_url($PAGE->url, array('updatesonly' => $updatesonly, 'contribonly' => $contribonly));
5d7a4bab 198 $deployer->initialize($myurl, new moodle_url('/admin'));
bcc8397a
DM
199
200 $deploydata = $deployer->submitted_data();
201 if (!empty($deploydata)) {
202 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
203 die();
204 }
205}
206
4df8bced 207echo $output->plugin_management_page($pluginman, $checker, $options);