print.drush.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/' . $library;
  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_log(dt('An existing @library was overwritten at @path', array('@library' => $library, '@path' => $path . '/' . $library)), 'notice');
  30. }
  31. if (preg_match('!api.github.com/repos/.*/releases/latest!', $url)) {
  32. $url = _print_drush_github_latest_url($url);
  33. }
  34. // Download the archive.
  35. $filename = _print_drush_download_file($url);
  36. if ($filename) {
  37. $extract_ret = _print_drush_download_extract($filename);
  38. if ($extract_ret) {
  39. // Remove the archive.
  40. drush_op('unlink', $filename);
  41. drush_log(dt('@file has been downloaded and extracted in @path', array('@file' => $filename, '@path' => $path)), 'success');
  42. }
  43. else {
  44. 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.',
  45. array('@file' => $filename, '@path' => $path)), 'warning');
  46. }
  47. }
  48. else {
  49. drush_log(dt('Drush was unable to download @library to @path', array('@library' => $library, '@path' => $path)), 'error');
  50. }
  51. // Set working directory back to the previous working directory.
  52. drush_op('chdir', $olddir);
  53. }
  54. /**
  55. * Get filename of latest from github.
  56. *
  57. * @param string $github_url
  58. * The github URL to the latest project release.
  59. *
  60. * @return string
  61. * The URL to the latest file release.
  62. */
  63. function _print_drush_github_latest_url($github_url) {
  64. $filename = _print_drush_download_file($github_url);
  65. $contents = file_get_contents($filename);
  66. drush_op('unlink', $filename);
  67. $json = json_decode($contents);
  68. if (isset($json->assets[0])) {
  69. $download_url = $json->assets[0]->browser_download_url;
  70. }
  71. else {
  72. // Try the zipball_url.
  73. $download_url = $json->zipball_url;
  74. }
  75. return $download_url;
  76. }
  77. /**
  78. * Download a file using wget or curl.
  79. *
  80. * Adapted from a function in drush/includes/drush.inc to support 302 redirects.
  81. *
  82. * @param string $download_url
  83. * The path to the file to download.
  84. *
  85. * @return string
  86. * The filename that was downloaded, or NULL if the file could not be
  87. * downloaded.
  88. */
  89. function _print_drush_download_file($download_url) {
  90. if (!drush_get_context('DRUSH_SIMULATE')) {
  91. $wget_ret = drush_shell_exec("wget -nv --content-disposition %s", $download_url);
  92. if ($wget_ret) {
  93. // Get the filename of the saved file from the output.
  94. $wget_out = explode('"', array_shift(drush_shell_exec_output()));
  95. $filename = $wget_out[1];
  96. }
  97. else {
  98. $tempnam = uniqid('print_drush_');
  99. $curl_ret = drush_shell_exec("curl -s -L -o %s %s -w '%%{url_effective}'", $tempnam, $download_url);
  100. if ($curl_ret) {
  101. // File was downloaded with the temporary name.
  102. // Find the effective name.
  103. $filename = explode('/', array_shift(drush_shell_exec_output()));
  104. $filename = array_pop($filename);
  105. // Rename file from tempname to effective name.
  106. if (!drush_op('rename', $tempnam, './' . $filename)) {
  107. $filename = $tempnam;
  108. }
  109. }
  110. else {
  111. $filename = FALSE;
  112. }
  113. }
  114. }
  115. else {
  116. $filename = basename($download_url);
  117. }
  118. return $filename;
  119. }
  120. /**
  121. * Helper to extract the downloaded zip/tar archive.
  122. *
  123. * @param string $filename
  124. * Filename of the file to extract.
  125. *
  126. * @return bool
  127. * TRUE on success, FALSE on failure
  128. */
  129. function _print_drush_download_extract($filename) {
  130. $arch_ret = FALSE;
  131. if (drush_op('is_file', $filename)) {
  132. $mime_type = drush_op('mime_content_type', $filename);
  133. switch ($mime_type) {
  134. case 1:
  135. $arch_ret = TRUE;
  136. break;
  137. case 'application/zip':
  138. // Decompress the zip archive.
  139. $arch_ret = drush_shell_exec('unzip -qq -o %s', $filename);
  140. // ZIP archives usually get the access rights wrong.
  141. drush_log(dt('@filename is a Zip file. Check the access permissions of the extracted files.', array('@filename' => $filename)), 'warning');
  142. break;
  143. case 'application/x-gzip':
  144. // Decompress the tar gz archive.
  145. $arch_ret = drush_shell_exec('tar xzf %s', $filename);
  146. break;
  147. case 'application/x-bzip2':
  148. // Decompress the tar bz2 archive.
  149. $arch_ret = drush_shell_exec('tar xjf %s', $filename);
  150. break;
  151. case 'application/x-xz':
  152. // Decompress the tar xz archive.
  153. $arch_ret = drush_shell_exec('tar xJf %s', $filename);
  154. break;
  155. default:
  156. drush_log(dt('Unknown MIME type: @type', array('@type' => $mime_type)), 'error');
  157. }
  158. }
  159. else {
  160. drush_log(dt('@filename not found.', array('@filename' => $filename)), 'error');
  161. }
  162. return $arch_ret;
  163. }