update.php 19 KB

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