MDL-49329 admin: Use the new plugins management on Plugins overview page
[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 *
21 * @package core
22 * @subpackage admin
23 * @copyright 2011 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once(dirname(dirname(__FILE__)) . '/config.php');
28require_once($CFG->libdir . '/adminlib.php');
436d9447 29require_once($CFG->libdir . '/filelib.php');
b9934a17 30
8726c07c
DM
31$fetchremote = optional_param('fetchremote', false, PARAM_BOOL); // Check for available plugins updates.
32$updatesonly = optional_param('updatesonly', false, PARAM_BOOL); // Show updateable plugins only.
33$contribonly = optional_param('contribonly', false, PARAM_BOOL); // Show additional plugins only.
34$uninstall = optional_param('uninstall', '', PARAM_COMPONENT); // Uninstall the plugin.
35$delete = optional_param('delete', '', PARAM_COMPONENT); // Delete the plugin folder after it is uninstalled.
36$confirmed = optional_param('confirm', false, PARAM_BOOL); // Confirm the uninstall/delete action.
37$return = optional_param('return', 'overview', PARAM_ALPHA); // Where to return after uninstall.
38$installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
39$installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
40$installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
41$confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
436d9447 42
a3cbc198
PS
43// NOTE: do not use admin_externalpage_setup() here because it loads
44// full admin tree which is not possible during uninstallation.
45
46require_login();
47$syscontext = context_system::instance();
48require_capability('moodle/site:config', $syscontext);
3204daea 49
8726c07c
DM
50// URL params we want to maintain on redirects.
51$pageparams = array('updatesonly' => $updatesonly, 'contribonly' => $contribonly);
52$pageurl = new moodle_url('/admin/plugins.php', $pageparams);
53
e87214bd 54$pluginman = core_plugin_manager::instance();
436d9447
DM
55
56if ($uninstall) {
57 require_sesskey();
a3cbc198
PS
58
59 if (!$confirmed) {
8726c07c 60 admin_externalpage_setup('pluginsoverview', '', $pageparams);
a3cbc198 61 } else {
8726c07c 62 $PAGE->set_url($pageurl);
a3cbc198
PS
63 $PAGE->set_context($syscontext);
64 $PAGE->set_pagelayout('maintenance');
65 $PAGE->set_popup_notification_allowed(false);
66 }
67
68 /** @var core_admin_renderer $output */
69 $output = $PAGE->get_renderer('core', 'admin');
70
436d9447
DM
71 $pluginfo = $pluginman->get_plugin_info($uninstall);
72
c2d2001a 73 // Make sure we know the plugin.
436d9447
DM
74 if (is_null($pluginfo)) {
75 throw new moodle_exception('err_uninstalling_unknown_plugin', 'core_plugin', '', array('plugin' => $uninstall),
e87214bd 76 'core_plugin_manager::get_plugin_info() returned null for the plugin to be uninstalled');
436d9447
DM
77 }
78
c2d2001a
DM
79 $pluginname = $pluginman->plugin_name($pluginfo->component);
80 $PAGE->set_title($pluginname);
81 $PAGE->navbar->add(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
82
83 if (!$pluginman->can_uninstall_plugin($pluginfo->component)) {
84 throw new moodle_exception('err_cannot_uninstall_plugin', 'core_plugin', '',
85 array('plugin' => $pluginfo->component),
e87214bd 86 'core_plugin_manager::can_uninstall_plugin() returned false');
436d9447
DM
87 }
88
89 if (!$confirmed) {
e87214bd
PS
90 $continueurl = new moodle_url($PAGE->url, array('uninstall' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1, 'return'=>$return));
91 $cancelurl = $pluginfo->get_return_url_after_uninstall($return);
92 echo $output->plugin_uninstall_confirm_page($pluginman, $pluginfo, $continueurl, $cancelurl);
436d9447
DM
93 exit();
94
95 } else {
e87214bd 96 $SESSION->pluginuninstallreturn = $pluginfo->get_return_url_after_uninstall($return);
3ca1b546
DM
97 $progress = new progress_trace_buffer(new text_progress_trace(), false);
98 $pluginman->uninstall_plugin($pluginfo->component, $progress);
99 $progress->finished();
436d9447
DM
100
101 if ($pluginman->is_plugin_folder_removable($pluginfo->component)) {
102 $continueurl = new moodle_url($PAGE->url, array('delete' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1));
3ca1b546 103 echo $output->plugin_uninstall_results_removable_page($pluginman, $pluginfo, $progress, $continueurl);
c05a5099
PS
104 // Reset op code caches.
105 if (function_exists('opcache_reset')) {
106 opcache_reset();
107 }
436d9447
DM
108 exit();
109
110 } else {
3ca1b546 111 echo $output->plugin_uninstall_results_page($pluginman, $pluginfo, $progress);
c05a5099
PS
112 // Reset op code caches.
113 if (function_exists('opcache_reset')) {
114 opcache_reset();
115 }
436d9447
DM
116 exit();
117 }
118 }
119}
120
121if ($delete and $confirmed) {
122 require_sesskey();
a3cbc198 123
8726c07c 124 $PAGE->set_url($pageurl);
a3cbc198
PS
125 $PAGE->set_context($syscontext);
126 $PAGE->set_pagelayout('maintenance');
127 $PAGE->set_popup_notification_allowed(false);
128
129 /** @var core_admin_renderer $output */
130 $output = $PAGE->get_renderer('core', 'admin');
131
436d9447
DM
132 $pluginfo = $pluginman->get_plugin_info($delete);
133
134 // Make sure we know the plugin.
135 if (is_null($pluginfo)) {
136 throw new moodle_exception('err_removing_unknown_plugin', 'core_plugin', '', array('plugin' => $delete),
e87214bd 137 'core_plugin_manager::get_plugin_info() returned null for the plugin to be deleted');
436d9447
DM
138 }
139
c2d2001a
DM
140 $pluginname = $pluginman->plugin_name($pluginfo->component);
141 $PAGE->set_title($pluginname);
142 $PAGE->navbar->add(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
143
436d9447
DM
144 // Make sure it is not installed.
145 if (!is_null($pluginfo->versiondb)) {
146 throw new moodle_exception('err_removing_installed_plugin', 'core_plugin', '',
147 array('plugin' => $pluginfo->component, 'versiondb' => $pluginfo->versiondb),
e87214bd 148 'core_plugin_manager::get_plugin_info() returned not-null versiondb for the plugin to be deleted');
436d9447
DM
149 }
150
151 // Make sure the folder is removable.
152 if (!$pluginman->is_plugin_folder_removable($pluginfo->component)) {
153 throw new moodle_exception('err_removing_unremovable_folder', 'core_plugin', '',
154 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir),
155 'plugin root folder is not removable as expected');
156 }
157
158 // Make sure the folder is within Moodle installation tree.
159 if (strpos($pluginfo->rootdir, $CFG->dirroot) !== 0) {
160 throw new moodle_exception('err_unexpected_plugin_rootdir', 'core_plugin', '',
161 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir, 'dirroot' => $CFG->dirroot),
162 'plugin root folder not in the moodle dirroot');
163 }
164
165 // So long, and thanks for all the bugs.
166 fulldelete($pluginfo->rootdir);
c05a5099
PS
167 // Reset op code caches.
168 if (function_exists('opcache_reset')) {
169 opcache_reset();
170 }
82b1cf00
PS
171 // We need to execute upgrade to make sure everything including caches is up to date.
172 redirect(new moodle_url('/admin/index.php'));
436d9447
DM
173}
174
8726c07c
DM
175// Install all avilable updates.
176if ($installupdatex) {
177 require_once($CFG->libdir.'/upgradelib.php');
178 require_sesskey();
179
180 $PAGE->set_url($pageurl);
181 $PAGE->set_context($syscontext);
182 $PAGE->set_pagelayout('maintenance');
183 $PAGE->set_popup_notification_allowed(false);
184
185 $installable = $pluginman->filter_installable($pluginman->available_updates());
186 upgrade_install_remote_plugins($installable, $confirminstallupdate,
187 get_string('updateavailableinstallallhead', 'core_admin'),
188 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
189 );
190}
191
192// Install single available update.
193if ($installupdate and $installupdateversion) {
194 require_once($CFG->libdir.'/upgradelib.php');
195 require_sesskey();
196
197 $PAGE->set_url($pageurl);
198 $PAGE->set_context($syscontext);
199 $PAGE->set_pagelayout('maintenance');
200 $PAGE->set_popup_notification_allowed(false);
201
202 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
203 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
204 upgrade_install_remote_plugins($installable, $confirminstallupdate,
205 get_string('updateavailableinstallallhead', 'core_admin'),
206 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
207 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
208 )
209 );
210 }
211}
212
213admin_externalpage_setup('pluginsoverview', '', $pageparams);
a3cbc198
PS
214
215/** @var core_admin_renderer $output */
216$output = $PAGE->get_renderer('core', 'admin');
217
e87214bd 218$checker = \core\update\checker::instance();
3204daea
DM
219
220if ($fetchremote) {
00ea11f8
DM
221 require_sesskey();
222 $checker->fetch();
8726c07c 223 redirect($PAGE->url);
bcc8397a
DM
224}
225
8726c07c 226echo $output->plugin_management_page($pluginman, $checker, $pageparams);