[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/cli/ -> upgrade.php (source)

   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      ),
  56      array(
  57          'h' => 'help'
  58      )
  59  );
  60  
  61  if ($options['lang']) {
  62      $SESSION->lang = $options['lang'];
  63  }
  64  
  65  $interactive = empty($options['non-interactive']);
  66  
  67  if ($unrecognized) {
  68      $unrecognized = implode("\n  ", $unrecognized);
  69      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  70  }
  71  
  72  if ($options['help']) {
  73      $help =
  74  "Command line Moodle upgrade.
  75  Please note you must execute this script with the same uid as apache!
  76  
  77  Site defaults may be changed via local/defaults.php.
  78  
  79  Options:
  80  --non-interactive     No interactive questions or confirmations
  81  --allow-unstable      Upgrade even if the version is not marked as stable yet,
  82                        required in non-interactive mode.
  83  --lang=CODE           Set preferred language for CLI output. Defaults to the
  84                        site language if not set. Defaults to 'en' if the lang
  85                        parameter is invalid or if the language pack is not
  86                        installed.
  87  -h, --help            Print out this help
  88  
  89  Example:
  90  \$sudo -u www-data /usr/bin/php admin/cli/upgrade.php
  91  "; //TODO: localize - to be translated later when everything is finished
  92  
  93      echo $help;
  94      die;
  95  }
  96  
  97  if (empty($CFG->version)) {
  98      cli_error(get_string('missingconfigversion', 'debug'));
  99  }
 100  
 101  require("$CFG->dirroot/version.php");       // defines $version, $release, $branch and $maturity
 102  $CFG->target_release = $release;            // used during installation and upgrades
 103  
 104  if ($version < $CFG->version) {
 105      cli_error(get_string('downgradedcore', 'error'));
 106  }
 107  
 108  $oldversion = "$CFG->release ($CFG->version)";
 109  $newversion = "$release ($version)";
 110  
 111  if (!moodle_needs_upgrading()) {
 112      cli_error(get_string('cliupgradenoneed', 'core_admin', $newversion), 0);
 113  }
 114  
 115  // Test environment first.
 116  list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
 117  if (!$envstatus) {
 118      $errors = environment_get_errors($environment_results);
 119      cli_heading(get_string('environment', 'admin'));
 120      foreach ($errors as $error) {
 121          list($info, $report) = $error;
 122          echo "!! $info !!\n$report\n\n";
 123      }
 124      exit(1);
 125  }
 126  
 127  // Test plugin dependencies.
 128  $failed = array();
 129  if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
 130      cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
 131      cli_error(get_string('pluginschecktodo', 'admin'));
 132  }
 133  
 134  if ($interactive) {
 135      $a = new stdClass();
 136      $a->oldversion = $oldversion;
 137      $a->newversion = $newversion;
 138      echo cli_heading(get_string('databasechecking', '', $a)) . PHP_EOL;
 139  }
 140  
 141  // make sure we are upgrading to a stable release or display a warning
 142  if (isset($maturity)) {
 143      if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
 144          $maturitylevel = get_string('maturity'.$maturity, 'admin');
 145  
 146          if ($interactive) {
 147              cli_separator();
 148              cli_heading(get_string('notice'));
 149              echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
 150              echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
 151              cli_separator();
 152          } else {
 153              cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
 154              cli_error(get_string('maturityallowunstable', 'admin'));
 155          }
 156      }
 157  }
 158  
 159  if ($interactive) {
 160      echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n";
 161      $prompt = get_string('cliyesnoprompt', 'admin');
 162      $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
 163      if ($input == get_string('clianswerno', 'admin')) {
 164          exit(1);
 165      }
 166  }
 167  
 168  if ($version > $CFG->version) {
 169      // We purge all of MUC's caches here.
 170      // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
 171      // This ensures a real config object is loaded and the stores will be purged.
 172      // This is the only way we can purge custom caches such as memcache or APC.
 173      // Note: all other calls to caches will still used the disabled API.
 174      cache_helper::purge_all(true);
 175      upgrade_core($version, true);
 176  }
 177  set_config('release', $release);
 178  set_config('branch', $branch);
 179  
 180  // unconditionally upgrade
 181  upgrade_noncore(true);
 182  
 183  // log in as admin - we need doanything permission when applying defaults
 184  \core\session\manager::set_user(get_admin());
 185  
 186  // apply all default settings, just in case do it twice to fill all defaults
 187  admin_apply_default_settings(NULL, false);
 188  admin_apply_default_settings(NULL, false);
 189  
 190  echo get_string('cliupgradefinished', 'admin')."\n";
 191  exit(0); // 0 means success


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1