MDL-26609 Provide a link to a docs page for more help with Moodle versions and their...
[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:
23 * - environment check is not present yet
24 * - su to apache account or sudo before execution
25 * - not compatible with Windows platform
26 *
30f58159 27 * @package core
3b596dbf 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
31 */
32
28bd3d9a 33define('CLI_SCRIPT', true);
3b596dbf 34
28bd3d9a 35require(dirname(dirname(dirname(__FILE__))).'/config.php');
16ae0853 36require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
37require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
38require_once($CFG->libdir.'/clilib.php'); // cli only functions
39require_once($CFG->libdir.'/environmentlib.php');
3b596dbf 40
41
42// now get cli options
3d673fc4
DM
43list($options, $unrecognized) = cli_get_params(
44 array(
45 'non-interactive' => false,
46 'allow-unstable' => false,
47 'help' => false
48 ),
49 array(
50 'h' => 'help'
51 )
52);
3b596dbf 53
54$interactive = empty($options['non-interactive']);
55
56if ($unrecognized) {
6b997b07 57 $unrecognized = implode("\n ", $unrecognized);
1494616f 58 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
3b596dbf 59}
60
61if ($options['help']) {
28bd3d9a 62 $help =
3b596dbf 63"Command line Moodle upgrade.
64Please note you must execute this script with the same uid as apache!
65
66Site defaults may be changed via local/defaults.php.
67
68Options:
69--non-interactive No interactive questions or confirmations
3d673fc4
DM
70--allow-unstable Upgrade even if the version is not marked as stable yet,
71 required in non-interactive mode.
3b596dbf 72-h, --help Print out this help
73
28bd3d9a
PS
74Example:
75\$sudo -u www-data /usr/bin/php admin/cli/upgrade.php
71d54993 76"; //TODO: localize - to be translated later when everything is finished
3b596dbf 77
78 echo $help;
79 die;
80}
81
82if (empty($CFG->version)) {
6b997b07 83 cli_error(get_string('missingconfigversion', 'debug'));
3b596dbf 84}
85
3d673fc4 86require("$CFG->dirroot/version.php"); // defines $version, $release and $maturity
3b596dbf 87$CFG->target_release = $release; // used during installation and upgrades
88
89if ($version < $CFG->version) {
80380bd7 90 cli_error(get_string('downgradedcore', 'error'));
3b596dbf 91}
92
6b997b07 93$newversion = "$release ($version)";
94
16ae0853 95// test environment first
96if (!check_moodle_environment($version, $environment_results, false, ENV_SELECT_RELEASE)) {
97 $errors = environment_get_errors($environment_results);
98 cli_heading(get_string('environment', 'admin'));
99 foreach ($errors as $error) {
100 list($info, $report) = $error;
101 echo "!! $info !!\n$report\n\n";
102 }
103 exit(1);
104}
105
3d673fc4
DM
106// make sure we are upgrading to a stable release or display a warning
107if (isset($maturity)) {
108 if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
109 $maturitylevel = get_string('maturity'.$maturity, 'admin');
110
111 if ($interactive) {
112 cli_separator();
113 cli_heading(get_string('notice'));
114 echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
dc41abd8 115 echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
3d673fc4
DM
116 cli_separator();
117 } else {
118 cli_error(get_string('maturitycorewarning', 'admin', $maturitylevel));
119 }
120 }
121}
122
3b596dbf 123if ($interactive) {
6b997b07 124 echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n";
125 $prompt = get_string('cliyesnoprompt', 'admin');
126 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
127 if ($input == get_string('clianswerno', 'admin')) {
128 exit(1);
129 }
3b596dbf 130}
131
132if ($version > $CFG->version) {
133 upgrade_core($version, true);
134}
135set_config('release', $release);
136
deba811a 137// unconditionally upgrade
3b596dbf 138upgrade_noncore(true);
139
140// log in as admin - we need doanything permission when applying defaults
141$admins = get_admins();
142$admin = reset($admins);
143session_set_user($admin);
144
145// apply all default settings, just in case do it twice to fill all defaults
054d3924
EL
146admin_apply_default_settings(NULL, false);
147admin_apply_default_settings(NULL, false);
3b596dbf 148
6b997b07 149echo get_string('cliupgradefinished', 'admin')."\n";
150exit(0); // 0 means success