Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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      --showsql                   Show sql queries before they are executed.
  43      --showdebugging             Show developer level debugging information.
  44  
  45  Examples:
  46  
  47      # php uninstall_plugins.php  --show-all
  48          Prints tab-separated list of all installed plugins.
  49  
  50      # php uninstall_plugins.php  --show-contrib
  51          Prints tab-separated list of all third-party installed plugins.
  52  
  53      # php uninstall_plugins.php  --show-missing
  54          Prints tab-separated list of all missing from disk plugins.
  55  
  56      # php uninstall_plugins.php  --purge-missing
  57          A dry run of uninstalling all missing plugins.
  58  
  59      # php uninstall_plugins.php  --purge-missing --run
  60          Run uninstall of all missing plugins.
  61  
  62      # php uninstall_plugins.php  --plugins=mod_assign,mod_forum
  63          A dry run of uninstalling mod_assign and mod_forum plugins.
  64  
  65      # php uninstall_plugins.php  --plugins=mod_assign,mod_forum --run
  66          Run uninstall for mod_assign and mod_forum plugins.
  67  ";
  68  
  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  ]);
  82  
  83  if ($unrecognised) {
  84      $unrecognised = implode(PHP_EOL.'  ', $unrecognised);
  85      cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised));
  86  }
  87  
  88  if ($options['help']) {
  89      cli_writeln($help);
  90      exit(0);
  91  }
  92  
  93  if ($options['showdebugging']) {
  94      set_debugging(DEBUG_DEVELOPER, true);
  95  }
  96  
  97  if ($options['showsql']) {
  98      $DB->set_debug(true);
  99  }
 100  
 101  $pluginman = core_plugin_manager::instance();
 102  $plugininfo = $pluginman->get_plugins();
 103  
 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;
 109              }
 110              $pluginstring = $plugin->component . "\t" . $plugin->displayname;
 111  
 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);
 117                  }
 118              }
 119          }
 120      }
 121  
 122      exit(0);
 123  }
 124  
 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) {
 129  
 130                  $pluginstring = $plugin->component . "\t" . $plugin->displayname;
 131  
 132                  if ($pluginman->can_uninstall_plugin($plugin->component)) {
 133                      if ($options['run']) {
 134                          cli_writeln('Uninstalling: ' . $pluginstring);
 135  
 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);
 142                      }
 143                  } else {
 144                      cli_writeln('Can not be uninstalled: ' . $pluginstring);
 145                  }
 146              }
 147          }
 148      }
 149  
 150      exit(0);
 151  }
 152  
 153  if ($options['plugins']) {
 154      $components = explode(',', $options['plugins']);
 155      foreach ($components as $component) {
 156          $plugin = $pluginman->get_plugin_info($component);
 157  
 158          if (is_null($plugin)) {
 159              cli_writeln('Unknown plugin: ' . $component);
 160          } else {
 161              $pluginstring = $plugin->component . "\t" . $plugin->displayname;
 162  
 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);
 172                  }
 173              } else {
 174                  cli_writeln('Can not be uninstalled: ' . $pluginstring);
 175              }
 176          }
 177      }
 178  
 179      exit(0);
 180  }
 181  
 182  cli_writeln($help);
 183  exit(0);