f41263ed9e5e62d9fc5c54fcdb8f912112398577
[moodle.git] / admin / cli / install_database.php
1 <?php
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/>.
18 /**
19  * This installs Moodle into empty database, config.php must already exist.
20  *
21  * This script is intended for advanced usage such as in Debian packages.
22  * - sudo to www-data (apache account) before
23  * - not compatible with Windows platform
24  *
25  * @package    core
26  * @subpackage cli
27  * @copyright  2010 Petr Skoda (http://skodak.org)
28  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29  */
31 define('CLI_SCRIPT', true);
32 define('CACHE_DISABLE_ALL', true);
34 // extra execution prevention - we can not just require config.php here
35 if (isset($_SERVER['REMOTE_ADDR'])) {
36     exit(1);
37 }
39 // Force OPcache reset if used, we do not want any stale caches
40 // when preparing test environment.
41 if (function_exists('opcache_reset')) {
42     opcache_reset();
43 }
45 $help =
46 "Advanced command line Moodle database installer.
47 Please note you must execute this script with the same uid as apache.
49 Site defaults may be changed via local/defaults.php.
51 Options:
52 --lang=CODE           Installation and default site language. Default is en.
53 --adminuser=USERNAME  Username for the moodle admin account. Default is admin.
54 --adminpass=PASSWORD  Password for the moodle admin account.
55 --adminemail=STRING   Email address for the moodle admin account.
56 --agree-license       Indicates agreement with software license.
57 --fullname=STRING     Name of the site
58 --shortname=STRING    Name of the site
59 -h, --help            Print out this help
61 Example:
62 \$sudo -u www-data /usr/bin/php admin/cli/install_database.php --lang=cs --adminpass=soMePass123 --agree-license
63 ";
65 // Check that PHP is of a sufficient version
66 if (version_compare(phpversion(), "5.6.5") < 0) {
67     $phpversion = phpversion();
68     // do NOT localise - lang strings would not work here and we CAN NOT move it after installib
69     fwrite(STDERR, "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).\n");
70     fwrite(STDERR, "Please upgrade your server software or install older Moodle version.\n");
71     exit(1);
72 }
74 // Nothing to do if config.php does not exist
75 $configfile = __DIR__.'/../../config.php';
76 if (!file_exists($configfile)) {
77     fwrite(STDERR, 'config.php does not exist, can not continue'); // do not localize
78     fwrite(STDERR, "\n");
79     exit(1);
80 }
82 // Include necessary libs
83 require($configfile);
85 require_once($CFG->libdir.'/clilib.php');
86 require_once($CFG->libdir.'/installlib.php');
87 require_once($CFG->libdir.'/adminlib.php');
88 require_once($CFG->libdir.'/componentlib.class.php');
90 // make sure no tables are installed yet
91 if ($DB->get_tables() ) {
92     cli_error(get_string('clitablesexist', 'install'));
93 }
95 $CFG->early_install_lang = true;
96 get_string_manager(true);
98 raise_memory_limit(MEMORY_EXTRA);
100 // now get cli options
101 list($options, $unrecognized) = cli_get_params(
102     array(
103         'lang'              => 'en',
104         'adminuser'         => 'admin',
105         'adminpass'         => '',
106         'adminemail'        => '',
107         'fullname'          => '',
108         'shortname'         => '',
109         'agree-license'     => false,
110         'help'              => false
111     ),
112     array(
113         'h' => 'help'
114     )
115 );
118 if ($options['help']) {
119     echo $help;
120     die;
123 if (!$options['agree-license']) {
124     cli_error('You have to agree to the license. --help prints out the help'); // TODO: localize
127 if ($options['adminpass'] === true or $options['adminpass'] === '') {
128     cli_error('You have to specify admin password. --help prints out the help'); // TODO: localize
131 // Validate that the address provided was an e-mail address.
132 if (!empty($options['adminemail']) && !validate_email($options['adminemail'])) {
133     $a = (object) array('option' => 'adminemail', 'value' => $options['adminemail']);
134     cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
137 $options['lang'] = clean_param($options['lang'], PARAM_SAFEDIR);
138 if (!file_exists($CFG->dirroot.'/install/lang/'.$options['lang'])) {
139     $options['lang'] = 'en';
141 $CFG->lang = $options['lang'];
143 // download required lang packs
144 if ($CFG->lang !== 'en') {
145     make_upload_directory('lang');
146     $installer = new lang_installer($CFG->lang);
147     $results = $installer->run();
148     foreach ($results as $langcode => $langstatus) {
149         if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
150             $a       = new stdClass();
151             $a->url  = $installer->lang_pack_url($langcode);
152             $a->dest = $CFG->dataroot.'/lang';
153             cli_problem(get_string('remotedownloaderror', 'error', $a));
154         }
155     }
158 // switch the string_manager instance to stop using install/lang/
159 $CFG->early_install_lang = false;
160 get_string_manager(true);
162 require("$CFG->dirroot/version.php");
164 // Test environment first.
165 require_once($CFG->libdir . '/environmentlib.php');
166 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
167 if (!$envstatus) {
168     $errors = environment_get_errors($environment_results);
169     cli_heading(get_string('environment', 'admin'));
170     foreach ($errors as $error) {
171         list($info, $report) = $error;
172         echo "!! $info !!\n$report\n\n";
173     }
174     exit(1);
177 // Test plugin dependencies.
178 $failed = array();
179 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
180     cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
181     cli_error(get_string('pluginschecktodo', 'admin'));
184 install_cli_database($options, true);
186 echo get_string('cliinstallfinished', 'install')."\n";
187 exit(0); // 0 means success