PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/cli/install_database.php

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