Projet

Général

Profil

Paste
Télécharger (9,34 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / libraries / libraries.drush.inc @ ed912c77

1 85ad3d82 Assos Assos
<?php
2
/**
3
 * @file
4
 * Drush integration for Libraries API.
5
 */
6
7
/**
8
 * Implements hook_drush_command().
9
 */
10
function libraries_drush_command() {
11 7c856df4 Assos Assos
  $items = array();
12
13 85ad3d82 Assos Assos
  $items['libraries-list'] = array(
14 7c856df4 Assos Assos
    'description' => dt('Show a list of registered libraries.'),
15 85ad3d82 Assos Assos
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
16 7c856df4 Assos Assos
    'aliases' => array('lls', 'lib-list'),
17 85ad3d82 Assos Assos
  );
18 7c856df4 Assos Assos
19
  $items['libraries-download'] = array(
20
    'description' => dt('Download library files of registered libraries.'),
21
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
22
    'aliases' => array('ldl', 'lib-download'),
23 85ad3d82 Assos Assos
    'arguments' => array(
24 7c856df4 Assos Assos
      'libraries' => 'A comma delimited list of library machine names.',
25 85ad3d82 Assos Assos
    ),
26 ed912c77 Assos Assos
    'options' => array(
27
      'all' => 'Download all registered libraries.',
28
    ),
29 7c856df4 Assos Assos
  );
30
31 85ad3d82 Assos Assos
  return $items;
32
}
33
34
/**
35 7c856df4 Assos Assos
 * Implements hook_drush_cache_clear().
36
 *
37
 * @see drush_cache_clear_types()
38 85ad3d82 Assos Assos
 */
39 7c856df4 Assos Assos
function libraries_drush_cache_clear(array &$types) {
40
  $types['libraries'] = 'libraries_drush_invalidate_cache';
41
}
42 85ad3d82 Assos Assos
43 7c856df4 Assos Assos
/**
44
 * Clears the library cache.
45
 */
46
function libraries_drush_invalidate_cache() {
47 ed912c77 Assos Assos
  libraries_cache_clear();
48 85ad3d82 Assos Assos
}
49
50
/**
51 7c856df4 Assos Assos
 * Command callback. Show a list of registered libraries.
52 85ad3d82 Assos Assos
 */
53 7c856df4 Assos Assos
function drush_libraries_list() {
54
  $libraries = libraries_detect();
55 85ad3d82 Assos Assos
  ksort($libraries);
56
57
  if (empty($libraries)) {
58
    drush_print('There are no registered libraries.');
59
  }
60
  else {
61 7c856df4 Assos Assos
    module_load_include('inc', 'libraries', 'libraries.admin');
62
63 85ad3d82 Assos Assos
    $rows = array();
64
    // drush_print_table() automatically treats the first row as the header, if
65
    // $header is TRUE.
66 7c856df4 Assos Assos
    $rows[] = array(
67
      dt('Name'),
68
      dt('Status'),
69
      dt('Version'),
70
      dt('Variants'),
71
      dt('Dependencies'),
72
      dt('Provider'),
73
    );
74 85ad3d82 Assos Assos
    foreach ($libraries as $name => $library) {
75
      // Only list installed variants.
76
      $variants = array();
77
      foreach ($library['variants'] as $variant_name => $variant) {
78
        if ($variant['installed']) {
79
          $variants[] = $variant_name;
80
        }
81
      }
82
83 7c856df4 Assos Assos
      $rows[] = array(
84
        $name,
85
        $library['installed'] ? dt('OK') : drupal_ucfirst($library['error']),
86
        ($library['installed'] && $library['version']) ? '-' : $library['version'],
87
        $variants ? implode(', ', $variants) : '-',
88
        $library['dependencies'] ? implode(', ', $library['dependencies']) : '-',
89
        libraries_admin_get_provider($library),
90
      );
91 85ad3d82 Assos Assos
    }
92 7c856df4 Assos Assos
93 85ad3d82 Assos Assos
    // Make the possible values for the 'Status' column and the 'Version' header
94
    // wrap nicely.
95 7c856df4 Assos Assos
    $widths = array(0, 12, 7, 0, 0, 0);
96 85ad3d82 Assos Assos
    drush_print_table($rows, TRUE, $widths);
97
  }
98
}
99
100
/**
101 7c856df4 Assos Assos
 * Command callback. Downloads a library.
102
 *
103
 * Only libraries that provide a download file URL can be downloaded.
104 85ad3d82 Assos Assos
 *
105 7c856df4 Assos Assos
 * @see hook_libraries_info()
106
 * @see drush_pm_download()
107 85ad3d82 Assos Assos
 */
108 7c856df4 Assos Assos
function drush_libraries_download() {
109
  drush_command_include('pm-download');
110
111 ed912c77 Assos Assos
  $all_libraries = libraries_detect();
112 7c856df4 Assos Assos
113 ed912c77 Assos Assos
  // Prepare a list of names of downloadable libraries.
114
  $downloadable_names = array();
115
  foreach ($all_libraries as $machine_name => $library) {
116
    // Skip libraries that are already installed.
117
    // @todo Allow (optionally) re-downloading installing libraries.
118
    if (!empty($library['download file url']) && !$library['installed']) {
119
      $downloadable_names[] = $machine_name;
120 7c856df4 Assos Assos
    }
121 ed912c77 Assos Assos
  }
122 7c856df4 Assos Assos
123 ed912c77 Assos Assos
  // Gather a list of libraries to download. If '--all' was specified, that
124
  // takes precedence over any other arguments. Otherwise and if no arguments
125
  // are specified, we present a choice of all downloadable libraries.
126
  if (drush_get_option('all', FALSE) && $downloadable_names) {
127
    $machine_names = $downloadable_names;
128
  }
129
  elseif (pm_parse_arguments(func_get_args(), FALSE)) {
130
    $machine_names = array();
131
    foreach (pm_parse_arguments(func_get_args(), FALSE) as $machine_name) {
132
      // If there was an error with with one of the libraries, continue to try
133
      // to install any remaining libraries.
134
      if (!isset($all_libraries[$machine_name])) {
135
        $message = dt("The !library library is not registered with Libraries API.\n", array('!library' => $machine_name));
136
        $message .= dt("Provide an info file for it or implement hook_libraries_info().\n");
137
        $message .= dt("See hook_libraries_info() for more information.\n");
138
        drush_set_error('DRUSH_LIBRARY_UKNOWN', $message);
139
        continue;
140
      }
141
      if (empty($all_libraries[$machine_name]['download file url'])) {
142
        $message = dt("The !library library cannot be downloaded.\n", array('!library' => $machine_name));
143
        $message .= dt("Libraries need to specify a download file URL to support being downloaded via Drush.\n");
144
        $message .= dt("See hook_libraries_info() for more information.\n");
145
        drush_set_error('DRUSH_LIBRARY_NOT_DOWNLOADABLE', $message);
146
        continue;
147
      }
148
      $machine_names[] = $machine_name;
149
    }
150
  }
151
  elseif ($downloadable_names) {
152
    $machine_names = drush_choice_multiple(drupal_map_assoc($downloadable_names), FALSE, 'Select which libraries to download.');
153
    // If the operation was cancelled by the user, or if no libraries were
154
    // selected, bail out without any further error message.
155
    if (!$machine_names) {
156
      return;
157 7c856df4 Assos Assos
    }
158 ed912c77 Assos Assos
  }
159
  else {
160
    drush_log(dt('There are no registered, uninstalled libraries that can be downloaded.'), 'warning');
161
    return;
162
  }
163
164
  foreach ($machine_names as $machine_name) {
165
    $download_url = $all_libraries[$machine_name]['download file url'];
166 7c856df4 Assos Assos
167
    drush_log(dt('Downloading library !name ...', array('!name' => $machine_name)));
168
169
    // @see package_handler_download_project() in wget.inc
170
    // It cannot be used directly because it will always try to extract the
171
    // archive which fails when downloading a single file.
172
    // @todo Modify upstream to be able to use
173
    //   package_handler_download_project() directly.
174
    // Prepare download path. On Windows file name cannot contain '?'.
175
    // See http://drupal.org/node/1782444
176
    $filename = str_replace('?', '_', basename($download_url));
177
    $download_path = drush_tempdir() . '/' . $filename;
178
179
    // Download the tarball.
180
    // Never cache the downloaded file. The downloading relies on the fact that
181
    // different versions of the library are available under the same URL as new
182
    // versions are released.
183
    $download_path = drush_download_file($download_url, $download_path, 0);
184
    if ($download_path || drush_get_context('DRUSH_SIMULATE')) {
185
      drush_log(dt('Downloading !filename was successful.', array('!filename' => $filename)));
186
    }
187
    else {
188
      drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', dt('Unable to download !project to !path from !url.', array('!project' => $machine_name, '!path' => $download_path, '!url' => $download_url)));
189
      drush_log(dt('Error downloading !name', array('!name' => $machine_name)), 'error');
190
      continue;
191
    }
192 85ad3d82 Assos Assos
193 7c856df4 Assos Assos
    // @todo Suport MD5 file hashing.
194 85ad3d82 Assos Assos
195 7c856df4 Assos Assos
    // Extract the tarball in place and return the full path to the untarred directory.
196
    $download_base = dirname($download_path);
197
    if (drush_file_is_tarball($download_path)) {
198
      if (!$tar_file_list = drush_tarball_extract($download_path, $download_base, TRUE)) {
199
        // An error has been logged.
200
        return FALSE;
201
      }
202
      $tar_directory = drush_trim_path($tar_file_list[0]);
203
      $download_path = $download_base . '/' . $tar_directory;
204
    }
205
    else {
206
      $download_path = $download_base;
207
    }
208 85ad3d82 Assos Assos
209 7c856df4 Assos Assos
    // Determine the install location for the project.  User provided
210
    // --destination has preference.
211
    $destination = drush_get_option('destination');
212
    if (!empty($destination)) {
213
      if (!file_exists($destination)) {
214
        drush_mkdir($destination);
215
      }
216
      $install_location = realpath($destination);
217
    }
218
    else {
219
      /** @see _pm_download_destination_lookup() */
220
      // _pm_download_destination_lookup() pluralizes the passed type by
221
      // appending an s.
222
      // This relies on the fact that there is no library named 'contrib'.
223
      // @todo Request that this be turned into a proper API upstream.
224
      $install_location = _pm_download_destination('librarie');
225
    }
226 85ad3d82 Assos Assos
227 7c856df4 Assos Assos
    // @todo Consider invoking a hook similar to
228
    //   hook_drush_pm_download_destination_alter().
229 85ad3d82 Assos Assos
230 7c856df4 Assos Assos
    // @todo Consider adding version-control support similar to pm-download.
231 85ad3d82 Assos Assos
232 7c856df4 Assos Assos
    $install_location .= '/' . $machine_name;
233 85ad3d82 Assos Assos
234 7c856df4 Assos Assos
    // Check if install location already exists.
235
    if (is_dir($install_location)) {
236
      if (drush_confirm(dt('Install location !location already exists. Do you want to overwrite it?', array('!location' => $install_location)))) {
237
        drush_delete_dir($install_location, TRUE);
238
      }
239
      else {
240 ed912c77 Assos Assos
        drush_log(dt("Skip installation of !project to !dest.", array('!project' => $machine_name, '!dest' => $install_location)), 'warning');
241 7c856df4 Assos Assos
        continue;
242
      }
243
    }
244 85ad3d82 Assos Assos
245 7c856df4 Assos Assos
    // Copy the project to the install location.
246
    if (drush_op('_drush_recursive_copy', $download_path, $install_location)) {
247
      drush_log(dt("Library !project downloaded to !dest.", array('!project' => $machine_name, '!dest' => $install_location)), 'success');
248 85ad3d82 Assos Assos
249 7c856df4 Assos Assos
      // @todo Consider invoking a hook similar to
250
      //   hook_drush_pm_post_download().
251 85ad3d82 Assos Assos
252 7c856df4 Assos Assos
      // @todo Support printing release notes.
253
    }
254
    else {
255
      // We don't `return` here in order to proceed with downloading additional projects.
256
      drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', dt("Project !project could not be downloaded to !dest.", array('!project' => $machine_name, '!dest' => $install_location)));
257
    }
258
259
    // @todo Consider adding notify support.
260 85ad3d82 Assos Assos
  }
261
}