Projet

Général

Profil

Paste
Télécharger (8,02 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / libraries / libraries.drush.inc @ 6ae446a4

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
    'required-arguments' => TRUE,
27
  );
28

    
29
  return $items;
30
}
31

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

    
41
/**
42
 * Clears the library cache.
43
 */
44
function libraries_drush_invalidate_cache() {
45
  // @see drupal_flush_all_caches()
46
  foreach (libraries_flush_caches() as $table) {
47
    cache_clear_all('*', $table, TRUE);
48
  }
49
}
50

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

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

    
64
    $rows = array();
65
    // drush_print_table() automatically treats the first row as the header, if
66
    // $header is TRUE.
67
    $rows[] = array(
68
      dt('Name'),
69
      dt('Status'),
70
      dt('Version'),
71
      dt('Variants'),
72
      dt('Dependencies'),
73
      dt('Provider'),
74
    );
75
    foreach ($libraries as $name => $library) {
76
      // Only list installed variants.
77
      $variants = array();
78
      foreach ($library['variants'] as $variant_name => $variant) {
79
        if ($variant['installed']) {
80
          $variants[] = $variant_name;
81
        }
82
      }
83

    
84
      $rows[] = array(
85
        $name,
86
        $library['installed'] ? dt('OK') : drupal_ucfirst($library['error']),
87
        ($library['installed'] && $library['version']) ? '-' : $library['version'],
88
        $variants ? implode(', ', $variants) : '-',
89
        $library['dependencies'] ? implode(', ', $library['dependencies']) : '-',
90
        libraries_admin_get_provider($library),
91
      );
92
    }
93

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

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

    
112
  $libraries = libraries_info();
113

    
114
  // @todo Consider supporting downloading all downloadable libraries.
115
  // @todo Consider offering a selection if no library is specified.
116
  foreach (pm_parse_arguments(func_get_args(), FALSE) as $machine_name) {
117
    if (!isset($libraries[$machine_name])) {
118
      $message = dt("The !library library is not registered with Libraries API.\n", array('!library' => $machine_name));
119
      $message .= dt("Provide an info file for it or implement hook_libraries_info().\n");
120
      $message .= dt("See hook_libraries_info() for more information.\n");
121
      drush_set_error('DRUSH_LIBRARY_UKNOWN', $message);
122
      continue;
123
    }
124
    $library = $libraries[$machine_name];
125

    
126
    if (empty($library['download file url'])) {
127
      $message = dt("The !library library cannot be downloaded.\n", array('!library' => $machine_name));
128
      $message .= dt("Libraries need to specify a download file URL to support being downloaded via Drush.\n");
129
      $message .= dt("See hook_libraries_info() for more information.\n");
130
      drush_set_error('DRUSH_LIBRARY_NOT_DOWNLOADABLE', $message);
131
      continue;
132
    }
133
    $download_url = $library['download file url'];
134

    
135
    drush_log(dt('Downloading library !name ...', array('!name' => $machine_name)));
136

    
137
    // @see package_handler_download_project() in wget.inc
138
    // It cannot be used directly because it will always try to extract the
139
    // archive which fails when downloading a single file.
140
    // @todo Modify upstream to be able to use
141
    //   package_handler_download_project() directly.
142
    // Prepare download path. On Windows file name cannot contain '?'.
143
    // See http://drupal.org/node/1782444
144
    $filename = str_replace('?', '_', basename($download_url));
145
    $download_path = drush_tempdir() . '/' . $filename;
146

    
147
    // Download the tarball.
148
    // Never cache the downloaded file. The downloading relies on the fact that
149
    // different versions of the library are available under the same URL as new
150
    // versions are released.
151
    $download_path = drush_download_file($download_url, $download_path, 0);
152
    if ($download_path || drush_get_context('DRUSH_SIMULATE')) {
153
      drush_log(dt('Downloading !filename was successful.', array('!filename' => $filename)));
154
    }
155
    else {
156
      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)));
157
      drush_log(dt('Error downloading !name', array('!name' => $machine_name)), 'error');
158
      continue;
159
    }
160

    
161
    // @todo Suport MD5 file hashing.
162

    
163
    // Extract the tarball in place and return the full path to the untarred directory.
164
    $download_base = dirname($download_path);
165
    if (drush_file_is_tarball($download_path)) {
166
      if (!$tar_file_list = drush_tarball_extract($download_path, $download_base, TRUE)) {
167
        // An error has been logged.
168
        return FALSE;
169
      }
170
      $tar_directory = drush_trim_path($tar_file_list[0]);
171
      $download_path = $download_base . '/' . $tar_directory;
172
    }
173
    else {
174
      $download_path = $download_base;
175
    }
176

    
177
    // Determine the install location for the project.  User provided
178
    // --destination has preference.
179
    $destination = drush_get_option('destination');
180
    if (!empty($destination)) {
181
      if (!file_exists($destination)) {
182
        drush_mkdir($destination);
183
      }
184
      $install_location = realpath($destination);
185
    }
186
    else {
187
      /** @see _pm_download_destination_lookup() */
188
      // _pm_download_destination_lookup() pluralizes the passed type by
189
      // appending an s.
190
      // This relies on the fact that there is no library named 'contrib'.
191
      // @todo Request that this be turned into a proper API upstream.
192
      $install_location = _pm_download_destination('librarie');
193
    }
194

    
195
    // @todo Consider invoking a hook similar to
196
    //   hook_drush_pm_download_destination_alter().
197

    
198
    // @todo Consider adding version-control support similar to pm-download.
199

    
200
    $install_location .= '/' . $machine_name;
201

    
202
    // Check if install location already exists.
203
    if (is_dir($install_location)) {
204
      if (drush_confirm(dt('Install location !location already exists. Do you want to overwrite it?', array('!location' => $install_location)))) {
205
        drush_delete_dir($install_location, TRUE);
206
      }
207
      else {
208
        drush_log(dt("Skip installation of !project to !dest.", array('!project' => $library['machine name'], '!dest' => $install_location)), 'warning');
209
        continue;
210
      }
211
    }
212

    
213
    // Copy the project to the install location.
214
    if (drush_op('_drush_recursive_copy', $download_path, $install_location)) {
215
      drush_log(dt("Library !project downloaded to !dest.", array('!project' => $machine_name, '!dest' => $install_location)), 'success');
216

    
217
      // @todo Consider invoking a hook similar to
218
      //   hook_drush_pm_post_download().
219

    
220
      // @todo Support printing release notes.
221
    }
222
    else {
223
      // We don't `return` here in order to proceed with downloading additional projects.
224
      drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', dt("Project !project could not be downloaded to !dest.", array('!project' => $machine_name, '!dest' => $install_location)));
225
    }
226

    
227
    // @todo Consider adding notify support.
228
  }
229
}