MDL-53260 upgrade: Option to display CLI upgrade messages in English
[moodle.git] / admin / cli / upgrade.php
CommitLineData
3b596dbf 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:
3b596dbf 23 * - su to apache account or sudo before execution
24 * - not compatible with Windows platform
25 *
30f58159 26 * @package core
3b596dbf 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
c05a5099
PS
32// Force OPcache reset if used, we do not want any stale caches
33// when detecting if upgrade necessary or when running upgrade.
34if (function_exists('opcache_reset') and !isset($_SERVER['REMOTE_ADDR'])) {
35 opcache_reset();
36}
37
28bd3d9a 38define('CLI_SCRIPT', true);
e2e35e71 39define('CACHE_DISABLE_ALL', true);
3b596dbf 40
28bd3d9a 41require(dirname(dirname(dirname(__FILE__))).'/config.php');
16ae0853 42require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
43require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
44require_once($CFG->libdir.'/clilib.php'); // cli only functions
45require_once($CFG->libdir.'/environmentlib.php');
3b596dbf 46
47// now get cli options
3d673fc4
DM
48list($options, $unrecognized) = cli_get_params(
49 array(
50 'non-interactive' => false,
51 'allow-unstable' => false,
14aaa2d7
NK
52 'help' => false,
53 'lang' => 'en'
3d673fc4
DM
54 ),
55 array(
56 'h' => 'help'
57 )
58);
3b596dbf 59
14aaa2d7
NK
60global $SESSION;
61if ($options['lang']) {
62 $SESSION->lang = $options['lang'];
63} else {
64 $SESSION->lang = 'en';
65}
66
3b596dbf 67$interactive = empty($options['non-interactive']);
68
69if ($unrecognized) {
6b997b07 70 $unrecognized = implode("\n ", $unrecognized);
1494616f 71 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
3b596dbf 72}
73
74if ($options['help']) {
28bd3d9a 75 $help =
3b596dbf 76"Command line Moodle upgrade.
77Please note you must execute this script with the same uid as apache!
78
79Site defaults may be changed via local/defaults.php.
80
81Options:
82--non-interactive No interactive questions or confirmations
3d673fc4
DM
83--allow-unstable Upgrade even if the version is not marked as stable yet,
84 required in non-interactive mode.
14aaa2d7 85--lang=CODE Set preferred language for CLI output, during upgrade process (default:en).
3b596dbf 86-h, --help Print out this help
87
28bd3d9a
PS
88Example:
89\$sudo -u www-data /usr/bin/php admin/cli/upgrade.php
71d54993 90"; //TODO: localize - to be translated later when everything is finished
3b596dbf 91
92 echo $help;
93 die;
94}
95
96if (empty($CFG->version)) {
6b997b07 97 cli_error(get_string('missingconfigversion', 'debug'));
3b596dbf 98}
99
ed01233a 100require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
3b596dbf 101$CFG->target_release = $release; // used during installation and upgrades
102
103if ($version < $CFG->version) {
80380bd7 104 cli_error(get_string('downgradedcore', 'error'));
3b596dbf 105}
106
1a4d6a5b 107$oldversion = "$CFG->release ($CFG->version)";
6b997b07 108$newversion = "$release ($version)";
109
91c038f9 110if (!moodle_needs_upgrading()) {
daf714ef 111 cli_error(get_string('cliupgradenoneed', 'core_admin', $newversion), 0);
91c038f9
DM
112}
113
faadd326 114// Test environment first.
cc359566
TH
115list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
116if (!$envstatus) {
16ae0853 117 $errors = environment_get_errors($environment_results);
118 cli_heading(get_string('environment', 'admin'));
119 foreach ($errors as $error) {
120 list($info, $report) = $error;
121 echo "!! $info !!\n$report\n\n";
122 }
123 exit(1);
124}
125
777781d1 126// Test plugin dependencies.
f1753a5b 127$failed = array();
e87214bd 128if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
f1753a5b 129 cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
faadd326
TH
130 cli_error(get_string('pluginschecktodo', 'admin'));
131}
132
1a4d6a5b
DM
133if ($interactive) {
134 $a = new stdClass();
135 $a->oldversion = $oldversion;
136 $a->newversion = $newversion;
137 echo cli_heading(get_string('databasechecking', '', $a)) . PHP_EOL;
138}
139
3d673fc4
DM
140// make sure we are upgrading to a stable release or display a warning
141if (isset($maturity)) {
142 if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
143 $maturitylevel = get_string('maturity'.$maturity, 'admin');
144
145 if ($interactive) {
146 cli_separator();
147 cli_heading(get_string('notice'));
148 echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
dc41abd8 149 echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
3d673fc4
DM
150 cli_separator();
151 } else {
2da7fbc0
DM
152 cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
153 cli_error(get_string('maturityallowunstable', 'admin'));
3d673fc4
DM
154 }
155 }
156}
157
3b596dbf 158if ($interactive) {
6b997b07 159 echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n";
160 $prompt = get_string('cliyesnoprompt', 'admin');
161 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
162 if ($input == get_string('clianswerno', 'admin')) {
163 exit(1);
164 }
3b596dbf 165}
166
167if ($version > $CFG->version) {
b0dd08dd
SH
168 // We purge all of MUC's caches here.
169 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
170 // This ensures a real config object is loaded and the stores will be purged.
171 // This is the only way we can purge custom caches such as memcache or APC.
172 // Note: all other calls to caches will still used the disabled API.
173 cache_helper::purge_all(true);
3b596dbf 174 upgrade_core($version, true);
175}
176set_config('release', $release);
ed01233a 177set_config('branch', $branch);
3b596dbf 178
deba811a 179// unconditionally upgrade
3b596dbf 180upgrade_noncore(true);
181
182// log in as admin - we need doanything permission when applying defaults
d79d5ac2 183\core\session\manager::set_user(get_admin());
3b596dbf 184
185// apply all default settings, just in case do it twice to fill all defaults
054d3924
EL
186admin_apply_default_settings(NULL, false);
187admin_apply_default_settings(NULL, false);
3b596dbf 188
6b997b07 189echo get_string('cliupgradefinished', 'admin')."\n";
190exit(0); // 0 means success