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
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. $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'] = 'Performed update: ' . $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. require_once $core . '/includes/entity.inc';
  132. include_once $core . '/includes/unicode.inc';
  133. update_prepare_d7_bootstrap();
  134. drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
  135. require_once $core . '/includes/install.inc';
  136. require_once $core . '/modules/system/system.install';
  137. // Load module basics.
  138. include_once $core . '/includes/module.inc';
  139. $module_list['system']['filename'] = 'modules/system/system.module';
  140. module_list(TRUE, FALSE, FALSE, $module_list);
  141. drupal_load('module', 'system');
  142. // Reset the module_implements() cache so that any new hook implementations
  143. // in updated code are picked up.
  144. module_implements('', FALSE, TRUE);
  145. // Set up $language, since the installer components require it.
  146. drupal_language_initialize();
  147. // Set up theme system for the maintenance page.
  148. drupal_maintenance_theme();
  149. // Check the update requirements for Drupal.
  150. update_check_requirements();
  151. // update_fix_d7_requirements() needs to run before bootstrapping beyond path.
  152. // So bootstrap to DRUPAL_BOOTSTRAP_LANGUAGE then include unicode.inc.
  153. drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
  154. update_fix_d7_requirements();
  155. // Now proceed with a full bootstrap.
  156. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
  157. drupal_maintenance_theme();
  158. drush_errors_on();
  159. include_once DRUPAL_ROOT . '/includes/batch.inc';
  160. drupal_load_updates();
  161. update_fix_compatibility();
  162. // Change query-strings on css/js files to enforce reload for all users.
  163. _drupal_flush_css_js();
  164. // Flush the cache of all data for the update status module.
  165. if (db_table_exists('cache_update')) {
  166. cache_clear_all('*', 'cache_update', TRUE);
  167. }
  168. module_list(TRUE, FALSE, TRUE);
  169. }
  170. function update_main() {
  171. update_main_prepare();
  172. $pending = update_get_update_list();
  173. $start = array();
  174. // Ensure system module's updates run first.
  175. $start['system'] = array();
  176. // Print a list of pending updates for this module and get confirmation.
  177. if (sizeof($pending)) {
  178. drush_print(dt('The following updates are pending:'));
  179. drush_print();
  180. foreach ($pending as $module => $updates) {
  181. if (isset($updates['start'])) {
  182. drush_print($module . ' module : ');
  183. if (isset($updates['start'])) {
  184. $start[$module] = $updates['start'];
  185. foreach ($updates['pending'] as $update) {
  186. drush_print($update, 2);
  187. }
  188. }
  189. drush_print();
  190. }
  191. }
  192. if (!drush_confirm(dt('Do you wish to run all pending updates?'))) {
  193. return drush_user_abort();
  194. }
  195. drush_update_batch($start);
  196. }
  197. else {
  198. drush_log(dt("No database updates required"), 'success');
  199. }
  200. }
  201. function _update_batch_command($id) {
  202. update_main_prepare();
  203. drush_batch_command($id);
  204. }
  205. /**
  206. * Start the database update batch process.
  207. *
  208. * @param $start
  209. * An array of all the modules and which update to start at.
  210. * @param $redirect
  211. * Path to redirect to when the batch has finished processing.
  212. * @param $url
  213. * URL of the batch processing page (should only be used for separate
  214. * scripts like update.php).
  215. * @param $batch
  216. * Optional parameters to pass into the batch API.
  217. * @param $redirect_callback
  218. * (optional) Specify a function to be called to redirect to the progressive
  219. * processing page.
  220. */
  221. function drush_update_batch($start) {
  222. // Resolve any update dependencies to determine the actual updates that will
  223. // be run and the order they will be run in.
  224. $updates = update_resolve_dependencies($start);
  225. // Store the dependencies for each update function in an array which the
  226. // batch API can pass in to the batch operation each time it is called. (We
  227. // do not store the entire update dependency array here because it is
  228. // potentially very large.)
  229. $dependency_map = array();
  230. foreach ($updates as $function => $update) {
  231. $dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : array();
  232. }
  233. $operations = array();
  234. foreach ($updates as $update) {
  235. if ($update['allowed']) {
  236. // Set the installed version of each module so updates will start at the
  237. // correct place. (The updates are already sorted, so we can simply base
  238. // this on the first one we come across in the above foreach loop.)
  239. if (isset($start[$update['module']])) {
  240. drupal_set_installed_schema_version($update['module'], $update['number'] - 1);
  241. unset($start[$update['module']]);
  242. }
  243. // Add this update function to the batch.
  244. $function = $update['module'] . '_update_' . $update['number'];
  245. $operations[] = array('drush_update_do_one', array($update['module'], $update['number'], $dependency_map[$function]));
  246. }
  247. }
  248. $batch['operations'] = $operations;
  249. $batch += array(
  250. 'title' => 'Updating',
  251. 'init_message' => 'Starting updates',
  252. '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.',
  253. 'finished' => 'drush_update_finished',
  254. 'file' => 'includes/update.inc',
  255. );
  256. batch_set($batch);
  257. drush_backend_batch_process('updatedb-batch-process');
  258. }
  259. function drush_update_finished($success, $results, $operations) {
  260. // Nothing to do here. All caches already cleared. Kept as documentation of 'finished' callback.
  261. }