MDL-41806 Add assessors to moodle_url class
[moodle.git] / admin / cli / install.php
blobfcd25b248fa96ebef5a16a2c0df7cadfb9a4ad63
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.
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 script creates config.php file and prepares database.
21 * This script is not intended for beginners!
22 * Potential problems:
23 * - su to apache account or sudo before execution
24 * - not compatible with Windows platform
26 * @package core
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
32 define('CLI_SCRIPT', true);
34 // extra execution prevention - we can not just require config.php here
35 if (isset($_SERVER['REMOTE_ADDR'])) {
36 exit(1);
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();
45 $help =
46 "Command line Moodle installer, creates config.php and initializes database.
47 Please note you must execute this script with the same uid as apache
48 or use chmod/chown after installation.
50 Site defaults may be changed via local/defaults.php.
52 Options:
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.
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,
60 must not be web accessible. Default is moodledata
61 in the parent directory.
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
67 --dbport=NUMBER Use database port.
68 --dbsocket=PATH Use database socket, 1 means default. Available for some databases only.
69 --prefix=STRING Table prefix for above database tables. Default is mdl_
70 --fullname=STRING The fullname of the site
71 --shortname=STRING The shortname of the site
72 --adminuser=USERNAME Username for the moodle admin account. Default is admin
73 --adminpass=PASSWORD Password for the moodle admin account,
74 required in non-interactive mode.
75 --non-interactive No interactive questions, installation fails if any
76 problem encountered.
77 --agree-license Indicates agreement with software license,
78 required in non-interactive mode.
79 --allow-unstable Install even if the version is not marked as stable yet,
80 required in non-interactive mode.
81 -h, --help Print out this help
83 Example:
84 \$sudo -u www-data /usr/bin/php admin/cli/install.php --lang=cs
85 "; //TODO: localize, mark as needed in install - to be translated later when everything is finished
88 // distro specific customisation
89 $distrolibfile = dirname(dirname(dirname(__FILE__))).'/install/distrolib.php';
90 $distro = null;
91 if (file_exists($distrolibfile)) {
92 require_once($distrolibfile);
93 if (function_exists('distro_get_config')) {
94 $distro = distro_get_config();
98 // Nothing to do if config.php exists
99 $configfile = dirname(dirname(dirname(__FILE__))).'/config.php';
100 if (file_exists($configfile)) {
101 require($configfile);
102 require_once($CFG->libdir.'/clilib.php');
103 list($options, $unrecognized) = cli_get_params(array('help'=>false), array('h'=>'help'));
105 if ($options['help']) {
106 echo $help;
107 echo "\n\n";
110 if ($DB->get_manager()->table_exists('config')) {
111 cli_error(get_string('clialreadyinstalled', 'install'));
112 } else {
113 cli_error(get_string('clialreadyconfigured', 'install'));
117 $olddir = getcwd();
119 // change directory so that includes below work properly
120 chdir(dirname($_SERVER['argv'][0]));
122 // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
123 // This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this.
124 if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
125 @date_default_timezone_set(@date_default_timezone_get());
128 // make sure PHP errors are displayed - helps with diagnosing of problems
129 @error_reporting(E_ALL);
130 @ini_set('display_errors', '1');
131 // we need a lot of memory
132 @ini_set('memory_limit', '128M');
134 /** Used by library scripts to check they are being called by Moodle */
135 define('MOODLE_INTERNAL', true);
137 // Disables all caching.
138 define('CACHE_DISABLE_ALL', true);
140 define('PHPUNIT_TEST', false);
142 define('IGNORE_COMPONENT_CACHE', true);
144 // Check that PHP is of a sufficient version
145 if (version_compare(phpversion(), "5.3.3") < 0) {
146 $phpversion = phpversion();
147 // do NOT localise - lang strings would not work here and we CAN NOT move it after installib
148 fwrite(STDERR, "Moodle 2.5 or later requires at least PHP 5.3.3 (currently using version $phpversion).\n");
149 fwrite(STDERR, "Please upgrade your server software or install older Moodle version.\n");
150 exit(1);
153 // set up configuration
154 $CFG = new stdClass();
155 $CFG->lang = 'en';
156 $CFG->dirroot = dirname(dirname(dirname(__FILE__)));
157 $CFG->libdir = "$CFG->dirroot/lib";
158 $CFG->wwwroot = "http://localhost";
159 $CFG->httpswwwroot = $CFG->wwwroot;
160 $CFG->docroot = 'http://docs.moodle.org';
161 $CFG->running_installer = true;
162 $CFG->early_install_lang = true;
163 $CFG->ostype = (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) ? 'WINDOWS' : 'UNIX';
164 $CFG->dboptions = array();
165 $CFG->debug = (E_ALL | E_STRICT);
166 $CFG->debugdisplay = true;
167 $CFG->debugdeveloper = true;
169 $parts = explode('/', str_replace('\\', '/', dirname(dirname(__FILE__))));
170 $CFG->admin = array_pop($parts);
172 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
173 //the problem is that we need specific version of quickforms and hacked excel files :-(
174 ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
176 require_once($CFG->libdir.'/classes/component.php');
177 require_once($CFG->libdir.'/classes/text.php');
178 require_once($CFG->libdir.'/classes/string_manager.php');
179 require_once($CFG->libdir.'/classes/string_manager_install.php');
180 require_once($CFG->libdir.'/classes/string_manager_standard.php');
181 require_once($CFG->libdir.'/installlib.php');
182 require_once($CFG->libdir.'/clilib.php');
183 require_once($CFG->libdir.'/setuplib.php');
184 require_once($CFG->libdir.'/weblib.php');
185 require_once($CFG->libdir.'/dmllib.php');
186 require_once($CFG->libdir.'/moodlelib.php');
187 require_once($CFG->libdir.'/deprecatedlib.php');
188 require_once($CFG->libdir.'/adminlib.php');
189 require_once($CFG->libdir.'/componentlib.class.php');
190 require_once($CFG->dirroot.'/cache/lib.php');
192 require($CFG->dirroot.'/version.php');
193 $CFG->target_release = $release;
195 //Database types
196 $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'),
197 'mariadb'=> moodle_database::get_driver_instance('mariadb', 'native'),
198 'pgsql' => moodle_database::get_driver_instance('pgsql', 'native'),
199 'oci' => moodle_database::get_driver_instance('oci', 'native'),
200 'sqlsrv' => moodle_database::get_driver_instance('sqlsrv', 'native'), // MS SQL*Server PHP driver
201 'mssql' => moodle_database::get_driver_instance('mssql', 'native'), // FreeTDS driver
203 foreach ($databases as $type=>$database) {
204 if ($database->driver_installed() !== true) {
205 unset($databases[$type]);
208 if (empty($databases)) {
209 $defaultdb = '';
210 } else {
211 reset($databases);
212 $defaultdb = key($databases);
215 // now get cli options
216 list($options, $unrecognized) = cli_get_params(
217 array(
218 'chmod' => isset($distro->directorypermissions) ? sprintf('%04o',$distro->directorypermissions) : '2777', // let distros set dir permissions
219 'lang' => $CFG->lang,
220 'wwwroot' => '',
221 'dataroot' => empty($distro->dataroot) ? str_replace('\\', '/', dirname(dirname(dirname(dirname(__FILE__)))).'/moodledata'): $distro->dataroot, // initialised later after including libs or by distro
222 'dbtype' => empty($distro->dbtype) ? $defaultdb : $distro->dbtype, // let distro skip dbtype selection
223 'dbhost' => empty($distro->dbhost) ? 'localhost' : $distro->dbhost, // let distros set dbhost
224 'dbname' => 'moodle',
225 'dbuser' => empty($distro->dbuser) ? 'root' : $distro->dbuser, // let distros set dbuser
226 'dbpass' => '',
227 'dbport' => '',
228 'dbsocket' => '',
229 'prefix' => 'mdl_',
230 'fullname' => '',
231 'shortname' => '',
232 'adminuser' => 'admin',
233 'adminpass' => '',
234 'non-interactive' => false,
235 'agree-license' => false,
236 'allow-unstable' => false,
237 'help' => false
239 array(
240 'h' => 'help'
244 $interactive = empty($options['non-interactive']);
246 // set up language
247 $lang = clean_param($options['lang'], PARAM_SAFEDIR);
248 if (file_exists($CFG->dirroot.'/install/lang/'.$lang)) {
249 $CFG->lang = $lang;
252 if ($unrecognized) {
253 $unrecognized = implode("\n ", $unrecognized);
254 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
257 if ($options['help']) {
258 echo $help;
259 die;
262 //Print header
263 echo get_string('cliinstallheader', 'install', $CFG->target_release)."\n";
265 //Fist select language
266 if ($interactive) {
267 cli_separator();
268 $languages = get_string_manager()->get_list_of_translations();
269 // Do not put the langs into columns because it is not compatible with RTL.
270 $langlist = implode("\n", $languages);
271 $default = $CFG->lang;
272 cli_heading(get_string('availablelangs', 'install'));
273 echo $langlist."\n";
274 $prompt = get_string('clitypevaluedefault', 'admin', $CFG->lang);
275 $error = '';
276 do {
277 echo $error;
278 $input = cli_input($prompt, $default);
279 $input = clean_param($input, PARAM_SAFEDIR);
281 if (!file_exists($CFG->dirroot.'/install/lang/'.$input)) {
282 $error = get_string('cliincorrectvalueretry', 'admin')."\n";
283 } else {
284 $error = '';
286 } while ($error !== '');
287 $CFG->lang = $input;
288 } else {
289 // already selected and verified
292 // Set directorypermissions first
293 $chmod = octdec(clean_param($options['chmod'], PARAM_INT));
294 if ($interactive) {
295 cli_separator();
296 cli_heading(get_string('datarootpermission', 'install'));
297 $prompt = get_string('clitypevaluedefault', 'admin', decoct($chmod));
298 $error = '';
299 do {
300 echo $error;
301 $input = cli_input($prompt, decoct($chmod));
302 $input = octdec(clean_param($input, PARAM_INT));
303 if (empty($input)) {
304 $error = get_string('cliincorrectvalueretry', 'admin')."\n";
305 } else {
306 $error = '';
308 } while ($error !== '');
309 $chmod = $input;
311 } else {
312 if (empty($chmod)) {
313 $a = (object)array('option' => 'chmod', 'value' => decoct($chmod));
314 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
317 $CFG->directorypermissions = $chmod;
318 $CFG->filepermissions = ($CFG->directorypermissions & 0666);
319 $CFG->umaskpermissions = (($CFG->directorypermissions & 0777) ^ 0777);
321 //We need wwwroot before we test dataroot
322 $wwwroot = clean_param($options['wwwroot'], PARAM_URL);
323 $wwwroot = trim($wwwroot, '/');
324 if ($interactive) {
325 cli_separator();
326 cli_heading(get_string('wwwroot', 'install'));
327 if (strpos($wwwroot, 'http') === 0) {
328 $prompt = get_string('clitypevaluedefault', 'admin', $wwwroot);
329 } else {
330 $wwwroot = null;
331 $prompt = get_string('clitypevalue', 'admin');
333 $error = '';
334 do {
335 echo $error;
336 $input = cli_input($prompt, $wwwroot);
337 $input = clean_param($input, PARAM_URL);
338 $input = trim($input, '/');
339 if (strpos($input, 'http') !== 0) {
340 $error = get_string('cliincorrectvalueretry', 'admin')."\n";
341 } else {
342 $error = '';
344 } while ($error !== '');
345 $wwwroot = $input;
347 } else {
348 if (strpos($wwwroot, 'http') !== 0) {
349 $a = (object)array('option'=>'wwwroot', 'value'=>$wwwroot);
350 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
353 $CFG->wwwroot = $wwwroot;
354 $CFG->httpswwwroot = $CFG->wwwroot;
357 //We need dataroot before lang download
358 $CFG->dataroot = $options['dataroot'];
359 if ($interactive) {
360 cli_separator();
361 $i=0;
362 while(is_dataroot_insecure()) {
363 $parrent = dirname($CFG->dataroot);
364 $i++;
365 if ($parrent == '/' or $parrent == '.' or preg_match('/^[a-z]:\\\?$/i', $parrent) or ($i > 100)) {
366 $CFG->dataroot = ''; //can not find secure location for dataroot
367 break;
369 $CFG->dataroot = dirname($parrent).'/moodledata';
371 cli_heading(get_string('dataroot', 'install'));
372 $error = '';
373 do {
374 if ($CFG->dataroot !== '') {
375 $prompt = get_string('clitypevaluedefault', 'admin', $CFG->dataroot);
376 } else {
377 $prompt = get_string('clitypevalue', 'admin');
379 echo $error;
380 $CFG->dataroot = cli_input($prompt, $CFG->dataroot);
381 if ($CFG->dataroot === '') {
382 $error = get_string('cliincorrectvalueretry', 'admin')."\n";
383 } else if (is_dataroot_insecure()) {
384 $CFG->dataroot = '';
385 $error = get_string('pathsunsecuredataroot', 'install')."\n";
386 } else {
387 if (install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
388 $error = '';
389 } else {
390 $a = (object)array('dataroot' => $CFG->dataroot);
391 $error = get_string('pathserrcreatedataroot', 'install', $a)."\n";
395 } while ($error !== '');
397 } else {
398 if (is_dataroot_insecure()) {
399 cli_error(get_string('pathsunsecuredataroot', 'install'));
401 if (!install_init_dataroot($CFG->dataroot, $CFG->directorypermissions)) {
402 $a = (object)array('dataroot' => $CFG->dataroot);
403 cli_error(get_string('pathserrcreatedataroot', 'install', $a));
406 $CFG->tempdir = $CFG->dataroot.'/temp';
407 $CFG->cachedir = $CFG->dataroot.'/cache';
408 $CFG->localcachedir = $CFG->dataroot.'/localcache';
410 // download required lang packs
411 if ($CFG->lang !== 'en') {
412 $installer = new lang_installer($CFG->lang);
413 $results = $installer->run();
414 foreach ($results as $langcode => $langstatus) {
415 if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
416 $a = new stdClass();
417 $a->url = $installer->lang_pack_url($langcode);
418 $a->dest = $CFG->dataroot.'/lang';
419 cli_problem(get_string('remotedownloaderror', 'error', $a));
424 // switch the string_manager instance to stop using install/lang/
425 $CFG->early_install_lang = false;
426 $CFG->langotherroot = $CFG->dataroot.'/lang';
427 $CFG->langlocalroot = $CFG->dataroot.'/lang';
428 get_string_manager(true);
430 // make sure we are installing stable release or require a confirmation
431 if (isset($maturity)) {
432 if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
433 $maturitylevel = get_string('maturity'.$maturity, 'admin');
435 if ($interactive) {
436 cli_separator();
437 cli_heading(get_string('notice'));
438 echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
439 echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
440 echo get_string('continue') . PHP_EOL;
441 $prompt = get_string('cliyesnoprompt', 'admin');
442 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
443 if ($input == get_string('clianswerno', 'admin')) {
444 exit(1);
446 } else {
447 cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
448 cli_error(get_string('maturityallowunstable', 'admin'));
453 // ask for db type - show only drivers available
454 if ($interactive) {
455 $options['dbtype'] = strtolower($options['dbtype']);
456 cli_separator();
457 cli_heading(get_string('databasetypehead', 'install'));
458 foreach ($databases as $type=>$database) {
459 echo " $type \n";
461 if (!empty($databases[$options['dbtype']])) {
462 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbtype']);
463 } else {
464 $prompt = get_string('clitypevalue', 'admin');
466 $CFG->dbtype = cli_input($prompt, $options['dbtype'], array_keys($databases));
468 } else {
469 if (empty($databases[$options['dbtype']])) {
470 $a = (object)array('option'=>'dbtype', 'value'=>$options['dbtype']);
471 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
473 $CFG->dbtype = $options['dbtype'];
475 $database = $databases[$CFG->dbtype];
478 // ask for db host
479 if ($interactive) {
480 cli_separator();
481 cli_heading(get_string('databasehost', 'install'));
482 if ($options['dbhost'] !== '') {
483 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbhost']);
484 } else {
485 $prompt = get_string('clitypevalue', 'admin');
487 $CFG->dbhost = cli_input($prompt, $options['dbhost']);
489 } else {
490 $CFG->dbhost = $options['dbhost'];
493 // ask for db name
494 if ($interactive) {
495 cli_separator();
496 cli_heading(get_string('databasename', 'install'));
497 if ($options['dbname'] !== '') {
498 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbname']);
499 } else {
500 $prompt = get_string('clitypevalue', 'admin');
502 $CFG->dbname = cli_input($prompt, $options['dbname']);
504 } else {
505 $CFG->dbname = $options['dbname'];
508 // ask for db prefix
509 if ($interactive) {
510 cli_separator();
511 cli_heading(get_string('dbprefix', 'install'));
512 //TODO: solve somehow the prefix trouble for oci
513 if ($options['prefix'] !== '') {
514 $prompt = get_string('clitypevaluedefault', 'admin', $options['prefix']);
515 } else {
516 $prompt = get_string('clitypevalue', 'admin');
518 $CFG->prefix = cli_input($prompt, $options['prefix']);
520 } else {
521 $CFG->prefix = $options['prefix'];
524 // ask for db port
525 if ($interactive) {
526 cli_separator();
527 cli_heading(get_string('databaseport', 'install'));
528 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbport']);
529 $CFG->dboptions['dbport'] = (int)cli_input($prompt, $options['dbport']);
531 } else {
532 $CFG->dboptions['dbport'] = (int)$options['dbport'];
534 if ($CFG->dboptions['dbport'] <= 0) {
535 $CFG->dboptions['dbport'] = '';
538 // ask for db socket
539 if ($CFG->ostype === 'WINDOWS') {
540 $CFG->dboptions['dbsocket'] = '';
542 } else if ($interactive and empty($CFG->dboptions['dbport'])) {
543 cli_separator();
544 cli_heading(get_string('databasesocket', 'install'));
545 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbsocket']);
546 $CFG->dboptions['dbsocket'] = cli_input($prompt, $options['dbsocket']);
548 } else {
549 $CFG->dboptions['dbsocket'] = $options['dbsocket'];
552 // ask for db user
553 if ($interactive) {
554 cli_separator();
555 cli_heading(get_string('databaseuser', 'install'));
556 if ($options['dbuser'] !== '') {
557 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbuser']);
558 } else {
559 $prompt = get_string('clitypevalue', 'admin');
561 $CFG->dbuser = cli_input($prompt, $options['dbuser']);
563 } else {
564 $CFG->dbuser = $options['dbuser'];
567 // ask for db password
568 if ($interactive) {
569 cli_separator();
570 cli_heading(get_string('databasepass', 'install'));
571 do {
572 if ($options['dbpass'] !== '') {
573 $prompt = get_string('clitypevaluedefault', 'admin', $options['dbpass']);
574 } else {
575 $prompt = get_string('clitypevalue', 'admin');
578 $CFG->dbpass = cli_input($prompt, $options['dbpass']);
579 if (function_exists('distro_pre_create_db')) { // Hook for distros needing to do something before DB creation
580 $distro = distro_pre_create_db($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbport'=>$CFG->dboptions['dbport'], 'dbsocket'=>$CFG->dboptions['dbsocket']), $distro);
582 $hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbport'=>$CFG->dboptions['dbport'], 'dbsocket'=>$CFG->dboptions['dbsocket']));
583 } while ($hint_database !== '');
585 } else {
586 $CFG->dbpass = $options['dbpass'];
587 $hint_database = install_db_validate($database, $CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, array('dbpersist'=>0, 'dbport'=>$CFG->dboptions['dbport'], 'dbsocket'=>$CFG->dboptions['dbsocket']));
588 if ($hint_database !== '') {
589 cli_error(get_string('dbconnectionerror', 'install'));
593 // ask for fullname
594 if ($interactive) {
595 cli_separator();
596 cli_heading(get_string('fullsitename', 'moodle'));
598 if ($options['fullname'] !== '') {
599 $prompt = get_string('clitypevaluedefault', 'admin', $options['fullname']);
600 } else {
601 $prompt = get_string('clitypevalue', 'admin');
604 do {
605 $options['fullname'] = cli_input($prompt, $options['fullname']);
606 } while (empty($options['fullname']));
607 } else {
608 if (empty($options['fullname'])) {
609 $a = (object)array('option'=>'fullname', 'value'=>$options['fullname']);
610 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
614 // ask for shortname
615 if ($interactive) {
616 cli_separator();
617 cli_heading(get_string('shortsitename', 'moodle'));
619 if ($options['shortname'] !== '') {
620 $prompt = get_string('clitypevaluedefault', 'admin', $options['shortname']);
621 } else {
622 $prompt = get_string('clitypevalue', 'admin');
625 do {
626 $options['shortname'] = cli_input($prompt, $options['shortname']);
627 } while (empty($options['shortname']));
628 } else {
629 if (empty($options['shortname'])) {
630 $a = (object)array('option'=>'shortname', 'value'=>$options['shortname']);
631 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
635 // ask for admin user name
636 if ($interactive) {
637 cli_separator();
638 cli_heading(get_string('cliadminusername', 'install'));
639 if (!empty($options['adminuser'])) {
640 $prompt = get_string('clitypevaluedefault', 'admin', $options['adminuser']);
641 } else {
642 $prompt = get_string('clitypevalue', 'admin');
644 do {
645 $options['adminuser'] = cli_input($prompt, $options['adminuser']);
646 } while (empty($options['adminuser']) or $options['adminuser'] === 'guest');
647 } else {
648 if (empty($options['adminuser']) or $options['adminuser'] === 'guest') {
649 $a = (object)array('option'=>'adminuser', 'value'=>$options['adminuser']);
650 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
654 // ask for admin user password
655 if ($interactive) {
656 cli_separator();
657 cli_heading(get_string('cliadminpassword', 'install'));
658 $prompt = get_string('clitypevalue', 'admin');
659 do {
660 $options['adminpass'] = cli_input($prompt);
661 } while (empty($options['adminpass']) or $options['adminpass'] === 'admin');
662 } else {
663 if (empty($options['adminpass']) or $options['adminpass'] === 'admin') {
664 $a = (object)array('option'=>'adminpass', 'value'=>$options['adminpass']);
665 cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
669 if ($interactive) {
670 if (!$options['agree-license']) {
671 cli_separator();
672 cli_heading(get_string('copyrightnotice'));
673 echo "Moodle - Modular Object-Oriented Dynamic Learning Environment\n";
674 echo get_string('gpl3')."\n\n";
675 echo get_string('doyouagree')."\n";
676 $prompt = get_string('cliyesnoprompt', 'admin');
677 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
678 if ($input == get_string('clianswerno', 'admin')) {
679 exit(1);
682 } else {
683 if (!$options['agree-license']) {
684 cli_error(get_string('climustagreelicense', 'install'));
688 // Finally we have all info needed for config.php
689 $configphp = install_generate_configphp($database, $CFG);
690 umask(0137);
691 if (($fh = fopen($configfile, 'w')) !== false) {
692 fwrite($fh, $configphp);
693 fclose($fh);
696 if (!file_exists($configfile)) {
697 cli_error('Can not create config file.');
700 // remember selected language
701 $installlang = $CFG->lang;
702 // return back to original dir before executing setup.php which changes the dir again
703 chdir($olddir);
704 // We have config.php, it is a real php script from now on :-)
705 require($configfile);
707 // use selected language
708 $CFG->lang = $installlang;
709 $SESSION->lang = $CFG->lang;
711 require("$CFG->dirroot/version.php");
713 // Test environment first.
714 require_once($CFG->libdir . '/environmentlib.php');
715 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
716 if (!$envstatus) {
717 $errors = environment_get_errors($environment_results);
718 cli_heading(get_string('environment', 'admin'));
719 foreach ($errors as $error) {
720 list($info, $report) = $error;
721 echo "!! $info !!\n$report\n\n";
723 exit(1);
726 // Test plugin dependencies.
727 require_once($CFG->libdir . '/pluginlib.php');
728 $failed = array();
729 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
730 cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
731 cli_error(get_string('pluginschecktodo', 'admin'));
734 install_cli_database($options, $interactive);
736 echo get_string('cliinstallfinished', 'install')."\n";
737 exit(0); // 0 means success