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. * Returns (and optionally stores) extra requirements that only apply during
  18. * particular parts of the update.php process.
  19. */
  20. function update_extra_requirements($requirements = NULL) {
  21. static $extra_requirements = array();
  22. if (isset($requirements)) {
  23. $extra_requirements += $requirements;
  24. }
  25. return $extra_requirements;
  26. }
  27. /**
  28. * Perform one update and store the results which will later be displayed on
  29. * the finished page.
  30. *
  31. * An update function can force the current and all later updates for this
  32. * module to abort by returning a $ret array with an element like:
  33. * $ret['#abort'] = array('success' => FALSE, 'query' => 'What went wrong');
  34. * The schema version will not be updated in this case, and all the
  35. * aborted updates will continue to appear on update.php as updates that
  36. * have not yet been run.
  37. *
  38. * @param $module
  39. * The module whose update will be run.
  40. * @param $number
  41. * The update number to run.
  42. * @param $context
  43. * The batch context array
  44. */
  45. function drush_update_do_one($module, $number, $dependency_map, &$context) {
  46. $function = $module . '_update_' . $number;
  47. // If this update was aborted in a previous step, or has a dependency that
  48. // was aborted in a previous step, go no further.
  49. if (!empty($context['results']['#abort']) && array_intersect($context['results']['#abort'], array_merge($dependency_map, array($function)))) {
  50. return;
  51. }
  52. $context['log'] = FALSE;
  53. $ret = array();
  54. if (function_exists($function)) {
  55. try {
  56. if ($context['log']) {
  57. Database::startLog($function);
  58. }
  59. drush_log("Executing " . $function);
  60. $ret['results']['query'] = $function($context['sandbox']);
  61. $ret['results']['success'] = TRUE;
  62. }
  63. // @TODO We may want to do different error handling for different exception
  64. // types, but for now we'll just print the message.
  65. catch (Exception $e) {
  66. $ret['#abort'] = array('success' => FALSE, 'query' => $e->getMessage());
  67. drush_set_error('DRUPAL_EXCEPTION', $e->getMessage());
  68. }
  69. if ($context['log']) {
  70. $ret['queries'] = Database::getLog($function);
  71. }
  72. }
  73. if (isset($context['sandbox']['#finished'])) {
  74. $context['finished'] = $context['sandbox']['#finished'];
  75. unset($context['sandbox']['#finished']);
  76. }
  77. if (!isset($context['results'][$module])) {
  78. $context['results'][$module] = array();
  79. }
  80. if (!isset($context['results'][$module][$number])) {
  81. $context['results'][$module][$number] = array();
  82. }
  83. $context['results'][$module][$number] = array_merge($context['results'][$module][$number], $ret);
  84. if (!empty($ret['#abort'])) {
  85. // Record this function in the list of updates that were aborted.
  86. $context['results']['#abort'][] = $function;
  87. }
  88. // Record the schema update if it was completed successfully.
  89. if ($context['finished'] == 1 && empty($ret['#abort'])) {
  90. drupal_set_installed_schema_version($module, $number);
  91. }
  92. $context['message'] = 'Updating ' . check_plain($module) . ' module';
  93. }
  94. /**
  95. * Check update requirements and report any errors.
  96. */
  97. function update_check_requirements() {
  98. $warnings = FALSE;
  99. // Check the system module and update.php requirements only.
  100. $requirements = system_requirements('update');
  101. $requirements += update_extra_requirements();
  102. // If there are issues, report them.
  103. foreach ($requirements as $requirement) {
  104. if (isset($requirement['severity']) && $requirement['severity'] != REQUIREMENT_OK) {
  105. $message = isset($requirement['description']) ? $requirement['description'] : '';
  106. if (isset($requirement['value']) && $requirement['value']) {
  107. $message .= ' (Currently using ' . $requirement['title'] . ' ' . $requirement['value'] . ')';
  108. }
  109. $warnings = TRUE;
  110. drupal_set_message($message, 'warning');
  111. }
  112. }
  113. return $warnings;
  114. }
  115. function update_main_prepare() {
  116. // Some unavoidable errors happen because the database is not yet up-to-date.
  117. // Our custom error handler is not yet installed, so we just suppress them.
  118. drush_errors_off();
  119. // We prepare a minimal bootstrap for the update requirements check to avoid
  120. // reaching the PHP memory limit.
  121. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  122. require_once DRUPAL_ROOT . '/includes/update.inc';
  123. require_once DRUPAL_ROOT . '/includes/common.inc';
  124. require_once DRUPAL_ROOT . '/includes/file.inc';
  125. require_once DRUPAL_ROOT . '/includes/entity.inc';
  126. include_once DRUPAL_ROOT . '/includes/unicode.inc';
  127. update_prepare_d7_bootstrap();
  128. drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
  129. require_once DRUPAL_ROOT . '/includes/install.inc';
  130. require_once DRUPAL_ROOT . '/modules/system/system.install';
  131. // Load module basics.
  132. include_once DRUPAL_ROOT . '/includes/module.inc';
  133. $module_list['system']['filename'] = 'modules/system/system.module';
  134. module_list(TRUE, FALSE, FALSE, $module_list);
  135. drupal_load('module', 'system');
  136. // Reset the module_implements() cache so that any new hook implementations
  137. // in updated code are picked up.
  138. module_implements('', FALSE, TRUE);
  139. // Set up $language, since the installer components require it.
  140. drupal_language_initialize();
  141. // Set up theme system for the maintenance page.
  142. drupal_maintenance_theme();
  143. // Check the update requirements for Drupal.
  144. update_check_requirements();
  145. // update_fix_d7_requirements() needs to run before bootstrapping beyond path.
  146. // So bootstrap to DRUPAL_BOOTSTRAP_LANGUAGE then include unicode.inc.
  147. drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
  148. update_fix_d7_requirements();
  149. // Now proceed with a full bootstrap.
  150. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
  151. drupal_maintenance_theme();
  152. drush_errors_on();
  153. // Rescan and repopulate the system table to ensure we have a full picture
  154. // of the platform.
  155. drush_get_projects();
  156. include_once DRUPAL_ROOT . '/includes/batch.inc';
  157. drupal_load_updates();
  158. update_fix_compatibility();
  159. // Change query-strings on css/js files to enforce reload for all users.
  160. _drupal_flush_css_js();
  161. // Flush the cache of all data for the update status module.
  162. if (db_table_exists('cache_update')) {
  163. cache_clear_all('*', 'cache_update', TRUE);
  164. }
  165. module_list(TRUE, FALSE, TRUE);
  166. }
  167. function update_main() {
  168. update_main_prepare();
  169. $pending = update_get_update_list();
  170. $start = array();
  171. // Ensure system module's updates run first
  172. $start['system'] = array();
  173. // Print a list of pending updates for this module and get confirmation.
  174. if (sizeof($pending)) {
  175. drush_print(dt('The following updates are pending:'));
  176. drush_print();
  177. foreach ($pending as $module => $updates) {
  178. if (isset($updates['start'])) {
  179. drush_print($module . ' module : ');
  180. if (isset($updates['start'])) {
  181. $start[$module] = $updates['start'];
  182. foreach ($updates['pending'] as $update) {
  183. drush_print($update, 2);
  184. }
  185. }
  186. drush_print();
  187. }
  188. }
  189. if (!drush_confirm(dt('Do you wish to run all pending updates?'))) {
  190. drush_die('Aborting.');
  191. }
  192. drush_update_batch($start);
  193. }
  194. else {
  195. drush_log(dt("No database updates required"), 'success');
  196. }
  197. }
  198. function _update_batch_command($id) {
  199. update_main_prepare();
  200. drush_batch_command($id);
  201. }
  202. /**
  203. * Start the database update batch process.
  204. *
  205. * @param $start
  206. * An array of all the modules and which update to start at.
  207. * @param $redirect
  208. * Path to redirect to when the batch has finished processing.
  209. * @param $url
  210. * URL of the batch processing page (should only be used for separate
  211. * scripts like update.php).
  212. * @param $batch
  213. * Optional parameters to pass into the batch API.
  214. * @param $redirect_callback
  215. * (optional) Specify a function to be called to redirect to the progressive
  216. * processing page.
  217. */
  218. function drush_update_batch($start) {
  219. // Resolve any update dependencies to determine the actual updates that will
  220. // be run and the order they will be run in.
  221. $updates = update_resolve_dependencies($start);
  222. // Store the dependencies for each update function in an array which the
  223. // batch API can pass in to the batch operation each time it is called. (We
  224. // do not store the entire update dependency array here because it is
  225. // potentially very large.)
  226. $dependency_map = array();
  227. foreach ($updates as $function => $update) {
  228. $dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : array();
  229. }
  230. $operations = array();
  231. foreach ($updates as $update) {
  232. if ($update['allowed']) {
  233. // Set the installed version of each module so updates will start at the
  234. // correct place. (The updates are already sorted, so we can simply base
  235. // this on the first one we come across in the above foreach loop.)
  236. if (isset($start[$update['module']])) {
  237. drupal_set_installed_schema_version($update['module'], $update['number'] - 1);
  238. unset($start[$update['module']]);
  239. }
  240. // Add this update function to the batch.
  241. $function = $update['module'] . '_update_' . $update['number'];
  242. $operations[] = array('drush_update_do_one', array($update['module'], $update['number'], $dependency_map[$function]));
  243. }
  244. }
  245. $batch['operations'] = $operations;
  246. $batch += array(
  247. 'title' => 'Updating',
  248. 'init_message' => 'Starting updates',
  249. '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.',
  250. 'finished' => 'drush_update_finished',
  251. 'file' => 'includes/update.inc',
  252. );
  253. batch_set($batch);
  254. drush_backend_batch_process('updatedb-batch-process');
  255. }
  256. function drush_update_finished($success, $results, $operations) {
  257. // Clear the caches in case the data has been updated.
  258. drupal_flush_all_caches();
  259. }