download.pm.inc

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

pm-download command implementation.

Functions

Namesort descending Description
drush_pm_download Command callback. Download Drupal core or any project.
drush_pm_download_validate Implements drush_hook_COMMAND_validate().
pm_drush_pm_download_destination_alter Implementation of hook_drush_pm_download_destination_alter().
_pm_download_destination Returns the best destination for a particular download type we can find.
_pm_download_destination_lookup Determines a candidate destination directory for a particular site path.

File

commands/pm/download.pm.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * pm-download command implementation.
  5. */
  6. use Drush\Log\LogLevel;
  7. use Drush\UpdateService\ReleaseInfo;
  8. /**
  9. * Implements drush_hook_COMMAND_validate().
  10. */
  11. function drush_pm_download_validate() {
  12. // Accomodate --select to the values accepted by release_info.
  13. $select = drush_get_option('select', 'auto');
  14. if ($select === TRUE) {
  15. drush_set_option('select', 'always');
  16. }
  17. else if ($select === FALSE) {
  18. drush_set_option('select', 'never');
  19. }
  20. // Validate the user specified destination directory.
  21. $destination = drush_get_option('destination');
  22. if (!empty($destination)) {
  23. $destination = rtrim($destination, DIRECTORY_SEPARATOR);
  24. if (!is_dir($destination)) {
  25. drush_print(dt("The directory !destination does not exist.", array('!destination' => $destination)));
  26. if (!drush_get_context('DRUSH_SIMULATE')) {
  27. if (drush_confirm(dt('Would you like to create it?'))) {
  28. drush_mkdir($destination, TRUE);
  29. }
  30. if (!is_dir($destination)) {
  31. return drush_set_error('DRUSH_PM_NO_DESTINATION', dt('Unable to create destination directory !destination.', array('!destination' => $destination)));
  32. }
  33. }
  34. }
  35. if (!is_writable($destination)) {
  36. return drush_set_error('DRUSH_PM_NO_DESTINATION', dt('Destination directory !destination is not writable.', array('!destination' => $destination)));
  37. }
  38. // Ignore --use-site-dir, if given.
  39. if (drush_get_option('use-site-dir', FALSE)) {
  40. drush_set_option('use-site-dir', FALSE);
  41. }
  42. }
  43. // Validate --variant or enforce a sane default.
  44. $variant = drush_get_option('variant', FALSE);
  45. if ($variant) {
  46. $variants = array('full', 'projects', 'profile-only');
  47. if (!in_array($variant, $variants)) {
  48. return drush_set_error('DRUSH_PM_PROFILE_INVALID_VARIANT', dt('Invalid variant !variant. Valid values: !variants.', array('!variant' => $variant, '!variants' => implode(', ', $variants))));
  49. }
  50. }
  51. // 'full' and 'projects' variants are only valid for wget package handler.
  52. $package_handler = drush_get_option('package-handler', 'wget');
  53. if (($package_handler != 'wget') && ($variant != 'profile-only')) {
  54. $new_variant = 'profile-only';
  55. if ($variant) {
  56. drush_log(dt('Variant !variant is incompatible with !ph package-handler.', array('!variant' => $variant, '!ph' => $package_handler)), LogLevel::WARNING);
  57. }
  58. }
  59. // If we are working on a drupal root, full variant is not an option.
  60. else if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_ROOT) {
  61. if ((!$variant) || (($variant == 'full') && (!isset($new_variant)))) {
  62. $new_variant = 'projects';
  63. }
  64. if ($variant == 'full') {
  65. drush_log(dt('Variant full is not a valid option within a Drupal root.'), LogLevel::WARNING);
  66. }
  67. }
  68. if (isset($new_variant)) {
  69. drush_set_option('variant', $new_variant);
  70. if ($variant) {
  71. drush_log(dt('Switching to --variant=!variant.', array('!variant' => $new_variant)), LogLevel::OK);
  72. }
  73. }
  74. }
  75. /**
  76. * Command callback. Download Drupal core or any project.
  77. */
  78. function drush_pm_download() {
  79. $release_info = drush_get_engine('release_info');
  80. if (!$requests = pm_parse_arguments(func_get_args(), FALSE)) {
  81. $requests = array('drupal');
  82. }
  83. // Pick cli options.
  84. $status_url = drush_get_option('source', ReleaseInfo::DEFAULT_URL);
  85. $restrict_to = drush_get_option('dev', '');
  86. $select = drush_get_option('select', 'auto');
  87. $all = drush_get_option('all', FALSE);
  88. // If we've bootstrapped a Drupal site and the user may have the chance
  89. // to select from a list of filtered releases, we want to pass
  90. // the installed project version, if any.
  91. $projects = array();
  92. if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_FULL) {
  93. if (!$all and in_array($select, array('auto', 'always'))) {
  94. $projects = drush_get_projects();
  95. }
  96. }
  97. // Get release history for each request and download the project.
  98. foreach ($requests as $request) {
  99. $request = pm_parse_request($request, $status_url, $projects);
  100. $version = isset($projects[$request['name']]) ? $projects[$request['name']]['version'] : NULL;
  101. $release = $release_info->selectReleaseBasedOnStrategy($request, $restrict_to, $select, $all, $version);
  102. if ($release == FALSE) {
  103. // Stop working on the first failure. Return silently on user abort.
  104. if (drush_get_context('DRUSH_USER_ABORT', FALSE)) {
  105. return FALSE;
  106. }
  107. // Signal that the command failed for all other problems.
  108. return drush_set_error('DRUSH_DOWNLOAD_FAILED', dt("Could not download requested project(s)."));
  109. }
  110. $request['version'] = $release['version'];
  111. $project_release_info = $release_info->get($request);
  112. $request['project_type'] = $project_release_info->getType();
  113. // Determine the name of the directory that will contain the project.
  114. // We face here all the assymetries to make it smooth for package handlers.
  115. // For Drupal core: --drupal-project-rename or drupal-x.y
  116. if (($request['project_type'] == 'core') ||
  117. (($request['project_type'] == 'profile') && (drush_get_option('variant', 'full') == 'full'))) {
  118. // Avoid downloading core into existing core.
  119. if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_ROOT) {
  120. if (strpos(realpath(drush_get_option('destination')), DRUPAL_ROOT) !== FALSE) {
  121. return drush_set_error('DRUSH_PM_DOWNLOAD_TRANSLATIONS_FORBIDDEN', dt('It\'s forbidden to download !project core into an existing core.', array('!project' => $request['name'])));
  122. }
  123. }
  124. if ($rename = drush_get_option('drupal-project-rename', FALSE)) {
  125. if ($rename === TRUE) {
  126. $request['project_dir'] = $request['name'];
  127. }
  128. else {
  129. $request['project_dir'] = $rename;
  130. }
  131. }
  132. else {
  133. // Set to drupal-x.y, the expected name for .tar.gz contents.
  134. // Explicitly needed for cvs package handler.
  135. $request['project_dir'] = strtolower(strtr($release['name'], ' ', '-'));
  136. }
  137. }
  138. // For the other project types we want the project name. Including core
  139. // variant for profiles. Note those come with drupal-x.y in the .tar.gz.
  140. else {
  141. $request['project_dir'] = $request['name'];
  142. }
  143. // Download the project to a temporary location.
  144. drush_log(dt('Downloading project !name ...', array('!name' => $request['name'])));
  145. $request['full_project_path'] = package_handler_download_project($request, $release);
  146. if (!$request['full_project_path']) {
  147. // Delete the cached update service file since it may be invalid.
  148. $release_info->clearCached($request);
  149. drush_log(dt('Error downloading !name', array('!name' => $request['name']), LogLevel::ERROR));
  150. continue;
  151. }
  152. // Determine the install location for the project. User provided
  153. // --destination has preference.
  154. $destination = drush_get_option('destination');
  155. if (!empty($destination)) {
  156. if (!file_exists($destination)) {
  157. drush_mkdir($destination);
  158. }
  159. $request['project_install_location'] = realpath($destination);
  160. }
  161. else {
  162. $request['project_install_location'] = _pm_download_destination($request['project_type']);
  163. }
  164. // If user did not provide --destination, then call the
  165. // download-destination-alter hook to give the chance to any commandfiles
  166. // to adjust the install location or abort it.
  167. if (empty($destination)) {
  168. $result = drush_command_invoke_all_ref('drush_pm_download_destination_alter', $request, $release);
  169. if (array_search(FALSE, $result, TRUE) !== FALSE) {
  170. return FALSE;
  171. }
  172. }
  173. // Load version control engine and detect if (the parent directory of) the
  174. // project install location is under a vcs.
  175. if (!$version_control = drush_pm_include_version_control($request['project_install_location'])) {
  176. continue;
  177. }
  178. $request['project_install_location'] .= '/' . $request['project_dir'];
  179. if ($version_control->engine == 'backup') {
  180. // Check if install location already exists.
  181. if (is_dir($request['project_install_location'])) {
  182. if (drush_confirm(dt('Install location !location already exists. Do you want to overwrite it?', array('!location' => $request['project_install_location'])))) {
  183. drush_delete_dir($request['project_install_location'], TRUE);
  184. }
  185. else {
  186. drush_log(dt("Skip installation of !project to !dest.", array('!project' => $request['name'], '!dest' => $request['project_install_location'])), LogLevel::WARNING);
  187. continue;
  188. }
  189. }
  190. }
  191. else {
  192. // Find and unlink all files but the ones in the vcs control directories.
  193. $skip_list = array('.', '..');
  194. $skip_list = array_merge($skip_list, drush_version_control_reserved_files());
  195. drush_scan_directory($request['project_install_location'], '/.*/', $skip_list, 'unlink', TRUE, 'filename', 0, TRUE);
  196. }
  197. // Copy the project to the install location.
  198. if (drush_op('_drush_recursive_copy', $request['full_project_path'], $request['project_install_location'])) {
  199. drush_log(dt("Project !project (!version) downloaded to !dest.", array('!project' => $request['name'], '!version' => $release['version'], '!dest' => $request['project_install_location'])), LogLevel::SUCCESS);
  200. // Adjust full_project_path to the final project location.
  201. $request['full_project_path'] = $request['project_install_location'];
  202. // If the version control engine is a proper vcs we also need to remove
  203. // orphan directories.
  204. if ($version_control->engine != 'backup') {
  205. $empty_dirs = drush_find_empty_directories($request['full_project_path'], $version_control->reserved_files());
  206. foreach ($empty_dirs as $empty_dir) {
  207. // Some VCS files are read-only on Windows (e.g., .svn/entries).
  208. drush_delete_dir($empty_dir, TRUE);
  209. }
  210. }
  211. // Post download actions.
  212. package_handler_post_download($request, $release);
  213. drush_command_invoke_all('drush_pm_post_download', $request, $release);
  214. $version_control->post_download($request);
  215. // Print release notes if --notes option is set.
  216. if (drush_get_option('notes') && !drush_get_context('DRUSH_PIPE')) {
  217. $project_release_info->getReleaseNotes($release['version'], FALSE);
  218. }
  219. // Inform the user about available modules a/o themes in the downloaded project.
  220. drush_pm_extensions_in_project($request);
  221. }
  222. else {
  223. // We don't `return` here in order to proceed with downloading additional projects.
  224. drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', dt("Project !project (!version) could not be downloaded to !dest.", array('!project' => $request['name'], '!version' => $release['version'], '!dest' => $request['project_install_location'])));
  225. }
  226. // Notify about this project.
  227. if (drush_notify_allowed('pm-download')) {
  228. $msg = dt('Project !project (!version) downloaded to !install.', array(
  229. '!project' => $name,
  230. '!version' => $release['version'],
  231. '!install' => $request['project_install_location'],
  232. ));
  233. drush_notify_send(drush_notify_command_message('pm-download', $msg));
  234. }
  235. }
  236. }
  237. /**
  238. * Implementation of hook_drush_pm_download_destination_alter().
  239. *
  240. * Built-in download-destination-alter hook. This particular version of
  241. * the hook will move modules that contain only drush commands to
  242. * /usr/share/drush/commands if it exists, or $HOME/.drush if the
  243. * site-wide location does not exist.
  244. */
  245. function pm_drush_pm_download_destination_alter(&$request, $release) {
  246. // A module is a pure drush command if it has no .module and contain
  247. // .drush.inc files. Skip this test for drush itself, though; we do
  248. // not want to download drush to the ~/.drush folder.
  249. if (in_array($request['project_type'], array('module', 'utility')) && ($request['name'] != 'drush')) {
  250. $drush_command_files = drush_scan_directory($request['full_project_path'], '/.*\.drush.inc/');
  251. if (!empty($drush_command_files)) {
  252. $module_files = drush_scan_directory($request['full_project_path'], '/.*\.module/');
  253. if (empty($module_files)) {
  254. $install_dir = drush_get_context('DRUSH_SITE_WIDE_COMMANDFILES');
  255. if (!is_dir($install_dir) || !is_writable($install_dir)) {
  256. $install_dir = drush_get_context('DRUSH_PER_USER_CONFIGURATION');
  257. }
  258. // Make the .drush dir if it does not already exist.
  259. if (!is_dir($install_dir)) {
  260. drush_mkdir($install_dir, FALSE);
  261. }
  262. // Change the location if the mkdir worked.
  263. if (is_dir($install_dir)) {
  264. $request['project_install_location'] = $install_dir;
  265. }
  266. }
  267. // We need to clear the drush commandfile cache so that
  268. // our newly-downloaded drush extension commandfiles can be found.
  269. drush_cache_clear_all();
  270. }
  271. }
  272. }
  273. /**
  274. * Determines a candidate destination directory for a particular site path.
  275. *
  276. * Optionally attempts to create the directory.
  277. *
  278. * @return String the candidate destination if it exists.
  279. */
  280. function _pm_download_destination_lookup($type, $drupal_root, $sitepath, $create = FALSE) {
  281. if ($type == 'theme engine') {
  282. $destination = 'themes/engines';
  283. }
  284. // Profiles in Drupal < 8
  285. elseif (($type == 'profile') && (drush_drupal_major_version() < 8)) {
  286. $destination = 'profiles';
  287. }
  288. // Type: module, theme or profile.
  289. else {
  290. $destination = $type . 's';
  291. // Prefer /contrib if it exists.
  292. if ($sitepath) {
  293. $destination = $sitepath . '/' . $destination;
  294. }
  295. $contrib = $destination . '/contrib';
  296. if (is_dir($contrib)) {
  297. $destination = $contrib;
  298. }
  299. }
  300. if ($create) {
  301. drush_log(dt('Attempting to create destination directory at !dir', array('!dir' => $destination)));
  302. drush_mkdir($destination, TRUE);
  303. }
  304. if (is_dir($destination)) {
  305. drush_log(dt('Using destination directory !dir', array('!dir' => $destination)));
  306. return $destination;
  307. }
  308. drush_log(dt('Could not find destination directory at !dir', array('!dir' => $destination)));
  309. return FALSE;
  310. }
  311. /**
  312. * Returns the best destination for a particular download type we can find.
  313. *
  314. * It is based on the project type and drupal and site contexts.
  315. */
  316. function _pm_download_destination($type) {
  317. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  318. $site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT');
  319. $full_site_root = (empty($drupal_root) || empty($site_root)) ? '' : $drupal_root .'/'. $site_root;
  320. $sitewide = empty($drupal_root) ? '' : $drupal_root . '/' . drush_drupal_sitewide_directory();
  321. $in_site_directory = FALSE;
  322. // Check if we are running within the site directory.
  323. if (strpos(realpath(drush_cwd()), realpath($full_site_root)) !== FALSE || (drush_get_option('use-site-dir', FALSE))) {
  324. $in_site_directory = TRUE;
  325. }
  326. $destination = '';
  327. if ($type != 'core') {
  328. // Attempt 1: If we are in a specific site directory, and the destination
  329. // directory already exists, then we use that.
  330. if (empty($destination) && $site_root && $in_site_directory) {
  331. $create_dir = drush_get_option('use-site-dir', FALSE);
  332. $destination = _pm_download_destination_lookup($type, $drupal_root, $full_site_root, $create_dir);
  333. }
  334. // Attempt 2: If the destination directory already exists for
  335. // the sitewide directory, use that.
  336. if (empty($destination) && $drupal_root) {
  337. $destination = _pm_download_destination_lookup($type, $drupal_root, $sitewide);
  338. }
  339. // Attempt 3: If a specific (non default) site directory exists and
  340. // the sitewide directory does not exist, then create destination
  341. // in the site specific directory.
  342. if (empty($destination) && $site_root && $site_root !== 'sites/default' && is_dir($full_site_root) && !is_dir($sitewide)) {
  343. $destination = _pm_download_destination_lookup($type, $drupal_root, $full_site_root, TRUE);
  344. }
  345. // Attempt 4: If sitewide directory exists, then create destination there.
  346. if (empty($destination) && is_dir($sitewide)) {
  347. $destination = _pm_download_destination_lookup($type, $drupal_root, $sitewide, TRUE);
  348. }
  349. // Attempt 5: If site directory exists (even default), then create
  350. // destination in that directory.
  351. if (empty($destination) && $site_root && is_dir($full_site_root)) {
  352. $destination = _pm_download_destination_lookup($type, $drupal_root, $full_site_root, TRUE);
  353. }
  354. }
  355. // Attempt 6: If we didn't find a valid directory yet (or we somehow found
  356. // one that doesn't exist) we always fall back to the current directory.
  357. if (empty($destination) || !is_dir($destination)) {
  358. $destination = drush_cwd();
  359. }
  360. return $destination;
  361. }