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 @ 1aa883a3

1
<?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
  $items = array();
12

    
13
  $items['libraries-list'] = array(
14
    'description' => dt('Show a list of registered libraries.'),
15
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
16
    'aliases' => array('lls', 'lib-list'),
17
  );
18

    
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
    'arguments' => array(
24
      'libraries' => 'A comma delimited list of library machine names.',
25
    ),
26
    'options' => array(
27
      'all' => 'Download all registered libraries.',
28
    ),
29
  );
30

    
31
  return $items;
32
}
33

    
34
/**
35
 * Implements hook_drush_cache_clear().
36
 *
37
 * @see drush_cache_clear_types()
38
 */
39
function libraries_drush_cache_clear(array &$types) {
40
  $types['libraries'] = 'libraries_drush_invalidate_cache';
41
}
42

    
43
/**
44
 * Clears the library cache.
45
 */
46
function libraries_drush_invalidate_cache() {
47
  libraries_cache_clear();
48
}
49

    
50
/**
51
 * Command callback. Show a list of registered libraries.
52
 */
53
function drush_libraries_list() {
54
  $libraries = libraries_detect();
55
  ksort($libraries);
56

    
57
  if (empty($libraries)) {
58
    drush_print('There are no registered libraries.');
59
  }
60
  else {
61
    module_load_include('inc', 'libraries', 'libraries.admin');
62

    
63
    $rows = array();
64
    // drush_print_table() automatically treats the first row as the header, if
65
    // $header is TRUE.
66
    $rows[] = array(
67
      dt('Name'),
68
      dt('Status'),
69
      dt('Version'),
70
      dt('Variants'),
71
      dt('Dependencies'),
72
      dt('Provider'),
73
    );
74
    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
      $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
    }
92

    
93
    // Make the possible values for the 'Status' column and the 'Version' header
94
    // wrap nicely.
95
    $widths = array(0, 12, 7, 0, 0, 0);
96
    drush_print_table($rows, TRUE, $widths);
97
  }
98
}
99

    
100
/**
101
 * Command callback. Downloads a library.
102
 *
103
 * Only libraries that provide a download file URL can be downloaded.
104
 *
105
 * @see hook_libraries_info()
106
 * @see drush_pm_download()
107
 */
108
function drush_libraries_download() {
109
  drush_command_include('pm-download');
110

    
111
  $all_libraries = libraries_detect();
112

    
113
  // 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
    }
121
  }
122

    
123
  // 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
    }
158
  }
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

    
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

    
193
    // @todo Suport MD5 file hashing.
194

    
195
    // 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

    
209
    // 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

    
227
    // @todo Consider invoking a hook similar to
228
    //   hook_drush_pm_download_destination_alter().
229

    
230
    // @todo Consider adding version-control support similar to pm-download.
231

    
232
    $install_location .= '/' . $machine_name;
233

    
234
    // 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
        drush_log(dt("Skip installation of !project to !dest.", array('!project' => $machine_name, '!dest' => $install_location)), 'warning');
241
        continue;
242
      }
243
    }
244

    
245
    // 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

    
249
      // @todo Consider invoking a hook similar to
250
      //   hook_drush_pm_post_download().
251

    
252
      // @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
  }
261
}