MDL-31832 CLI upgrade checks if the upgrade is actually needed
[moodle.git] / admin / cli / upgrade.php
blob2b9fae766a2112fbfa66f76f363c6a22ff006af0
1 <?php
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.
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/>.
18 /**
19 * This script creates config.php file and prepares database.
21 * This script is not intended for beginners!
22 * Potential problems:
23 * - environment check is not present yet
24 * - su to apache account or sudo before execution
25 * - not compatible with Windows platform
27 * @package core
28 * @subpackage cli
29 * @copyright 2009 Petr Skoda (http://skodak.org)
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 define('CLI_SCRIPT', true);
35 require(dirname(dirname(dirname(__FILE__))).'/config.php');
36 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
37 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
38 require_once($CFG->libdir.'/clilib.php'); // cli only functions
39 require_once($CFG->libdir.'/environmentlib.php');
42 // now get cli options
43 list($options, $unrecognized) = cli_get_params(
44 array(
45 'non-interactive' => false,
46 'allow-unstable' => false,
47 'help' => false
49 array(
50 'h' => 'help'
54 $interactive = empty($options['non-interactive']);
56 if ($unrecognized) {
57 $unrecognized = implode("\n ", $unrecognized);
58 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
61 if ($options['help']) {
62 $help =
63 "Command line Moodle upgrade.
64 Please note you must execute this script with the same uid as apache!
66 Site defaults may be changed via local/defaults.php.
68 Options:
69 --non-interactive No interactive questions or confirmations
70 --allow-unstable Upgrade even if the version is not marked as stable yet,
71 required in non-interactive mode.
72 -h, --help Print out this help
74 Example:
75 \$sudo -u www-data /usr/bin/php admin/cli/upgrade.php
76 "; //TODO: localize - to be translated later when everything is finished
78 echo $help;
79 die;
82 if (empty($CFG->version)) {
83 cli_error(get_string('missingconfigversion', 'debug'));
86 require("$CFG->dirroot/version.php"); // defines $version, $release and $maturity
87 $CFG->target_release = $release; // used during installation and upgrades
89 if ($version < $CFG->version) {
90 cli_error(get_string('downgradedcore', 'error'));
93 $oldversion = "$CFG->release ($CFG->version)";
94 $newversion = "$release ($version)";
96 if (!moodle_needs_upgrading()) {
97 cli_error(get_string('cliupgradenoneed', 'core_admin', $newversion), 63);
100 // test environment first
101 if (!check_moodle_environment(normalize_version($release), $environment_results, false, ENV_SELECT_RELEASE)) {
102 $errors = environment_get_errors($environment_results);
103 cli_heading(get_string('environment', 'admin'));
104 foreach ($errors as $error) {
105 list($info, $report) = $error;
106 echo "!! $info !!\n$report\n\n";
108 exit(1);
111 if ($interactive) {
112 $a = new stdClass();
113 $a->oldversion = $oldversion;
114 $a->newversion = $newversion;
115 echo cli_heading(get_string('databasechecking', '', $a)) . PHP_EOL;
118 // make sure we are upgrading to a stable release or display a warning
119 if (isset($maturity)) {
120 if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
121 $maturitylevel = get_string('maturity'.$maturity, 'admin');
123 if ($interactive) {
124 cli_separator();
125 cli_heading(get_string('notice'));
126 echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
127 echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
128 cli_separator();
129 } else {
130 cli_error(get_string('maturitycorewarning', 'admin', $maturitylevel));
135 if ($interactive) {
136 echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n";
137 $prompt = get_string('cliyesnoprompt', 'admin');
138 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
139 if ($input == get_string('clianswerno', 'admin')) {
140 exit(1);
144 if ($version > $CFG->version) {
145 upgrade_core($version, true);
147 set_config('release', $release);
149 // unconditionally upgrade
150 upgrade_noncore(true);
152 // log in as admin - we need doanything permission when applying defaults
153 $admins = get_admins();
154 $admin = reset($admins);
155 session_set_user($admin);
157 // apply all default settings, just in case do it twice to fill all defaults
158 admin_apply_default_settings(NULL, false);
159 admin_apply_default_settings(NULL, false);
161 echo get_string('cliupgradefinished', 'admin')."\n";
162 exit(0); // 0 means success