Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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