Merge branch 'MDL-75909-311' of https://github.com/andrewnicols/moodle into MOODLE_31...
[moodle.git] / admin / cli / uninstall_plugins.php
blob8d9ad2b31e593c7de233a391718d914718f1e42b
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 * CLI script to uninstall plugins.
20 * @package core
21 * @subpackage cli
22 * @copyright 2018 Dmitrii Metelkin <dmitriim@catalyst-au.net>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('CLI_SCRIPT', true);
28 require(__DIR__ . '/../../config.php');
29 require_once($CFG->libdir . '/clilib.php');
30 require_once($CFG->libdir . '/adminlib.php');
32 $help = "Command line tool to uninstall plugins.
34 Options:
35 -h --help Print this help.
36 --show-all Displays a list of all installed plugins.
37 --show-contrib Displays a list of all third-party installed plugins.
38 --show-missing Displays a list of plugins missing from disk.
39 --purge-missing Uninstall all missing from disk plugins.
40 --plugins=<plugin name> A comma separated list of plugins to be uninstalled. E.g. mod_assign,mod_forum
41 --run Execute uninstall. If this option is not set, then the script will be run in a dry mode.
42 --showsql Show sql queries before they are executed.
43 --showdebugging Show developer level debugging information.
45 Examples:
47 # php uninstall_plugins.php --show-all
48 Prints tab-separated list of all installed plugins.
50 # php uninstall_plugins.php --show-contrib
51 Prints tab-separated list of all third-party installed plugins.
53 # php uninstall_plugins.php --show-missing
54 Prints tab-separated list of all missing from disk plugins.
56 # php uninstall_plugins.php --purge-missing
57 A dry run of uninstalling all missing plugins.
59 # php uninstall_plugins.php --purge-missing --run
60 Run uninstall of all missing plugins.
62 # php uninstall_plugins.php --plugins=mod_assign,mod_forum
63 A dry run of uninstalling mod_assign and mod_forum plugins.
65 # php uninstall_plugins.php --plugins=mod_assign,mod_forum --run
66 Run uninstall for mod_assign and mod_forum plugins.
69 list($options, $unrecognised) = cli_get_params([
70 'help' => false,
71 'show-all' => false,
72 'show-contrib' => false,
73 'show-missing' => false,
74 'purge-missing' => false,
75 'plugins' => false,
76 'run' => false,
77 'showsql' => false,
78 'showdebugging' => false,
79 ], [
80 'h' => 'help'
81 ]);
83 if ($unrecognised) {
84 $unrecognised = implode(PHP_EOL.' ', $unrecognised);
85 cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised));
88 if ($options['help']) {
89 cli_writeln($help);
90 exit(0);
93 if ($options['showdebugging']) {
94 set_debugging(DEBUG_DEVELOPER, true);
97 if ($options['showsql']) {
98 $DB->set_debug(true);
101 $pluginman = core_plugin_manager::instance();
102 $plugininfo = $pluginman->get_plugins();
104 if ($options['show-all'] || $options['show-missing'] || $options['show-contrib']) {
105 foreach ($plugininfo as $type => $plugins) {
106 foreach ($plugins as $name => $plugin) {
107 if ($options['show-contrib'] && $plugin->is_standard()) {
108 continue;
110 $pluginstring = $plugin->component . "\t" . $plugin->displayname;
112 if ($options['show-all'] || $options['show-contrib']) {
113 cli_writeln($pluginstring);
114 } else {
115 if ($plugin->get_status() === core_plugin_manager::PLUGIN_STATUS_MISSING) {
116 cli_writeln($pluginstring);
122 exit(0);
125 if ($options['purge-missing']) {
126 foreach ($plugininfo as $type => $plugins) {
127 foreach ($plugins as $name => $plugin) {
128 if ($plugin->get_status() === core_plugin_manager::PLUGIN_STATUS_MISSING) {
130 $pluginstring = $plugin->component . "\t" . $plugin->displayname;
132 if ($pluginman->can_uninstall_plugin($plugin->component)) {
133 if ($options['run']) {
134 cli_writeln('Uninstalling: ' . $pluginstring);
136 $progress = new progress_trace_buffer(new text_progress_trace(), true);
137 $pluginman->uninstall_plugin($plugin->component, $progress);
138 $progress->finished();
139 cli_write($progress->get_buffer());
140 } else {
141 cli_writeln('Will be uninstalled: ' . $pluginstring);
143 } else {
144 cli_writeln('Can not be uninstalled: ' . $pluginstring);
150 exit(0);
153 if ($options['plugins']) {
154 $components = explode(',', $options['plugins']);
155 foreach ($components as $component) {
156 $plugin = $pluginman->get_plugin_info($component);
158 if (is_null($plugin)) {
159 cli_writeln('Unknown plugin: ' . $component);
160 } else {
161 $pluginstring = $plugin->component . "\t" . $plugin->displayname;
163 if ($pluginman->can_uninstall_plugin($plugin->component)) {
164 if ($options['run']) {
165 cli_writeln('Uninstalling: ' . $pluginstring);
166 $progress = new progress_trace_buffer(new text_progress_trace(), true);
167 $pluginman->uninstall_plugin($plugin->component, $progress);
168 $progress->finished();
169 cli_write($progress->get_buffer());
170 } else {
171 cli_writeln('Will be uninstalled: ' . $pluginstring);
173 } else {
174 cli_writeln('Can not be uninstalled: ' . $pluginstring);
179 exit(0);
182 cli_writeln($help);
183 exit(0);