comparison update.php @ 0:ff03f76ab3fe

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