Commit | Line | Data |
---|---|---|
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: | |
3b596dbf | 23 | * - su to apache account or sudo before execution |
24 | * - not compatible with Windows platform | |
25 | * | |
b7315f35 | 26 | * @package core |
3b596dbf | 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 | ||
28bd3d9a PS |
32 | define('CLI_SCRIPT', true); |
33 | ||
34 | // extra execution prevention - we can not just require config.php here | |
3b596dbf | 35 | if (isset($_SERVER['REMOTE_ADDR'])) { |
28bd3d9a | 36 | exit(1); |
3b596dbf | 37 | } |
38 | ||
5a411e81 | 39 | $help = |
40 | "Command line Moodle installer, creates config.php and initializes database. | |
41 | Please note you must execute this script with the same uid as apache | |
42 | or use chmod/chown after installation. | |
43 | ||
44 | Site defaults may be changed via local/defaults.php. | |
45 | ||
46 | Options: | |
7be67681 DM |
47 | --chmod=OCTAL-MODE Permissions of new directories created within dataroot. |
48 | Default is 2777. You may want to change it to 2770 | |
49 | or 2750 or 750. See chmod man page for details. | |
5a411e81 | 50 | --lang=CODE Installation and default site language. |
51 | --wwwroot=URL Web address for the Moodle site, | |
52 | required in non-interactive mode. | |
53 | --dataroot=DIR Location of the moodle data folder, | |
59ba2bb0 DM |
54 | must not be web accessible. Default is moodledata |
55 | in the parent directory. | |
5a411e81 | 56 | --dbtype=TYPE Database type. Default is mysqli |
57 | --dbhost=HOST Database host. Default is localhost | |
58 | --dbname=NAME Database name. Default is moodle | |
59 | --dbuser=USERNAME Database user. Default is root | |
60 | --dbpass=PASSWORD Database password. Default is blank | |
61 | --dbsocket Use database sockets. Available for some databases only. | |
62 | --prefix=STRING Table prefix for above database tables. Default is mdl_ | |
9632db7a DM |
63 | --fullname=STRING The fullname of the site |
64 | --shortname=STRING The shortname of the site | |
7217676b DM |
65 | --adminuser=USERNAME Username for the moodle admin account. Default is admin |
66 | --adminpass=PASSWORD Password for the moodle admin account, | |
5a411e81 | 67 | required in non-interactive mode. |
68 | --non-interactive No interactive questions, installation fails if any | |
69 | problem encountered. | |
c31d94e8 | 70 | --agree-license Indicates agreement with software license, |
5a411e81 | 71 | required in non-interactive mode. |
3d673fc4 DM |
72 | --allow-unstable Install even if the version is not marked as stable yet, |
73 | required in non-interactive mode. | |
5a411e81 | 74 | -h, --help Print out this help |
75 | ||
28bd3d9a PS |
76 | Example: |
77 | \$sudo -u www-data /usr/bin/php admin/cli/install.php --lang=cs | |
c31d94e8 | 78 | "; //TODO: localize, mark as needed in install - to be translated later when everything is finished |
3b596dbf | 79 | |
3b596dbf | 80 | |
b1d53365 AA |
81 | // distro specific customisation |
82 | $distrolibfile = dirname(dirname(dirname(__FILE__))).'/install/distrolib.php'; | |
83 | $distro = null; | |
84 | if (file_exists($distrolibfile)) { | |
85 | require_once($distrolibfile); | |
86 | if (function_exists('distro_get_config')) { | |
87 | $distro = distro_get_config(); | |
88 | } | |
89 | } | |
90 | ||
3b596dbf | 91 | // Nothing to do if config.php exists |
92 | $configfile = dirname(dirname(dirname(__FILE__))).'/config.php'; | |
93 | if (file_exists($configfile)) { | |
5a411e81 | 94 | require($configfile); |
c31d94e8 | 95 | require_once($CFG->libdir.'/clilib.php'); |
96 | list($options, $unrecognized) = cli_get_params(array('help'=>false), array('h'=>'help')); | |
97 | ||
98 | if ($options['help']) { | |
99 | echo $help; | |
100 | echo "\n\n"; | |
101 | } | |
102 | ||
f433088d PS |
103 | if ($DB->get_manager()->table_exists('config')) { |
104 | cli_error(get_string('clialreadyinstalled', 'install')); | |
105 | } else { | |
106 | cli_error(get_string('clialreadyconfigured', 'install')); | |
107 | } | |
3b596dbf | 108 | } |
109 | ||
5a411e81 | 110 | $olddir = getcwd(); |
111 | ||
ce8df92d | 112 | // change directory so that includes below work properly |
5a411e81 | 113 | chdir(dirname($_SERVER['argv'][0])); |
114 | ||
211c9008 PS |
115 | // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined. |
116 | // This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this. | |
117 | if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) { | |
118 | @date_default_timezone_set(@date_default_timezone_get()); | |
119 | } | |
120 | ||
3b596dbf | 121 | // make sure PHP errors are displayed - helps with diagnosing of problems |
122 | @error_reporting(E_ALL); | |
123 | @ini_set('display_errors', '1'); | |
124 | // we need a lot of memory | |
125 | @ini_set('memory_limit', '128M'); | |
126 | ||
460ebd6c PS |
127 | /** Used by library scripts to check they are being called by Moodle */ |
128 | define('MOODLE_INTERNAL', true); | |
129 | ||
3b596dbf | 130 | // Check that PHP is of a sufficient version |
eab044a0 | 131 | if (version_compare(phpversion(), "5.3.2") < 0) { |
3b596dbf | 132 | $phpversion = phpversion(); |
133 | // do NOT localise - lang strings would not work here and we CAN NOT move it after installib | |
eab044a0 PS |
134 | fwrite(STDERR, "Moodle 2.1 or later requires at least PHP 5.3.2 (currently using version $phpversion).\n"); |
135 | fwrite(STDERR, "Please upgrade your server software or install older Moodle version.\n"); | |
136 | exit(1); | |
3b596dbf | 137 | } |
138 | ||
139 | // set up configuration | |
140 | $CFG = new stdClass(); | |
3a915b06 | 141 | $CFG->lang = 'en'; |
3b5ff37f | 142 | $CFG->dirroot = dirname(dirname(dirname(__FILE__))); |
3b596dbf | 143 | $CFG->libdir = "$CFG->dirroot/lib"; |
144 | $CFG->wwwroot = "http://localhost"; | |
145 | $CFG->httpswwwroot = $CFG->wwwroot; | |
3b596dbf | 146 | $CFG->docroot = 'http://docs.moodle.org'; |
fb344c2d | 147 | $CFG->running_installer = true; |
3a915b06 PS |
148 | $CFG->early_install_lang = true; |
149 | ||
3b596dbf | 150 | $parts = explode('/', str_replace('\\', '/', dirname(dirname(__FILE__)))); |
151 | $CFG->admin = array_pop($parts); | |
152 | ||
3b596dbf | 153 | //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else |
154 | //the problem is that we need specific version of quickforms and hacked excel files :-( | |
155 | ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); | |
156 | ||
157 | require_once($CFG->libdir.'/installlib.php'); | |
158 | require_once($CFG->libdir.'/clilib.php'); | |
159 | require_once($CFG->libdir.'/setuplib.php'); | |
160 | require_once($CFG->libdir.'/textlib.class.php'); | |
161 | require_once($CFG->libdir.'/weblib.php'); | |
162 | require_once($CFG->libdir.'/dmllib.php'); | |
163 | require_once($CFG->libdir.'/moodlelib.php'); | |
164 | require_once($CFG->libdir.'/deprecatedlib.php'); | |
165 | require_once($CFG->libdir.'/adminlib.php'); | |
3b596dbf | 166 | require_once($CFG->libdir.'/componentlib.class.php'); |
468cc572 | 167 | require_once($CFG->dirroot.'/cache/lib.php'); |
3b596dbf | 168 | |
3d673fc4 DM |
169 | require($CFG->dirroot.'/version.php'); |
170 | $CFG->target_release = $release; | |
171 | ||
3b596dbf | 172 | //Database types |
173 | $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'), | |
174 | 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'), | |
175 | 'oci' => moodle_database::get_driver_instance('oci', 'native'), | |
7e60d0d6 EL |
176 | 'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver |
177 | 'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver | |
3b596dbf | 178 | ); |
179 | foreach ($databases as $type=>$database) { | |
180 | if ($database->driver_installed() !== true) { | |
181 | unset($databases[$type]); | |
182 | } | |
183 | } | |
184 | if (empty($databases)) { | |
c31d94e8 | 185 | $defaultdb = ''; |
186 | } else { | |
187 | reset($databases); | |
188 | $defaultdb = key($databases); | |
3b596dbf | 189 | } |
190 | ||
3b596dbf | 191 | // now get cli options |
7217676b DM |
192 | list($options, $unrecognized) = cli_get_params( |
193 | array( | |
b1d53365 | 194 | 'chmod' => isset($distro->directorypermissions) ? sprintf('%04o',$distro->directorypermissions) : '2777', // let distros set dir permissions |
7217676b DM |
195 | 'lang' => $CFG->lang, |
196 | 'wwwroot' => '', | |
b1d53365 AA |
197 | 'dataroot' => empty($distro->dataroot) ? str_replace('\\', '/', dirname(dirname(dirname(dirname(__FILE__)))).'/moodledata'): $distro->dataroot, // initialised later after including libs or by distro |
198 | 'dbtype' => empty($distro->dbtype) ? $defaultdb : $distro->dbtype, // let distro skip dbtype selection | |
199 | 'dbhost' => empty($distro->dbhost) ? 'localhost' : $distro->dbhost, // let distros set dbhost | |
7217676b | 200 | 'dbname' => 'moodle', |
b1d53365 | 201 | 'dbuser' => empty($distro->dbuser) ? 'root' : $distro->dbuser, // let distros set dbuser |
7217676b DM |
202 | 'dbpass' => '', |
203 | 'dbsocket' => false, | |
204 | 'prefix' => 'mdl_', | |
9632db7a DM |
205 | 'fullname' => '', |
206 | 'shortname' => '', | |
7217676b DM |
207 | 'adminuser' => 'admin', |
208 | 'adminpass' => '', | |
209 | 'non-interactive' => false, | |
210 | 'agree-license' => false, | |
3d673fc4 | 211 | 'allow-unstable' => false, |
7217676b DM |
212 | 'help' => false |
213 | ), | |
214 | array( | |
215 | 'h' => 'help' | |
216 | ) | |
217 | ); | |
3b596dbf | 218 | |
219 | $interactive = empty($options['non-interactive']); | |
220 | ||
221 | // set up language | |
222 | $lang = clean_param($options['lang'], PARAM_SAFEDIR); | |
223 | if (file_exists($CFG->dirroot.'/install/lang/'.$lang)) { | |
224 | $CFG->lang = $lang; | |
3b596dbf | 225 | } |
226 | ||
227 | if ($unrecognized) { | |
c31d94e8 | 228 | $unrecognized = implode("\n ", $unrecognized); |
1494616f | 229 | cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); |
3b596dbf | 230 | } |
231 | ||
232 | if ($options['help']) { | |
3b596dbf | 233 | echo $help; |
234 | die; | |
235 | } | |
236 | ||
3b596dbf | 237 | //Print header |
c31d94e8 | 238 | echo get_string('cliinstallheader', 'install', $CFG->target_release)."\n"; |
3b596dbf | 239 | |
240 | //Fist select language | |
241 | if ($interactive) { | |
c31d94e8 | 242 | cli_separator(); |
1f96e907 | 243 | $languages = get_string_manager()->get_list_of_translations(); |
960b6529 PS |
244 | // Do not put the langs into columns because it is not compatible with RTL. |
245 | $langlist = implode("\n", $languages); | |
3a915b06 | 246 | $default = $CFG->lang; |
c31d94e8 | 247 | cli_heading(get_string('availablelangs', 'install')); |
248 | echo $langlist."\n"; | |
3a915b06 | 249 | $prompt = get_string('clitypevaluedefault', 'admin', $CFG->lang); |
3b596dbf | 250 | $error = ''; |
251 | do { | |
252 | echo $error; | |
253 | $input = cli_input($prompt, $default); | |
254 | $input = clean_param($input, PARAM_SAFEDIR); | |
255 | ||
3a915b06 | 256 | if (!file_exists($CFG->dirroot.'/install/lang/'.$input)) { |
c31d94e8 | 257 | $error = get_string('cliincorrectvalueretry', 'admin')."\n"; |
3b596dbf | 258 | } else { |
259 | $error = ''; | |
260 | } | |
261 | } while ($error !== ''); | |
3a915b06 | 262 | $CFG->lang = $input; |
3b596dbf | 263 | } else { |
c31d94e8 | 264 | // already selected and verified |
3b596dbf | 265 | } |
266 | ||
7be67681 DM |
267 | // Set directorypermissions first |
268 | $chmod = octdec(clean_param($options['chmod'], PARAM_INT)); | |
269 | if ($interactive) { | |
270 | cli_separator(); | |
6e175676 | 271 | cli_heading(get_string('datarootpermission', 'install')); |
7be67681 DM |
272 | $prompt = get_string('clitypevaluedefault', 'admin', decoct($chmod)); |
273 | $error = ''; | |
274 | do { | |
275 | echo $error; | |
b7315f35 | 276 | $input = cli_input($prompt, decoct($chmod)); |
7be67681 DM |
277 | $input = octdec(clean_param($input, PARAM_INT)); |
278 | if (empty($input)) { | |
279 | $error = get_string('cliincorrectvalueretry', 'admin')."\n"; | |
280 | } else { | |
281 | $error = ''; | |
282 | } | |
b7315f35 | 283 | } while ($error !== ''); |
7be67681 DM |
284 | $chmod = $input; |
285 | ||
286 | } else { | |
287 | if (empty($chmod)) { | |
288 | $a = (object)array('option' => 'chmod', 'value' => decoct($chmod)); | |
289 | cli_error(get_string('cliincorrectvalueerror', 'admin', $a)); | |
290 | } | |
291 | } | |
292 | $CFG->directorypermissions = $chmod; | |
3b596dbf | 293 | |
294 | //We need wwwroot before we test dataroot | |
295 | $wwwroot = clean_param($options['wwwroot'], PARAM_URL); | |
296 | $wwwroot = trim($wwwroot, '/'); | |
297 | if ($interactive) { | |
c31d94e8 | 298 | cli_separator(); |
299 | cli_heading(get_string('wwwroot', 'install')); | |
3b596dbf | 300 | if (strpos($wwwroot, 'http') === 0) { |
c31d94e8 | 301 | $prompt = get_string('clitypevaluedefault', 'admin', $wwwroot); |
3b596dbf | 302 | } else { |
c31d94e8 | 303 | $wwwroot = null; |
304 | $prompt = get_string('clitypevalue', 'admin'); | |
3b596dbf | 305 | } |
306 | $error = ''; | |
307 | do { | |
308 | echo $error; | |
309 | $input = cli_input($prompt, $wwwroot); | |
310 | $input = clean_param($input, PARAM_URL); | |
311 | $input = trim($input, '/'); | |
312 | if (strpos($input, 'http') !== 0) { | |
c31d94e8 | 313 | $error = get_string('cliincorrectvalueretry', 'admin')."\n"; |
3b596dbf | 314 | } else { |
315 | $error = ''; | |
316 | } | |
317 | } while ($error !== ''); | |
318 | $wwwroot = $input; | |
319 | ||
320 | } else { | |
321 | if (strpos($wwwroot, 'http') !== 0) { | |
c31d94e8 | 322 | $a = (object)array('option'=>'wwwroot', 'value'=>$wwwroot); |
323 | cli_error(get_string('cliincorrectvalueerror', 'admin', $a)); | |
3b596dbf | 324 | } |
325 | } | |
326 | $CFG->wwwroot = $wwwroot; | |
327 | $CFG->httpswwwroot = $CFG->wwwroot; | |
3b596dbf | 328 | |
329 | ||
330 | //We need dataroot before lang download | |
ad72df40 | 331 | $CFG->dataroot = $options['dataroot']; |
3b596dbf | 332 | if ($interactive) { |
c31d94e8 | 333 | cli_separator(); |
3b596dbf | 334 | $i=0; |
335 | while(is_dataroot_insecure()) { | |
336 | $parrent = dirname($CFG->dataroot); | |
337 | $i++; | |
338 | if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) { | |
339 | $CFG->dataroot = ''; //can not find secure location for dataroot | |
340 | break; | |
341 | } | |
342 | $CFG->dataroot = dirname($parrent).'/moodledata'; | |
343 | } | |
c31d94e8 | 344 | cli_heading(get_string('dataroot', 'install')); |
3b596dbf | 345 | $error = ''; |
346 | do { | |
347 | if ($CFG->dataroot !== '') { | |
c31d94e8 | 348 | $prompt = get_string('clitypevaluedefault', 'admin', $CFG->dataroot); |
3b596dbf | 349 | } else { |
c31d94e8 | 350 | $prompt = get_string('clitypevalue', 'admin'); |
3b596dbf | 351 | } |
352 | echo $error; | |
353 | $CFG->dataroot = cli_input($prompt, $CFG->dataroot); | |
354 | if ($CFG->dataroot === '') { | |
c31d94e8 | 355 | $error = get_string('cliincorrectvalueretry', 'admin')."\n"; |
3b596dbf | 356 | } else if (is_dataroot_insecure()) { |
357 | $CFG->dataroot = ''; | |
a18d577d | 358 | $error = get_string('pathsunsecuredataroot', 'install')."\n"; |
3b596dbf | 359 | } else { |
211c9008 | 360 | if (install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) { |
3b596dbf | 361 | $error = ''; |
362 | } else { | |
59ba2bb0 DM |
363 | $a = (object)array('dataroot' => $CFG->dataroot); |
364 | $error = get_string('pathserrcreatedataroot', 'install', $a)."\n"; | |
3b596dbf | 365 | } |
366 | } | |
367 | ||
368 | } while ($error !== ''); | |
369 | ||
370 | } else { | |
371 | if (is_dataroot_insecure()) { | |
59ba2bb0 | 372 | cli_error(get_string('pathsunsecuredataroot', 'install')); |
c31d94e8 | 373 | } |
211c9008 | 374 | if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) { |
59ba2bb0 DM |
375 | $a = (object)array('dataroot' => $CFG->dataroot); |
376 | cli_error(get_string('pathserrcreatedataroot', 'install', $a)); | |
3b596dbf | 377 | } |
378 | } | |
b907d3f5 DM |
379 | $CFG->tempdir = $CFG->dataroot.'/temp'; |
380 | $CFG->cachedir = $CFG->dataroot.'/cache'; | |
3b596dbf | 381 | |
74a4c9a9 DM |
382 | // download required lang packs |
383 | if ($CFG->lang !== 'en') { | |
384 | $installer = new lang_installer($CFG->lang); | |
385 | $results = $installer->run(); | |
386 | foreach ($results as $langcode => $langstatus) { | |
387 | if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) { | |
388 | $a = new stdClass(); | |
389 | $a->url = $installer->lang_pack_url($langcode); | |
390 | $a->dest = $CFG->dataroot.'/lang'; | |
391 | cli_problem(get_string('remotedownloaderror', 'error', $a)); | |
3b596dbf | 392 | } |
393 | } | |
3b596dbf | 394 | } |
3b596dbf | 395 | |
7d73574c | 396 | // switch the string_manager instance to stop using install/lang/ |
3a915b06 | 397 | $CFG->early_install_lang = false; |
877a172d DM |
398 | $CFG->langotherroot = $CFG->dataroot.'/lang'; |
399 | $CFG->langlocalroot = $CFG->dataroot.'/lang'; | |
7d73574c | 400 | get_string_manager(true); |
3a915b06 | 401 | |
3d673fc4 DM |
402 | // make sure we are installing stable release or require a confirmation |
403 | if (isset($maturity)) { | |
404 | if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) { | |
405 | $maturitylevel = get_string('maturity'.$maturity, 'admin'); | |
406 | ||
407 | if ($interactive) { | |
408 | cli_separator(); | |
409 | cli_heading(get_string('notice')); | |
410 | echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL; | |
dc41abd8 | 411 | echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL; |
7119e1a0 | 412 | echo get_string('continue') . PHP_EOL; |
3d673fc4 DM |
413 | $prompt = get_string('cliyesnoprompt', 'admin'); |
414 | $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin'))); | |
415 | if ($input == get_string('clianswerno', 'admin')) { | |
416 | exit(1); | |
417 | } | |
418 | } else { | |
2da7fbc0 DM |
419 | cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel)); |
420 | cli_error(get_string('maturityallowunstable', 'admin')); | |
3d673fc4 DM |
421 | } |
422 | } | |
423 | } | |
424 | ||
3b596dbf | 425 | // ask for db type - show only drivers available |
426 | if ($interactive) { | |
427 | $options['dbtype'] = strtolower($options['dbtype']); | |
c31d94e8 | 428 | cli_separator(); |
429 | cli_heading(get_string('databasetypehead', 'install')); | |
3b596dbf | 430 | foreach ($databases as $type=>$database) { |
c31d94e8 | 431 | echo " $type \n"; |
3b596dbf | 432 | } |
3b596dbf | 433 | if (!empty($databases[$options['dbtype']])) { |
c31d94e8 | 434 | $prompt = get_string('clitypevaluedefault', 'admin', $options['dbtype']); |
3b596dbf | 435 | } else { |
c31d94e8 | 436 | $prompt = get_string('clitypevalue', 'admin'); |
3b596dbf | 437 | } |
3b596dbf | 438 | $CFG->dbtype = cli_input($prompt, $options['dbtype'], array_keys($databases)); |
439 | ||
440 | } else { | |
441 | if (empty($databases[$options['dbtype']])) { | |
c31d94e8 | 442 | $a = (object)array('option'=>'dbtype', 'value'=>$options['dbtype']); |
443 | cli_error(get_string('cliincorrectvalueerror', 'admin', $a)); | |
3b596dbf | 444 | } |
445 | $CFG->dbtype = $options['dbtype']; | |
446 | } | |
447 | $database = $databases[$CFG->dbtype]; | |
448 | ||
449 | ||
450 | // ask for db host | |
451 | if ($interactive) { | |
c31d94e8 | 452 | cli_separator(); |
453 | cli_heading(get_string('databasehost', 'install')); | |
3b596dbf | 454 | if ($options['dbhost'] !== '') { |
c31d94e8 | 455 | $prompt = get_string('clitypevaluedefault', 'admin', $options['dbhost']); |
3b596dbf | 456 | } else { |
c31d94e8 | 457 | $prompt = get_string('clitypevalue', 'admin'); |
3b596dbf | 458 | } |
3b596dbf | 459 | $CFG->dbhost = cli_input($prompt, $options['dbhost']); |
460 | ||
461 | } else { | |
462 | $CFG->dbhost = $options['dbhost']; | |
463 | } | |
464 | ||
465 | // ask for db name | |
466 | if ($interactive) { | |
c31d94e8 | 467 | cli_separator(); |
468 | cli_heading(get_string('databasename', 'install')); | |
3b596dbf | 469 | if ($options['dbname'] !== '') { |
c31d94e8 | 470 | $prompt = get_string('clitypevaluedefault', 'admin', $options['dbname']); |
3b596dbf | 471 | } else { |
c31d94e8 | 472 | $prompt = get_string('clitypevalue', 'admin'); |
3b596dbf | 473 | } |
3b596dbf | 474 | $CFG->dbname = cli_input($prompt, $options['dbname']); |
475 | ||
476 | } else { | |
477 | $CFG->dbname = $options['dbname']; | |
478 | } | |
479 | ||
480 | // ask for db prefix | |
481 | if ($interactive) { | |
c31d94e8 | 482 | cli_separator(); |
483 | cli_heading(get_string('dbprefix', 'install')); | |
3b596dbf | 484 | //TODO: solve somehow the prefix trouble for oci |
485 | if ($options['prefix'] !== '') { | |
c31d94e8 | 486 | $prompt = get_string('clitypevaluedefault', 'admin', $options['prefix']); |
3b596dbf | 487 | } else { |
c31d94e8 | 488 | $prompt = get_string('clitypevalue', 'admin'); |
3b596dbf | 489 | } |
3b596dbf | 490 | $CFG->prefix = cli_input($prompt, $options['prefix']); |
491 | ||
492 | } else { | |
493 | $CFG->prefix = $options['prefix']; | |
494 | } | |
495 | ||
496 | // ask for db user | |
497 | if ($interactive) { | |
c31d94e8 | 498 | cli_separator(); |
499 | cli_heading(get_string('databaseuser', 'install')); | |
3b596dbf | 500 | if ($options['dbuser'] !== '') { |
c31d94e8 | 501 | $prompt = get_string('clitypevaluedefault', 'admin', $options['dbuser']); |
3b596dbf | 502 | } else { |
c31d94e8 | 503 | $prompt = get_string('clitypevalue', 'admin'); |
3b596dbf | 504 | } |
3b596dbf | 505 | $CFG->dbuser = cli_input($prompt, $options['dbuser']); |
506 | ||
507 | } else { | |
508 | $CFG->dbuser = $options['dbuser']; | |
509 | } | |
510 | ||
511 | // ask for db password | |
512 | if ($interactive) { | |
c31d94e8 | 513 | cli_separator(); |
514 | cli_heading(get_string('databasepass', 'install')); | |
3b596dbf | 515 | do { |
516 | if ($options['dbpass'] !== '') { | |
c31d94e8 | 517 | $prompt = get_string('clitypevaluedefault', 'admin', $options['dbpass']); |
3b596dbf | 518 | } else { |
c31d94e8 | 519 | $prompt = get_string('clitypevalue', 'admin'); |
3b596dbf | 520 | } |
521 | ||
522 | $CFG->dbpass = cli_input($prompt, $options['dbpass']); | |
b1d53365 AA |
523 | if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation |
524 | $distro = distro_pre_create_db($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbsocket'=>$options['dbsocket']), $distro); | |
525 | } | |
a0b7200d | 526 | $hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbsocket'=>$options['dbsocket'])); |
3b596dbf | 527 | } while ($hint_database !== ''); |
528 | ||
529 | } else { | |
530 | $CFG->dbpass = $options['dbpass']; | |
a0b7200d | 531 | $hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbsocket'=>$options['dbsocket'])); |
3b596dbf | 532 | if ($hint_database !== '') { |
c31d94e8 | 533 | cli_error(get_string('dbconnectionerror', 'install')); |
3b596dbf | 534 | } |
535 | } | |
536 | ||
9632db7a DM |
537 | // ask for fullname |
538 | if ($interactive) { | |
539 | cli_separator(); | |
540 | cli_heading(get_string('fullsitename', 'moodle')); | |
541 | ||
542 | if ($options['fullname'] !== '') { | |
543 | $prompt = get_string('clitypevaluedefault', 'admin', $options['fullname']); | |
544 | } else { | |
545 | $prompt = get_string('clitypevalue', 'admin'); | |
546 | } | |
547 | ||
548 | do { | |
549 | $options['fullname'] = cli_input($prompt, $options['fullname']); | |
550 | } while (empty($options['fullname'])); | |
551 | } else { | |
552 | if (empty($options['fullname'])) { | |
553 | $a = (object)array('option'=>'fullname', 'value'=>$options['fullname']); | |
554 | cli_error(get_string('cliincorrectvalueerror', 'admin', $a)); | |
555 | } | |
556 | } | |
557 | ||
558 | // ask for shortname | |
559 | if ($interactive) { | |
560 | cli_separator(); | |
561 | cli_heading(get_string('shortsitename', 'moodle')); | |
562 | ||
563 | if ($options['shortname'] !== '') { | |
564 | $prompt = get_string('clitypevaluedefault', 'admin', $options['shortname']); | |
565 | } else { | |
566 | $prompt = get_string('clitypevalue', 'admin'); | |
567 | } | |
568 | ||
569 | do { | |
570 | $options['shortname'] = cli_input($prompt, $options['shortname']); | |
571 | } while (empty($options['shortname'])); | |
572 | } else { | |
573 | if (empty($options['shortname'])) { | |
574 | $a = (object)array('option'=>'shortname', 'value'=>$options['shortname']); | |
575 | cli_error(get_string('cliincorrectvalueerror', 'admin', $a)); | |
576 | } | |
577 | } | |
578 | ||
7217676b DM |
579 | // ask for admin user name |
580 | if ($interactive) { | |
581 | cli_separator(); | |
582 | cli_heading(get_string('cliadminusername', 'install')); | |
583 | if (!empty($options['adminuser'])) { | |
584 | $prompt = get_string('clitypevaluedefault', 'admin', $options['adminuser']); | |
585 | } else { | |
586 | $prompt = get_string('clitypevalue', 'admin'); | |
587 | } | |
588 | do { | |
589 | $options['adminuser'] = cli_input($prompt, $options['adminuser']); | |
590 | } while (empty($options['adminuser']) or $options['adminuser'] === 'guest'); | |
591 | } else { | |
592 | if (empty($options['adminuser']) or $options['adminuser'] === 'guest') { | |
593 | $a = (object)array('option'=>'adminuser', 'value'=>$options['adminuser']); | |
594 | cli_error(get_string('cliincorrectvalueerror', 'admin', $a)); | |
595 | } | |
596 | } | |
597 | ||
3b596dbf | 598 | // ask for admin user password |
599 | if ($interactive) { | |
c31d94e8 | 600 | cli_separator(); |
601 | cli_heading(get_string('cliadminpassword', 'install')); | |
602 | $prompt = get_string('clitypevalue', 'admin'); | |
3b596dbf | 603 | do { |
7217676b DM |
604 | $options['adminpass'] = cli_input($prompt); |
605 | } while (empty($options['adminpass']) or $options['adminpass'] === 'admin'); | |
3b596dbf | 606 | } else { |
7217676b DM |
607 | if (empty($options['adminpass']) or $options['adminpass'] === 'admin') { |
608 | $a = (object)array('option'=>'adminpass', 'value'=>$options['adminpass']); | |
c31d94e8 | 609 | cli_error(get_string('cliincorrectvalueerror', 'admin', $a)); |
3b596dbf | 610 | } |
611 | } | |
612 | ||
613 | if ($interactive) { | |
c31d94e8 | 614 | if (!$options['agree-license']) { |
fb344c2d | 615 | cli_separator(); |
616 | cli_heading(get_string('copyrightnotice')); | |
05736058 | 617 | echo "Moodle - Modular Object-Oriented Dynamic Learning Environment\n"; |
852a1f66 | 618 | echo get_string('gpl3')."\n\n"; |
fb344c2d | 619 | echo get_string('doyouagree')."\n"; |
b8523467 | 620 | $prompt = get_string('cliyesnoprompt', 'admin'); |
05736058 | 621 | $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin'))); |
b8523467 | 622 | if ($input == get_string('clianswerno', 'admin')) { |
623 | exit(1); | |
624 | } | |
3b596dbf | 625 | } |
626 | } else { | |
c31d94e8 | 627 | if (!$options['agree-license']) { |
b8523467 | 628 | cli_error(get_string('climustagreelicense', 'install')); |
3b596dbf | 629 | } |
630 | } | |
631 | ||
632 | // Finally we have all info needed for config.php | |
633 | $configphp = install_generate_configphp($database, $CFG); | |
634 | umask(0137); | |
635 | if (($fh = fopen($configfile, 'w')) !== false) { | |
636 | fwrite($fh, $configphp); | |
637 | fclose($fh); | |
638 | } | |
639 | ||
640 | if (!file_exists($configfile)) { | |
641 | cli_error('Can not create config file.'); | |
3b596dbf | 642 | } |
643 | ||
f433088d PS |
644 | // remember selected language |
645 | $installlang = $CFG->lang; | |
646 | // return back to original dir before executing setup.php which changes the dir again | |
647 | chdir($olddir); | |
648 | // We have config.php, it is a real php script from now on :-) | |
649 | require($configfile); | |
650 | ||
651 | // use selected language | |
652 | $CFG->lang = $installlang; | |
653 | $SESSION->lang = $CFG->lang; | |
654 | ||
655 | require("$CFG->dirroot/version.php"); | |
656 | ||
faadd326 | 657 | // Test environment first. |
f433088d | 658 | require_once($CFG->libdir . '/environmentlib.php'); |
faadd326 TH |
659 | list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE); |
660 | if (!$envstatus) { | |
661 | $errors = environment_get_errors($environment_results); | |
662 | cli_heading(get_string('environment', 'admin')); | |
663 | foreach ($errors as $error) { | |
664 | list($info, $report) = $error; | |
665 | echo "!! $info !!\n$report\n\n"; | |
666 | } | |
667 | exit(1); | |
668 | } | |
669 | ||
777781d1 | 670 | // Test plugin dependencies. |
f433088d | 671 | require_once($CFG->libdir . '/pluginlib.php'); |
f1753a5b DM |
672 | $failed = array(); |
673 | if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) { | |
674 | cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed))))); | |
faadd326 TH |
675 | cli_error(get_string('pluginschecktodo', 'admin')); |
676 | } | |
677 | ||
b7315f35 | 678 | install_cli_database($options, $interactive); |
3b596dbf | 679 | |
b8523467 | 680 | echo get_string('cliinstallfinished', 'install')."\n"; |
681 | exit(0); // 0 means success |