update.php

  1. drupal
    1. 4.6 update.php
    2. 4.7 update.php
    3. 5 update.php
    4. 6 update.php
    5. 7 update.php
    6. 8 core/lib/Drupal/Core/Database/Driver/sqlite/Update.php
    7. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Update.php
    8. 8 core/lib/Drupal/Core/Database/Query/Update.php
    9. 8 core/update.php
    10. 8 core/lib/Drupal/Core/Database/Driver/mysql/Update.php

Administrative page for handling updates from one Drupal version to another.

Point your browser to "http://www.example.com/core/update.php" and follow the instructions.

If you are not logged in using either the site maintenance account or an account with the "Administer software updates" permission, you will need to modify the access check statement inside your settings.php file. After finishing the upgrade, be sure to open settings.php again, and change it back to its original state!

Constants

NameDescription
DRUPAL_ROOTRoot directory of Drupal installation.
MAINTENANCE_MODEGlobal flag indicating that update.php is being run.

Functions & methods

NameDescription
update_access_allowedDetermines if the current user is allowed to run update.php.
update_access_denied_page
update_check_requirementsCheck update requirements and report any errors or (optionally) warnings.
update_extra_requirementsReturns (and optionally stores) extra requirements that only apply during particular parts of the update.php process.
update_helpful_links
update_info_page
update_results_page
update_script_selection_form
update_selection_page
update_task_listAdd the update task list to the current page.

File

