#!/usr/bin/php " Example: {$script} "http://mysite.org/node" All arguments are long options. -h, --help This page. -r, --root Set the working directory for the script to the specified path. To execute Drupal this has to be the root directory of your Drupal installation, f.e. /home/www/foo/drupal (assuming Drupal running on Unix). Current directory is not required. Use surrounding quotation marks on Windows. -s, --site Used to sprecify with site will be used for the upgrade. If no site is selected then default will be used. -l, --list List all pending updates. If there are updates available then the script with exit with a 1 return flag -u, --update Process all pending updates. If there are any updates are not processed successfully then the script with exit with a return flag of 1. -m, --maintenance Set the site into maintenance while the updates are being run. If there are any failures, the site will remain in maintenance mode. An optional message can be specified which will set the message on the maintenance screen. -d, --dry-run No updates will be run but the system will pretend to do the updates -v, --verbose This option displays the options as they are set, but will produce errors from setting the session. --versions List the versions of all the modules in the system. To run this script without --root argument invoke it from the root directory of your Drupal installation with ./scripts/{$script} \n EOF; if (version_compare(phpversion(), '5.3.0', 'le')) { echo "Warning: This version of PHP doesn't support long options\n"; } exit; } // define default settings $_SERVER['HTTP_HOST'] = 'default'; $_SERVER['PHP_SELF'] = '/update.php'; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['SERVER_SOFTWARE'] = 'PHP CLI'; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['QUERY_STRING'] = ''; $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/'; $_SERVER['SCRIPT_NAME'] = '/'. basename($_SERVER['SCRIPT_NAME']); // toggle verbose mode $_verbose_mode = isset($args['h']) || isset($args['help']) ? TRUE : FALSE; $dryrun = FALSE; $maintenance = FALSE; $message = NULL; // parse invocation arguments if (isset($args['r']) || isset($args['root'])) { // change working directory $path = isset($args['r']) ? $args['r'] : $args['root']; if (is_dir($path)) { chdir($path); if ($_verbose_mode) { echo "cwd changed to: {$path}\n"; } } else { echo "\nERROR: {$path} not found.\n\n"; } } if (isset($args['s']) || isset($args['site'])) { $site = isset($args['s']) ? $args['s'] : $args['site']; if (file_exists('./sites/'. $site)) { $_SERVER['HTTP_HOST'] = array_shift($site); $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/update.php'; } else { echo "ERROR: Unable to locate site {$site}\n"; exit(1); } } $dryrun = (isset($args['s']) || isset($args['site'])) ? TRUE : FALSE; if (isset($args['m']) || isset($args['maintenance'])) { $maintenance = TRUE; $opt = isset($args['m']) ? $args['m'] : $args['maintenance']; if (is_string($opt)) { $message = $opt; } } include_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); include_once './includes/install.inc'; drupal_load_updates(); /** * Get all updates that need to be applied. */ function drupal_get_updates() { $module_updates = array(); foreach (module_list() as $module) { $updates = drupal_get_schema_versions($module); if ($updates !== FALSE) { $updates = array_filter($updates, create_function('$a', 'return $a > drupal_get_installed_schema_version("'. $module .'");')); if (!empty($updates)) { $module_updates[$module] = drupal_map_assoc($updates); } } } return $module_updates; } function update_show_updates($updates) { echo t('Modules Requiring Updates:') ."\n\n"; $files = module_rebuild_cache(); foreach ($updates as $module => $update_list) { echo "{$files[$module]->info['name']} ({$files[$module]->name})\n"; echo " ". t('Pending Updates:') .' '. implode(',', $update_list) ."\n\n"; } } function update_show_versions() { $files = module_rebuild_cache(); printf("%-20s %-20s %-19s %5s\n\n", 'Name', 'Module', 'Version', 'Schema'); foreach ($files as $module => $file) { printf("%-20s %-20s %-19s %5s\n", $file->info['name'], $module, $file->info['version'], $file->schema_version); } } function update_do_updates($updates) { global $results; $results = array(); if ($maintenance) { variable_set('site_offline', 1); if (!empty($message)) { variable_set('site_offine_message', $message); } } foreach ($updates as $module => $update_list) { echo $module; $total_updates = count($update_list); while ($update = array_shift($update_list)) { do { $finished = update_data($module, $update); if ($finished !== 1) { echo ' ...'. number_format(((($total_updates - count($update_list)+1)/$total_updates)+$finished)*100, 2) .'%'; } } while ($finished !== 1); if (!empty($update_list)) { echo ' ...'. number_format((($total_updates - count($update_list))/$total_updates)*100, 2) .'%'; } } echo " ...done\n"; } // When no updates remain, clear the caches in case the data has been updated. if (!empty($updates)) { cache_clear_all('*', 'cache', TRUE); cache_clear_all('*', 'cache_page', TRUE); cache_clear_all('*', 'cache_menu', TRUE); cache_clear_all('*', 'cache_filter', TRUE); drupal_clear_css_cache(); } $update_ok = TRUE; foreach ($results as $module => $result) { echo $module ."\n"; foreach ($result as $number => $queries) { foreach ($queries as $query) { if (!$query['success']) { $update_ok = FALSE; echo "*** Failure! "; } echo '- '. $query['query'] ."\n"; } if (empty($queries)) { echo "No queries\n"; } } echo "\n"; } if ($maintenance && $update_ok) { variable_del('site_offline'); if (!empty($message)) { variable_del('site_offine_message'); } } return $update_ok; } function update_data($module, $number) { global $dryrun, $results; if (!$dryrun) { $ret = module_invoke($module, 'update_'. $number); } $finished = 1; if (isset($ret['#finished'])) { $finished = $ret['#finished']; unset($ret['#finished']); } if (!isset($results[$module])) { $results[$module] = array(); } if (!isset($results[$module][$number])) { $results[$module][$number] = array(); } $results[$module][$number] = array_merge($results[$module][$number], $ret); if ($finished == 1 && !$dryrun) { // Update the installed version drupal_set_installed_schema_version($module, $number); } return $finished; } function update_sql($sql) { $result = db_query($sql); return array('success' => $result !== FALSE, 'query' => check_plain($sql)); } $user = user_load(array('uid' => 1)); while ($arg = array_shift($args)) { switch ($arg) { case 'l': case 'list': if ($updates = drupal_get_updates()) { update_show_updates($updates); exit(1); } else { echo t('No Updates available') ."\n"; } break 2; case 'versions': update_show_versions(); break 2; case 'u': case 'update': if ($updates = drupal_get_updates()) { if (!update_do_updates($updates)) { exit(1); } } break 2; } } exit();