45038baad1a46eeb5c10f3c5a104b95953abeb2f
[yaffs-website] / vendor / drush / drush / commands / core / drupal / environment_7.inc
1 <?php
2 /**
3  * @file
4  *   Specific functions for a drupal 7 environment.
5  *   drush_include_engine() magically includes either this file
6  *   or environment_X.inc depending on which version of drupal drush
7  *   is called from.
8  */
9
10 /**
11  * Get complete information for all available modules.
12  *
13  * @param $include_hidden
14  *   Boolean to indicate whether hidden modules should be excluded or not.
15  * @return
16  *   An array containing module info for all available modules.
17  */
18 function drush_get_modules($include_hidden = TRUE) {
19   $modules = system_rebuild_module_data();
20   if (!$include_hidden) {
21     foreach ($modules as $key => $module) {
22       if (isset($module->info['hidden'])) {
23         unset($modules[$key]);
24       }
25     }
26   }
27
28   return $modules;
29 }
30
31 /**
32  * Returns drupal required modules, including modules declared as required dynamically.
33  */
34 function _drush_drupal_required_modules($module_info) {
35   $required = drupal_required_modules();
36   foreach ($module_info as $name => $module) {
37     if (isset($module->info['required']) && $module->info['required']) {
38       $required[] = $name;
39     }
40   }
41   return array_unique($required);
42 }
43
44 /**
45  * Return dependencies and its status for modules.
46  *
47  * @param $modules
48  *   Array of module names
49  * @param $module_info
50  *   Drupal 'files' array for modules as returned by drush_get_modules().
51  * @return
52  *   Array with dependencies and status for $modules
53  */
54 function drush_check_module_dependencies($modules, $module_info) {
55   $status = array();
56   foreach ($modules as $key => $module) {
57     $dependencies = array_reverse($module_info[$module]->requires);
58     $unmet_dependencies = array_diff(array_keys($dependencies), array_keys($module_info));
59     if (!empty($unmet_dependencies)) {
60       $status[$key]['error'] = array(
61           'code' => 'DRUSH_PM_ENABLE_DEPENDENCY_NOT_FOUND',
62           'message' => dt('Module !module cannot be enabled because it depends on the following modules which could not be found: !unmet_dependencies', array('!module' => $module, '!unmet_dependencies' => implode(',', $unmet_dependencies)))
63       );
64     }
65     else {
66       // check for version incompatibility
67       foreach ($dependencies as $dependency_name => $v) {
68         $current_version = $module_info[$dependency_name]->info['version'];
69         $current_version = str_replace(drush_get_drupal_core_compatibility() . '-', '', $current_version);
70         $incompatibility = drupal_check_incompatibility($v, $current_version);
71         if (isset($incompatibility)) {
72           $status[$key]['error'] = array(
73             'code' => 'DRUSH_PM_ENABLE_DEPENDENCY_VERSION_MISMATCH',
74             'message' => dt('Module !module cannot be enabled because it depends on !dependency !required_version but !current_version is available', array('!module' => $module, '!dependency' => $dependency_name, '!required_version' => $incompatibility, '!current_version' => $current_version))
75           );
76         }
77       }
78     }
79     $status[$key]['unmet-dependencies'] = $unmet_dependencies;
80     $status[$key]['dependencies'] = $dependencies;
81   }
82
83   return $status;
84 }
85
86 /**
87  * Return dependents of modules.
88  *
89  * @param $modules
90  *   Array of module names
91  * @param $module_info
92  *   Drupal 'files' array for modules as returned by drush_get_modules().
93  * @return
94  *   Array with dependents for each one of $modules
95  */
96 function drush_module_dependents($modules, $module_info) {
97   $dependents = array();
98   foreach ($modules as $module) {
99     $dependents = array_merge($dependents, drupal_map_assoc(array_keys($module_info[$module]->required_by)));
100   }
101
102   return array_unique($dependents);
103 }
104
105 /**
106  * Returns a list of enabled modules.
107  *
108  * This is a simplified version of module_list().
109  */
110 function drush_module_list() {
111   $enabled = array();
112   $rsc = drush_db_select('system', 'name', 'type=:type AND status=:status', array(':type' => 'module', ':status' => 1));
113   while ($row = drush_db_result($rsc)) {
114     $enabled[$row] = $row;
115   }
116
117   return $enabled;
118 }
119
120 /**
121  * Return a list of extensions from a list of named extensions.
122  * Both enabled and disabled/uninstalled extensions are returned.
123  */
124 function drush_get_named_extensions_list($extensions) {
125   $result = array();
126   $rsc = drush_db_select('system', array('name', 'status'), 'name IN (:extensions)', array(':extensions' => $extensions));
127   while ($row = drush_db_fetch_object($rsc)) {
128     $result[$row->name] = $row;
129   }
130   return $result;
131 }
132
133 /**
134  * Enable a list of modules. It is assumed the list contains all the dependencies not already enabled.
135  *
136  * @param $modules
137  *   Array of module names
138  */
139 function drush_module_enable($modules) {
140   // The list of modules already have all the dependencies, but they might not
141   // be in the correct order. Still pass $enable_dependencies = TRUE so that
142   // Drupal will enable the modules in the correct order.
143   module_enable($modules);
144   // Flush all caches.
145   drupal_flush_all_caches();
146 }
147
148 /**
149  * Disable a list of modules. It is assumed the list contains all dependents not already disabled.
150  *
151  * @param $modules
152  *   Array of module names
153  */
154 function drush_module_disable($modules) {
155   // The list of modules already have all the dependencies, but they might not
156   // be in the correct order. Still pass $enable_dependencies = TRUE so that
157   // Drupal will enable the modules in the correct order.
158   module_disable($modules);
159   // Flush all caches.
160   drupal_flush_all_caches();
161 }
162
163 /**
164  * Uninstall a list of modules.
165  *
166  * @param $modules
167  *   Array of module names
168  */
169 function drush_module_uninstall($modules) {
170   require_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
171   // Break off 8.x functionality when we get another change.
172   if (drush_drupal_major_version() >= 8) {
173     module_uninstall($modules);
174   }
175   else {
176     drupal_uninstall_modules($modules);
177   }
178 }
179
180 /**
181  * Checks that a given module exists and is enabled.
182  *
183  * @see module_exists().
184  *
185  */
186 function drush_module_exists($module) {
187   return module_exists($module);
188 }
189
190 /**
191  * Determines which modules are implementing a hook.
192  *
193  */
194 function drush_module_implements($hook, $sort = FALSE, $reset = FALSE) {
195   return module_implements($hook, $sort, $reset);
196 }
197
198 /**
199  * Invokes a hook in a particular module.
200  *
201  */
202 function drush_module_invoke($module, $hook) {
203   $args = func_get_args();
204   return call_user_func_array('module_invoke', $args);
205 }
206
207 /**
208  * Invokes a hook in all enabled modules that implement it.
209  *
210  */
211 function drush_module_invoke_all($hook) {
212   $args = func_get_args();
213   return call_user_func_array('module_invoke_all', $args);
214 }
215
216 /**
217  * Returns a list of enabled themes. Use drush_get_themes() if you need to rebuild
218  * and include hidden/disabled as well.
219  *
220  * @return array
221  *  A list of themes keyed by name.
222  */
223 function drush_theme_list() {
224   $enabled = array();
225   foreach (list_themes() as $key => $info) {
226     if ($info->status) {
227       $enabled[$key] = $info;
228     }
229   }
230   return $enabled;
231 }
232
233 /**
234  * Get complete information for all available themes.
235  *
236  * @param $include_hidden
237  *   Boolean to indicate whether hidden themes should be excluded or not.
238  * @return
239  *   An array containing theme info for all available themes.
240  */
241 function drush_get_themes($include_hidden = TRUE) {
242   $themes = system_rebuild_theme_data();
243   if (!$include_hidden) {
244     foreach ($themes as $key => $theme) {
245       if (isset($theme->info['hidden'])) {
246         unset($themes[$key]);
247       }
248     }
249   }
250
251   return $themes;
252 }
253
254 /**
255  * Enable a list of themes.
256  *
257  * @param $themes
258  *  Array of theme names.
259  */
260 function drush_theme_enable($themes) {
261   theme_enable($themes);
262 }
263
264 /**
265  * Disable a list of themes.
266  *
267  * @param $themes
268  *  Array of theme names.
269  */
270 function drush_theme_disable($themes) {
271   theme_disable($themes);
272 }
273
274 /**
275  * Helper function to obtain the severity levels based on Drupal version.
276  *
277  * This is a copy of watchdog_severity_levels() without t().
278  *
279  * Severity levels, as defined in RFC 3164: http://www.ietf.org/rfc/rfc3164.txt.
280  *
281  * @return
282  *   Array of watchdog severity levels.
283  */
284 function drush_watchdog_severity_levels() {
285   return array(
286     WATCHDOG_EMERGENCY=> 'emergency',
287     WATCHDOG_ALERT    => 'alert',
288     WATCHDOG_CRITICAL => 'critical',
289     WATCHDOG_ERROR    => 'error',
290     WATCHDOG_WARNING  => 'warning',
291     WATCHDOG_NOTICE   => 'notice',
292     WATCHDOG_INFO     => 'info',
293     WATCHDOG_DEBUG    => 'debug',
294   );
295 }
296
297 /**
298  * Helper function to obtain the message types based on drupal version.
299  *
300  * @return
301  *   Array of watchdog message types.
302  */
303 function drush_watchdog_message_types() {
304   return drupal_map_assoc(_dblog_get_message_types());
305 }
306
307 function _drush_theme_default() {
308   return variable_get('theme_default', 'garland');
309 }
310
311 function _drush_theme_admin() {
312   return variable_get('admin_theme', drush_theme_get_default());
313 }
314
315 function _drush_file_public_path() {
316   return variable_get('file_public_path', conf_path() . '/files');
317 }
318
319 function _drush_file_private_path() {
320   return variable_get('file_private_path', FALSE);
321 }
322
323 /**
324  * Gets the extension name.
325  *
326  * @param $info
327  *   The extension info.
328  * @return string
329  *   The extension name.
330  */
331 function _drush_extension_get_name($info) {
332   return $info->name;
333 }
334
335 /**
336  * Gets the extension type.
337  *
338  * @param $info
339  *   The extension info.
340  * @return string
341  *   The extension type.
342  */
343 function _drush_extension_get_type($info) {
344   return $info->type;
345 }
346
347 /**
348  * Gets the extension path.
349  *
350  * @param $info
351  *   The extension info.
352  * @return string
353  *   The extension path.
354  */
355 function _drush_extension_get_path($info) {
356   return dirname($info->filename);
357 }
358
359 /*
360  * Wrapper for CSRF token generation.
361  */
362 function drush_get_token($value = NULL) {
363   return drupal_get_token($value);
364 }
365
366 /*
367  * Wrapper for _url().
368  */
369 function drush_url($path = NULL, array $options = array()) {
370   return url($path, $options);
371 }
372
373 /**
374  * Output a Drupal render array, object or plain string as plain text.
375  *
376  * @param string $data
377  *   Data to render.
378  *
379  * @return string
380  *   The plain-text representation of the input.
381  */
382 function drush_render($data) {
383   if (is_array($data)) {
384     $data = drupal_render($data);
385   }
386
387   $data = drupal_html_to_text($data);
388   return $data;
389 }