2009-05-27 09:52:25 +00:00
< ? php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This script creates config . php file and prepares database .
*
* This script is not intended for beginners !
* Potential problems :
* - su to apache account or sudo before execution
* - not compatible with Windows platform
*
2010-07-30 11:58:02 +00:00
* @ package core
2009-05-27 09:52:25 +00:00
* @ subpackage cli
* @ copyright 2009 Petr Skoda ( http :// skodak . org )
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
*/
2010-08-17 12:33:30 +00:00
define ( 'CLI_SCRIPT' , true );
// extra execution prevention - we can not just require config.php here
2009-05-27 09:52:25 +00:00
if ( isset ( $_SERVER [ 'REMOTE_ADDR' ])) {
2010-08-17 12:33:30 +00:00
exit ( 1 );
2009-05-27 09:52:25 +00:00
}
2013-07-01 21:06:44 +02:00
// Force OPcache reset if used, we do not want any stale caches
// when preparing test environment.
if ( function_exists ( 'opcache_reset' )) {
opcache_reset ();
}
2009-05-29 07:45:06 +00:00
$help =
" Command line Moodle installer, creates config.php and initializes database.
Please note you must execute this script with the same uid as apache
or use chmod / chown after installation .
Site defaults may be changed via local / defaults . php .
Options :
2010-05-20 13:40:45 +00:00
-- chmod = OCTAL - MODE Permissions of new directories created within dataroot .
Default is 2777. You may want to change it to 2770
or 2750 or 750. See chmod man page for details .
2009-05-29 07:45:06 +00:00
-- lang = CODE Installation and default site language .
-- wwwroot = URL Web address for the Moodle site ,
required in non - interactive mode .
-- dataroot = DIR Location of the moodle data folder ,
2010-05-10 15:59:21 +00:00
must not be web accessible . Default is moodledata
in the parent directory .
2009-05-29 07:45:06 +00:00
-- dbtype = TYPE Database type . Default is mysqli
-- dbhost = HOST Database host . Default is localhost
-- dbname = NAME Database name . Default is moodle
-- dbuser = USERNAME Database user . Default is root
-- dbpass = PASSWORD Database password . Default is blank
2013-06-15 15:07:28 +02:00
-- dbport = NUMBER Use database port .
-- dbsocket = PATH Use database socket , 1 means default . Available for some databases only .
2009-05-29 07:45:06 +00:00
-- prefix = STRING Table prefix for above database tables . Default is mdl_
2010-10-27 19:18:45 +00:00
-- fullname = STRING The fullname of the site
-- shortname = STRING The shortname of the site
2015-04-15 10:33:14 +08:00
-- summary = STRING The summary to be displayed on the front page
2010-05-10 21:57:48 +00:00
-- adminuser = USERNAME Username for the moodle admin account . Default is admin
-- adminpass = PASSWORD Password for the moodle admin account ,
2009-05-29 07:45:06 +00:00
required in non - interactive mode .
2015-01-09 14:08:33 +08:00
-- adminemail = STRING Email address for the moodle admin account .
2015-09-16 00:18:06 +02:00
-- upgradekey = STRING The upgrade key to be set in the config . php , leave empty to not set it .
2009-05-29 07:45:06 +00:00
-- non - interactive No interactive questions , installation fails if any
problem encountered .
2009-05-30 17:14:24 +00:00
-- agree - license Indicates agreement with software license ,
2009-05-29 07:45:06 +00:00
required in non - interactive mode .
2011-02-25 23:39:42 +01:00
-- allow - unstable Install even if the version is not marked as stable yet ,
required in non - interactive mode .
2014-06-26 14:27:07 -07:00
-- skip - database Stop the installation before installing the database .
2009-05-29 07:45:06 +00:00
- h , -- help Print out this help
2010-08-17 12:33:30 +00:00
Example :
\ $sudo - u www - data / usr / bin / php admin / cli / install . php -- lang = cs
2009-05-30 17:14:24 +00:00
" ; //TODO: localize, mark as needed in install - to be translated later when everything is finished
2009-05-27 09:52:25 +00:00
2011-08-22 19:45:58 +04:00
// distro specific customisation
2016-02-26 17:47:58 +11:00
$distrolibfile = __DIR__ . '/../../install/distrolib.php' ;
2011-08-22 19:45:58 +04:00
$distro = null ;
if ( file_exists ( $distrolibfile )) {
require_once ( $distrolibfile );
if ( function_exists ( 'distro_get_config' )) {
$distro = distro_get_config ();
}
}
2009-05-27 09:52:25 +00:00
// Nothing to do if config.php exists
2016-02-26 17:47:58 +11:00
$configfile = __DIR__ . '/../../config.php' ;
2009-05-27 09:52:25 +00:00
if ( file_exists ( $configfile )) {
2009-05-29 07:45:06 +00:00
require ( $configfile );
2009-05-30 17:14:24 +00:00
require_once ( $CFG -> libdir . '/clilib.php' );
list ( $options , $unrecognized ) = cli_get_params ( array ( 'help' => false ), array ( 'h' => 'help' ));
if ( $options [ 'help' ]) {
echo $help ;
echo " \n \n " ;
}
2011-10-27 12:47:05 +02:00
if ( $DB -> get_manager () -> table_exists ( 'config' )) {
cli_error ( get_string ( 'clialreadyinstalled' , 'install' ));
} else {
cli_error ( get_string ( 'clialreadyconfigured' , 'install' ));
}
2009-05-27 09:52:25 +00:00
}
2009-05-29 07:45:06 +00:00
$olddir = getcwd ();
2012-02-10 02:44:27 +08:00
// change directory so that includes below work properly
2009-05-29 07:45:06 +00:00
chdir ( dirname ( $_SERVER [ 'argv' ][ 0 ]));
2010-08-29 14:15:43 +00:00
// Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
2015-03-28 18:46:16 +13:00
if ( ! function_exists ( 'date_default_timezone_set' ) or ! function_exists ( 'date_default_timezone_get' )) {
fwrite ( STDERR , " Timezone functions are not available. \n " );
exit ( 1 );
2010-08-29 14:15:43 +00:00
}
2015-03-28 18:46:16 +13:00
date_default_timezone_set ( @ date_default_timezone_get ());
2010-08-29 14:15:43 +00:00
2009-05-27 09:52:25 +00:00
// make sure PHP errors are displayed - helps with diagnosing of problems
@ error_reporting ( E_ALL );
@ ini_set ( 'display_errors' , '1' );
// we need a lot of memory
@ ini_set ( 'memory_limit' , '128M' );
2010-07-26 20:17:36 +00:00
/** Used by library scripts to check they are being called by Moodle */
define ( 'MOODLE_INTERNAL' , true );
2013-04-27 21:54:06 +02:00
// Disables all caching.
2012-11-22 10:33:57 +13:00
define ( 'CACHE_DISABLE_ALL' , true );
2013-06-18 10:31:26 +02:00
define ( 'PHPUNIT_TEST' , false );
2013-07-04 19:09:21 +02:00
define ( 'IGNORE_COMPONENT_CACHE' , true );
2017-06-08 10:51:32 +01:00
// Check that PHP is of a sufficient version as soon as possible.
require_once ( __DIR__ . '/../../lib/phpminimumversionlib.php' );
moodle_require_minimum_php_version ();
2009-05-27 09:52:25 +00:00
// set up configuration
2013-09-23 21:35:18 +02:00
global $CFG ;
2009-05-27 09:52:25 +00:00
$CFG = new stdClass ();
2010-04-10 07:24:56 +00:00
$CFG -> lang = 'en' ;
2016-02-26 17:47:58 +11:00
$CFG -> dirroot = dirname ( dirname ( __DIR__ ));
2009-05-27 09:52:25 +00:00
$CFG -> libdir = " $CFG->dirroot /lib " ;
$CFG -> wwwroot = " http://localhost " ;
2019-11-13 19:58:10 +01:00
$CFG -> httpswwwroot = $CFG -> wwwroot ;
2009-05-27 09:52:25 +00:00
$CFG -> docroot = 'http://docs.moodle.org' ;
2009-05-31 11:45:18 +00:00
$CFG -> running_installer = true ;
2010-04-10 07:24:56 +00:00
$CFG -> early_install_lang = true ;
2013-05-03 14:29:07 +08:00
$CFG -> ostype = ( stristr ( PHP_OS , 'win' ) && ! stristr ( PHP_OS , 'darwin' )) ? 'WINDOWS' : 'UNIX' ;
2013-06-15 15:07:28 +02:00
$CFG -> dboptions = array ();
2013-08-10 22:46:49 +02:00
$CFG -> debug = ( E_ALL | E_STRICT );
$CFG -> debugdisplay = true ;
$CFG -> debugdeveloper = true ;
2010-04-10 07:24:56 +00:00
2016-02-26 17:47:58 +11:00
$parts = explode ( '/' , str_replace ( '\\' , '/' , dirname ( __DIR__ )));
2009-05-27 09:52:25 +00:00
$CFG -> admin = array_pop ( $parts );
//point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
//the problem is that we need specific version of quickforms and hacked excel files :-(
ini_set ( 'include_path' , $CFG -> libdir . '/pear' . PATH_SEPARATOR . ini_get ( 'include_path' ));
2013-06-18 10:31:26 +02:00
require_once ( $CFG -> libdir . '/classes/component.php' );
2013-06-24 12:39:32 +02:00
require_once ( $CFG -> libdir . '/classes/text.php' );
2013-08-03 22:19:18 +02:00
require_once ( $CFG -> libdir . '/classes/string_manager.php' );
require_once ( $CFG -> libdir . '/classes/string_manager_install.php' );
require_once ( $CFG -> libdir . '/classes/string_manager_standard.php' );
2009-05-27 09:52:25 +00:00
require_once ( $CFG -> libdir . '/installlib.php' );
require_once ( $CFG -> libdir . '/clilib.php' );
require_once ( $CFG -> libdir . '/setuplib.php' );
require_once ( $CFG -> libdir . '/weblib.php' );
require_once ( $CFG -> libdir . '/dmllib.php' );
require_once ( $CFG -> libdir . '/moodlelib.php' );
require_once ( $CFG -> libdir . '/deprecatedlib.php' );
require_once ( $CFG -> libdir . '/adminlib.php' );
require_once ( $CFG -> libdir . '/componentlib.class.php' );
2012-11-06 17:24:15 +13:00
require_once ( $CFG -> dirroot . '/cache/lib.php' );
2009-05-27 09:52:25 +00:00
2013-09-23 15:38:45 +12:00
// Register our classloader, in theory somebody might want to replace it to load other hacked core classes.
// Required because the database checks below lead to session interaction which is going to lead us to requiring autoloaded classes.
if ( defined ( 'COMPONENT_CLASSLOADER' )) {
spl_autoload_register ( COMPONENT_CLASSLOADER );
} else {
spl_autoload_register ( 'core_component::classloader' );
}
2011-02-25 23:39:42 +01:00
require ( $CFG -> dirroot . '/version.php' );
$CFG -> target_release = $release ;
2014-06-24 10:48:28 +12:00
\core\session\manager :: init_empty_session ();
2013-09-23 21:35:18 +02:00
global $SESSION ;
global $USER ;
global $COURSE ;
$COURSE = new stdClass ();
$COURSE -> id = 1 ;
global $SITE ;
$SITE = $COURSE ;
define ( 'SITEID' , 1 );
2009-05-27 09:52:25 +00:00
//Database types
$databases = array ( 'mysqli' => moodle_database :: get_driver_instance ( 'mysqli' , 'native' ),
2020-02-21 15:55:24 -05:00
'auroramysql' => moodle_database :: get_driver_instance ( 'auroramysql' , 'native' ),
2013-07-21 13:00:11 +02:00
'mariadb' => moodle_database :: get_driver_instance ( 'mariadb' , 'native' ),
2009-05-27 09:52:25 +00:00
'pgsql' => moodle_database :: get_driver_instance ( 'pgsql' , 'native' ),
'oci' => moodle_database :: get_driver_instance ( 'oci' , 'native' ),
2010-06-24 16:48:11 +00:00
'sqlsrv' => moodle_database :: get_driver_instance ( 'sqlsrv' , 'native' ), // MS SQL*Server PHP driver
2009-05-27 09:52:25 +00:00
);
foreach ( $databases as $type => $database ) {
if ( $database -> driver_installed () !== true ) {
unset ( $databases [ $type ]);
}
}
if ( empty ( $databases )) {
2009-05-30 17:14:24 +00:00
$defaultdb = '' ;
} else {
reset ( $databases );
$defaultdb = key ( $databases );
2009-05-27 09:52:25 +00:00
}
// now get cli options
2010-05-10 21:57:48 +00:00
list ( $options , $unrecognized ) = cli_get_params (
array (
2011-08-22 19:45:58 +04:00
'chmod' => isset ( $distro -> directorypermissions ) ? sprintf ( '%04o' , $distro -> directorypermissions ) : '2777' , // let distros set dir permissions
2010-05-10 21:57:48 +00:00
'lang' => $CFG -> lang ,
'wwwroot' => '' ,
2016-02-26 17:47:58 +11:00
'dataroot' => empty ( $distro -> dataroot ) ? str_replace ( '\\' , '/' , dirname ( dirname ( dirname ( __DIR__ ))) . '/moodledata' ) : $distro -> dataroot , // initialised later after including libs or by distro
2011-08-22 19:45:58 +04:00
'dbtype' => empty ( $distro -> dbtype ) ? $defaultdb : $distro -> dbtype , // let distro skip dbtype selection
'dbhost' => empty ( $distro -> dbhost ) ? 'localhost' : $distro -> dbhost , // let distros set dbhost
2010-05-10 21:57:48 +00:00
'dbname' => 'moodle' ,
2011-08-22 19:45:58 +04:00
'dbuser' => empty ( $distro -> dbuser ) ? 'root' : $distro -> dbuser , // let distros set dbuser
2010-05-10 21:57:48 +00:00
'dbpass' => '' ,
2013-06-15 15:07:28 +02:00
'dbport' => '' ,
'dbsocket' => '' ,
2010-05-10 21:57:48 +00:00
'prefix' => 'mdl_' ,
2010-10-27 19:18:45 +00:00
'fullname' => '' ,
'shortname' => '' ,
2015-04-15 10:33:14 +08:00
'summary' => '' ,
2010-05-10 21:57:48 +00:00
'adminuser' => 'admin' ,
'adminpass' => '' ,
2015-01-09 14:08:33 +08:00
'adminemail' => '' ,
2015-09-16 00:18:06 +02:00
'upgradekey' => '' ,
2010-05-10 21:57:48 +00:00
'non-interactive' => false ,
'agree-license' => false ,
2011-02-25 23:39:42 +01:00
'allow-unstable' => false ,
2014-06-26 14:27:07 -07:00
'skip-database' => false ,
2010-05-10 21:57:48 +00:00
'help' => false
),
array (
'h' => 'help'
)
);
2009-05-27 09:52:25 +00:00
$interactive = empty ( $options [ 'non-interactive' ]);
2020-07-14 14:27:58 +08:00
$skipdatabase = $options [ 'skip-database' ];
2009-05-27 09:52:25 +00:00
// set up language
$lang = clean_param ( $options [ 'lang' ], PARAM_SAFEDIR );
2015-09-15 22:08:04 +02:00
$languages = get_string_manager () -> get_list_of_translations ();
if ( array_key_exists ( $lang , $languages )) {
2009-05-27 09:52:25 +00:00
$CFG -> lang = $lang ;
}
if ( $unrecognized ) {
2009-05-30 17:14:24 +00:00
$unrecognized = implode ( " \n " , $unrecognized );
2009-05-31 12:04:51 +00:00
cli_error ( get_string ( 'cliunknowoption' , 'admin' , $unrecognized ));
2009-05-27 09:52:25 +00:00
}
if ( $options [ 'help' ]) {
echo $help ;
die ;
}
//Print header
2015-06-17 22:48:14 +02:00
cli_logo ();
echo PHP_EOL ;
2009-05-30 17:14:24 +00:00
echo get_string ( 'cliinstallheader' , 'install' , $CFG -> target_release ) . " \n " ;
2009-05-27 09:52:25 +00:00
//Fist select language
if ( $interactive ) {
2009-05-30 17:14:24 +00:00
cli_separator ();
2012-10-07 21:03:44 +02:00
// Do not put the langs into columns because it is not compatible with RTL.
2010-04-10 07:24:56 +00:00
$default = $CFG -> lang ;
2015-09-15 22:08:04 +02:00
cli_heading ( get_string ( 'chooselanguagehead' , 'install' ));
if ( array_key_exists ( $default , $languages )) {
echo $default . ' - ' . $languages [ $default ] . " \n " ;
}
if ( $default !== 'en' ) {
echo 'en - English (en)' . " \n " ;
}
echo '? - ' . get_string ( 'availablelangs' , 'install' ) . " \n " ;
2010-04-10 07:24:56 +00:00
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $CFG -> lang );
2009-05-27 09:52:25 +00:00
$error = '' ;
do {
echo $error ;
$input = cli_input ( $prompt , $default );
2015-09-15 22:08:04 +02:00
if ( $input === '?' ) {
echo implode ( " \n " , $languages ) . " \n " ;
$error = " \n " ;
2009-05-27 09:52:25 +00:00
} else {
2015-09-15 22:08:04 +02:00
$input = clean_param ( $input , PARAM_SAFEDIR );
if ( ! array_key_exists ( $input , $languages )) {
$error = get_string ( 'cliincorrectvalueretry' , 'admin' ) . " \n " ;
} else {
$error = '' ;
}
2009-05-27 09:52:25 +00:00
}
} while ( $error !== '' );
2010-04-10 07:24:56 +00:00
$CFG -> lang = $input ;
2009-05-27 09:52:25 +00:00
} else {
2009-05-30 17:14:24 +00:00
// already selected and verified
2009-05-27 09:52:25 +00:00
}
2010-05-20 13:40:45 +00:00
// Set directorypermissions first
$chmod = octdec ( clean_param ( $options [ 'chmod' ], PARAM_INT ));
if ( $interactive ) {
cli_separator ();
2011-11-22 10:02:42 +01:00
cli_heading ( get_string ( 'datarootpermission' , 'install' ));
2010-05-20 13:40:45 +00:00
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , decoct ( $chmod ));
$error = '' ;
do {
echo $error ;
2010-07-30 11:58:02 +00:00
$input = cli_input ( $prompt , decoct ( $chmod ));
2010-05-20 13:40:45 +00:00
$input = octdec ( clean_param ( $input , PARAM_INT ));
if ( empty ( $input )) {
$error = get_string ( 'cliincorrectvalueretry' , 'admin' ) . " \n " ;
} else {
$error = '' ;
}
2010-07-30 11:58:02 +00:00
} while ( $error !== '' );
2010-05-20 13:40:45 +00:00
$chmod = $input ;
} else {
if ( empty ( $chmod )) {
$a = ( object ) array ( 'option' => 'chmod' , 'value' => decoct ( $chmod ));
cli_error ( get_string ( 'cliincorrectvalueerror' , 'admin' , $a ));
}
}
$CFG -> directorypermissions = $chmod ;
2013-08-04 10:18:10 +02:00
$CFG -> filepermissions = ( $CFG -> directorypermissions & 0666 );
$CFG -> umaskpermissions = (( $CFG -> directorypermissions & 0777 ) ^ 0777 );
2009-05-27 09:52:25 +00:00
//We need wwwroot before we test dataroot
$wwwroot = clean_param ( $options [ 'wwwroot' ], PARAM_URL );
$wwwroot = trim ( $wwwroot , '/' );
if ( $interactive ) {
2009-05-30 17:14:24 +00:00
cli_separator ();
cli_heading ( get_string ( 'wwwroot' , 'install' ));
2009-05-27 09:52:25 +00:00
if ( strpos ( $wwwroot , 'http' ) === 0 ) {
2009-05-30 17:14:24 +00:00
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $wwwroot );
2009-05-27 09:52:25 +00:00
} else {
2009-05-30 17:14:24 +00:00
$wwwroot = null ;
$prompt = get_string ( 'clitypevalue' , 'admin' );
2009-05-27 09:52:25 +00:00
}
$error = '' ;
do {
echo $error ;
$input = cli_input ( $prompt , $wwwroot );
$input = clean_param ( $input , PARAM_URL );
$input = trim ( $input , '/' );
if ( strpos ( $input , 'http' ) !== 0 ) {
2009-05-30 17:14:24 +00:00
$error = get_string ( 'cliincorrectvalueretry' , 'admin' ) . " \n " ;
2009-05-27 09:52:25 +00:00
} else {
$error = '' ;
}
} while ( $error !== '' );
$wwwroot = $input ;
} else {
if ( strpos ( $wwwroot , 'http' ) !== 0 ) {
2009-05-30 17:14:24 +00:00
$a = ( object ) array ( 'option' => 'wwwroot' , 'value' => $wwwroot );
cli_error ( get_string ( 'cliincorrectvalueerror' , 'admin' , $a ));
2009-05-27 09:52:25 +00:00
}
}
$CFG -> wwwroot = $wwwroot ;
2019-11-13 19:58:10 +01:00
$CFG -> httpswwwroot = $CFG -> wwwroot ;
2009-05-27 09:52:25 +00:00
//We need dataroot before lang download
2012-01-15 11:05:00 +01:00
$CFG -> dataroot = $options [ 'dataroot' ];
2009-05-27 09:52:25 +00:00
if ( $interactive ) {
2009-05-30 17:14:24 +00:00
cli_separator ();
2009-05-27 09:52:25 +00:00
$i = 0 ;
while ( is_dataroot_insecure ()) {
$parrent = dirname ( $CFG -> dataroot );
$i ++ ;
if ( $parrent == '/' or $parrent == '.' or preg_match ( '/^[a-z]:\\\?$/i' , $parrent ) or ( $i > 100 )) {
$CFG -> dataroot = '' ; //can not find secure location for dataroot
break ;
}
$CFG -> dataroot = dirname ( $parrent ) . '/moodledata' ;
}
2009-05-30 17:14:24 +00:00
cli_heading ( get_string ( 'dataroot' , 'install' ));
2009-05-27 09:52:25 +00:00
$error = '' ;
do {
if ( $CFG -> dataroot !== '' ) {
2009-05-30 17:14:24 +00:00
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $CFG -> dataroot );
2009-05-27 09:52:25 +00:00
} else {
2009-05-30 17:14:24 +00:00
$prompt = get_string ( 'clitypevalue' , 'admin' );
2009-05-27 09:52:25 +00:00
}
echo $error ;
$CFG -> dataroot = cli_input ( $prompt , $CFG -> dataroot );
if ( $CFG -> dataroot === '' ) {
2009-05-30 17:14:24 +00:00
$error = get_string ( 'cliincorrectvalueretry' , 'admin' ) . " \n " ;
2009-05-27 09:52:25 +00:00
} else if ( is_dataroot_insecure ()) {
$CFG -> dataroot = '' ;
2010-05-10 14:24:36 +00:00
$error = get_string ( 'pathsunsecuredataroot' , 'install' ) . " \n " ;
2009-05-27 09:52:25 +00:00
} else {
2010-08-29 14:15:43 +00:00
if ( install_init_dataroot ( $CFG -> dataroot , $CFG -> directorypermissions )) {
2009-05-27 09:52:25 +00:00
$error = '' ;
} else {
2010-05-10 15:59:21 +00:00
$a = ( object ) array ( 'dataroot' => $CFG -> dataroot );
$error = get_string ( 'pathserrcreatedataroot' , 'install' , $a ) . " \n " ;
2009-05-27 09:52:25 +00:00
}
}
} while ( $error !== '' );
} else {
if ( is_dataroot_insecure ()) {
2010-05-10 15:59:21 +00:00
cli_error ( get_string ( 'pathsunsecuredataroot' , 'install' ));
2009-05-30 17:14:24 +00:00
}
2010-08-29 14:15:43 +00:00
if ( ! install_init_dataroot ( $CFG -> dataroot , $CFG -> directorypermissions )) {
2010-05-10 15:59:21 +00:00
$a = ( object ) array ( 'dataroot' => $CFG -> dataroot );
cli_error ( get_string ( 'pathserrcreatedataroot' , 'install' , $a ));
2009-05-27 09:52:25 +00:00
}
}
2013-07-07 21:42:03 +02:00
$CFG -> tempdir = $CFG -> dataroot . '/temp' ;
2018-03-01 00:36:25 +01:00
$CFG -> backuptempdir = $CFG -> tempdir . '/backup' ;
2013-07-07 21:42:03 +02:00
$CFG -> cachedir = $CFG -> dataroot . '/cache' ;
$CFG -> localcachedir = $CFG -> dataroot . '/localcache' ;
2009-05-27 09:52:25 +00:00
2011-03-29 21:09:59 +02:00
// download required lang packs
if ( $CFG -> lang !== 'en' ) {
$installer = new lang_installer ( $CFG -> lang );
$results = $installer -> run ();
foreach ( $results as $langcode => $langstatus ) {
if ( $langstatus === lang_installer :: RESULT_DOWNLOADERROR ) {
$a = new stdClass ();
$a -> url = $installer -> lang_pack_url ( $langcode );
$a -> dest = $CFG -> dataroot . '/lang' ;
cli_problem ( get_string ( 'remotedownloaderror' , 'error' , $a ));
2009-05-27 09:52:25 +00:00
}
}
}
2010-05-10 14:46:59 +00:00
// switch the string_manager instance to stop using install/lang/
2010-04-10 07:24:56 +00:00
$CFG -> early_install_lang = false ;
2010-05-10 17:16:34 +00:00
$CFG -> langotherroot = $CFG -> dataroot . '/lang' ;
$CFG -> langlocalroot = $CFG -> dataroot . '/lang' ;
2010-05-10 14:46:59 +00:00
get_string_manager ( true );
2010-04-10 07:24:56 +00:00
2011-02-25 23:39:42 +01:00
// make sure we are installing stable release or require a confirmation
if ( isset ( $maturity )) {
if (( $maturity < MATURITY_STABLE ) and ! $options [ 'allow-unstable' ]) {
$maturitylevel = get_string ( 'maturity' . $maturity , 'admin' );
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'notice' ));
echo get_string ( 'maturitycorewarning' , 'admin' , $maturitylevel ) . PHP_EOL ;
2011-03-03 16:54:04 +01:00
echo get_string ( 'morehelp' ) . ': ' . get_docs_url ( 'admin/versions' ) . PHP_EOL ;
2011-03-20 19:25:47 +01:00
echo get_string ( 'continue' ) . PHP_EOL ;
2011-02-25 23:39:42 +01:00
$prompt = get_string ( 'cliyesnoprompt' , 'admin' );
$input = cli_input ( $prompt , '' , array ( get_string ( 'clianswerno' , 'admin' ), get_string ( 'cliansweryes' , 'admin' )));
if ( $input == get_string ( 'clianswerno' , 'admin' )) {
exit ( 1 );
}
} else {
2012-05-28 10:59:22 +02:00
cli_problem ( get_string ( 'maturitycorewarning' , 'admin' , $maturitylevel ));
cli_error ( get_string ( 'maturityallowunstable' , 'admin' ));
2011-02-25 23:39:42 +01:00
}
}
}
2009-05-27 09:52:25 +00:00
// ask for db type - show only drivers available
if ( $interactive ) {
$options [ 'dbtype' ] = strtolower ( $options [ 'dbtype' ]);
2009-05-30 17:14:24 +00:00
cli_separator ();
cli_heading ( get_string ( 'databasetypehead' , 'install' ));
2009-05-27 09:52:25 +00:00
foreach ( $databases as $type => $database ) {
2009-05-30 17:14:24 +00:00
echo " $type \n " ;
2009-05-27 09:52:25 +00:00
}
if ( ! empty ( $databases [ $options [ 'dbtype' ]])) {
2009-05-30 17:14:24 +00:00
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'dbtype' ]);
2009-05-27 09:52:25 +00:00
} else {
2009-05-30 17:14:24 +00:00
$prompt = get_string ( 'clitypevalue' , 'admin' );
2009-05-27 09:52:25 +00:00
}
$CFG -> dbtype = cli_input ( $prompt , $options [ 'dbtype' ], array_keys ( $databases ));
} else {
if ( empty ( $databases [ $options [ 'dbtype' ]])) {
2009-05-30 17:14:24 +00:00
$a = ( object ) array ( 'option' => 'dbtype' , 'value' => $options [ 'dbtype' ]);
cli_error ( get_string ( 'cliincorrectvalueerror' , 'admin' , $a ));
2009-05-27 09:52:25 +00:00
}
$CFG -> dbtype = $options [ 'dbtype' ];
}
$database = $databases [ $CFG -> dbtype ];
2019-08-20 15:40:38 +10:00
// We cannot do any validation until all DB connection data is provided.
$hintdatabase = '' ;
do {
echo $hintdatabase ;
// Ask for db host.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'databasehost' , 'install' ));
if ( $options [ 'dbhost' ] !== '' ) {
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'dbhost' ]);
} else {
$prompt = get_string ( 'clitypevalue' , 'admin' );
}
$CFG -> dbhost = cli_input ( $prompt , $options [ 'dbhost' ]);
2009-05-27 09:52:25 +00:00
} else {
2019-08-20 15:40:38 +10:00
$CFG -> dbhost = $options [ 'dbhost' ];
2009-05-27 09:52:25 +00:00
}
2019-08-20 15:40:38 +10:00
// Ask for db name.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'databasename' , 'install' ));
if ( $options [ 'dbname' ] !== '' ) {
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'dbname' ]);
} else {
$prompt = get_string ( 'clitypevalue' , 'admin' );
}
$CFG -> dbname = cli_input ( $prompt , $options [ 'dbname' ]);
2009-05-27 09:52:25 +00:00
} else {
2019-08-20 15:40:38 +10:00
$CFG -> dbname = $options [ 'dbname' ];
2009-05-27 09:52:25 +00:00
}
2019-08-20 15:40:38 +10:00
// Ask for db prefix.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'dbprefix' , 'install' ));
//TODO: solve somehow the prefix trouble for oci.
if ( $options [ 'prefix' ] !== '' ) {
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'prefix' ]);
} else {
$prompt = get_string ( 'clitypevalue' , 'admin' );
}
$CFG -> prefix = cli_input ( $prompt , $options [ 'prefix' ]);
2009-05-27 09:52:25 +00:00
} else {
2019-08-20 15:40:38 +10:00
$CFG -> prefix = $options [ 'prefix' ];
2009-05-27 09:52:25 +00:00
}
2019-08-20 15:40:38 +10:00
// Ask for db port.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'databaseport' , 'install' ));
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'dbport' ]);
$CFG -> dboptions [ 'dbport' ] = ( int ) cli_input ( $prompt , $options [ 'dbport' ]);
2009-05-27 09:52:25 +00:00
2019-08-20 15:40:38 +10:00
} else {
$CFG -> dboptions [ 'dbport' ] = ( int ) $options [ 'dbport' ];
}
if ( $CFG -> dboptions [ 'dbport' ] <= 0 ) {
$CFG -> dboptions [ 'dbport' ] = '' ;
}
2013-06-15 15:07:28 +02:00
2019-08-20 15:40:38 +10:00
// Ask for db socket.
if ( $CFG -> ostype === 'WINDOWS' ) {
$CFG -> dboptions [ 'dbsocket' ] = '' ;
2013-06-15 15:07:28 +02:00
2019-08-20 15:40:38 +10:00
} else if ( $interactive and empty ( $CFG -> dboptions [ 'dbport' ])) {
cli_separator ();
cli_heading ( get_string ( 'databasesocket' , 'install' ));
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'dbsocket' ]);
$CFG -> dboptions [ 'dbsocket' ] = cli_input ( $prompt , $options [ 'dbsocket' ]);
2013-06-15 15:07:28 +02:00
2019-08-20 15:40:38 +10:00
} else {
$CFG -> dboptions [ 'dbsocket' ] = $options [ 'dbsocket' ];
}
2013-06-15 15:07:28 +02:00
2019-08-20 15:40:38 +10:00
// Ask for db user.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'databaseuser' , 'install' ));
if ( $options [ 'dbuser' ] !== '' ) {
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'dbuser' ]);
} else {
$prompt = get_string ( 'clitypevalue' , 'admin' );
}
$CFG -> dbuser = cli_input ( $prompt , $options [ 'dbuser' ]);
2013-06-15 15:07:28 +02:00
2009-05-27 09:52:25 +00:00
} else {
2019-08-20 15:40:38 +10:00
$CFG -> dbuser = $options [ 'dbuser' ];
2009-05-27 09:52:25 +00:00
}
2019-08-20 15:40:38 +10:00
// Ask for db password.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'databasepass' , 'install' ));
2009-05-27 09:52:25 +00:00
if ( $options [ 'dbpass' ] !== '' ) {
2009-05-30 17:14:24 +00:00
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'dbpass' ]);
2009-05-27 09:52:25 +00:00
} else {
2009-05-30 17:14:24 +00:00
$prompt = get_string ( 'clitypevalue' , 'admin' );
2009-05-27 09:52:25 +00:00
}
$CFG -> dbpass = cli_input ( $prompt , $options [ 'dbpass' ]);
2019-08-20 15:40:38 +10:00
if ( function_exists ( 'distro_pre_create_db' )) { // Hook for distros needing to do something before DB creation.
$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 );
2011-08-22 19:45:58 +04:00
}
2019-08-20 15:40:38 +10:00
$hintdatabase = 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' ]));
2009-05-27 09:52:25 +00:00
2019-08-20 15:40:38 +10:00
} else {
$CFG -> dbpass = $options [ 'dbpass' ];
$hintdatabase = 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' ]));
if ( $hintdatabase !== '' ) {
cli_error ( get_string ( 'dbconnectionerror' , 'install' ));
}
2009-05-27 09:52:25 +00:00
}
2019-08-20 15:40:38 +10:00
} while ( $hintdatabase !== '' );
2009-05-27 09:52:25 +00:00
2020-07-14 14:27:58 +08:00
// If --skip-database option is provided, we do not need to ask for site fullname, shortname, adminuser, adminpass, adminemail.
// These fields will be requested during the database install part.
if ( ! $skipdatabase ) {
// Ask for fullname.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'fullsitename' , 'moodle' ));
2010-10-27 19:18:45 +00:00
2020-07-14 14:27:58 +08:00
if ( $options [ 'fullname' ] !== '' ) {
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'fullname' ]);
} else {
$prompt = get_string ( 'clitypevalue' , 'admin' );
}
do {
$options [ 'fullname' ] = cli_input ( $prompt , $options [ 'fullname' ]);
} while ( empty ( $options [ 'fullname' ]));
2010-10-27 19:18:45 +00:00
} else {
2020-07-14 14:27:58 +08:00
if ( empty ( $options [ 'fullname' ])) {
$a = ( object )[ 'option' => 'fullname' , 'value' => $options [ 'fullname' ]];
cli_error ( get_string ( 'cliincorrectvalueerror' , 'admin' , $a ));
}
2010-10-27 19:18:45 +00:00
}
2020-07-14 14:27:58 +08:00
// Ask for shortname.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'shortsitename' , 'moodle' ));
2010-10-27 19:18:45 +00:00
2020-07-14 14:27:58 +08:00
if ( $options [ 'shortname' ] !== '' ) {
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'shortname' ]);
} else {
$prompt = get_string ( 'clitypevalue' , 'admin' );
}
2010-10-27 19:18:45 +00:00
2020-07-14 14:27:58 +08:00
do {
$options [ 'shortname' ] = cli_input ( $prompt , $options [ 'shortname' ]);
} while ( empty ( $options [ 'shortname' ]));
2010-10-27 19:18:45 +00:00
} else {
2020-07-14 14:27:58 +08:00
if ( empty ( $options [ 'shortname' ])) {
$a = ( object )[ 'option' => 'shortname' , 'value' => $options [ 'shortname' ]];
cli_error ( get_string ( 'cliincorrectvalueerror' , 'admin' , $a ));
}
2010-10-27 19:18:45 +00:00
}
2020-07-14 14:27:58 +08:00
// Ask for admin user name.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'cliadminusername' , 'install' ));
if ( ! empty ( $options [ 'adminuser' ])) {
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'adminuser' ]);
} else {
$prompt = get_string ( 'clitypevalue' , 'admin' );
}
do {
$options [ 'adminuser' ] = cli_input ( $prompt , $options [ 'adminuser' ]);
} while ( empty ( $options [ 'adminuser' ]) or $options [ 'adminuser' ] === 'guest' );
} else {
if (( empty ( $options [ 'adminuser' ]) || $options [ 'adminuser' ] === 'guest' )) {
$a = ( object )[ 'option' => 'adminuser' , 'value' => $options [ 'adminuser' ]];
cli_error ( get_string ( 'cliincorrectvalueerror' , 'admin' , $a ));
}
2010-10-27 19:18:45 +00:00
}
2020-07-14 14:27:58 +08:00
// Ask for admin user password.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'cliadminpassword' , 'install' ));
2010-05-10 21:57:48 +00:00
$prompt = get_string ( 'clitypevalue' , 'admin' );
2020-07-14 14:27:58 +08:00
do {
$options [ 'adminpass' ] = cli_input ( $prompt );
} while ( empty ( $options [ 'adminpass' ]) or $options [ 'adminpass' ] === 'admin' );
} else {
if (( empty ( $options [ 'adminpass' ]) or $options [ 'adminpass' ] === 'admin' )) {
$a = ( object )[ 'option' => 'adminpass' , 'value' => $options [ 'adminpass' ]];
cli_error ( get_string ( 'cliincorrectvalueerror' , 'admin' , $a ));
}
2010-05-10 21:57:48 +00:00
}
2020-07-14 14:27:58 +08:00
// Ask for the admin email address.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'cliadminemail' , 'install' ));
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'adminemail' ]);
$options [ 'adminemail' ] = cli_input ( $prompt , $options [ 'adminemail' ]);
2010-05-10 21:57:48 +00:00
}
2020-07-14 14:27:58 +08:00
// Validate that the address provided was an e-mail address.
if ( ! empty ( $options [ 'adminemail' ]) && ! validate_email ( $options [ 'adminemail' ])) {
$a = ( object )[ 'option' => 'adminemail' , 'value' => $options [ 'adminemail' ]];
2009-05-30 17:14:24 +00:00
cli_error ( get_string ( 'cliincorrectvalueerror' , 'admin' , $a ));
2009-05-27 09:52:25 +00:00
}
}
2015-09-16 00:18:06 +02:00
// Ask for the upgrade key.
if ( $interactive ) {
cli_separator ();
cli_heading ( get_string ( 'upgradekeyset' , 'admin' ));
if ( $options [ 'upgradekey' ] !== '' ) {
$prompt = get_string ( 'clitypevaluedefault' , 'admin' , $options [ 'upgradekey' ]);
$options [ 'upgradekey' ] = cli_input ( $prompt , $options [ 'upgradekey' ]);
} else {
$prompt = get_string ( 'clitypevalue' , 'admin' );
$options [ 'upgradekey' ] = cli_input ( $prompt );
}
}
// Set the upgrade key if it was provided.
if ( $options [ 'upgradekey' ] !== '' ) {
$CFG -> upgradekey = $options [ 'upgradekey' ];
}
2020-07-14 14:27:58 +08:00
// The user does not also need to pass agree-license when --skip-database is provided as the user will need to accept
// the license again in the database install part.
if ( ! $skipdatabase ) {
if ( $interactive ) {
if ( ! $options [ 'agree-license' ]) {
cli_separator ();
cli_heading ( get_string ( 'copyrightnotice' ));
echo " Moodle - Modular Object-Oriented Dynamic Learning Environment \n " ;
echo get_string ( 'gpl3' ) . " \n \n " ;
echo get_string ( 'doyouagree' ) . " \n " ;
$prompt = get_string ( 'cliyesnoprompt' , 'admin' );
$input = cli_input ( $prompt , '' , array ( get_string ( 'clianswerno' , 'admin' ), get_string ( 'cliansweryes' , 'admin' )));
if ( $input == get_string ( 'clianswerno' , 'admin' )) {
exit ( 1 );
}
}
} else {
if ( ! $options [ 'agree-license' ] && ! $skipdatabase ) {
cli_error ( get_string ( 'climustagreelicense' , 'install' ));
2009-05-30 19:02:04 +00:00
}
2009-05-27 09:52:25 +00:00
}
}
// Finally we have all info needed for config.php
$configphp = install_generate_configphp ( $database , $CFG );
umask ( 0137 );
if (( $fh = fopen ( $configfile , 'w' )) !== false ) {
fwrite ( $fh , $configphp );
fclose ( $fh );
}
if ( ! file_exists ( $configfile )) {
cli_error ( 'Can not create config file.' );
}
2011-10-27 12:47:05 +02:00
// remember selected language
$installlang = $CFG -> lang ;
// return back to original dir before executing setup.php which changes the dir again
chdir ( $olddir );
// We have config.php, it is a real php script from now on :-)
require ( $configfile );
// use selected language
$CFG -> lang = $installlang ;
$SESSION -> lang = $CFG -> lang ;
require ( " $CFG->dirroot /version.php " );
2011-10-19 13:47:02 +01:00
// Test environment first.
2011-10-27 12:47:05 +02:00
require_once ( $CFG -> libdir . '/environmentlib.php' );
2011-10-19 13:47:02 +01:00
list ( $envstatus , $environment_results ) = check_moodle_environment ( normalize_version ( $release ), ENV_SELECT_RELEASE );
if ( ! $envstatus ) {
$errors = environment_get_errors ( $environment_results );
cli_heading ( get_string ( 'environment' , 'admin' ));
foreach ( $errors as $error ) {
list ( $info , $report ) = $error ;
echo " !! $info !! \n $report\n\n " ;
}
exit ( 1 );
}
2011-10-19 15:43:49 +01:00
// Test plugin dependencies.
2012-05-24 14:31:13 +02:00
$failed = array ();
2013-10-04 22:40:44 +02:00
if ( ! core_plugin_manager :: instance () -> all_plugins_ok ( $version , $failed )) {
2012-05-24 14:31:13 +02:00
cli_problem ( get_string ( 'pluginscheckfailed' , 'admin' , array ( 'pluginslist' => implode ( ', ' , array_unique ( $failed )))));
2011-10-19 13:47:02 +01:00
cli_error ( get_string ( 'pluginschecktodo' , 'admin' ));
}
2020-07-14 14:27:58 +08:00
if ( ! $skipdatabase ) {
2014-06-26 14:27:07 -07:00
install_cli_database ( $options , $interactive );
2017-06-19 05:22:58 +00:00
// This needs to happen at the end to ensure it occurs after all caches
// have been purged for the last time.
// This will build a cached version of the current theme for the user
// to immediately start browsing the site.
require_once ( $CFG -> libdir . '/upgradelib.php' );
upgrade_themes ();
2014-06-26 14:27:07 -07:00
} else {
echo get_string ( 'cliskipdatabase' , 'install' ) . " \n " ;
}
2009-05-27 09:52:25 +00:00
2009-05-30 19:02:04 +00:00
echo get_string ( 'cliinstallfinished' , 'install' ) . " \n " ;
exit ( 0 ); // 0 means success