print.drush.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * common drush functions for the print submodules.
  5. */
  6. /**
  7. * Download and extract the lib
  8. *
  9. * @param string $library
  10. * library to download.
  11. * @param string $url
  12. * URL of the file to download
  13. */
  14. function _print_drush_download_lib($library, $url) {
  15. $path = drush_get_option('path');
  16. if (empty($path)) {
  17. $path = drush_get_context('DRUSH_DRUPAL_ROOT') . '/sites/all/libraries';
  18. }
  19. // Create the path if it does not exist.
  20. if (!is_dir($path)) {
  21. drush_op('mkdir', $path);
  22. drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
  23. }
  24. // Chdir to the download location.
  25. $olddir = getcwd();
  26. drush_op('chdir', $path);
  27. // Warn about an existing dir
  28. if (is_dir($library)) {
  29. // drush_op('rmdir', $library); // Directory must be empty for the php rmdir to work..
  30. drush_log(dt('An existing @library was overwritten at @path', array('@library' => $library, '@path' => $path . '/' . $library)), 'notice');
  31. }
  32. // Download the archive
  33. $filename = _print_drush_download_file($url);
  34. if ($filename) {
  35. $extract_ret = _print_drush_download_extract($filename);
  36. if ($extract_ret) {
  37. // Remove the archive
  38. drush_op('unlink', $filename);
  39. drush_log(dt('@file has been downloaded and extracted in @path', array('@file' => $filename, '@path' => $path)), 'success');
  40. }
  41. else {
  42. drush_log(dt('@file has been downloaded to @path, but extract failed. Check that you have the necessary program installed, and if necessary extract it manually.',
  43. array('@file' => $filename, '@path' => $path)), 'warning');
  44. }
  45. }
  46. else {
  47. drush_log(dt('Drush was unable to download @library to @path', array('@library' => $library, '@path' => $path)), 'error');
  48. }
  49. // Set working directory back to the previous working directory.
  50. drush_op('chdir', $olddir);
  51. }
  52. /**
  53. * Download a file using wget or curl
  54. *
  55. * Adapted from a function in drush/includes/drush.inc to support 302 redirects.
  56. *
  57. * @param string $download_url
  58. * The path to the file to download
  59. *
  60. * @return string
  61. * The filename that was downloaded, or NULL if the file could not be
  62. * downloaded.
  63. */
  64. function _print_drush_download_file($download_url) {
  65. $wget_ret = drush_shell_exec("wget -nv --trust-server-names %s", $download_url);
  66. if (!drush_get_context('DRUSH_SIMULATE')) {
  67. if ($wget_ret) {
  68. // Get the filename of the saved file from the output
  69. $wget_out = explode('"', array_shift(drush_shell_exec_output()));
  70. $filename = $wget_out[1];
  71. }
  72. else {
  73. $tempnam = uniqid('print_drush_');
  74. $curl_ret = drush_shell_exec("curl -s -L -o %s %s -w '%%{url_effective}'", $tempnam, $download_url);
  75. if ($curl_ret) {
  76. // File was donwloaded with the tempname
  77. // Find the effective name
  78. $filename = explode('/', array_shift(drush_shell_exec_output()));
  79. $filename = array_pop($filename);
  80. // Rename file from tempname to effective name
  81. if (!drush_op('rename', $tempnam, './' . $filename)) {
  82. $filename = $tempnam;
  83. }
  84. }
  85. else {
  86. $filename = FALSE;
  87. }
  88. }
  89. }
  90. else {
  91. $filename = basename($download_url);
  92. }
  93. return $filename;
  94. }
  95. /**
  96. * Helper to extract the downloaded zip/tar archive.
  97. *
  98. * @param string $filename
  99. * filename of the file to extract
  100. *
  101. * @return bool
  102. * TRUE on success, FALSE on failure
  103. */
  104. function _print_drush_download_extract($filename) {
  105. $arch_ret = FALSE;
  106. if (drush_op('is_file', $filename)) {
  107. switch (drush_op('mime_content_type', $filename)) {
  108. case 1:
  109. $arch_ret = TRUE;
  110. break;
  111. case 'application/zip':
  112. // Decompress the zip archive
  113. $arch_ret = drush_shell_exec('unzip -qq -o %s', $filename);
  114. // ZIP archives usually get the access rights wrong
  115. drush_log(dt('@filename is a Zip file. Check the access permissions of the extracted files.', array('@filename' => $filename)), 'warning');
  116. break;
  117. case 'application/x-gzip':
  118. // Decompress the tar gz archive
  119. $arch_ret = drush_shell_exec('tar xzf %s', $filename);
  120. break;
  121. case 'application/x-bzip2':
  122. // Decompress the tar bz2 archive
  123. $arch_ret = drush_shell_exec('tar xjf %s', $filename);
  124. break;
  125. case 'application/x-xz':
  126. // Decompress the tar xz archive
  127. $arch_ret = drush_shell_exec('tar xJf %s', $filename);
  128. break;
  129. }
  130. }
  131. else {
  132. drush_log(dt('@filename not found.', array('@filename' => $filename)), 'error');
  133. }
  134. return $arch_ret;
  135. }