update.inc

  1. 8.0.x commands/core/drupal/update.inc
  2. 6.x commands/core/drupal/update.inc
  3. 7.x commands/core/drupal/update.inc
  4. 4.x commands/core/drupal/update.inc
  5. 5.x commands/core/drupal/update.inc
  6. master commands/core/drupal/update.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
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.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. $ret['results']['success'] = TRUE;
  68. }
  69. // @TODO We may want to do different error handling for different exception
  70. // types, but for now we'll just print the message.
  71. catch (Exception $e) {
  72. $ret['#abort'] = array('success' => FALSE, 'query' => $e->getMessage());
  73. drush_set_error('DRUPAL_EXCEPTION', $e->getMessage());
  74. }
  75. if ($context['log']) {
  76. $ret['queries'] = Database::getLog($function);
  77. }
  78. }
  79. if (isset($context['sandbox']['#finished'])) {
  80. $context['finished'] = $context['sandbox']['#finished'];
  81. unset($context['sandbox']['#finished']);
  82. }
  83. if (!isset($context['results'][$module])) {
  84. $context['results'][$module] = array();
  85. }
  86. if (!isset($context['results'][$module][$number])) {
  87. $context['results'][$module][$number] = array();
  88. }
  89. $context['results'][$module][$number] = array_merge($context['results'][$module][$number], $ret);
  90. if (!empty($ret['#abort'])) {
  91. // Record this function in the list of updates that were aborted.
  92. $context['results']['#abort'][] = $function;
  93. }
  94. // Record the schema update if it was completed successfully.
  95. if ($context['finished'] == 1 && empty($ret['#abort'])) {
  96. drupal_set_installed_schema_version($module, $number);
  97. }
  98. $context['message'] = 'Performing ' . $function;
  99. }
  100. /**
  101. * Check update requirements and report any errors.
  102. */
  103. function update_check_requirements() {
  104. $warnings = FALSE;
  105. // Check the system module and update.php requirements only.
  106. $requirements = system_requirements('update');
  107. $requirements += update_extra_requirements();
  108. // If there are issues, report them.
  109. foreach ($requirements as $requirement) {
  110. if (isset($requirement['severity']) && $requirement['severity'] != REQUIREMENT_OK) {
  111. $message = isset($requirement['description']) ? $requirement['description'] : '';
  112. if (isset($requirement['value']) && $requirement['value']) {
  113. $message .= ' (Currently using ' . $requirement['title'] . ' ' . $requirement['value'] . ')';
  114. }
  115. $warnings = TRUE;
  116. drupal_set_message($message, 'warning');
  117. }
  118. }
  119. return $warnings;
  120. }
  121. function update_main_prepare() {
  122. // Some unavoidable errors happen because the database is not yet up-to-date.
  123. // Our custom error handler is not yet installed, so we just suppress them.
  124. drush_errors_off();
  125. // We prepare a minimal bootstrap for the update requirements check to avoid
  126. // reaching the PHP memory limit.
  127. $core = DRUSH_DRUPAL_CORE;
  128. require_once $core . '/includes/bootstrap.inc';
  129. require_once $core . '/includes/common.inc';
  130. require_once $core . '/includes/file.inc';
  131. include_once $core . '/includes/unicode.inc';
  132. update_prepare_d8_bootstrap();
  133. drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
  134. require_once $core . '/includes/install.inc';
  135. require_once $core . '/modules/system/system.install';
  136. // Load module basics.
  137. include_once $core . '/includes/module.inc';
  138. $module_list['system']['filename'] = 'modules/system/system.module';
  139. module_list(TRUE, FALSE, FALSE, $module_list);
  140. drupal_load('module', 'system');
  141. // Reset the module_implements() cache so that any new hook implementations
  142. // in updated code are picked up.
  143. \Drupal::moduleHandler()->resetImplementations();
  144. // Set up $language, since the installer components require it.
  145. drupal_language_initialize();
  146. // Set up theme system for the maintenance page.
  147. drupal_maintenance_theme();
  148. // Check the update requirements for Drupal.
  149. update_check_requirements();
  150. // update_fix_d7_requirements() needs to run before bootstrapping beyond path.
  151. // So bootstrap to DRUPAL_BOOTSTRAP_LANGUAGE then include unicode.inc.
  152. drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
  153. update_fix_d8_requirements();
  154. // Clear the module_implements() cache before the full bootstrap. The calls
  155. // above to drupal_maintenance_theme() and update_check_requirements() have
  156. // invoked hooks before all modules have actually been loaded by the full
  157. // bootstrap. This means that the module_implements() results for any hooks
  158. // that have been invoked, including hook_module_implements_alter(), is a
  159. // smaller set of modules than should be returned normally.
  160. // @see https://github.com/drush-ops/drush/pull/399
  161. \Drupal::moduleHandler()->resetImplementations();
  162. // Now proceed with a full bootstrap.
  163. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
  164. drupal_maintenance_theme();
  165. drush_errors_on();
  166. include_once $core . '/includes/batch.inc';
  167. drupal_load_updates();
  168. update_fix_compatibility();
  169. // Change query-strings on css/js files to enforce reload for all users.
  170. _drupal_flush_css_js();
  171. // Flush the cache of all data for the update status module.
  172. if (db_table_exists('cache_update')) {
  173. cache_clear_all('*', 'cache_update', TRUE);
  174. }
  175. module_implements_reset();
  176. }
  177. function update_main() {
  178. update_main_prepare();
  179. $pending = update_get_update_list();
  180. $start = array();
  181. // Ensure system module's updates run first.
  182. $start['system'] = array();
  183. // Print a list of pending updates for this module and get confirmation.
  184. if (count($pending)) {
  185. drush_print(dt('The following updates are pending:'));
  186. drush_print();
  187. foreach ($pending as $module => $updates) {
  188. if (isset($updates['start'])) {
  189. drush_print($module . ' module : ');
  190. if (isset($updates['start'])) {
  191. $start[$module] = $updates['start'];
  192. foreach ($updates['pending'] as $update) {
  193. drush_print($update, 2);
  194. }
  195. }
  196. drush_print();
  197. }
  198. }
  199. if (!drush_confirm(dt('Do you wish to run all pending updates?'))) {
  200. return drush_user_abort();
  201. }
  202. drush_update_batch($start);
  203. }
  204. else {
  205. drush_log(dt("No database updates required"), 'success');
  206. }
  207. }
  208. function _update_batch_command($id) {
  209. update_main_prepare();
  210. drush_batch_command($id);
  211. }
  212. /**
  213. * Start the database update batch process.
  214. *
  215. * @param $start
  216. * An array of all the modules and which update to start at.
  217. * @param $redirect
  218. * Path to redirect to when the batch has finished processing.
  219. * @param $url
  220. * URL of the batch processing page (should only be used for separate
  221. * scripts like update.php).
  222. * @param $batch
  223. * Optional parameters to pass into the batch API.
  224. * @param $redirect_callback
  225. * (optional) Specify a function to be called to redirect to the progressive
  226. * processing page.
  227. */
  228. function drush_update_batch($start) {
  229. // Resolve any update dependencies to determine the actual updates that will
  230. // be run and the order they will be run in.
  231. $updates = update_resolve_dependencies($start);
  232. // Store the dependencies for each update function in an array which the
  233. // batch API can pass in to the batch operation each time it is called. (We
  234. // do not store the entire update dependency array here because it is
  235. // potentially very large.)
  236. $dependency_map = array();
  237. foreach ($updates as $function => $update) {
  238. $dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : array();
  239. }
  240. $operations = array();
  241. foreach ($updates as $update) {
  242. if ($update['allowed']) {
  243. // Set the installed version of each module so updates will start at the
  244. // correct place. (The updates are already sorted, so we can simply base
  245. // this on the first one we come across in the above foreach loop.)
  246. if (isset($start[$update['module']])) {
  247. drupal_set_installed_schema_version($update['module'], $update['number'] - 1);
  248. unset($start[$update['module']]);
  249. }
  250. // Add this update function to the batch.
  251. $function = $update['module'] . '_update_' . $update['number'];
  252. $operations[] = array('drush_update_do_one', array($update['module'], $update['number'], $dependency_map[$function]));
  253. }
  254. }
  255. $batch['operations'] = $operations;
  256. $batch += array(
  257. 'title' => 'Updating',
  258. 'init_message' => 'Starting updates',
  259. '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.',
  260. 'finished' => 'drush_update_finished',
  261. 'file' => 'includes/update.inc',
  262. );
  263. batch_set($batch);
  264. drush_backend_batch_process('updatedb-batch-process');
  265. }
  266. function drush_update_finished($success, $results, $operations) {
  267. // Nothing to do here. All caches already cleared. Kept as documentation of 'finished' callback.
  268. }