Further modules included.
[yaffs-website] / web / modules / contrib / libraries / libraries.drush.inc
1 <?php
2
3 /**
4  * @file
5  * Drush integration for Libraries API.
6  */
7
8 use Drupal\Component\Utility\Unicode;
9
10 /**
11  * Implements hook_drush_command().
12  */
13 function libraries_drush_command() {
14   $items['libraries-list'] = array(
15     'callback' => 'libraries_drush_list',
16     'description' => dt('Lists registered library information.'),
17     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
18   );
19   /**$items['libraries-download'] = array(
20     'callback' => 'libraries_drush_download',
21     'description' => dt('Downloads a registered library into the libraries directory for the active site.'),
22     'arguments' => array(
23       'name' => dt('The internal name of the registered library.'),
24     ),
25   );*/
26   return $items;
27 }
28
29 /**
30  * Implements hook_drush_help().
31  */
32 function libraries_drush_help($section) {
33   switch ($section) {
34     case 'drush:libraries-list':
35       return dt('Lists registered library information.');
36
37     case 'drush:libraries-download':
38       return dt('Downloads a registered library into the libraries directory for the active site.
39
40 See libraries-list for a list of registered libraries.');
41   }
42 }
43
44 /**
45  * Implements hook_drush_cache_clear().
46  *
47  * @see drush_cache_clear_types()
48  */
49 function libraries_drush_cache_clear(&$types) {
50   $types['libraries'] = 'libraries_drush_invalidate_cache';
51 }
52
53 /**
54  * Clears the library cache.
55  */
56 function libraries_drush_invalidate_cache() {
57   \Drupal::cache('libraries')->deleteAll();
58 }
59
60 /**
61  * Lists registered library information.
62  */
63 function libraries_drush_list() {
64   $libraries = array();
65   foreach (libraries_info() as $name => $info) {
66     $libraries[$name] = libraries_detect($name);
67   }
68   ksort($libraries);
69
70   if (empty($libraries)) {
71     drush_print('There are no registered libraries.');
72   }
73
74   else {
75     $rows = array();
76     // drush_print_table() automatically treats the first row as the header, if
77     // $header is TRUE.
78     $rows[] = array(dt('Name'), dt('Status'), dt('Version'), dt('Variants'), dt('Dependencies'));
79     foreach ($libraries as $name => $library) {
80       $status = ($library['installed'] ? dt('OK') : Unicode::ucfirst($library['error']));
81       $version = (($library['installed'] && !empty($library['version'])) ? $library['version'] : '-');
82
83       // Only list installed variants.
84       $variants = array();
85       foreach ($library['variants'] as $variant_name => $variant) {
86         if ($variant['installed']) {
87           $variants[] = $variant_name;
88         }
89       }
90       $variants = (empty($variants) ? '-' : implode(', ', $variants));
91
92       $dependencies = (!empty($library['dependencies']) ? implode(', ', $library['dependencies']) : '-');
93
94       $rows[] = array($name, $status, $version, $variants, $dependencies);
95     }
96     // Make the possible values for the 'Status' column and the 'Version' header
97     // wrap nicely.
98     $widths = array(0, 12, 7, 0, 0);
99     drush_print_table($rows, TRUE, $widths);
100   }
101 }
102
103 /**
104  * Downloads a library.
105  *
106  * @param $name
107  *   The internal name of the library to download.
108  */
109 function libraries_drush_download($name) {
110   return;
111
112   // @todo Looks wonky?
113   if (!drush_shell_exec('type unzip')) {
114     return drush_set_error(dt('Missing dependency: unzip. Install it before using this command.'));
115   }
116
117   // @todo Simply use current drush site.
118   $args = func_get_args();
119   if ($args[0]) {
120     $path = $args[0];
121   }
122   else {
123     $path = 'sites/all/libraries';
124   }
125
126   // Create the path if it does not exist.
127   if (!is_dir($path)) {
128     drush_op('mkdir', $path);
129     drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
130   }
131
132   // Set the directory to the download location.
133   $olddir = getcwd();
134   chdir($path);
135
136   $filename = basename(COLORBOX_DOWNLOAD_URI);
137   $dirname = basename(COLORBOX_DOWNLOAD_URI, '.zip');
138
139   // Remove any existing Colorbox plugin directory
140   if (is_dir($dirname)) {
141     drush_log(dt('A existing Colorbox plugin was overwritten at @path', array('@path' => $path)), 'notice');
142   }
143   // Remove any existing Colorbox plugin zip archive
144   if (is_file($filename)) {
145     drush_op('unlink', $filename);
146   }
147
148   // Download the zip archive
149   if (!drush_shell_exec('wget '. COLORBOX_DOWNLOAD_URI)) {
150     drush_shell_exec('curl -O '. COLORBOX_DOWNLOAD_URI);
151   }
152
153   if (is_file($filename)) {
154     // Decompress the zip archive
155     drush_shell_exec('unzip -qq -o '. $filename);
156     // Remove the zip archive
157     drush_op('unlink', $filename);
158   }
159
160   // Set working directory back to the previous working directory.
161   chdir($olddir);
162
163   if (is_dir($path .'/'. $dirname)) {
164     drush_log(dt('Colorbox plugin has been downloaded to @path', array('@path' => $path)), 'success');
165   }
166   else {
167     drush_log(dt('Drush was unable to download the Colorbox plugin to @path', array('@path' => $path)), 'error');
168   }
169 }