Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * CLI script to uninstall plugins.
  19   *
  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
  24   */
  25  
  26  define('CLI_SCRIPT', true);
  27  
  28  require(__DIR__ . '/../../config.php');
  29  require_once($CFG->libdir . '/clilib.php');
  30  require_once($CFG->libdir . '/adminlib.php');
  31  
  32  $help = "Command line tool to uninstall plugins.
  33  
  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  
  43  Examples:
  44  
  45      # php uninstall_plugins.php  --show-all
  46          Prints tab-separated list of all installed plugins.
  47  
  48      # php uninstall_plugins.php  --show-contrib
  49          Prints tab-separated list of all third-party installed plugins.
  50  
  51      # php uninstall_plugins.php  --show-missing
  52          Prints tab-separated list of all missing from disk plugins.
  53  
  54      # php uninstall_plugins.php  --purge-missing
  55          A dry run of uninstalling all missing plugins.
  56  
  57      # php uninstall_plugins.php  --purge-missing --run
  58          Run uninstall of all missing plugins.
  59  
  60      # php uninstall_plugins.php  --plugins=mod_assign,mod_forum
  61          A dry run of uninstalling mod_assign and mod_forum plugins.
  62  
  63      # php uninstall_plugins.php  --plugins=mod_assign,mod_forum --run
  64          Run uninstall for mod_assign and mod_forum plugins.
  65  ";
  66  
  67  list($options, $unrecognised) = cli_get_params([
  68      'help' => false,
  69      'show-all' => false,
  70      'show-contrib' => false,
  71      'show-missing' => false,
  72      'purge-missing' => false,
  73      'plugins' => false,
  74      'run' => false,
  75  ], [
  76      'h' => 'help'
  77  ]);
  78  
  79  if ($unrecognised) {
  80      $unrecognised = implode(PHP_EOL.'  ', $unrecognised);
  81      cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised));
  82  }
  83  
  84  if ($options['help']) {
  85      cli_writeln($help);
  86      exit(0);
  87  }
  88  
  89  $pluginman = core_plugin_manager::instance();
  90  $plugininfo = $pluginman->get_plugins();
  91  
  92  if ($options['show-all'] || $options['show-missing'] || $options['show-contrib']) {
  93      foreach ($plugininfo as $type => $plugins) {
  94          foreach ($plugins as $name => $plugin) {
  95              if ($options['show-contrib'] && $plugin->is_standard()) {
  96                  continue;
  97              }
  98              $pluginstring = $plugin->component . "\t" . $plugin->displayname;
  99  
 100              if ($options['show-all'] || $options['show-contrib']) {
 101                  cli_writeln($pluginstring);
 102              } else {
 103                  if ($plugin->get_status() === core_plugin_manager::PLUGIN_STATUS_MISSING) {
 104                      cli_writeln($pluginstring);
 105                  }
 106              }
 107          }
 108      }
 109  
 110      exit(0);
 111  }
 112  
 113  if ($options['purge-missing']) {
 114      foreach ($plugininfo as $type => $plugins) {
 115          foreach ($plugins as $name => $plugin) {
 116              if ($plugin->get_status() === core_plugin_manager::PLUGIN_STATUS_MISSING) {
 117  
 118                  $pluginstring = $plugin->component . "\t" . $plugin->displayname;
 119  
 120                  if ($pluginman->can_uninstall_plugin($plugin->component)) {
 121                      if ($options['run']) {
 122                          cli_writeln('Uninstalling: ' . $pluginstring);
 123  
 124                          $progress = new progress_trace_buffer(new text_progress_trace(), true);
 125                          $pluginman->uninstall_plugin($plugin->component, $progress);
 126                          $progress->finished();
 127                          cli_write($progress->get_buffer());
 128                      } else {
 129                          cli_writeln('Will be uninstalled: ' . $pluginstring);
 130                      }
 131                  } else {
 132                      cli_writeln('Can not be uninstalled: ' . $pluginstring);
 133                  }
 134              }
 135          }
 136      }
 137  
 138      exit(0);
 139  }
 140  
 141  if ($options['plugins']) {
 142      $components = explode(',', $options['plugins']);
 143      foreach ($components as $component) {
 144          $plugin = $pluginman->get_plugin_info($component);
 145  
 146          if (is_null($plugin)) {
 147              cli_writeln('Unknown plugin: ' . $component);
 148          } else {
 149              $pluginstring = $plugin->component . "\t" . $plugin->displayname;
 150  
 151              if ($pluginman->can_uninstall_plugin($plugin->component)) {
 152                  if ($options['run']) {
 153                      cli_writeln('Uninstalling: ' . $pluginstring);
 154                      $progress = new progress_trace_buffer(new text_progress_trace(), true);
 155                      $pluginman->uninstall_plugin($plugin->component, $progress);
 156                      $progress->finished();
 157                      cli_write($progress->get_buffer());
 158                  } else {
 159                      cli_writeln('Will be uninstalled: ' . $pluginstring);
 160                  }
 161              } else {
 162                  cli_writeln('Can not be uninstalled: ' . $pluginstring);
 163              }
 164          }
 165      }
 166  
 167      exit(0);
 168  }
 169  
 170  cli_writeln($help);
 171  exit(0);