update_7.inc

  1. 8.0.x commands/core/drupal/update_7.inc
  2. 6.x commands/core/drupal/update_7.inc
  3. 7.x commands/core/drupal/update_7.inc
  4. 3.x commands/core/drupal/update_7.inc
  5. 5.x commands/core/drupal/update_7.inc
  6. master commands/core/drupal/update_7.inc

Update.php for provisioned sites. This file is a derivative of the standard drupal update.php, which has been modified to allow being run from the command line.

Functions

Namesort descending Description
drush_update_batch Start the database update batch process.
drush_update_do_one Perform one update and store the results which will later be displayed on the finished page.
drush_update_finished
updatedb_status Return a 2 item array with
update_check_requirements Check update requirements and report any errors.
update_extra_requirements Returns (and optionally stores) extra requirements that only apply during particular parts of the update.php process.
update_main
update_main_prepare
_update_batch_command

Constants

Namesort descending Description
MAINTENANCE_MODE Global flag to identify update.php run, and so avoid various unwanted operations, such as hook_init() and hook_exit() invokes, css/js preprocessing and translation, and solve some theming issues. This flag is checked on several places in Drupal code…

File

commands/core/drupal/update_7.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Update.php for provisioned sites.
  5. * This file is a derivative of the standard drupal update.php,
  6. * which has been modified to allow being run from the command
  7. * line.
  8. */
  9. /**
  10. * Global flag to identify update.php run, and so avoid various unwanted
  11. * operations, such as hook_init() and hook_exit() invokes, css/js preprocessing
  12. * and translation, and solve some theming issues. This flag is checked on several
  13. * places in Drupal code (not just update.php).
  14. */
  15. define('MAINTENANCE_MODE', 'update');
  16. /**
  17. * Drupal's update.inc has functions that are in previous update_X.inc files
  18. * for example, update_check_incompatibility() which can prove useful when
  19. * enabling modules.
  20. */
  21. require_once DRUSH_DRUPAL_CORE . '/includes/update.inc';
  22. /**
  23. * Returns (and optionally stores) extra requirements that only apply during
  24. * particular parts of the update.php process.
  25. */
  26. function update_extra_requirements($requirements = NULL) {
  27. static $extra_requirements = array();
  28. if (isset($requirements)) {
  29. $extra_requirements += $requirements;
  30. }
  31. return $extra_requirements;
  32. }
  33. /**
  34. * Perform one update and store the results which will later be displayed on
  35. * the finished page.
  36. *
  37. * An update function can force the current and all later updates for this
  38. * module to abort by returning a $ret array with an element like:
  39. * $ret['#abort'] = array('success' => FALSE, 'query' => 'What went wrong');
  40. * The schema version will not be updated in this case, and all the
  41. * aborted updates will continue to appear on update.php as updates that
  42. * have not yet been run.
  43. *
  44. * @param $module
  45. * The module whose update will be run.
  46. * @param $number
  47. * The update number to run.
  48. * @param $context
  49. * The batch context array
  50. */
  51. function drush_update_do_one($module, $number, $dependency_map, &$context) {
  52. $function = $module . '_update_' . $number;
  53. // If this update was aborted in a previous step, or has a dependency that
  54. // was aborted in a previous step, go no further.
  55. if (!empty($context['results']['#abort']) && array_intersect($context['results']['#abort'], array_merge($dependency_map, array($function)))) {
  56. return;
  57. }
  58. $context['log'] = FALSE;
  59. $ret = array();
  60. if (function_exists($function)) {
  61. try {
  62. if ($context['log']) {
  63. Database::startLog($function);
  64. }
  65. drush_log("Executing " . $function);
  66. $ret['results']['query'] = $function($context['sandbox']);
  67. // If the update hook returned a status message (common in batch updates),
  68. // show it to the user.
  69. if ($ret['results']['query']) {
  70. drush_log($ret['results']['query'], 'ok');
  71. }
  72. $ret['results']['success'] = TRUE;
  73. }
  74. // @TODO We may want to do different error handling for different exception
  75. // types, but for now we'll just print the message.
  76. catch (Exception $e) {
  77. $ret['#abort'] = array('success' => FALSE, 'query' => $e->getMessage());
  78. drush_set_error('DRUPAL_EXCEPTION', $e->getMessage());
  79. }
  80. if ($context['log']) {
  81. $ret['queries'] = Database::getLog($function);
  82. }
  83. }
  84. if (isset($context['sandbox']['#finished'])) {
  85. $context['finished'] = $context['sandbox']['#finished'];
  86. unset($context['sandbox']['#finished']);
  87. }
  88. if (!isset($context['results'][$module])) {
  89. $context['results'][$module] = array();
  90. }
  91. if (!isset($context['results'][$module][$number])) {
  92. $context['results'][$module][$number] = array();
  93. }
  94. $context['results'][$module][$number] = array_merge($context['results'][$module][$number], $ret);
  95. if (!empty($ret['#abort'])) {
  96. // Record this function in the list of updates that were aborted.
  97. $context['results']['#abort'][] = $function;
  98. }
  99. // Record the schema update if it was completed successfully.
  100. if ($context['finished'] == 1 && empty($ret['#abort'])) {
  101. drupal_set_installed_schema_version($module, $number);
  102. }
  103. $context['message'] = 'Performed update: ' . $function;
  104. }
  105. /**
  106. * Check update requirements and report any errors.
  107. */
  108. function update_check_requirements() {
  109. $warnings = FALSE;
  110. // Check the system module and update.php requirements only.
  111. $requirements = system_requirements('update');
  112. $requirements += update_extra_requirements();
  113. // If there are issues, report them.
  114. foreach ($requirements as $requirement) {
  115. if (isset($requirement['severity']) && $requirement['severity'] != REQUIREMENT_OK) {
  116. $message = isset($requirement['description']) ? $requirement['description'] : '';
  117. if (isset($requirement['value']) && $requirement['value']) {
  118. $message .= ' (Currently using ' . $requirement['title'] . ' ' . $requirement['value'] . ')';
  119. }
  120. $warnings = TRUE;
  121. drupal_set_message($message, 'warning');
  122. }
  123. }
  124. return $warnings;
  125. }
  126. function update_main_prepare() {
  127. // Some unavoidable errors happen because the database is not yet up-to-date.
  128. // Our custom error handler is not yet installed, so we just suppress them.
  129. drush_errors_off();
  130. // We prepare a minimal bootstrap for the update requirements check to avoid
  131. // reaching the PHP memory limit.
  132. $core = DRUSH_DRUPAL_CORE;
  133. require_once $core . '/includes/bootstrap.inc';
  134. require_once $core . '/includes/common.inc';
  135. require_once $core . '/includes/file.inc';
  136. require_once $core . '/includes/entity.inc';
  137. include_once $core . '/includes/unicode.inc';
  138. update_prepare_d7_bootstrap();
  139. drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
  140. require_once $core . '/includes/install.inc';
  141. require_once $core . '/modules/system/system.install';
  142. // Load module basics.
  143. include_once $core . '/includes/module.inc';
  144. $module_list['system']['filename'] = 'modules/system/system.module';
  145. module_list(TRUE, FALSE, FALSE, $module_list);
  146. drupal_load('module', 'system');
  147. // Reset the module_implements() cache so that any new hook implementations
  148. // in updated code are picked up.
  149. module_implements('', FALSE, TRUE);
  150. // Set up $language, since the installer components require it.
  151. drupal_language_initialize();
  152. // Set up theme system for the maintenance page.
  153. drupal_maintenance_theme();
  154. // Check the update requirements for Drupal.
  155. update_check_requirements();
  156. // update_fix_d7_requirements() needs to run before bootstrapping beyond path.
  157. // So bootstrap to DRUPAL_BOOTSTRAP_LANGUAGE then include unicode.inc.
  158. drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
  159. update_fix_d7_requirements();
  160. // Clear the module_implements() cache before the full bootstrap. The calls
  161. // above to drupal_maintenance_theme() and update_check_requirements() have
  162. // invoked hooks before all modules have actually been loaded by the full
  163. // bootstrap. This means that the module_implements() results for any hooks
  164. // that have been invoked, including hook_module_implements_alter(), is a
  165. // smaller set of modules than should be returned normally.
  166. // @see https://github.com/drush-ops/drush/pull/399
  167. module_implements('', FALSE, TRUE);
  168. // Now proceed with a full bootstrap.
  169. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
  170. drupal_maintenance_theme();
  171. drush_errors_on();
  172. include_once DRUPAL_ROOT . '/includes/batch.inc';
  173. drupal_load_updates();
  174. update_fix_compatibility();
  175. // Change query-strings on css/js files to enforce reload for all users.
  176. _drupal_flush_css_js();
  177. // Flush the cache of all data for the update status module.
  178. if (db_table_exists('cache_update')) {
  179. cache_clear_all('*', 'cache_update', TRUE);
  180. }
  181. module_list(TRUE, FALSE, TRUE);
  182. }
  183. function update_main() {
  184. update_main_prepare();
  185. list($pending, $start) = updatedb_status();
  186. if ($pending) {
  187. // @todo get table header working.
  188. // $headers = array(dt('Module'), dt('ID'), dt('Description'));
  189. drush_print_table($pending);
  190. if (!drush_confirm(dt('Do you wish to run all pending updates?'))) {
  191. return drush_user_abort();
  192. }
  193. drush_update_batch($start);
  194. }
  195. else {
  196. drush_log(dt("No database updates required"), 'success');
  197. }
  198. }
  199. function _update_batch_command($id) {
  200. update_main_prepare();
  201. drush_batch_command($id);
  202. }
  203. /**
  204. * Start the database update batch process.
  205. *
  206. * @param $start
  207. * An array of all the modules and which update to start at.
  208. * @param $redirect
  209. * Path to redirect to when the batch has finished processing.
  210. * @param $url
  211. * URL of the batch processing page (should only be used for separate
  212. * scripts like update.php).
  213. * @param $batch
  214. * Optional parameters to pass into the batch API.
  215. * @param $redirect_callback
  216. * (optional) Specify a function to be called to redirect to the progressive
  217. * processing page.
  218. */
  219. function drush_update_batch($start) {
  220. // Resolve any update dependencies to determine the actual updates that will
  221. // be run and the order they will be run in.
  222. $updates = update_resolve_dependencies($start);
  223. // Store the dependencies for each update function in an array which the
  224. // batch API can pass in to the batch operation each time it is called. (We
  225. // do not store the entire update dependency array here because it is
  226. // potentially very large.)
  227. $dependency_map = array();
  228. foreach ($updates as $function => $update) {
  229. $dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : array();
  230. }
  231. $operations = array();
  232. foreach ($updates as $update) {
  233. if ($update['allowed']) {
  234. // Set the installed version of each module so updates will start at the
  235. // correct place. (The updates are already sorted, so we can simply base
  236. // this on the first one we come across in the above foreach loop.)
  237. if (isset($start[$update['module']])) {
  238. drupal_set_installed_schema_version($update['module'], $update['number'] - 1);
  239. unset($start[$update['module']]);
  240. }
  241. // Add this update function to the batch.
  242. $function = $update['module'] . '_update_' . $update['number'];
  243. $operations[] = array('drush_update_do_one', array($update['module'], $update['number'], $dependency_map[$function]));
  244. }
  245. }
  246. $batch['operations'] = $operations;
  247. $batch += array(
  248. 'title' => 'Updating',
  249. 'init_message' => 'Starting updates',
  250. 'error_message' => 'An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.',
  251. 'finished' => 'drush_update_finished',
  252. 'file' => 'includes/update.inc',
  253. );
  254. batch_set($batch);
  255. drush_backend_batch_process('updatedb-batch-process');
  256. }
  257. function drush_update_finished($success, $results, $operations) {
  258. // Nothing to do here. All caches already cleared. Kept as documentation of 'finished' callback.
  259. }
  260. /**
  261. * Return a 2 item array with
  262. * - an array where each item is a 3 item associative array describing a pending update.
  263. * - an array listing the first update to run, keyed by module.
  264. */
  265. function updatedb_status() {
  266. $pending = update_get_update_list();
  267. $return = array();
  268. // Ensure system module's updates run first.
  269. $start['system'] = array();
  270. // Print a list of pending updates for this module and get confirmation.
  271. foreach ($pending as $module => $updates) {
  272. if (isset($updates['start'])) {
  273. foreach ($updates['pending'] as $update_id => $description) {
  274. // Strip cruft from front.
  275. $description = str_replace($update_id . ' - ', '', $description);
  276. $return[] = array('module' => ucfirst($module), 'update_id' => $update_id, 'description' => $description);
  277. }
  278. if (isset($updates['start'])) {
  279. $start[$module] = $updates['start'];
  280. }
  281. }
  282. }
  283. return array($return, $start);
  284. }