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