cee6c48e83166ce45aae09b83ceb887df2f09cd9
[yaffs-website] / vendor / drush / drush / includes / drupal.inc
1 <?php
2
3 /**
4  * @file
5  * Utility functions related to Drupal.
6  */
7
8 use Drush\Log\LogLevel;
9
10 /**
11  * Loads the Drupal autoloader and returns the instance.
12  */
13 function drush_drupal_load_autoloader($drupal_root) {
14   static $autoloader = FALSE;
15
16   if (!$autoloader) {
17     $autoloader = require $drupal_root .'/autoload.php';
18     if ($autoloader === TRUE) {
19       // The autoloader was already require(). Assume that Drush and Drupal share an autoloader per
20       // "Point autoload.php to the proper vendor directory" - https://www.drupal.org/node/2404989
21       $autoloader = drush_get_context('DRUSH_CLASSLOADER');
22     }
23   }
24   return $autoloader;
25 }
26
27 /**
28  * Detects the version number of the current Drupal installation,
29  * if any. Returns FALSE if there is no current Drupal installation,
30  * or it is somehow broken.
31  *
32  * @return
33  *   A string containing the version number of the current
34  *   Drupal installation, if any. Otherwise, return FALSE.
35  */
36 function drush_drupal_version($drupal_root = NULL) {
37   static $version = FALSE;
38
39   if (!$version) {
40     if (($drupal_root != NULL) || ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT'))) {
41       $bootstrap = drush_bootstrap_class_for_root($drupal_root);
42       if ($bootstrap) {
43         $version = $bootstrap->get_version($drupal_root);
44       }
45     }
46   }
47   return $version;
48 }
49
50 function drush_drupal_cache_clear_all() {
51   if (drush_drupal_major_version() >= 8) {
52     drush_invoke_process('@self', 'cache-rebuild');
53   }
54   else {
55     drush_invoke_process('@self', 'cache-clear', array('all'));
56   }
57 }
58
59 /**
60  * Returns the Drupal major version number (6, 7, 8 ...)
61  */
62 function drush_drupal_major_version($drupal_root = NULL) {
63   $major_version = FALSE;
64   if ($version = drush_drupal_version($drupal_root)) {
65     $version_parts = explode('.', $version);
66     if (is_numeric($version_parts[0])) {
67       $major_version = (integer)$version_parts[0];
68     }
69   }
70   return $major_version;
71 }
72
73 /**
74  * Log Drupal watchdog() calls.
75  *
76  * A sneaky implementation of hook_watchdog(), for D6/D7.
77  */
78 function system_watchdog($log_entry) {
79   // Transform non informative severity levels to 'error' for compatibility with _drush_print_log.
80   // Other severity levels are coincident with the ones we use in drush.
81   if (drush_drupal_major_version() >= 6 && $log_entry['severity'] <= 2) {
82     $severity = 'error';
83   }
84   else {
85     drush_include_engine('drupal', 'environment');
86     $levels = drush_watchdog_severity_levels();
87     $severity = $levels[$log_entry['severity']];
88   }
89   // Format the message.
90   if (is_array($log_entry['variables'])) {
91     $message = strtr($log_entry['message'], $log_entry['variables']);
92   }
93   else {
94     $message = $log_entry['message'];
95   }
96
97   // decode_entities() only loaded after FULL bootstrap.
98   if (function_exists('decode_entities')) {
99     $message = decode_entities($message);
100   }
101   $message = strip_tags($message);
102
103   // Log or print or ignore. Just printing saves memory but thats rarely needed.
104   switch (drush_get_option('watchdog', 'log')) {
105     case 'log':
106       drush_log('WD '. $log_entry['type'] . ': ' . $message, $severity);
107       break;
108     case 'print':
109       // Disable in backend mode since it logs output and the goal is to conserve memory.
110       // @see _drush_bootstrap_drush().
111       if (ob_get_length() === FALSE) {
112         drush_print('WD '. $severity . ' ' . $log_entry['type'] . ': ' . $message);
113       }
114       break;
115     default:
116       // Do nothing.
117   }
118 }
119
120 /**
121  * Log the return value of Drupal hook_update_n functions.
122  *
123  * This is used during install and update to log the output
124  * of the update process to the logging system.
125  */
126 function _drush_log_update_sql($ret) {
127   if (count($ret)) {
128     foreach ($ret as $info) {
129       if (is_array($info)) {
130         if (!$info['success']) {
131           drush_set_error('DRUPAL_UPDATE_FAILED', $info['query']);
132         }
133         else {
134           drush_log($info['query'], ($info['success']) ? LogLevel::SUCCESS : LogLevel::ERROR);
135         }
136       }
137     }
138   }
139 }
140
141 function drush_find_profiles($drupal_root , $key = 'name') {
142   return drush_scan_directory($drupal_root . '/profiles', "/.*\.profile$/", array('.', '..', 'CVS', 'tests'), 0, 2, $key);
143 }
144
145 /**
146  * Parse Drupal info file format.
147  *
148  * Copied with modifications from includes/common.inc.
149  *
150  * @see drupal_parse_info_file
151  */
152 function drush_drupal_parse_info_file($filename) {
153   if (!file_exists($filename)) {
154     return array();
155   }
156
157   $data = file_get_contents($filename);
158   return _drush_drupal_parse_info_file($data);
159 }
160
161 /**
162  * Parse the info file.
163  */
164 function _drush_drupal_parse_info_file($data, $merge_item = NULL) {
165   if (!$data) {
166     return FALSE;
167   }
168
169   if (preg_match_all('
170     @^\s*                           # Start at the beginning of a line, ignoring leading whitespace
171     ((?:
172       [^=;\[\]]|                    # Key names cannot contain equal signs, semi-colons or square brackets,
173       \[[^\[\]]*\]                  # unless they are balanced and not nested
174     )+?)
175     \s*=\s*                         # Key/value pairs are separated by equal signs (ignoring white-space)
176     (?:
177       ("(?:[^"]|(?<=\\\\)")*")|     # Double-quoted string, which may contain slash-escaped quotes/slashes
178       (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
179       ([^\r\n]*?)                   # Non-quoted string
180     )\s*$                           # Stop at the next end of a line, ignoring trailing whitespace
181     @msx', $data, $matches, PREG_SET_ORDER)) {
182     $info = array();
183     foreach ($matches as $match) {
184       // Fetch the key and value string.
185       $i = 0;
186       foreach (array('key', 'value1', 'value2', 'value3') as $var) {
187         $$var = isset($match[++$i]) ? $match[$i] : '';
188       }
189       $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
190
191       // Parse array syntax.
192       $keys = preg_split('/\]?\[/', rtrim($key, ']'));
193       $last = array_pop($keys);
194       $parent = &$info;
195
196       // Create nested arrays.
197       foreach ($keys as $key) {
198         if ($key == '') {
199           $key = count($parent);
200         }
201         if (isset($merge_item) && isset($parent[$key]) && !is_array($parent[$key])) {
202           $parent[$key] = array($merge_item => $parent[$key]);
203         }
204         if (!isset($parent[$key]) || !is_array($parent[$key])) {
205           $parent[$key] = array();
206         }
207         $parent = &$parent[$key];
208       }
209
210       // Handle PHP constants.
211       if (defined($value)) {
212         $value = constant($value);
213       }
214
215       // Insert actual value.
216       if ($last == '') {
217         $last = count($parent);
218       }
219       if (isset($merge_item) && isset($parent[$last]) && is_array($parent[$last])) {
220         $parent[$last][$merge_item] = $value;
221       }
222       else {
223         $parent[$last] = $value;
224       }
225     }
226     return $info;
227   }
228   return FALSE;
229 }
230
231 /**
232  * Build a cache id to store the install_profile for a given site.
233  */
234 function drush_cid_install_profile() {
235   return drush_get_cid('install_profile', array(), array(drush_get_context('DRUSH_SELECTED_DRUPAL_SITE_CONF_PATH')));
236 }
237
238 /*
239  * An array of options shared by sql-sanitize and sql-sync commands.
240  */
241 function drupal_sanitize_options() {
242   return array(
243     'sanitize-password' => array(
244       'description' => 'The password to assign to all accounts in the sanitization operation, or "no" to keep passwords unchanged.',
245       'example-value' => 'password',
246       'value' => 'required',
247     ),
248     'sanitize-email' => array(
249       'description' => 'The pattern for test email addresses in the sanitization operation, or "no" to keep email addresses unchanged.  May contain replacement patterns %uid, %mail or %name.',
250       'example-value' => 'user+%uid@localhost',
251       'value' => 'required',
252     ),
253   );
254 }