upgrade.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 script creates config.php file and prepares database.
  18. *
  19. * This script is not intended for beginners!
  20. * Potential problems:
  21. * - su to apache account or sudo before execution
  22. * - not compatible with Windows platform
  23. *
  24. * @package core
  25. * @subpackage cli
  26. * @copyright 2009 Petr Skoda (http://skodak.org)
  27. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28. */
  29. // Force OPcache reset if used, we do not want any stale caches
  30. // when detecting if upgrade necessary or when running upgrade.
  31. if (function_exists('opcache_reset') and !isset($_SERVER['REMOTE_ADDR'])) {
  32. opcache_reset();
  33. }
  34. define('CLI_SCRIPT', true);
  35. define('CACHE_DISABLE_ALL', true);
  36. require(__DIR__.'/../../config.php');
  37. require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
  38. require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
  39. require_once($CFG->libdir.'/clilib.php'); // cli only functions
  40. require_once($CFG->libdir.'/environmentlib.php');
  41. // now get cli options
  42. $lang = isset($SESSION->lang) ? $SESSION->lang : $CFG->lang;
  43. list($options, $unrecognized) = cli_get_params(
  44. array(
  45. 'non-interactive' => false,
  46. 'allow-unstable' => false,
  47. 'help' => false,
  48. 'lang' => $lang
  49. ),
  50. array(
  51. 'h' => 'help'
  52. )
  53. );
  54. if ($options['lang']) {
  55. $SESSION->lang = $options['lang'];
  56. }
  57. $interactive = empty($options['non-interactive']);
  58. if ($unrecognized) {
  59. $unrecognized = implode("\n ", $unrecognized);
  60. cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  61. }
  62. if ($options['help']) {
  63. $help =
  64. "Command line Moodle upgrade.
  65. Please note you must execute this script with the same uid as apache!
  66. Site defaults may be changed via local/defaults.php.
  67. Options:
  68. --non-interactive No interactive questions or confirmations
  69. --allow-unstable Upgrade even if the version is not marked as stable yet,
  70. required in non-interactive mode.
  71. --lang=CODE Set preferred language for CLI output. Defaults to the
  72. site language if not set. Defaults to 'en' if the lang
  73. parameter is invalid or if the language pack is not
  74. installed.
  75. -h, --help Print out this help
  76. Example:
  77. \$sudo -u www-data /usr/bin/php admin/cli/upgrade.php
  78. "; //TODO: localize - to be translated later when everything is finished
  79. echo $help;
  80. die;
  81. }
  82. if (empty($CFG->version)) {
  83. cli_error(get_string('missingconfigversion', 'debug'));
  84. }
  85. require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
  86. $CFG->target_release = $release; // used during installation and upgrades
  87. if ($version < $CFG->version) {
  88. cli_error(get_string('downgradedcore', 'error'));
  89. }
  90. $oldversion = "$CFG->release ($CFG->version)";
  91. $newversion = "$release ($version)";
  92. if (!moodle_needs_upgrading()) {
  93. cli_error(get_string('cliupgradenoneed', 'core_admin', $newversion), 0);
  94. }
  95. // Test environment first.
  96. list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
  97. if (!$envstatus) {
  98. $errors = environment_get_errors($environment_results);
  99. cli_heading(get_string('environment', 'admin'));
  100. foreach ($errors as $error) {
  101. list($info, $report) = $error;
  102. echo "!! $info !!\n$report\n\n";
  103. }
  104. exit(1);
  105. }
  106. // Test plugin dependencies.
  107. $failed = array();
  108. if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
  109. cli_problem(get_string('pluginscheckfailed', 'admin', array('pluginslist' => implode(', ', array_unique($failed)))));
  110. cli_error(get_string('pluginschecktodo', 'admin'));
  111. }
  112. if ($interactive) {
  113. $a = new stdClass();
  114. $a->oldversion = $oldversion;
  115. $a->newversion = $newversion;
  116. echo cli_heading(get_string('databasechecking', '', $a)) . PHP_EOL;
  117. }
  118. // make sure we are upgrading to a stable release or display a warning
  119. if (isset($maturity)) {
  120. if (($maturity < MATURITY_STABLE) and !$options['allow-unstable']) {
  121. $maturitylevel = get_string('maturity'.$maturity, 'admin');
  122. if ($interactive) {
  123. cli_separator();
  124. cli_heading(get_string('notice'));
  125. echo get_string('maturitycorewarning', 'admin', $maturitylevel) . PHP_EOL;
  126. echo get_string('morehelp') . ': ' . get_docs_url('admin/versions') . PHP_EOL;
  127. cli_separator();
  128. } else {
  129. cli_problem(get_string('maturitycorewarning', 'admin', $maturitylevel));
  130. cli_error(get_string('maturityallowunstable', 'admin'));
  131. }
  132. }
  133. }
  134. if ($interactive) {
  135. echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n";
  136. $prompt = get_string('cliyesnoprompt', 'admin');
  137. $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
  138. if ($input == get_string('clianswerno', 'admin')) {
  139. exit(1);
  140. }
  141. }
  142. if ($version > $CFG->version) {
  143. // We purge all of MUC's caches here.
  144. // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
  145. // This ensures a real config object is loaded and the stores will be purged.
  146. // This is the only way we can purge custom caches such as memcache or APC.
  147. // Note: all other calls to caches will still used the disabled API.
  148. cache_helper::purge_all(true);
  149. upgrade_core($version, true);
  150. }
  151. set_config('release', $release);
  152. set_config('branch', $branch);
  153. // unconditionally upgrade
  154. upgrade_noncore(true);
  155. // log in as admin - we need doanything permission when applying defaults
  156. \core\session\manager::set_user(get_admin());
  157. // apply all default settings, just in case do it twice to fill all defaults
  158. admin_apply_default_settings(NULL, false);
  159. admin_apply_default_settings(NULL, false);
  160. echo get_string('cliupgradefinished', 'admin')."\n";
  161. exit(0); // 0 means success