Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This script creates config.php file and prepares database.
  20   *
  21   * This script is not intended for beginners!
  22   * Potential problems:
  23   * - su to apache account or sudo before execution
  24   * - not compatible with Windows platform
  25   *
  26   * @package    core
  27   * @subpackage cli
  28   * @copyright  2009 Petr Skoda (http://skodak.org)
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  
  32  // Force OPcache reset if used, we do not want any stale caches
  33  // when detecting if upgrade necessary or when running upgrade.
  34  if (function_exists('opcache_reset') and !isset($_SERVER['REMOTE_ADDR'])) {
  35      opcache_reset();
  36  }
  37  
  38  define('CLI_SCRIPT', true);
  39  define('CACHE_DISABLE_ALL', true);
  40  
  41  require(__DIR__.'/../../config.php');
  42  require_once($CFG->libdir.'/adminlib.php');       // various admin-only functions
  43  require_once($CFG->libdir.'/upgradelib.php');     // general upgrade/install related functions
  44  require_once($CFG->libdir.'/clilib.php');         // cli only functions
  45  require_once($CFG->libdir.'/environmentlib.php');
  46  
  47  // now get cli options
  48  $lang = isset($SESSION->lang) ? $SESSION->lang : $CFG->lang;
  49  list($options, $unrecognized) = cli_get_params(
  50      array(
  51          'non-interactive'   => false,
  52          'allow-unstable'    => false,
  53          'help'              => false,
  54          'lang'              => $lang,
  55          'verbose-settings'  => false,
  56          'is-pending'        => false,
  57      ),
  58      array(
  59          'h' => 'help'
  60      )
  61  );
  62  
  63  if ($options['lang']) {
  64      $SESSION->lang = $options['lang'];
  65  }
  66  
  67  $interactive = empty($options['non-interactive']);
  68  
  69  if ($unrecognized) {
  70      $unrecognized = implode("\n  ", $unrecognized);
  71      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  72  }
  73  
  74  if ($options['help']) {
  75      $help =
  76  "Command line Moodle upgrade.
  77  Please note you must execute this script with the same uid as apache!
  78  
  79  Site defaults may be changed via local/defaults.php.
  80  
  81  Options:
  82  --non-interactive     No interactive questions or confirmations
  83  --allow-unstable      Upgrade even if the version is not marked as stable yet,
  84                        required in non-interactive mode.
  85  --lang=CODE           Set preferred language for CLI output. Defaults to the
  86                        site language if not set. Defaults to 'en' if the lang
  87                        parameter is invalid or if the language pack is not
  88                        installed.
  89  --verbose-settings    Show new settings values. By default only the name of
  90                        new core or plugin settings are displayed. This option
  91                        outputs the new values as well as the setting name.
  92  --is-pending          If an upgrade is needed it exits with an error code of
  93                        2 so it distinct from other types of errors.
  94  -h, --help            Print out this help
  95  
  96  Example:
  97  \$sudo -u www-data /usr/bin/php admin/cli/upgrade.php
  98  "; //TODO: localize - to be translated later when everything is finished
  99  
 100      echo $help;
 101      die;
 102  }
 103  
 104  if (empty($CFG->version)) {
 105      cli_error(get_string('missingconfigversion', 'debug'));
 106  }
 107  
 108  require("$CFG->dirroot/version.php");       // defines $version, $release, $branch and $maturity
 109  $CFG->target_release = $release;            // used during installation and upgrades
 110  
 111  if ($version < $CFG->version) {
 112      cli_error(get_string('downgradedcore', 'error'));
 113  }
 114  
 115  $oldversion = "$CFG->release ($CFG->version)";
 116  $newversion = "$release ($version)";
 117  
 118  if (!moodle_needs_upgrading()) {
 119      cli_error(get_string('cliupgradenoneed', 'core_admin', $newversion), 0);
 120  }
 121  
 122  if ($options['is-pending']) {
 123      cli_error(get_string('cliupgradepending', 'core_admin'), 2);
 124  }
 125  
 126  // Test environment first.
 127  list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
 128  if (!$envstatus) {
 129      $errors = environment_get_errors($environment_results);
 130      cli_heading(get_string('environment', 'admin'));
 131      foreach ($errors as $error) {
 132          list($info, $report) = $error;
 133          echo "!! $info !!\n$report\n\n";
 134      }
 135      exit(1);
 136  }
 137  
 138  // Test plugin dependencies.
 139  $failed = array();
 140  if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed, $CFG->branch)) {
 141      cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
 142      cli_error(get_string('pluginschecktodo', 'admin'));
 143  }
 144  
 145  $a = new stdClass();
 146  $a->oldversion = $oldversion;
 147  $a->newversion = $newversion;
 148  
 149  if ($interactive) {
 150      echo cli_heading(get_string('databasechecking', '', $a)) . PHP_EOL;
 151  }
 152  
 153  // make sure we are upgrading to a stable release or display a warning
 154  if (isset($maturity)) {
 155      if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
 156          $maturitylevel = get_string('maturity'.$maturity, 'admin');
 157  
 158          if ($interactive) {
 159              cli_separator();
 160              cli_heading(get_string('notice'));
 161              echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
 162              echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
 163              cli_separator();
 164          } else {
 165              cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
 166              cli_error(get_string('maturityallowunstable', 'admin'));
 167          }
 168      }
 169  }
 170  
 171  if ($interactive) {
 172      echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n";
 173      $prompt = get_string('cliyesnoprompt', 'admin');
 174      $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
 175      if ($input == get_string('clianswerno', 'admin')) {
 176          exit(1);
 177      }
 178  }
 179  
 180  if ($version > $CFG->version) {
 181      // We purge all of MUC's caches here.
 182      // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
 183      // This ensures a real config object is loaded and the stores will be purged.
 184      // This is the only way we can purge custom caches such as memcache or APC.
 185      // Note: all other calls to caches will still used the disabled API.
 186      cache_helper::purge_all(true);
 187      upgrade_core($version, true);
 188  }
 189  set_config('release', $release);
 190  set_config('branch', $branch);
 191  
 192  // unconditionally upgrade
 193  upgrade_noncore(true);
 194  
 195  // log in as admin - we need doanything permission when applying defaults
 196  \core\session\manager::set_user(get_admin());
 197  
 198  // Apply default settings and output those that have changed.
 199  cli_heading(get_string('cliupgradedefaultheading', 'admin'));
 200  $settingsoutput = admin_apply_default_settings(null, false);
 201  
 202  foreach ($settingsoutput as $setting => $value) {
 203  
 204      if ($options['verbose-settings']) {
 205          $stringvlaues = array(
 206                  'name' => $setting,
 207                  'defaultsetting' => var_export($value, true) // Expand objects.
 208          );
 209          echo get_string('cliupgradedefaultverbose', 'admin', $stringvlaues) . PHP_EOL;
 210  
 211      } else {
 212          echo get_string('cliupgradedefault', 'admin', $setting) . PHP_EOL;
 213  
 214      }
 215  }
 216  
 217  // This needs to happen at the end to ensure it occurs after all caches
 218  // have been purged for the last time.
 219  // This will build a cached version of the current theme for the user
 220  // to immediately start browsing the site.
 221  upgrade_themes();
 222  
 223  echo get_string('cliupgradefinished', 'admin', $a)."\n";
 224  exit(0); // 0 means success