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] [Versions 401 and 402] [Versions 401 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  // Make sure there are no files left over from previous versions.
 139  if (upgrade_stale_php_files_present()) {
 140      cli_problem(get_string('upgradestalefiles', 'admin'));
 141  
 142      // Stale file info contains HTML elements which aren't suitable for CLI.
 143      $upgradestalefilesinfo = get_string('upgradestalefilesinfo', 'admin', get_docs_url('Upgrading'));
 144      cli_error(strip_tags($upgradestalefilesinfo));
 145  }
 146  
 147  // Test plugin dependencies.
 148  $failed = array();
 149  if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed, $CFG->branch)) {
 150      cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
 151      cli_error(get_string('pluginschecktodo', 'admin'));
 152  }
 153  
 154  $a = new stdClass();
 155  $a->oldversion = $oldversion;
 156  $a->newversion = $newversion;
 157  
 158  if ($interactive) {
 159      echo cli_heading(get_string('databasechecking', '', $a)) . PHP_EOL;
 160  }
 161  
 162  // make sure we are upgrading to a stable release or display a warning
 163  if (isset($maturity)) {
 164      if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
 165          $maturitylevel = get_string('maturity'.$maturity, 'admin');
 166  
 167          if ($interactive) {
 168              cli_separator();
 169              cli_heading(get_string('notice'));
 170              echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
 171              echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
 172              cli_separator();
 173          } else {
 174              cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
 175              cli_error(get_string('maturityallowunstable', 'admin'));
 176          }
 177      }
 178  }
 179  
 180  if ($interactive) {
 181      echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n";
 182      $prompt = get_string('cliyesnoprompt', 'admin');
 183      $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
 184      if ($input == get_string('clianswerno', 'admin')) {
 185          exit(1);
 186      }
 187  }
 188  
 189  if ($version > $CFG->version) {
 190      // We purge all of MUC's caches here.
 191      // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
 192      // This ensures a real config object is loaded and the stores will be purged.
 193      // This is the only way we can purge custom caches such as memcache or APC.
 194      // Note: all other calls to caches will still used the disabled API.
 195      cache_helper::purge_all(true);
 196      upgrade_core($version, true);
 197  }
 198  set_config('release', $release);
 199  set_config('branch', $branch);
 200  
 201  // unconditionally upgrade
 202  upgrade_noncore(true);
 203  
 204  // log in as admin - we need doanything permission when applying defaults
 205  \core\session\manager::set_user(get_admin());
 206  
 207  // Apply default settings and output those that have changed.
 208  cli_heading(get_string('cliupgradedefaultheading', 'admin'));
 209  $settingsoutput = admin_apply_default_settings(null, false);
 210  
 211  foreach ($settingsoutput as $setting => $value) {
 212  
 213      if ($options['verbose-settings']) {
 214          $stringvlaues = array(
 215                  'name' => $setting,
 216                  'defaultsetting' => var_export($value, true) // Expand objects.
 217          );
 218          echo get_string('cliupgradedefaultverbose', 'admin', $stringvlaues) . PHP_EOL;
 219  
 220      } else {
 221          echo get_string('cliupgradedefault', 'admin', $setting) . PHP_EOL;
 222  
 223      }
 224  }
 225  
 226  // This needs to happen at the end to ensure it occurs after all caches
 227  // have been purged for the last time.
 228  // This will build a cached version of the current theme for the user
 229  // to immediately start browsing the site.
 230  upgrade_themes();
 231  
 232  echo get_string('cliupgradefinished', 'admin', $a)."\n";
 233  exit(0); // 0 means success