wget.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file Drush PM Wget extension
  4. */
  5. /**
  6. * Validate this package handler can run.
  7. */
  8. function package_handler_validate() {
  9. // Check wget or curl command exists. Disable possible output.
  10. $debug = drush_get_context('DRUSH_DEBUG');
  11. drush_set_context('DRUSH_DEBUG', FALSE);
  12. $success = drush_shell_exec('wget --version');
  13. if (!$success) {
  14. $success = drush_shell_exec('curl --version');
  15. // Old version of curl shipped in darwin returns error status for --version
  16. // and --help. Give the chance to use it.
  17. if (!$success) {
  18. $success = drush_shell_exec('which curl');
  19. }
  20. }
  21. drush_set_context('DRUSH_DEBUG', $debug);
  22. if (!$success) {
  23. return drush_set_error('DRUSH_SHELL_COMMAND_NOT_FOUND', dt('wget nor curl executables found.'));
  24. }
  25. }
  26. /**
  27. * Download a project.
  28. *
  29. * @param $request Array with information on the request to download.
  30. * @param $release The release details array from drupal.org.
  31. */
  32. function package_handler_download_project(&$request, $release) {
  33. // Install profiles come in several variants. User may specify which one she wants.
  34. if ($request['project_type'] == 'profile') {
  35. // @todo Use xpath to get the right file url.
  36. $files = $release['files'];
  37. foreach ($files as $key => $file) {
  38. if ((string)$file->variant == drush_get_option('variant', 'full') && (string)$file->archive_type == 'tar.gz') {
  39. $release['download_link'] = (string)$file->url;
  40. $release['mdhash'] = (string)$file->md5;
  41. break;
  42. }
  43. }
  44. }
  45. $filename = explode('/', $release['download_link']);
  46. $filename = array_pop($filename);
  47. // Download the project.
  48. if (!drush_shell_exec("wget -P . %s", $release['download_link'])) {
  49. drush_shell_exec("curl -O %s", $release['download_link']);
  50. }
  51. if (file_exists($filename) || drush_get_context('DRUSH_SIMULATE')) {
  52. drush_log("Downloading " . $filename . " was successful.");
  53. }
  54. else {
  55. return drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', 'Unable to download ' . $filename . ' to ' . $request['base_project_path'] . ' from '. $release['download_link']);
  56. }
  57. // Check Md5 hash.
  58. if (drush_op('md5_file', $filename) != $release['mdhash'] && !drush_get_context('DRUSH_SIMULATE')) {
  59. drush_set_error('DRUSH_PM_FILE_CORRUPT', "File $filename is corrupt (wrong md5 checksum).");
  60. drush_op('unlink', $filename);
  61. return FALSE;
  62. }
  63. else {
  64. drush_log("Md5 checksum of $filename verified.");
  65. }
  66. // Extract the tarball.
  67. $file_list = drush_tarball_extract($filename, $request['base_project_path'], TRUE);
  68. drush_op('unlink', $filename);
  69. // Move untarred directory to project_dir, if distinct.
  70. if (($request['project_type'] == 'core') || (($request['project_type'] == 'profile') && (drush_get_option('variant', 'core') == 'core'))) {
  71. // Obtain the dodgy project_dir for drupal core.
  72. $project_dir = rtrim($file_list[0], DIRECTORY_SEPARATOR);
  73. if ($request['project_dir'] != $project_dir) {
  74. $path = $request['base_project_path'];
  75. drush_move_dir($path . '/'. $project_dir, $path . '/' . $request['project_dir']);
  76. }
  77. }
  78. return TRUE;
  79. }
  80. /**
  81. * This is an alias of the download function, since they are identical
  82. */
  83. function package_handler_update_project(&$request, $release) {
  84. return package_handler_download_project($request, $release);
  85. }
  86. /**
  87. * Post download action.
  88. *
  89. * This action take place once the project is placed in its final location.
  90. */
  91. function package_handler_post_download($project) {
  92. }