update.php 19 KB

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