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 * CLI script to uninstall plugins.
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.
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.
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([
72 'show-contrib' => false,
73 'show-missing' => false,
74 'purge-missing' => false,
78 'showdebugging' => false,
84 $unrecognised = implode(PHP_EOL
.' ', $unrecognised);
85 cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised));
88 if ($options['help']) {
93 if ($options['showdebugging']) {
94 set_debugging(DEBUG_DEVELOPER
, true);
97 if ($options['showsql']) {
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()) {
110 $pluginstring = $plugin->component
. "\t" . $plugin->displayname
;
112 if ($options['show-all'] ||
$options['show-contrib']) {
113 cli_writeln($pluginstring);
115 if ($plugin->get_status() === core_plugin_manager
::PLUGIN_STATUS_MISSING
) {
116 cli_writeln($pluginstring);
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());
141 cli_writeln('Will be uninstalled: ' . $pluginstring);
144 cli_writeln('Can not be uninstalled: ' . $pluginstring);
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);
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());
171 cli_writeln('Will be uninstalled: ' . $pluginstring);
174 cli_writeln('Can not be uninstalled: ' . $pluginstring);