PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Library/drush/commands/core/drupal/update.inc

http://github.com/jyr/MNPP
Pascal | 310 lines | 145 code | 22 blank | 143 comment | 12 complexity | ef2c127aa16a579b35e15c98f28f2882 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, LGPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, BSD-2-Clause
  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. module_implements('', FALSE, TRUE);
  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. // Now proceed with a full bootstrap.
  155. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
  156. drupal_maintenance_theme();
  157. drush_errors_on();
  158. include_once $core . '/includes/batch.inc';
  159. drupal_load_updates();
  160. update_fix_compatibility();
  161. // Change query-strings on css/js files to enforce reload for all users.
  162. _drupal_flush_css_js();
  163. // Flush the cache of all data for the update status module.
  164. if (db_table_exists('cache_update')) {
  165. cache_clear_all('*', 'cache_update', TRUE);
  166. }
  167. module_implements_reset();
  168. }
  169. function update_main() {
  170. update_main_prepare();
  171. $pending = update_get_update_list();
  172. $start = array();
  173. // Ensure system module's updates run first.
  174. $start['system'] = array();
  175. // Print a list of pending updates for this module and get confirmation.
  176. if (sizeof($pending)) {
  177. drush_print(dt('The following updates are pending:'));
  178. drush_print();
  179. foreach ($pending as $module => $updates) {
  180. if (isset($updates['start'])) {
  181. drush_print($module . ' module : ');
  182. if (isset($updates['start'])) {
  183. $start[$module] = $updates['start'];
  184. foreach ($updates['pending'] as $update) {
  185. drush_print($update, 2);
  186. }
  187. }
  188. drush_print();
  189. }
  190. }
  191. if (!drush_confirm(dt('Do you wish to run all pending updates?'))) {
  192. return drush_user_abort();
  193. }
  194. drush_update_batch($start);
  195. }
  196. else {
  197. drush_log(dt("No database updates required"), 'success');
  198. }
  199. }
  200. function _update_batch_command($id) {
  201. update_main_prepare();
  202. drush_batch_command($id);
  203. }
  204. /**
  205. * Start the database update batch process.
  206. *
  207. * @param $start
  208. * An array of all the modules and which update to start at.
  209. * @param $redirect
  210. * Path to redirect to when the batch has finished processing.
  211. * @param $url
  212. * URL of the batch processing page (should only be used for separate
  213. * scripts like update.php).
  214. * @param $batch
  215. * Optional parameters to pass into the batch API.
  216. * @param $redirect_callback
  217. * (optional) Specify a function to be called to redirect to the progressive
  218. * processing page.
  219. */
  220. function drush_update_batch($start) {
  221. // Resolve any update dependencies to determine the actual updates that will
  222. // be run and the order they will be run in.
  223. $updates = update_resolve_dependencies($start);
  224. // Store the dependencies for each update function in an array which the
  225. // batch API can pass in to the batch operation each time it is called. (We
  226. // do not store the entire update dependency array here because it is
  227. // potentially very large.)
  228. $dependency_map = array();
  229. foreach ($updates as $function => $update) {
  230. $dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : array();
  231. }
  232. $operations = array();
  233. foreach ($updates as $update) {
  234. if ($update['allowed']) {
  235. // Set the installed version of each module so updates will start at the
  236. // correct place. (The updates are already sorted, so we can simply base
  237. // this on the first one we come across in the above foreach loop.)
  238. if (isset($start[$update['module']])) {
  239. drupal_set_installed_schema_version($update['module'], $update['number'] - 1);
  240. unset($start[$update['module']]);
  241. }
  242. // Add this update function to the batch.
  243. $function = $update['module'] . '_update_' . $update['number'];
  244. $operations[] = array('drush_update_do_one', array($update['module'], $update['number'], $dependency_map[$function]));
  245. }
  246. }
  247. $batch['operations'] = $operations;
  248. $batch += array(
  249. 'title' => 'Updating',
  250. 'init_message' => 'Starting updates',
  251. '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.',
  252. 'finished' => 'drush_update_finished',
  253. 'file' => 'includes/update.inc',
  254. );
  255. batch_set($batch);
  256. drush_backend_batch_process('updatedb-batch-process');
  257. }
  258. function drush_update_finished($success, $results, $operations) {
  259. // Nothing to do here. All caches already cleared. Kept as documentation of 'finished' callback.
  260. }