core/update.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page for handling updates from one Drupal version to another.
  5. *
  6. * Point your browser to "http://www.example.com/core/update.php" and follow the
  7. * instructions.
  8. *
  9. * If you are not logged in using either the site maintenance account or an
  10. * account with the "Administer software updates" permission, you will need to
  11. * modify the access check statement inside your settings.php file. After
  12. * finishing the upgrade, be sure to open settings.php again, and change it
  13. * back to its original state!
  14. */
  15. // Change the directory to the Drupal root.
  16. chdir('..');
  17. /**
  18. * Root directory of Drupal installation.
  19. */
  20. define('DRUPAL_ROOT', getcwd());
  21. // Exit early if running an incompatible PHP version to avoid fatal errors.
  22. // The minimum version is specified explicitly, as DRUPAL_MINIMUM_PHP is not
  23. // yet available. It is defined in bootstrap.inc, but it is not possible to
  24. // load that file yet as it would cause a fatal error on older versions of PHP.
  25. if (version_compare(PHP_VERSION, '5.3.3') < 0) {
  26. print 'Your PHP installation is too old. Drupal requires at least PHP 5.3.3. See the <a href="http://drupal.org/requirements">system requirements</a> page for more information.';
  27. exit;
  28. }
  29. /**
  30. * Global flag indicating that update.php is being run.
  31. *
  32. * When this flag is set, various operations do not take place, such as invoking
  33. * hook_init() and hook_exit(), css/js preprocessing, and translation.
  34. *
  35. * This constant is defined using define() instead of const so that PHP
  36. * versions older than 5.3 can display the proper PHP requirements instead of
  37. * causing a fatal error.
  38. */
  39. define('MAINTENANCE_MODE', 'update');
  40. function update_selection_page() {
  41. drupal_set_title('Drupal database update');
  42. $elements = drupal_get_form('update_script_selection_form');
  43. $output = drupal_render($elements);
  44. update_task_list('select');
  45. return $output;
  46. }
  47. function update_script_selection_form($form, &$form_state) {
  48. $count = 0;
  49. $incompatible_count = 0;
  50. $form['start'] = array(
  51. '#tree' => TRUE,
  52. '#type' => 'fieldset',
  53. '#collapsed' => TRUE,
  54. '#collapsible' => TRUE,
  55. );
  56. // Ensure system.module's updates appear first.
  57. $form['start']['system'] = array();
  58. $updates = update_get_update_list();
  59. $starting_updates = array();
  60. $incompatible_updates_exist = FALSE;
  61. foreach ($updates as $module => $update) {
  62. if (!isset($update['start'])) {
  63. $form['start'][$module] = array(
  64. '#type' => 'item',
  65. '#title' => $module . ' module',
  66. '#markup' => $update['warning'],
  67. '#prefix' => '<div class="messages warning">',
  68. '#suffix' => '</div>',
  69. );
  70. $incompatible_updates_exist = TRUE;
  71. continue;
  72. }
  73. if (!empty($update['pending'])) {
  74. $starting_updates[$module] = $update['start'];
  75. $form['start'][$module] = array(
  76. '#type' => 'hidden',
  77. '#value' => $update['start'],
  78. );
  79. $form['start'][$module . '_updates'] = array(
  80. '#theme' => 'item_list',
  81. '#items' => $update['pending'],
  82. '#title' => $module . ' module',
  83. );
  84. }
  85. if (isset($update['pending'])) {
  86. $count = $count + count($update['pending']);
  87. }
  88. }
  89. // Find and label any incompatible updates.
  90. foreach (update_resolve_dependencies($starting_updates) as $function => $data) {
  91. if (!$data['allowed']) {
  92. $incompatible_updates_exist = TRUE;
  93. $incompatible_count++;
  94. $module_update_key = $data['module'] . '_updates';
  95. if (isset($form['start'][$module_update_key]['#items'][$data['number']])) {
  96. $text = $data['missing_dependencies'] ? 'This update will been skipped due to the following missing dependencies: <em>' . implode(', ', $data['missing_dependencies']) . '</em>' : "This update will be skipped due to an error in the module's code.";
  97. $form['start'][$module_update_key]['#items'][$data['number']] .= '<div class="warning">' . $text . '</div>';
  98. }
  99. // Move the module containing this update to the top of the list.
  100. $form['start'] = array($module_update_key => $form['start'][$module_update_key]) + $form['start'];
  101. }
  102. }
  103. // Warn the user if any updates were incompatible.
  104. if ($incompatible_updates_exist) {
  105. drupal_set_message('Some of the pending updates cannot be applied because their dependencies were not met.', 'warning');
  106. }
  107. if (empty($count)) {
  108. drupal_set_message(t('No pending updates.'));
  109. unset($form);
  110. $form['links'] = array(
  111. '#markup' => theme('item_list', array('items' => update_helpful_links())),
  112. );
  113. // No updates to run, so caches won't get flushed later. Clear them now.
  114. drupal_flush_all_caches();
  115. }
  116. else {
  117. $form['help'] = array(
  118. '#markup' => '<p>The version of Drupal you are updating from has been automatically detected.</p>',
  119. '#weight' => -5,
  120. );
  121. if ($incompatible_count) {
  122. $form['start']['#title'] = format_plural(
  123. $count,
  124. '1 pending update (@number_applied to be applied, @number_incompatible skipped)',
  125. '@count pending updates (@number_applied to be applied, @number_incompatible skipped)',
  126. array('@number_applied' => $count - $incompatible_count, '@number_incompatible' => $incompatible_count)
  127. );
  128. }
  129. else {
  130. $form['start']['#title'] = format_plural($count, '1 pending update', '@count pending updates');
  131. }
  132. $form['has_js'] = array(
  133. '#type' => 'hidden',
  134. '#default_value' => FALSE,
  135. );
  136. $form['actions'] = array('#type' => 'actions');
  137. $form['actions']['submit'] = array(
  138. '#type' => 'submit',
  139. '#value' => 'Apply pending updates',
  140. );
  141. }
  142. return $form;
  143. }
  144. function update_helpful_links() {
  145. // NOTE: we can't use l() here because the URL would point to
  146. // 'core/update.php?q=admin'.
  147. $links[] = '<a href="' . base_path() . '">Front page</a>';
  148. if (user_access('access administration pages')) {
  149. $links[] = '<a href="' . base_path() . '?q=admin">Administration pages</a>';
  150. }
  151. return $links;
  152. }
  153. function update_results_page() {
  154. drupal_set_title('Drupal database update');
  155. $links = update_helpful_links();
  156. update_task_list();
  157. // Report end result.
  158. if (module_exists('dblog') && user_access('access site reports')) {
  159. $log_message = ' All errors have been <a href="' . base_path() . '?q=admin/reports/dblog">logged</a>.';
  160. }
  161. else {
  162. $log_message = ' All errors have been logged.';
  163. }
  164. if ($_SESSION['update_success']) {
  165. $output = '<p>Updates were attempted. If you see no failures below, you may proceed happily back to your <a href="' . base_path() . '">site</a>. Otherwise, you may need to update your database manually.' . $log_message . '</p>';
  166. }
  167. else {
  168. list($module, $version) = array_pop(reset($_SESSION['updates_remaining']));
  169. $output = '<p class="error">The update process was aborted prematurely while running <strong>update #' . $version . ' in ' . $module . '.module</strong>.' . $log_message;
  170. if (module_exists('dblog')) {
  171. $output .= ' You may need to check the <code>watchdog</code> database table manually.';
  172. }
  173. $output .= '</p>';
  174. }
  175. if (!empty($GLOBALS['update_free_access'])) {
  176. $output .= "<p><strong>Reminder: don't forget to set the <code>\$update_free_access</code> value in your <code>settings.php</code> file back to <code>FALSE</code>.</strong></p>";
  177. }
  178. $output .= theme('item_list', array('items' => $links));
  179. // Output a list of queries executed.
  180. if (!empty($_SESSION['update_results'])) {
  181. $all_messages = '';
  182. foreach ($_SESSION['update_results'] as $module => $updates) {
  183. if ($module != '#abort') {
  184. $module_has_message = FALSE;
  185. $query_messages = '';
  186. foreach ($updates as $number => $queries) {
  187. $messages = array();
  188. foreach ($queries as $query) {
  189. // If there is no message for this update, don't show anything.
  190. if (empty($query['query'])) {
  191. continue;
  192. }
  193. if ($query['success']) {
  194. $messages[] = '<li class="success">' . $query['query'] . '</li>';
  195. }
  196. else {
  197. $messages[] = '<li class="failure"><strong>Failed:</strong> ' . $query['query'] . '</li>';
  198. }
  199. }
  200. if ($messages) {
  201. $module_has_message = TRUE;
  202. $query_messages .= '<h4>Update #' . $number . "</h4>\n";
  203. $query_messages .= '<ul>' . implode("\n", $messages) . "</ul>\n";
  204. }
  205. }
  206. // If there were any messages in the queries then prefix them with the
  207. // module name and add it to the global message list.
  208. if ($module_has_message) {
  209. $all_messages .= '<h3>' . $module . " module</h3>\n" . $query_messages;
  210. }
  211. }
  212. }
  213. if ($all_messages) {
  214. $output .= '<div id="update-results"><h2>The following updates returned messages</h2>';
  215. $output .= $all_messages;
  216. $output .= '</div>';
  217. }
  218. }
  219. unset($_SESSION['update_results']);
  220. unset($_SESSION['update_success']);
  221. return $output;
  222. }
  223. function update_info_page() {
  224. // Change query-strings on css/js files to enforce reload for all users.
  225. _drupal_flush_css_js();
  226. // Flush the cache of all data for the update status module.
  227. if (db_table_exists('cache_update')) {
  228. cache('update')->flush();
  229. }
  230. update_task_list('info');
  231. drupal_set_title('Drupal database update');
  232. $token = drupal_get_token('update');
  233. $output = '<p>Use this utility to update your database whenever a new release of Drupal or a module is installed.</p><p>For more detailed information, see the <a href="http://drupal.org/upgrade">upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>';
  234. $output .= "<ol>\n";
  235. $output .= "<li><strong>Back up your database</strong>. This process will change your database values and in case of emergency you may need to revert to a backup.</li>\n";
  236. $output .= "<li><strong>Back up your code</strong>. Hint: when backing up module code, do not leave that backup in the 'modules' or 'sites/*/modules' directories as this may confuse Drupal's auto-discovery mechanism.</li>\n";
  237. $output .= '<li>Put your site into <a href="' . base_path() . '?q=admin/config/development/maintenance">maintenance mode</a>.</li>' . "\n";
  238. $output .= "<li>Install your new files in the appropriate location, as described in the handbook.</li>\n";
  239. $output .= "</ol>\n";
  240. $output .= "<p>When you have performed the steps above, you may proceed.</p>\n";
  241. $form_action = check_url(drupal_current_script_url(array('op' => 'selection', 'token' => $token)));
  242. $output .= '<form method="post" action="' . $form_action . '"><p><input type="submit" value="Continue" class="form-submit" /></p></form>';
  243. $output .= "\n";
  244. return $output;
  245. }
  246. function update_access_denied_page() {
  247. drupal_add_http_header('Status', '403 Forbidden');
  248. watchdog('access denied', 'update.php', NULL, WATCHDOG_WARNING);
  249. drupal_set_title('Access denied');
  250. return '<p>Access denied. You are not authorized to access this page. Log in using either an account with the <em>administer software updates</em> permission or the site maintenance account (the account you created during installation). If you cannot log in, you will have to edit <code>settings.php</code> to bypass this access check. To do this:</p>
  251. <ol>
  252. <li>With a text editor find the settings.php file on your system. From the main Drupal directory that you installed all the files into, go to <code>sites/your_site_name</code> if such directory exists, or else to <code>sites/default</code> which applies otherwise.</li>
  253. <li>There is a line inside your settings.php file that says <code>$update_free_access = FALSE;</code>. Change it to <code>$update_free_access = TRUE;</code>.</li>
  254. <li>As soon as the update.php script is done, you must change the settings.php file back to its original form with <code>$update_free_access = FALSE;</code>.</li>
  255. <li>To avoid having this problem in the future, remember to log in to your website using either an account with the <em>administer software updates</em> permission or the site maintenance account (the account you created during installation) before you backup your database at the beginning of the update process.</li>
  256. </ol>';
  257. }
  258. /**
  259. * Determines if the current user is allowed to run update.php.
  260. *
  261. * @return
  262. * TRUE if the current user should be granted access, or FALSE otherwise.
  263. */
  264. function update_access_allowed() {
  265. global $update_free_access, $user;
  266. // Allow the global variable in settings.php to override the access check.
  267. if (!empty($update_free_access)) {
  268. return TRUE;
  269. }
  270. // Calls to user_access() might fail during the Drupal 6 to 7 update process,
  271. // so we fall back on requiring that the user be logged in as user #1.
  272. try {
  273. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'user') . '/user.module';
  274. return user_access('administer software updates');
  275. }
  276. catch (Exception $e) {
  277. return ($user->uid == 1);
  278. }
  279. }
  280. /**
  281. * Add the update task list to the current page.
  282. */
  283. function update_task_list($active = NULL) {
  284. // Default list of tasks.
  285. $tasks = array(
  286. 'requirements' => 'Verify requirements',
  287. 'info' => 'Overview',
  288. 'select' => 'Review updates',
  289. 'run' => 'Run updates',
  290. 'finished' => 'Review log',
  291. );
  292. drupal_add_region_content('sidebar_first', theme('task_list', array('items' => $tasks, 'active' => $active)));
  293. }
  294. /**
  295. * Returns (and optionally stores) extra requirements that only apply during
  296. * particular parts of the update.php process.
  297. */
  298. function update_extra_requirements($requirements = NULL) {
  299. static $extra_requirements = array();
  300. if (isset($requirements)) {
  301. $extra_requirements += $requirements;
  302. }
  303. return $extra_requirements;
  304. }
  305. /**
  306. * Check update requirements and report any errors or (optionally) warnings.
  307. *
  308. * @param $skip_warnings
  309. * (optional) If set to TRUE, requirement warnings will be ignored, and a
  310. * report will only be issued if there are requirement errors. Defaults to
  311. * FALSE.
  312. */
  313. function update_check_requirements($skip_warnings = FALSE) {
  314. // Check requirements of all loaded modules.
  315. $requirements = module_invoke_all('requirements', 'update');
  316. $requirements += update_extra_requirements();
  317. $severity = drupal_requirements_severity($requirements);
  318. // If there are errors, always display them. If there are only warnings, skip
  319. // them if the caller has indicated they should be skipped.
  320. if ($severity == REQUIREMENT_ERROR || ($severity == REQUIREMENT_WARNING && !$skip_warnings)) {
  321. update_task_list('requirements');
  322. drupal_set_title('Requirements problem');
  323. $status_report = theme('status_report', array('requirements' => $requirements));
  324. $status_report .= 'Check the messages and <a href="' . check_url(drupal_requirements_url($severity)) . '">try again</a>.';
  325. print theme('update_page', array('content' => $status_report));
  326. exit();
  327. }
  328. }
  329. // Some unavoidable errors happen because the database is not yet up-to-date.
  330. // Our custom error handler is not yet installed, so we just suppress them.
  331. ini_set('display_errors', FALSE);
  332. // We prepare a minimal bootstrap for the update requirements check to avoid
  333. // reaching the PHP memory limit.
  334. require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
  335. require_once DRUPAL_ROOT . '/core/includes/update.inc';
  336. require_once DRUPAL_ROOT . '/core/includes/common.inc';
  337. require_once DRUPAL_ROOT . '/core/includes/file.inc';
  338. require_once DRUPAL_ROOT . '/core/includes/unicode.inc';
  339. require_once DRUPAL_ROOT . '/core/includes/schema.inc';
  340. update_prepare_d8_bootstrap();
  341. // Determine if the current user has access to run update.php.
  342. drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
  343. // The interface language global has been renamed in D8, we must ensure that it
  344. // contains a valid value while language settings are upgraded.
  345. // @todo Remove this globals reference entirely: http://drupal.org/node/1510686
  346. $GLOBALS[LANGUAGE_TYPE_INTERFACE] = language_default();
  347. // Ensure the default language is properly registered within the Dependency
  348. // Injection container during the upgrade process.
  349. $default = language_default();
  350. drupal_container()->register(LANGUAGE_TYPE_INTERFACE, 'Drupal\\Core\\Language\\Language')
  351. ->addMethodCall('extend', array($default));
  352. // Only allow the requirements check to proceed if the current user has access
  353. // to run updates (since it may expose sensitive information about the site's
  354. // configuration).
  355. $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
  356. if (empty($op) && update_access_allowed()) {
  357. require_once DRUPAL_ROOT . '/core/includes/install.inc';
  358. require_once DRUPAL_ROOT . '/core/modules/system/system.install';
  359. // Load module basics.
  360. include_once DRUPAL_ROOT . '/core/includes/module.inc';
  361. $module_list['system']['filename'] = 'core/modules/system/system.module';
  362. module_list(TRUE, FALSE, FALSE, $module_list);
  363. drupal_load('module', 'system');
  364. // Reset the module_implements() cache so that any new hook implementations
  365. // in updated code are picked up.
  366. module_implements_reset();
  367. // Set up $language, since the installer components require it.
  368. drupal_language_initialize();
  369. // Set up theme system for the maintenance page.
  370. drupal_maintenance_theme();
  371. // Check the update requirements for Drupal. Only report on errors at this
  372. // stage, since the real requirements check happens further down.
  373. update_check_requirements(TRUE);
  374. // Redirect to the update information page if all requirements were met.
  375. install_goto('core/update.php?op=info');
  376. }
  377. // update_fix_d8_requirements() needs to run before bootstrapping beyond path.
  378. // So bootstrap to DRUPAL_BOOTSTRAP_LANGUAGE then include unicode.inc.
  379. drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
  380. include_once DRUPAL_ROOT . '/core/includes/unicode.inc';
  381. update_fix_d8_requirements();
  382. // Now proceed with a full bootstrap.
  383. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  384. drupal_maintenance_theme();
  385. // Turn error reporting back on. From now on, only fatal errors (which are
  386. // not passed through the error handler) will cause a message to be printed.
  387. ini_set('display_errors', TRUE);
  388. // Only proceed with updates if the user is allowed to run them.
  389. if (update_access_allowed()) {
  390. include_once DRUPAL_ROOT . '/core/includes/install.inc';
  391. include_once DRUPAL_ROOT . '/core/includes/batch.inc';
  392. drupal_load_updates();
  393. update_fix_compatibility();
  394. // Check the update requirements for all modules. If there are warnings, but
  395. // no errors, skip reporting them if the user has provided a URL parameter
  396. // acknowledging the warnings and indicating a desire to continue anyway. See
  397. // drupal_requirements_url().
  398. $skip_warnings = !empty($_GET['continue']);
  399. update_check_requirements($skip_warnings);
  400. $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
  401. switch ($op) {
  402. // update.php ops.
  403. case 'selection':
  404. if (isset($_GET['token']) && $_GET['token'] == drupal_get_token('update')) {
  405. $output = update_selection_page();
  406. break;
  407. }
  408. case 'Apply pending updates':
  409. if (isset($_GET['token']) && $_GET['token'] == drupal_get_token('update')) {
  410. // Generate absolute URLs for the batch processing (using $base_root),
  411. // since the batch API will pass them to url() which does not handle
  412. // update.php correctly by default.
  413. $batch_url = $base_root . drupal_current_script_url();
  414. $redirect_url = $base_root . drupal_current_script_url(array('op' => 'results'));
  415. update_batch($_POST['start'], $redirect_url, $batch_url);
  416. break;
  417. }
  418. case 'info':
  419. $output = update_info_page();
  420. break;
  421. case 'results':
  422. $output = update_results_page();
  423. break;
  424. // Regular batch ops : defer to batch processing API.
  425. default:
  426. update_task_list('run');
  427. $output = _batch_page();
  428. break;
  429. }
  430. }
  431. else {
  432. $output = update_access_denied_page();
  433. }
  434. if (isset($output) && $output) {
  435. // Explicitly start a session so that the update.php token will be accepted.
  436. drupal_session_start();
  437. // We defer the display of messages until all updates are done.
  438. $progress_page = ($batch = batch_get()) && isset($batch['running']);
  439. print theme('update_page', array('content' => $output, 'show_messages' => !$progress_page));
  440. }
Login or register to post comments