source: moodle/trunk/fuentes/admin/cli/upgrade.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

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