MDL-65453 core: fix behaviour of --skip-database option on install
[moodle.git] / admin / cli / install.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:
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
32define('CLI_SCRIPT', true);
33
34// extra execution prevention - we can not just require config.php here
3b596dbf 35if (isset($_SERVER['REMOTE_ADDR'])) {
28bd3d9a 36 exit(1);
3b596dbf 37}
38
c05a5099
PS
39// Force OPcache reset if used, we do not want any stale caches
40// when preparing test environment.
41if (function_exists('opcache_reset')) {
42 opcache_reset();
43}
44
5a411e81 45$help =
46"Command line Moodle installer, creates config.php and initializes database.
47Please note you must execute this script with the same uid as apache
48or use chmod/chown after installation.
49
50Site defaults may be changed via local/defaults.php.
51
52Options:
7be67681
DM
53--chmod=OCTAL-MODE Permissions of new directories created within dataroot.
54 Default is 2777. You may want to change it to 2770
55 or 2750 or 750. See chmod man page for details.
5a411e81 56--lang=CODE Installation and default site language.
57--wwwroot=URL Web address for the Moodle site,
58 required in non-interactive mode.
59--dataroot=DIR Location of the moodle data folder,
59ba2bb0
DM
60 must not be web accessible. Default is moodledata
61 in the parent directory.
5a411e81 62--dbtype=TYPE Database type. Default is mysqli
63--dbhost=HOST Database host. Default is localhost
64--dbname=NAME Database name. Default is moodle
65--dbuser=USERNAME Database user. Default is root
66--dbpass=PASSWORD Database password. Default is blank
1826d0ea
PS
67--dbport=NUMBER Use database port.
68--dbsocket=PATH Use database socket, 1 means default. Available for some databases only.
5a411e81 69--prefix=STRING Table prefix for above database tables. Default is mdl_
9632db7a
DM
70--fullname=STRING The fullname of the site
71--shortname=STRING The shortname of the site
65903c76 72--summary=STRING The summary to be displayed on the front page
7217676b
DM
73--adminuser=USERNAME Username for the moodle admin account. Default is admin
74--adminpass=PASSWORD Password for the moodle admin account,
5a411e81 75 required in non-interactive mode.
da992717 76--adminemail=STRING Email address for the moodle admin account.
6ef02f84 77--upgradekey=STRING The upgrade key to be set in the config.php, leave empty to not set it.
5a411e81 78--non-interactive No interactive questions, installation fails if any
79 problem encountered.
c31d94e8 80--agree-license Indicates agreement with software license,
5a411e81 81 required in non-interactive mode.
3d673fc4
DM
82--allow-unstable Install even if the version is not marked as stable yet,
83 required in non-interactive mode.
096fd596 84--skip-database Stop the installation before installing the database.
5a411e81 85-h, --help Print out this help
86
28bd3d9a
PS
87Example:
88\$sudo -u www-data /usr/bin/php admin/cli/install.php --lang=cs
c31d94e8 89"; //TODO: localize, mark as needed in install - to be translated later when everything is finished
3b596dbf 90
3b596dbf 91
b1d53365 92// distro specific customisation
1fcf0ca8 93$distrolibfile = __DIR__.'/../../install/distrolib.php';
b1d53365
AA
94$distro = null;
95if (file_exists($distrolibfile)) {
96 require_once($distrolibfile);
97 if (function_exists('distro_get_config')) {
98 $distro = distro_get_config();
99 }
100}
101
3b596dbf 102// Nothing to do if config.php exists
1fcf0ca8 103$configfile = __DIR__.'/../../config.php';
3b596dbf 104if (file_exists($configfile)) {
5a411e81 105 require($configfile);
c31d94e8 106 require_once($CFG->libdir.'/clilib.php');
107 list($options, $unrecognized) = cli_get_params(array('help'=>false), array('h'=>'help'));
108
109 if ($options['help']) {
110 echo $help;
111 echo "\n\n";
112 }
113
f433088d
PS
114 if ($DB->get_manager()->table_exists('config')) {
115 cli_error(get_string('clialreadyinstalled', 'install'));
116 } else {
117 cli_error(get_string('clialreadyconfigured', 'install'));
118 }
3b596dbf 119}
120
5a411e81 121$olddir = getcwd();
122
ce8df92d 123// change directory so that includes below work properly
5a411e81 124chdir(dirname($_SERVER['argv'][0]));
125
211c9008 126// Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
d6e7a63d
PS
127if (!function_exists('date_default_timezone_set') or !function_exists('date_default_timezone_get')) {
128 fwrite(STDERR, "Timezone functions are not available.\n");
129 exit(1);
211c9008 130}
d6e7a63d 131date_default_timezone_set(@date_default_timezone_get());
211c9008 132
3b596dbf 133// make sure PHP errors are displayed - helps with diagnosing of problems
134@error_reporting(E_ALL);
135@ini_set('display_errors', '1');
136// we need a lot of memory
137@ini_set('memory_limit', '128M');
138
460ebd6c
PS
139/** Used by library scripts to check they are being called by Moodle */
140define('MOODLE_INTERNAL', true);
141
e2e35e71 142// Disables all caching.
94ef67cf
SH
143define('CACHE_DISABLE_ALL', true);
144
1abf2f6a
PS
145define('PHPUNIT_TEST', false);
146
d7245e34
PS
147define('IGNORE_COMPONENT_CACHE', true);
148
64e57937
DP
149// Check that PHP is of a sufficient version as soon as possible.
150require_once(__DIR__.'/../../lib/phpminimumversionlib.php');
151moodle_require_minimum_php_version();
3b596dbf 152
153// set up configuration
1a4621d8 154global $CFG;
3b596dbf 155$CFG = new stdClass();
3a915b06 156$CFG->lang = 'en';
1fcf0ca8 157$CFG->dirroot = dirname(dirname(__DIR__));
3b596dbf 158$CFG->libdir = "$CFG->dirroot/lib";
159$CFG->wwwroot = "http://localhost";
469d4b7f 160$CFG->httpswwwroot = $CFG->wwwroot;
3b596dbf 161$CFG->docroot = 'http://docs.moodle.org';
fb344c2d 162$CFG->running_installer = true;
3a915b06 163$CFG->early_install_lang = true;
cf4cef7d 164$CFG->ostype = (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) ? 'WINDOWS' : 'UNIX';
1826d0ea 165$CFG->dboptions = array();
96f81ea3
PS
166$CFG->debug = (E_ALL | E_STRICT);
167$CFG->debugdisplay = true;
168$CFG->debugdeveloper = true;
3a915b06 169
1fcf0ca8 170$parts = explode('/', str_replace('\\', '/', dirname(__DIR__)));
3b596dbf 171$CFG->admin = array_pop($parts);
172
3b596dbf 173//point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
174//the problem is that we need specific version of quickforms and hacked excel files :-(
175ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
176
1abf2f6a 177require_once($CFG->libdir.'/classes/component.php');
d534708f 178require_once($CFG->libdir.'/classes/text.php');
99a9d8d9
PS
179require_once($CFG->libdir.'/classes/string_manager.php');
180require_once($CFG->libdir.'/classes/string_manager_install.php');
181require_once($CFG->libdir.'/classes/string_manager_standard.php');
3b596dbf 182require_once($CFG->libdir.'/installlib.php');
183require_once($CFG->libdir.'/clilib.php');
184require_once($CFG->libdir.'/setuplib.php');
3b596dbf 185require_once($CFG->libdir.'/weblib.php');
186require_once($CFG->libdir.'/dmllib.php');
187require_once($CFG->libdir.'/moodlelib.php');
188require_once($CFG->libdir.'/deprecatedlib.php');
189require_once($CFG->libdir.'/adminlib.php');
3b596dbf 190require_once($CFG->libdir.'/componentlib.class.php');
468cc572 191require_once($CFG->dirroot.'/cache/lib.php');
3b596dbf 192
98d696b6
SH
193// Register our classloader, in theory somebody might want to replace it to load other hacked core classes.
194// Required because the database checks below lead to session interaction which is going to lead us to requiring autoloaded classes.
195if (defined('COMPONENT_CLASSLOADER')) {
196 spl_autoload_register(COMPONENT_CLASSLOADER);
197} else {
198 spl_autoload_register('core_component::classloader');
199}
200
3d673fc4
DM
201require($CFG->dirroot.'/version.php');
202$CFG->target_release = $release;
203
2e00d01d 204\core\session\manager::init_empty_session();
1a4621d8
PS
205global $SESSION;
206global $USER;
1a4621d8
PS
207
208global $COURSE;
209$COURSE = new stdClass();
210$COURSE->id = 1;
211
212global $SITE;
213$SITE = $COURSE;
214define('SITEID', 1);
215
3b596dbf 216//Database types
217$databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
17601a7e 218 'mariadb'=> moodle_database::get_driver_instance('mariadb', 'native'),
3b596dbf 219 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'),
220 'oci' => moodle_database::get_driver_instance('oci', 'native'),
7e60d0d6 221 'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
3b596dbf 222 );
223foreach ($databases as $type=>$database) {
224 if ($database->driver_installed() !== true) {
225 unset($databases[$type]);
226 }
227}
228if (empty($databases)) {
c31d94e8 229 $defaultdb = '';
230} else {
231 reset($databases);
232 $defaultdb = key($databases);
3b596dbf 233}
234
3b596dbf 235// now get cli options
7217676b
DM
236list($options, $unrecognized) = cli_get_params(
237 array(
b1d53365 238 'chmod' => isset($distro->directorypermissions) ? sprintf('%04o',$distro->directorypermissions) : '2777', // let distros set dir permissions
7217676b
DM
239 'lang' => $CFG->lang,
240 'wwwroot' => '',
1fcf0ca8 241 'dataroot' => empty($distro->dataroot) ? str_replace('\\', '/', dirname(dirname(dirname(__DIR__))).'/moodledata'): $distro->dataroot, // initialised later after including libs or by distro
b1d53365
AA
242 'dbtype' => empty($distro->dbtype) ? $defaultdb : $distro->dbtype, // let distro skip dbtype selection
243 'dbhost' => empty($distro->dbhost) ? 'localhost' : $distro->dbhost, // let distros set dbhost
7217676b 244 'dbname' => 'moodle',
b1d53365 245 'dbuser' => empty($distro->dbuser) ? 'root' : $distro->dbuser, // let distros set dbuser
7217676b 246 'dbpass' => '',
1826d0ea
PS
247 'dbport' => '',
248 'dbsocket' => '',
7217676b 249 'prefix' => 'mdl_',
9632db7a
DM
250 'fullname' => '',
251 'shortname' => '',
65903c76 252 'summary' => '',
7217676b
DM
253 'adminuser' => 'admin',
254 'adminpass' => '',
da992717 255 'adminemail' => '',
6ef02f84 256 'upgradekey' => '',
7217676b
DM
257 'non-interactive' => false,
258 'agree-license' => false,
3d673fc4 259 'allow-unstable' => false,
096fd596 260 'skip-database' => false,
7217676b
DM
261 'help' => false
262 ),
263 array(
264 'h' => 'help'
265 )
266);
3b596dbf 267
268$interactive = empty($options['non-interactive']);
9ae2623d 269$skipdatabase = $options['skip-database'];
3b596dbf 270
271// set up language
272$lang = clean_param($options['lang'], PARAM_SAFEDIR);
7d62bc24
DM
273$languages = get_string_manager()->get_list_of_translations();
274if (array_key_exists($lang, $languages)) {
3b596dbf 275 $CFG->lang = $lang;
3b596dbf 276}
277
278if ($unrecognized) {
c31d94e8 279 $unrecognized = implode("\n ", $unrecognized);
1494616f 280 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
3b596dbf 281}
282
283if ($options['help']) {
3b596dbf 284 echo $help;
285 die;
286}
287
3b596dbf 288//Print header
394034ec
DM
289cli_logo();
290echo PHP_EOL;
c31d94e8 291echo get_string('cliinstallheader', 'install', $CFG->target_release)."\n";
3b596dbf 292
293//Fist select language
294if ($interactive) {
c31d94e8 295 cli_separator();
960b6529 296 // Do not put the langs into columns because it is not compatible with RTL.
3a915b06 297 $default = $CFG->lang;
7d62bc24
DM
298 cli_heading(get_string('chooselanguagehead', 'install'));
299 if (array_key_exists($default, $languages)) {
300 echo $default.' - '.$languages[$default]."\n";
301 }
302 if ($default !== 'en') {
303 echo 'en - English (en)'."\n";
304 }
305 echo '? - '.get_string('availablelangs', 'install')."\n";
3a915b06 306 $prompt = get_string('clitypevaluedefault', 'admin', $CFG->lang);
3b596dbf 307 $error = '';
308 do {
309 echo $error;
310 $input = cli_input($prompt, $default);
3b596dbf 311
7d62bc24
DM
312 if ($input === '?') {
313 echo implode("\n", $languages)."\n";
314 $error = "\n";
315
3b596dbf 316 } else {
7d62bc24
DM
317 $input = clean_param($input, PARAM_SAFEDIR);
318
319 if (!array_key_exists($input, $languages)) {
320 $error = get_string('cliincorrectvalueretry', 'admin')."\n";
321 } else {
322 $error = '';
323 }
3b596dbf 324 }
325 } while ($error !== '');
3a915b06 326 $CFG->lang = $input;
3b596dbf 327} else {
c31d94e8 328 // already selected and verified
3b596dbf 329}
330
7be67681
DM
331// Set directorypermissions first
332$chmod = octdec(clean_param($options['chmod'], PARAM_INT));
333if ($interactive) {
334 cli_separator();
6e175676 335 cli_heading(get_string('datarootpermission', 'install'));
7be67681
DM
336 $prompt = get_string('clitypevaluedefault', 'admin', decoct($chmod));
337 $error = '';
338 do {
339 echo $error;
b7315f35 340 $input = cli_input($prompt, decoct($chmod));
7be67681
DM
341 $input = octdec(clean_param($input, PARAM_INT));
342 if (empty($input)) {
343 $error = get_string('cliincorrectvalueretry', 'admin')."\n";
344 } else {
345 $error = '';
346 }
b7315f35 347 } while ($error !== '');
7be67681
DM
348 $chmod = $input;
349
350} else {
351 if (empty($chmod)) {
352 $a = (object)array('option' => 'chmod', 'value' => decoct($chmod));
353 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
354 }
355}
356$CFG->directorypermissions = $chmod;
7f0ffc54
PS
357$CFG->filepermissions = ($CFG->directorypermissions & 0666);
358$CFG->umaskpermissions = (($CFG->directorypermissions & 0777) ^ 0777);
3b596dbf 359
360//We need wwwroot before we test dataroot
361$wwwroot = clean_param($options['wwwroot'], PARAM_URL);
362$wwwroot = trim($wwwroot, '/');
363if ($interactive) {
c31d94e8 364 cli_separator();
365 cli_heading(get_string('wwwroot', 'install'));
3b596dbf 366 if (strpos($wwwroot, 'http') === 0) {
c31d94e8 367 $prompt = get_string('clitypevaluedefault', 'admin', $wwwroot);
3b596dbf 368 } else {
c31d94e8 369 $wwwroot = null;
370 $prompt = get_string('clitypevalue', 'admin');
3b596dbf 371 }
372 $error = '';
373 do {
374 echo $error;
375 $input = cli_input($prompt, $wwwroot);
376 $input = clean_param($input, PARAM_URL);
377 $input = trim($input, '/');
378 if (strpos($input, 'http') !== 0) {
c31d94e8 379 $error = get_string('cliincorrectvalueretry', 'admin')."\n";
3b596dbf 380 } else {
381 $error = '';
382 }
383 } while ($error !== '');
384 $wwwroot = $input;
385
386} else {
387 if (strpos($wwwroot, 'http') !== 0) {
c31d94e8 388 $a = (object)array('option'=>'wwwroot', 'value'=>$wwwroot);
389 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
3b596dbf 390 }
391}
392$CFG->wwwroot = $wwwroot;
469d4b7f 393$CFG->httpswwwroot = $CFG->wwwroot;
3b596dbf 394
395//We need dataroot before lang download
ad72df40 396$CFG->dataroot = $options['dataroot'];
3b596dbf 397if ($interactive) {
c31d94e8 398 cli_separator();
3b596dbf 399 $i=0;
400 while(is_dataroot_insecure()) {
401 $parrent = dirname($CFG->dataroot);
402 $i++;
403 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
404 $CFG->dataroot = ''; //can not find secure location for dataroot
405 break;
406 }
407 $CFG->dataroot = dirname($parrent).'/moodledata';
408 }
c31d94e8 409 cli_heading(get_string('dataroot', 'install'));
3b596dbf 410 $error = '';
411 do {
412 if ($CFG->dataroot !== '') {
c31d94e8 413 $prompt = get_string('clitypevaluedefault', 'admin', $CFG->dataroot);
3b596dbf 414 } else {
c31d94e8 415 $prompt = get_string('clitypevalue', 'admin');
3b596dbf 416 }
417 echo $error;
418 $CFG->dataroot = cli_input($prompt, $CFG->dataroot);
419 if ($CFG->dataroot === '') {
c31d94e8 420 $error = get_string('cliincorrectvalueretry', 'admin')."\n";
3b596dbf 421 } else if (is_dataroot_insecure()) {
422 $CFG->dataroot = '';
a18d577d 423 $error = get_string('pathsunsecuredataroot', 'install')."\n";
3b596dbf 424 } else {
211c9008 425 if (install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
3b596dbf 426 $error = '';
427 } else {
59ba2bb0
DM
428 $a = (object)array('dataroot' => $CFG->dataroot);
429 $error = get_string('pathserrcreatedataroot', 'install', $a)."\n";
3b596dbf 430 }
431 }
432
433 } while ($error !== '');
434
435} else {
436 if (is_dataroot_insecure()) {
59ba2bb0 437 cli_error(get_string('pathsunsecuredataroot', 'install'));
c31d94e8 438 }
211c9008 439 if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
59ba2bb0
DM
440 $a = (object)array('dataroot' => $CFG->dataroot);
441 cli_error(get_string('pathserrcreatedataroot', 'install', $a));
3b596dbf 442 }
443}
85b38061 444$CFG->tempdir = $CFG->dataroot.'/temp';
ef844148 445$CFG->backuptempdir = $CFG->tempdir.'/backup';
85b38061
PS
446$CFG->cachedir = $CFG->dataroot.'/cache';
447$CFG->localcachedir = $CFG->dataroot.'/localcache';
3b596dbf 448
74a4c9a9
DM
449// download required lang packs
450if ($CFG->lang !== 'en') {
451 $installer = new lang_installer($CFG->lang);
452 $results = $installer->run();
453 foreach ($results as $langcode => $langstatus) {
454 if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
455 $a = new stdClass();
456 $a->url = $installer->lang_pack_url($langcode);
457 $a->dest = $CFG->dataroot.'/lang';
458 cli_problem(get_string('remotedownloaderror', 'error', $a));
3b596dbf 459 }
460 }
3b596dbf 461}
3b596dbf 462
7d73574c 463// switch the string_manager instance to stop using install/lang/
3a915b06 464$CFG->early_install_lang = false;
877a172d
DM
465$CFG->langotherroot = $CFG->dataroot.'/lang';
466$CFG->langlocalroot = $CFG->dataroot.'/lang';
7d73574c 467get_string_manager(true);
3a915b06 468
3d673fc4
DM
469// make sure we are installing stable release or require a confirmation
470if (isset($maturity)) {
471 if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
472 $maturitylevel = get_string('maturity'.$maturity, 'admin');
473
474 if ($interactive) {
475 cli_separator();
476 cli_heading(get_string('notice'));
477 echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
dc41abd8 478 echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
7119e1a0 479 echo get_string('continue') . PHP_EOL;
3d673fc4
DM
480 $prompt = get_string('cliyesnoprompt', 'admin');
481 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
482 if ($input == get_string('clianswerno', 'admin')) {
483 exit(1);
484 }
485 } else {
2da7fbc0
DM
486 cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
487 cli_error(get_string('maturityallowunstable', 'admin'));
3d673fc4
DM
488 }
489 }
490}
491
3b596dbf 492// ask for db type - show only drivers available
493if ($interactive) {
494 $options['dbtype'] = strtolower($options['dbtype']);
c31d94e8 495 cli_separator();
496 cli_heading(get_string('databasetypehead', 'install'));
3b596dbf 497 foreach ($databases as $type=>$database) {
c31d94e8 498 echo " $type \n";
3b596dbf 499 }
3b596dbf 500 if (!empty($databases[$options['dbtype']])) {
c31d94e8 501 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbtype']);
3b596dbf 502 } else {
c31d94e8 503 $prompt = get_string('clitypevalue', 'admin');
3b596dbf 504 }
3b596dbf 505 $CFG->dbtype = cli_input($prompt, $options['dbtype'], array_keys($databases));
506
507} else {
508 if (empty($databases[$options['dbtype']])) {
c31d94e8 509 $a = (object)array('option'=>'dbtype', 'value'=>$options['dbtype']);
510 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
3b596dbf 511 }
512 $CFG->dbtype = $options['dbtype'];
513}
514$database = $databases[$CFG->dbtype];
515
516
fffb043e
SR
517// We cannot do any validation until all DB connection data is provided.
518$hintdatabase = '';
519do {
520 echo $hintdatabase;
521
522 // Ask for db host.
523 if ($interactive) {
524 cli_separator();
525 cli_heading(get_string('databasehost', 'install'));
526 if ($options['dbhost'] !== '') {
527 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbhost']);
528 } else {
529 $prompt = get_string('clitypevalue', 'admin');
530 }
531 $CFG->dbhost = cli_input($prompt, $options['dbhost']);
532
3b596dbf 533 } else {
fffb043e 534 $CFG->dbhost = $options['dbhost'];
3b596dbf 535 }
3b596dbf 536
fffb043e
SR
537 // Ask for db name.
538 if ($interactive) {
539 cli_separator();
540 cli_heading(get_string('databasename', 'install'));
541 if ($options['dbname'] !== '') {
542 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbname']);
543 } else {
544 $prompt = get_string('clitypevalue', 'admin');
545 }
546 $CFG->dbname = cli_input($prompt, $options['dbname']);
3b596dbf 547
3b596dbf 548 } else {
fffb043e 549 $CFG->dbname = $options['dbname'];
3b596dbf 550 }
3b596dbf 551
fffb043e
SR
552 // Ask for db prefix.
553 if ($interactive) {
554 cli_separator();
555 cli_heading(get_string('dbprefix', 'install'));
556 //TODO: solve somehow the prefix trouble for oci.
557 if ($options['prefix'] !== '') {
558 $prompt = get_string('clitypevaluedefault', 'admin', $options['prefix']);
559 } else {
560 $prompt = get_string('clitypevalue', 'admin');
561 }
562 $CFG->prefix = cli_input($prompt, $options['prefix']);
3b596dbf 563
3b596dbf 564 } else {
fffb043e 565 $CFG->prefix = $options['prefix'];
3b596dbf 566 }
3b596dbf 567
fffb043e
SR
568 // Ask for db port.
569 if ($interactive) {
570 cli_separator();
571 cli_heading(get_string('databaseport', 'install'));
572 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbport']);
573 $CFG->dboptions['dbport'] = (int) cli_input($prompt, $options['dbport']);
3b596dbf 574
fffb043e
SR
575 } else {
576 $CFG->dboptions['dbport'] = (int) $options['dbport'];
577 }
578 if ($CFG->dboptions['dbport'] <= 0) {
579 $CFG->dboptions['dbport'] = '';
580 }
1826d0ea 581
fffb043e
SR
582 // Ask for db socket.
583 if ($CFG->ostype === 'WINDOWS') {
584 $CFG->dboptions['dbsocket'] = '';
1826d0ea 585
fffb043e
SR
586 } else if ($interactive and empty($CFG->dboptions['dbport'])) {
587 cli_separator();
588 cli_heading(get_string('databasesocket', 'install'));
589 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbsocket']);
590 $CFG->dboptions['dbsocket'] = cli_input($prompt, $options['dbsocket']);
1826d0ea 591
fffb043e
SR
592 } else {
593 $CFG->dboptions['dbsocket'] = $options['dbsocket'];
594 }
1826d0ea 595
fffb043e
SR
596 // Ask for db user.
597 if ($interactive) {
598 cli_separator();
599 cli_heading(get_string('databaseuser', 'install'));
600 if ($options['dbuser'] !== '') {
601 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbuser']);
602 } else {
603 $prompt = get_string('clitypevalue', 'admin');
604 }
605 $CFG->dbuser = cli_input($prompt, $options['dbuser']);
1826d0ea 606
3b596dbf 607 } else {
fffb043e 608 $CFG->dbuser = $options['dbuser'];
3b596dbf 609 }
3b596dbf 610
fffb043e
SR
611 // Ask for db password.
612 if ($interactive) {
613 cli_separator();
614 cli_heading(get_string('databasepass', 'install'));
3b596dbf 615
3b596dbf 616 if ($options['dbpass'] !== '') {
c31d94e8 617 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbpass']);
3b596dbf 618 } else {
c31d94e8 619 $prompt = get_string('clitypevalue', 'admin');
3b596dbf 620 }
621
622 $CFG->dbpass = cli_input($prompt, $options['dbpass']);
fffb043e
SR
623 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation.
624 $distro = distro_pre_create_db($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix,
625 array('dbpersist' => 0, 'dbport' => $CFG->dboptions['dbport'], 'dbsocket' => $CFG->dboptions['dbsocket']),
626 $distro);
b1d53365 627 }
fffb043e
SR
628 $hintdatabase = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix,
629 array('dbpersist' => 0, 'dbport' => $CFG->dboptions['dbport'], 'dbsocket' => $CFG->dboptions['dbsocket']));
3b596dbf 630
fffb043e
SR
631 } else {
632 $CFG->dbpass = $options['dbpass'];
633 $hintdatabase = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix,
634 array('dbpersist' => 0, 'dbport' => $CFG->dboptions['dbport'], 'dbsocket' => $CFG->dboptions['dbsocket']));
635 if ($hintdatabase !== '') {
636 cli_error(get_string('dbconnectionerror', 'install'));
637 }
3b596dbf 638 }
fffb043e 639} while ($hintdatabase !== '');
3b596dbf 640
9ae2623d
SL
641// If --skip-database option is provided, we do not need to ask for site fullname, shortname, adminuser, adminpass, adminemail.
642// These fields will be requested during the database install part.
643if (!$skipdatabase) {
644 // Ask for fullname.
645 if ($interactive) {
646 cli_separator();
647 cli_heading(get_string('fullsitename', 'moodle'));
9632db7a 648
9ae2623d
SL
649 if ($options['fullname'] !== '') {
650 $prompt = get_string('clitypevaluedefault', 'admin', $options['fullname']);
651 } else {
652 $prompt = get_string('clitypevalue', 'admin');
653 }
654
655 do {
656 $options['fullname'] = cli_input($prompt, $options['fullname']);
657 } while (empty($options['fullname']));
9632db7a 658 } else {
9ae2623d
SL
659 if (empty($options['fullname'])) {
660 $a = (object)['option' => 'fullname', 'value' => $options['fullname']];
661 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
662 }
9632db7a
DM
663 }
664
9ae2623d
SL
665 // Ask for shortname.
666 if ($interactive) {
667 cli_separator();
668 cli_heading(get_string('shortsitename', 'moodle'));
9632db7a 669
9ae2623d
SL
670 if ($options['shortname'] !== '') {
671 $prompt = get_string('clitypevaluedefault', 'admin', $options['shortname']);
672 } else {
673 $prompt = get_string('clitypevalue', 'admin');
674 }
9632db7a 675
9ae2623d
SL
676 do {
677 $options['shortname'] = cli_input($prompt, $options['shortname']);
678 } while (empty($options['shortname']));
9632db7a 679 } else {
9ae2623d
SL
680 if (empty($options['shortname'])) {
681 $a = (object)['option' => 'shortname', 'value' => $options['shortname']];
682 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
683 }
9632db7a
DM
684 }
685
9ae2623d
SL
686 // Ask for admin user name.
687 if ($interactive) {
688 cli_separator();
689 cli_heading(get_string('cliadminusername', 'install'));
690 if (!empty($options['adminuser'])) {
691 $prompt = get_string('clitypevaluedefault', 'admin', $options['adminuser']);
692 } else {
693 $prompt = get_string('clitypevalue', 'admin');
694 }
695 do {
696 $options['adminuser'] = cli_input($prompt, $options['adminuser']);
697 } while (empty($options['adminuser']) or $options['adminuser'] === 'guest');
698 } else {
699 if ((empty($options['adminuser']) || $options['adminuser'] === 'guest')) {
700 $a = (object)['option' => 'adminuser', 'value' => $options['adminuser']];
701 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
702 }
9632db7a 703 }
9632db7a 704
9ae2623d
SL
705 // Ask for admin user password.
706 if ($interactive) {
707 cli_separator();
708 cli_heading(get_string('cliadminpassword', 'install'));
7217676b 709 $prompt = get_string('clitypevalue', 'admin');
9ae2623d
SL
710 do {
711 $options['adminpass'] = cli_input($prompt);
712 } while (empty($options['adminpass']) or $options['adminpass'] === 'admin');
713 } else {
714 if ((empty($options['adminpass']) or $options['adminpass'] === 'admin')) {
715 $a = (object)['option' => 'adminpass', 'value' => $options['adminpass']];
716 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
717 }
7217676b 718 }
9ae2623d
SL
719
720 // Ask for the admin email address.
721 if ($interactive) {
722 cli_separator();
723 cli_heading(get_string('cliadminemail', 'install'));
724 $prompt = get_string('clitypevaluedefault', 'admin', $options['adminemail']);
725 $options['adminemail'] = cli_input($prompt, $options['adminemail']);
7217676b 726 }
7217676b 727
9ae2623d
SL
728 // Validate that the address provided was an e-mail address.
729 if (!empty($options['adminemail']) && !validate_email($options['adminemail'])) {
730 $a = (object)['option' => 'adminemail', 'value' => $options['adminemail']];
c31d94e8 731 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
3b596dbf 732 }
733}
734
6ef02f84
DM
735// Ask for the upgrade key.
736if ($interactive) {
737 cli_separator();
738 cli_heading(get_string('upgradekeyset', 'admin'));
739 if ($options['upgradekey'] !== '') {
740 $prompt = get_string('clitypevaluedefault', 'admin', $options['upgradekey']);
741 $options['upgradekey'] = cli_input($prompt, $options['upgradekey']);
742 } else {
743 $prompt = get_string('clitypevalue', 'admin');
744 $options['upgradekey'] = cli_input($prompt);
745 }
746}
747
748// Set the upgrade key if it was provided.
749if ($options['upgradekey'] !== '') {
750 $CFG->upgradekey = $options['upgradekey'];
751}
752
9ae2623d
SL
753// The user does not also need to pass agree-license when --skip-database is provided as the user will need to accept
754// the license again in the database install part.
755if (!$skipdatabase) {
756 if ($interactive) {
757 if (!$options['agree-license']) {
758 cli_separator();
759 cli_heading(get_string('copyrightnotice'));
760 echo "Moodle - Modular Object-Oriented Dynamic Learning Environment\n";
761 echo get_string('gpl3')."\n\n";
762 echo get_string('doyouagree')."\n";
763 $prompt = get_string('cliyesnoprompt', 'admin');
764 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
765 if ($input == get_string('clianswerno', 'admin')) {
766 exit(1);
767 }
768 }
769 } else {
770 if (!$options['agree-license'] && !$skipdatabase) {
771 cli_error(get_string('climustagreelicense', 'install'));
b8523467 772 }
3b596dbf 773 }
774}
775
776// Finally we have all info needed for config.php
777$configphp = install_generate_configphp($database, $CFG);
778umask(0137);
779if (($fh = fopen($configfile, 'w')) !== false) {
780 fwrite($fh, $configphp);
781 fclose($fh);
782}
783
784if (!file_exists($configfile)) {
785 cli_error('Can not create config file.');
3b596dbf 786}
787
f433088d
PS
788// remember selected language
789$installlang = $CFG->lang;
790// return back to original dir before executing setup.php which changes the dir again
791chdir($olddir);
792// We have config.php, it is a real php script from now on :-)
793require($configfile);
794
795// use selected language
796$CFG->lang = $installlang;
797$SESSION->lang = $CFG->lang;
798
799require("$CFG->dirroot/version.php");
800
faadd326 801// Test environment first.
f433088d 802require_once($CFG->libdir . '/environmentlib.php');
faadd326
TH
803list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
804if (!$envstatus) {
805 $errors = environment_get_errors($environment_results);
806 cli_heading(get_string('environment', 'admin'));
807 foreach ($errors as $error) {
808 list($info, $report) = $error;
809 echo "!! $info !!\n$report\n\n";
810 }
811 exit(1);
812}
813
777781d1 814// Test plugin dependencies.
f1753a5b 815$failed = array();
e87214bd 816if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
f1753a5b 817 cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
faadd326
TH
818 cli_error(get_string('pluginschecktodo', 'admin'));
819}
820
9ae2623d 821if (!$skipdatabase) {
096fd596 822 install_cli_database($options, $interactive);
0f268f5d
RW
823 // This needs to happen at the end to ensure it occurs after all caches
824 // have been purged for the last time.
825 // This will build a cached version of the current theme for the user
826 // to immediately start browsing the site.
827 require_once($CFG->libdir.'/upgradelib.php');
828 upgrade_themes();
096fd596
CF
829} else {
830 echo get_string('cliskipdatabase', 'install')."\n";
831}
3b596dbf 832
b8523467 833echo get_string('cliinstallfinished', 'install')."\n";
834exit(0); // 0 means success