Merge branch 'MDL-68782-master' of git://github.com/aanabit/moodle
[moodle.git] / admin / cli / uninstall_plugins.php
blobe093d896d51046bc53cd8138bb8855ffd9234274
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-missing Displays a list of plugins missing from disk.
38 --purge-missing Uninstall all missing from disk plugins.
39 --plugins=<plugin name> A comma separated list of plugins to be uninstalled. E.g. mod_assign,mod_forum
40 --run Execute uninstall. If this option is not set, then the script will be run in a dry mode.
42 Examples:
44 # php uninstall_plugins.php --show-all
45 Prints tab-separated list of all installed plugins.
47 # php uninstall_plugins.php --show-missing
48 Prints tab-separated list of all missing from disk plugins.
50 # php uninstall_plugins.php --purge-missing
51 A dry run of uninstalling all missing plugins.
53 # php uninstall_plugins.php --purge-missing --run
54 Run uninstall of all missing plugins.
56 # php uninstall_plugins.php --plugins=mod_assign,mod_forum
57 A dry run of uninstalling mod_assign and mod_forum plugins.
59 # php uninstall_plugins.php --plugins=mod_assign,mod_forum --run
60 Run uninstall for mod_assign and mod_forum plugins.
63 list($options, $unrecognised) = cli_get_params([
64 'help' => false,
65 'show-all' => false,
66 'show-missing' => false,
67 'purge-missing' => false,
68 'plugins' => false,
69 'run' => false,
70 ], [
71 'h' => 'help'
72 ]);
74 if ($unrecognised) {
75 $unrecognised = implode(PHP_EOL.' ', $unrecognised);
76 cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised));
79 if ($options['help']) {
80 cli_writeln($help);
81 exit(0);
84 $pluginman = core_plugin_manager::instance();
85 $plugininfo = $pluginman->get_plugins();
87 if ($options['show-all'] || $options['show-missing']) {
88 foreach ($plugininfo as $type => $plugins) {
89 foreach ($plugins as $name => $plugin) {
90 $pluginstring = $plugin->component . "\t" . $plugin->displayname;
92 if ($options['show-all']) {
93 cli_writeln($pluginstring);
94 } else {
95 if ($plugin->get_status() === core_plugin_manager::PLUGIN_STATUS_MISSING) {
96 cli_writeln($pluginstring);
102 exit(0);
105 if ($options['purge-missing']) {
106 foreach ($plugininfo as $type => $plugins) {
107 foreach ($plugins as $name => $plugin) {
108 if ($plugin->get_status() === core_plugin_manager::PLUGIN_STATUS_MISSING) {
110 $pluginstring = $plugin->component . "\t" . $plugin->displayname;
112 if ($pluginman->can_uninstall_plugin($plugin->component)) {
113 if ($options['run']) {
114 cli_writeln('Uninstalling: ' . $pluginstring);
116 $progress = new progress_trace_buffer(new text_progress_trace(), true);
117 $pluginman->uninstall_plugin($plugin->component, $progress);
118 $progress->finished();
119 cli_write($progress->get_buffer());
120 } else {
121 cli_writeln('Will be uninstalled: ' . $pluginstring);
123 } else {
124 cli_writeln('Can not be uninstalled: ' . $pluginstring);
130 exit(0);
133 if ($options['plugins']) {
134 $components = explode(',', $options['plugins']);
135 foreach ($components as $component) {
136 $plugin = $pluginman->get_plugin_info($component);
138 if (is_null($plugin)) {
139 cli_writeln('Unknown plugin: ' . $component);
140 } else {
141 $pluginstring = $plugin->component . "\t" . $plugin->displayname;
143 if ($pluginman->can_uninstall_plugin($plugin->component)) {
144 if ($options['run']) {
145 cli_writeln('Uninstalling: ' . $pluginstring);
146 $progress = new progress_trace_buffer(new text_progress_trace(), true);
147 $pluginman->uninstall_plugin($plugin->component, $progress);
148 $progress->finished();
149 cli_write($progress->get_buffer());
150 } else {
151 cli_writeln('Will be uninstalled: ' . $pluginstring);
153 } else {
154 cli_writeln('Can not be uninstalled: ' . $pluginstring);
159 exit(0);
162 cli_writeln($help);
163 exit(0);