MDL-15249 sample cli scripts - sample implementation only, no localization - ideally...
[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) {
51 $error = implode("\n ", $unrecognized);
52 cli_error("Unrecognized options:\n $error \n. Please use --help option."); // TODO: localize
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
68"; //TODO: localize
69
70 echo $help;
71 die;
72}
73
74if (empty($CFG->version)) {
75 cli_error('missingconfigversion', 'debug');
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
85if ($interactive) {
86 $prompt = "Do you really want to upgrade Moodle at '$CFG->wwwroot' from '$CFG->release' to '$release'?\nPress ctrl+c to cancel."; // TODO: localize
87 cli_input($prompt);
88}
89
90if ($version > $CFG->version) {
91 upgrade_core($version, true);
92}
93set_config('release', $release);
94
95// uncoditionally upgrade
96upgrade_noncore(true);
97
98// log in as admin - we need doanything permission when applying defaults
99$admins = get_admins();
100$admin = reset($admins);
101session_set_user($admin);
102
103// apply all default settings, just in case do it twice to fill all defaults
104admin_apply_default_settings(NULL, true);
105admin_apply_default_settings(NULL, true);
106
107echo "yay!\n";
108
109exit(0); // 0 means success