updatecode.pm.inc

  1. 8.0.x commands/pm/updatecode.pm.inc
  2. 6.x commands/pm/updatecode.pm.inc
  3. 7.x commands/pm/updatecode.pm.inc
  4. 3.x commands/pm/updatecode.pm.inc
  5. 4.x commands/pm/updatecode.pm.inc
  6. 5.x commands/pm/updatecode.pm.inc
  7. master commands/pm/updatecode.pm.inc

pm-updatecode command implementation.

Functions

Namesort descending Description
drush_pm_updatecode Command callback. Displays update status info and allows to update installed projects.
drush_pm_updatecode_rollback Rollback the update process.
pm_update_finish Run the post-update hooks after updatecode is finished for one project.
pm_update_packages Update projects according to an array of releases and print the release notes for each project, following interactive confirmation from the user.
pm_update_project Update one project -- a module, theme or Drupal core.
_pm_update_core Update drupal core, following interactive confirmation from the user.
_pm_update_move_files Move some files from one location to another.

File

commands/pm/updatecode.pm.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * pm-updatecode command implementation.
  5. */
  6. use Drush\Log\LogLevel;
  7. /**
  8. * Command callback. Displays update status info and allows to update installed projects.
  9. *
  10. * Pass specific projects as arguments, otherwise we update all that have
  11. * candidate releases.
  12. *
  13. * This command prompts for confirmation before updating, so it is safe to run
  14. * just to check on. In this case, say at the confirmation prompt.
  15. */
  16. function drush_pm_updatecode() {
  17. // In --pipe mode, just run pm-updatestatus and exit.
  18. if (drush_get_context('DRUSH_PIPE')) {
  19. drush_set_option('strict', 0);
  20. return drush_invoke('pm-updatestatus');
  21. }
  22. $update_status = drush_get_engine('update_status');
  23. // Get specific requests.
  24. $requests = pm_parse_arguments(func_get_args(), FALSE);
  25. // Print report of modules to update, and record
  26. // result of that function in $update_info.
  27. $updatestatus_options = array();
  28. foreach (array('lock', 'unlock', 'lock-message', 'update-backend', 'check-disabled', 'security-only') as $option) {
  29. $value = drush_get_option($option, FALSE);
  30. if ($value) {
  31. $updatestatus_options[$option] = $value;
  32. }
  33. }
  34. $backend_options = array(
  35. 'integrate' => FALSE,
  36. );
  37. $values = drush_invoke_process("@self", 'pm-updatestatus', func_get_args(), $updatestatus_options, $backend_options);
  38. if (!is_array($values) || $values['error_status']) {
  39. return drush_set_error('pm-updatestatus failed.');
  40. }
  41. $last = $update_status->lastCheck();
  42. drush_print(dt('Update information last refreshed: ') . ($last ? format_date($last) : dt('Never')));
  43. drush_print($values['output']);
  44. $update_info = $values['object'];
  45. // Prevent update of core if --no-core was specified.
  46. if (isset($update_info['drupal']) && drush_get_option('no-core', FALSE)) {
  47. unset($update_info['drupal']);
  48. drush_print(dt('Skipping core update (--no-core specified).'));
  49. }
  50. // Remove locked and non-updateable projects.
  51. foreach ($update_info as $name => $project) {
  52. if ((isset($project['locked']) && !isset($requests[$name])) || (!isset($project['updateable']) || !$project['updateable'])) {
  53. unset($update_info[$name]);
  54. }
  55. }
  56. // Do no updates in simulated mode.
  57. if (drush_get_context('DRUSH_SIMULATE')) {
  58. return drush_log(dt('No action taken in simulated mode.'), LogLevel::OK);
  59. return TRUE;
  60. }
  61. $tmpfile = drush_tempnam('pm-updatecode.');
  62. $core_update_available = FALSE;
  63. if (isset($update_info['drupal'])) {
  64. $drupal_project = $update_info['drupal'];
  65. unset($update_info['drupal']);
  66. // At present we need to update drupal core after non-core projects
  67. // are updated.
  68. if (empty($update_info)) {
  69. return _pm_update_core($drupal_project, $tmpfile);
  70. }
  71. // If there are modules other than drupal core enabled, then update them
  72. // first.
  73. else {
  74. $core_update_available = TRUE;
  75. if ($drupal_project['status'] == DRUSH_UPDATESTATUS_NOT_SECURE) {
  76. drush_print(dt("NOTE: A security update for the Drupal core is available."));
  77. }
  78. else {
  79. drush_print(dt("NOTE: A code update for the Drupal core is available."));
  80. }
  81. drush_print(dt("Drupal core will be updated after all of the non-core projects are updated.\n"));
  82. }
  83. }
  84. // If there are no releases to update, then print a final
  85. // exit message.
  86. if (empty($update_info)) {
  87. if (drush_get_option('security-only')) {
  88. return drush_log(dt('No security updates available.'), LogLevel::OK);
  89. }
  90. else {
  91. return drush_log(dt('No code updates available.'), LogLevel::OK);
  92. }
  93. }
  94. // Offer to update to the identified releases.
  95. if (!pm_update_packages($update_info, $tmpfile)) {
  96. return FALSE;
  97. }
  98. // After projects are updated we can update core.
  99. if ($core_update_available) {
  100. drush_print();
  101. return _pm_update_core($drupal_project, $tmpfile);
  102. }
  103. }
  104. /**
  105. * Update drupal core, following interactive confirmation from the user.
  106. *
  107. * @param $project
  108. * The drupal project information from the drupal.org update service,
  109. * copied from $update_info['drupal']. @see drush_pm_updatecode.
  110. *
  111. * @return bool
  112. * Success or failure. An error message will be logged.
  113. */
  114. function _pm_update_core(&$project, $tmpfile) {
  115. $release_info = drush_get_engine('release_info');
  116. drush_print(dt('Code updates will be made to drupal core.'));
  117. drush_print(dt("WARNING: Updating core will discard any modifications made to Drupal core files, most noteworthy among these are .htaccess and robots.txt. If you have made any modifications to these files, please back them up before updating so that you can re-create your modifications in the updated version of the file."));
  118. drush_print(dt("Note: Updating core can potentially break your site. It is NOT recommended to update production sites without prior testing."));
  119. drush_print();
  120. if (drush_get_option('notes', FALSE)) {
  121. drush_print('Obtaining release notes for above projects...');
  122. #TODO# Build the $request array from info in $project.
  123. $request = pm_parse_request('drupal');
  124. $release_info->get($request)->getReleaseNotes(NULL, TRUE, $tmpfile);
  125. }
  126. if(!drush_confirm(dt('Do you really want to continue?'))) {
  127. drush_print(dt('Rolling back all changes. Run again with --no-core to update modules only.'));
  128. return drush_user_abort();
  129. }
  130. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  131. // We need write permission on $drupal_root.
  132. if (!is_writable($drupal_root)) {
  133. return drush_set_error('DRUSH_PATH_NO_WRITABLE', dt('Drupal root path is not writable.'));
  134. }
  135. // Create a directory 'core' if it does not already exist.
  136. $project['path'] = 'drupal-' . $project['candidate_version'];
  137. $project['full_project_path'] = $drupal_root . '/' . $project['path'];
  138. if (!is_dir($project['full_project_path'])) {
  139. drush_mkdir($project['full_project_path']);
  140. }
  141. // Create a list of directories to exclude from the update process.
  142. // On Drupal >=8 skip also directories in the document root.
  143. if (drush_drupal_major_version() >= 8) {
  144. $skip_list = array('sites', $project['path'], 'modules', 'profiles', 'themes');
  145. }
  146. else {
  147. $skip_list = array('sites', $project['path']);
  148. }
  149. // Add non-writable directories: we can't move them around.
  150. // We will also use $items_to_test later for $version_control check.
  151. $items_to_test = drush_scan_directory($drupal_root, '/.*/', array_merge(array('.', '..'), $skip_list), 0, FALSE, 'basename', 0, TRUE);
  152. foreach (array_keys($items_to_test) as $item) {
  153. if (is_dir($item) && !is_writable($item)) {
  154. $skip_list[] = $item;
  155. unset($items_to_test[$item]);
  156. }
  157. elseif (is_link($item)) {
  158. $skip_list[] = $item;
  159. unset($items_to_test[$item]);
  160. }
  161. }
  162. $project['skip_list'] = $skip_list;
  163. // Move all files and folders in $drupal_root to the new 'core' directory
  164. // except for the items in the skip list
  165. _pm_update_move_files($drupal_root, $project['full_project_path'], $project['skip_list']);
  166. // Set a context variable to indicate that rollback should reverse
  167. // the _pm_update_move_files above.
  168. drush_set_context('DRUSH_PM_DRUPAL_CORE', $project);
  169. if (!$version_control = drush_pm_include_version_control($project['full_project_path'])) {
  170. return FALSE;
  171. }
  172. // Check we have a version control system, and it clears its pre-flight.
  173. if (!$version_control->pre_update($project, $items_to_test)) {
  174. return FALSE;
  175. }
  176. // Update core.
  177. if (pm_update_project($project, $version_control) === FALSE) {
  178. return FALSE;
  179. }
  180. // Take the updated files in the 'core' directory that have been updated,
  181. // and move all except for the items in the skip list back to
  182. // the drupal root.
  183. _pm_update_move_files($project['full_project_path'], $drupal_root, $project['skip_list']);
  184. drush_delete_dir($project['full_project_path']);
  185. $project['full_project_path'] = $drupal_root;
  186. // If there is a backup target, then find items
  187. // in the backup target that do not exist at the
  188. // drupal root. These are to be moved back.
  189. if (array_key_exists('backup_target', $project)) {
  190. _pm_update_move_files($project['backup_target'], $drupal_root, $project['skip_list'], FALSE);
  191. _pm_update_move_files($project['backup_target'] . '/profiles', $drupal_root . '/profiles', array('default'), FALSE);
  192. }
  193. pm_update_finish($project, $version_control);
  194. return TRUE;
  195. }
  196. /**
  197. * Move some files from one location to another.
  198. */
  199. function _pm_update_move_files($src_dir, $dest_dir, $skip_list, $remove_conflicts = TRUE) {
  200. $items_to_move = drush_scan_directory($src_dir, '/.*/', array_merge(array('.', '..'), $skip_list), 0, FALSE, 'filename', 0, TRUE);
  201. foreach ($items_to_move as $filename => $info) {
  202. if ($remove_conflicts) {
  203. drush_delete_dir($dest_dir . '/' . basename($filename));
  204. }
  205. if (!file_exists($dest_dir . '/' . basename($filename))) {
  206. $move_result = drush_move_dir($filename, $dest_dir . '/' . basename($filename));
  207. }
  208. }
  209. return TRUE;
  210. }
  211. /**
  212. * Update projects according to an array of releases and print the release notes
  213. * for each project, following interactive confirmation from the user.
  214. *
  215. * @param $update_info
  216. * An array of projects from the drupal.org update service, with an additional
  217. * array key candidate_version that specifies the version to be installed.
  218. */
  219. function pm_update_packages($update_info, $tmpfile) {
  220. $release_info = drush_get_engine('release_info');
  221. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  222. $print = '';
  223. $status = array();
  224. foreach($update_info as $project) {
  225. $print .= $project['title'] . " [" . $project['name'] . '-' . $project['candidate_version'] . "], ";
  226. $status[$project['status']] = $project['status'];
  227. }
  228. // We print the list of the projects that need to be updated.
  229. if (isset($status[DRUSH_UPDATESTATUS_NOT_SECURE])) {
  230. if (isset($status[DRUSH_UPDATESTATUS_NOT_CURRENT])) {
  231. $title = (dt('Security and code updates will be made to the following projects:'));
  232. }
  233. else {
  234. $title = (dt('Security updates will be made to the following projects:'));
  235. }
  236. }
  237. else {
  238. $title = (dt('Code updates will be made to the following projects:'));
  239. }
  240. $print = "$title " . (substr($print, 0, strlen($print)-2));
  241. drush_print($print);
  242. file_put_contents($tmpfile, "\n\n$print\n\n", FILE_APPEND);
  243. // Print the release notes for projects to be updated.
  244. if (drush_get_option('notes', FALSE)) {
  245. drush_print('Obtaining release notes for above projects...');
  246. #TODO# Build the $request array from info in $project.
  247. foreach (array_keys($update_info) as $project_name) {
  248. $request = pm_parse_request($project_name);
  249. $release_info->get($request)->getReleaseNotes(NULL, TRUE, $tmpfile);
  250. }
  251. }
  252. // We print some warnings before the user confirms the update.
  253. drush_print();
  254. if (drush_get_option('no-backup', FALSE)) {
  255. drush_print(dt("Note: You have selected to not store backups."));
  256. }
  257. else {
  258. drush_print(dt("Note: A backup of your project will be stored to backups directory if it is not managed by a supported version control system."));
  259. drush_print(dt('Note: If you have made any modifications to any file that belongs to one of these projects, you will have to migrate those modifications after updating.'));
  260. }
  261. if(!drush_confirm(dt('Do you really want to continue with the update process?'))) {
  262. return drush_user_abort();
  263. }
  264. // Now we start the actual updating.
  265. foreach($update_info as $project) {
  266. if (empty($project['path'])) {
  267. return drush_set_error('DRUSH_PM_UPDATING_NO_PROJECT_PATH', dt('The !project project path is not available, perhaps the !type is enabled but has been deleted from disk.', array('!project' => $project['name'], '!type' => $project['project_type'])));
  268. }
  269. drush_log(dt('Starting to update !project code at !dir...', array('!project' => $project['title'], '!dir' => $project['path'])));
  270. // Define and check the full path to project directory and base (parent) directory.
  271. $project['full_project_path'] = $drupal_root . '/' . $project['path'];
  272. if (stripos($project['path'], $project['project_type']) === FALSE || !is_dir($project['full_project_path'])) {
  273. return drush_set_error('DRUSH_PM_UPDATING_PATH_NOT_FOUND', dt('The !project directory could not be found within the !types directory at !full_project_path, perhaps the project is enabled but has been deleted from disk.', array('!project' => $project['name'], '!type' => $project['project_type'], '!full_project_path' => $project['full_project_path'])));
  274. }
  275. if (!$version_control = drush_pm_include_version_control($project['full_project_path'])) {
  276. return FALSE;
  277. }
  278. // Check we have a version control system, and it clears its pre-flight.
  279. if (!$version_control->pre_update($project)) {
  280. return FALSE;
  281. }
  282. // Run update on one project.
  283. if (pm_update_project($project, $version_control) === FALSE) {
  284. return FALSE;
  285. }
  286. pm_update_finish($project, $version_control);
  287. }
  288. return TRUE;
  289. }
  290. /**
  291. * Update one project -- a module, theme or Drupal core.
  292. *
  293. * @param $project
  294. * The project to upgrade. $project['full_project_path'] must be set
  295. * to the location where this project is stored.
  296. * @return bool
  297. * Success or failure. An error message will be logged.
  298. */
  299. function pm_update_project($project, $version_control) {
  300. // 1. If the version control engine is a proper vcs we need to remove project
  301. // files in order to not have orphan files after update.
  302. // 2. If the package-handler is cvs or git, it will remove upstream removed
  303. // files and no orphans will exist after update.
  304. // So, we must remove all files previous update if the directory is not a
  305. // working copy of cvs or git but we don't need to remove them if the version
  306. // control engine is backup, as it did already move the project out to the
  307. // backup directory.
  308. if (($version_control->engine != 'backup') && (drush_get_option('package-handler', 'wget') == 'wget')) {
  309. // Find and unlink all files but the ones in the vcs control directories.
  310. $skip_list = array('.', '..');
  311. $skip_list = array_merge($skip_list, drush_version_control_reserved_files());
  312. drush_scan_directory($project['full_project_path'], '/.*/', $skip_list, 'unlink', TRUE, 'filename', 0, TRUE);
  313. }
  314. // Add the project to a context so we can roll back if needed.
  315. $updated = drush_get_context('DRUSH_PM_UPDATED');
  316. $updated[] = $project;
  317. drush_set_context('DRUSH_PM_UPDATED', $updated);
  318. if (!package_handler_update_project($project, $project['releases'][$project['candidate_version']])) {
  319. return drush_set_error('DRUSH_PM_UPDATING_FAILED', dt('Updating project !project failed. Attempting to roll back to previously installed version.', array('!project' => $project['name'])));
  320. }
  321. // If the version control engine is a proper vcs we also need to remove
  322. // orphan directories.
  323. if (($version_control->engine != 'backup') && (drush_get_option('package-handler', 'wget') == 'wget')) {
  324. $files = drush_find_empty_directories($project['full_project_path'], $version_control->reserved_files());
  325. array_map('drush_delete_dir', $files);
  326. }
  327. return TRUE;
  328. }
  329. /**
  330. * Run the post-update hooks after updatecode is finished for one project.
  331. */
  332. function pm_update_finish($project, $version_control) {
  333. drush_print(dt('Project !project was updated successfully. Installed version is now !version.', array('!project' => $project['name'], '!version' => $project['candidate_version'])));
  334. drush_command_invoke_all('pm_post_update', $project['name'], $project['releases'][$project['candidate_version']], $project);
  335. $version_control->post_update($project);
  336. }
  337. /**
  338. * Rollback the update process.
  339. */
  340. function drush_pm_updatecode_rollback() {
  341. $projects = array_reverse(drush_get_context('DRUSH_PM_UPDATED', array()));
  342. foreach($projects as $project) {
  343. drush_log(dt('Rolling back update of !project code ...', array('!project' => $project['title'])));
  344. // Check we have a version control system, and it clears it's pre-flight.
  345. if (!$version_control = drush_pm_include_version_control($project['path'])) {
  346. return FALSE;
  347. }
  348. $version_control->rollback($project);
  349. }
  350. // Post rollback, we will do additional repair if the project is drupal core.
  351. $drupal_core = drush_get_context('DRUSH_PM_DRUPAL_CORE', FALSE);
  352. if ($drupal_core) {
  353. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  354. _pm_update_move_files($drupal_core['full_project_path'], $drupal_root, $drupal_core['skip_list']);
  355. drush_delete_dir($drupal_core['full_project_path']);
  356. }
  357. }