MDL-19350 upgrade review - adding missing savepoints to main upgrade.
[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 *
27 * @package moodlecore
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
33if (isset($_SERVER['REMOTE_ADDR'])) {
34 error_log("admin/cli/upgrade.php can not be called from web server!");
35 exit;
36}
37
38require_once dirname(dirname(dirname(__FILE__))).'/config.php';
39require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
40require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
41require_once($CFG->libdir.'/clilib.php'); // cli only functions
42
43
44// now get cli options
45list($options, $unrecognized) = cli_get_params(array('non-interactive'=>false, 'help'=>false),
46 array('h'=>'help'));
47
48$interactive = empty($options['non-interactive']);
49
50if ($unrecognized) {
6b997b07 51 $unrecognized = implode("\n ", $unrecognized);
1494616f 52 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
3b596dbf 53}
54
55if ($options['help']) {
56
57$help =
58"Command line Moodle upgrade.
59Please note you must execute this script with the same uid as apache!
60
61Site defaults may be changed via local/defaults.php.
62
63Options:
64--non-interactive No interactive questions or confirmations
65-h, --help Print out this help
66
67Example: \$sudo -u wwwrun /usr/bin/php admin/cli/upgrade.php
71d54993 68"; //TODO: localize - to be translated later when everything is finished
3b596dbf 69
70 echo $help;
71 die;
72}
73
74if (empty($CFG->version)) {
6b997b07 75 cli_error(get_string('missingconfigversion', 'debug'));
3b596dbf 76}
77
78require("$CFG->dirroot/version.php"); // defines $version and $release
79$CFG->target_release = $release; // used during installation and upgrades
80
81if ($version < $CFG->version) {
82 cli_error('The code you are using is OLDER than the version that made these databases!'); // TODO: localize
83}
84
6b997b07 85$newversion = "$release ($version)";
86
3b596dbf 87if ($interactive) {
6b997b07 88 echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n";
89 $prompt = get_string('cliyesnoprompt', 'admin');
90 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
91 if ($input == get_string('clianswerno', 'admin')) {
92 exit(1);
93 }
3b596dbf 94}
95
96if ($version > $CFG->version) {
97 upgrade_core($version, true);
98}
99set_config('release', $release);
100
101// uncoditionally upgrade
102upgrade_noncore(true);
103
104// log in as admin - we need doanything permission when applying defaults
105$admins = get_admins();
106$admin = reset($admins);
107session_set_user($admin);
108
109// apply all default settings, just in case do it twice to fill all defaults
110admin_apply_default_settings(NULL, true);
111admin_apply_default_settings(NULL, true);
112
6b997b07 113echo get_string('cliupgradefinished', 'admin')."\n";
114exit(0); // 0 means success