3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * This script creates config.php file and prepares database.
21 * This script is not intended for beginners!
23 * - su to apache account or sudo before execution
24 * - not compatible with Windows platform
28 * @copyright 2009 Petr Skoda (http://skodak.org)
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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'])) {
38 define('CLI_SCRIPT', true);
39 define('CACHE_DISABLE_ALL', true);
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');
47 // now get cli options
48 $lang = isset($SESSION->lang) ? $SESSION->lang : $CFG->lang;
49 list($options, $unrecognized) = cli_get_params(
51 'non-interactive' => false,
52 'allow-unstable' => false,
55 'verbose-settings' => false
62 if ($options['lang']) {
63 $SESSION->lang = $options['lang'];
66 $interactive = empty($options['non-interactive']);
69 $unrecognized = implode("\n ", $unrecognized);
70 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
73 if ($options['help']) {
75 "Command line Moodle upgrade.
76 Please note you must execute this script with the same uid as apache!
78 Site defaults may be changed via local/defaults.php.
81 --non-interactive No interactive questions or confirmations
82 --allow-unstable Upgrade even if the version is not marked as stable yet,
83 required in non-interactive mode.
84 --lang=CODE Set preferred language for CLI output. Defaults to the
85 site language if not set. Defaults to 'en' if the lang
86 parameter is invalid or if the language pack is not
88 --verbose-settings Show new settings values. By default only the name of
89 new core or plugin settings are displayed. This option
90 outputs the new values as well as the setting name.
91 -h, --help Print out this help
94 \$sudo -u www-data /usr/bin/php admin/cli/upgrade.php
95 "; //TODO: localize - to be translated later when everything is finished
101 if (empty($CFG->version)) {
102 cli_error(get_string('missingconfigversion', 'debug'));
105 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
106 $CFG->target_release = $release; // used during installation and upgrades
108 if ($version < $CFG->version) {
109 cli_error(get_string('downgradedcore', 'error'));
112 $oldversion = "$CFG->release ($CFG->version)";
113 $newversion = "$release ($version)";
115 if (!moodle_needs_upgrading()) {
116 cli_error(get_string('cliupgradenoneed', 'core_admin', $newversion), 0);
119 // Test environment first.
120 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
122 $errors = environment_get_errors($environment_results);
123 cli_heading(get_string('environment', 'admin'));
124 foreach ($errors as $error) {
125 list($info, $report) = $error;
126 echo "!! $info !!\n$report\n\n";
131 // Test plugin dependencies.
133 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
134 cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
135 cli_error(get_string('pluginschecktodo', 'admin'));
139 $a->oldversion = $oldversion;
140 $a->newversion = $newversion;
143 echo cli_heading(get_string('databasechecking', '', $a)) . PHP_EOL;
146 // make sure we are upgrading to a stable release or display a warning
147 if (isset($maturity)) {
148 if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
149 $maturitylevel = get_string('maturity'.$maturity, 'admin');
153 cli_heading(get_string('notice'));
154 echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
155 echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
158 cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
159 cli_error(get_string('maturityallowunstable', 'admin'));
165 echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n";
166 $prompt = get_string('cliyesnoprompt', 'admin');
167 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
168 if ($input == get_string('clianswerno', 'admin')) {
173 if ($version > $CFG->version) {
174 // We purge all of MUC's caches here.
175 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
176 // This ensures a real config object is loaded and the stores will be purged.
177 // This is the only way we can purge custom caches such as memcache or APC.
178 // Note: all other calls to caches will still used the disabled API.
179 cache_helper::purge_all(true);
180 upgrade_core($version, true);
182 set_config('release', $release);
183 set_config('branch', $branch);
185 // unconditionally upgrade
186 upgrade_noncore(true);
188 // log in as admin - we need doanything permission when applying defaults
189 \core\session\manager::set_user(get_admin());
191 // Apply default settings and output those that have changed.
192 cli_heading(get_string('cliupgradedefaultheading', 'admin'));
193 $settingsoutput = admin_apply_default_settings(null, false);
195 foreach ($settingsoutput as $setting => $value) {
197 if ($options['verbose-settings']) {
198 $stringvlaues = array(
200 'defaultsetting' => var_export($value, true) // Expand objects.
202 echo get_string('cliupgradedefaultverbose', 'admin', $stringvlaues) . PHP_EOL;
205 echo get_string('cliupgradedefault', 'admin', $setting) . PHP_EOL;
210 // This needs to happen at the end to ensure it occurs after all caches
211 // have been purged for the last time.
212 // This will build a cached version of the current theme for the user
213 // to immediately start browsing the site.
216 echo get_string('cliupgradefinished', 'admin', $a)."\n";
217 exit(0); // 0 means success