5 * Utility functions related to Drupal.
8 use Drush\Log\LogLevel;
11 * Loads the Drupal autoloader and returns the instance.
13 function drush_drupal_load_autoloader($drupal_root) {
14 static $autoloader = FALSE;
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');
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.
33 * A string containing the version number of the current
34 * Drupal installation, if any. Otherwise, return FALSE.
36 function drush_drupal_version($drupal_root = NULL) {
37 static $version = FALSE;
40 if (($drupal_root != NULL) || ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT'))) {
41 $bootstrap = drush_bootstrap_class_for_root($drupal_root);
43 $version = $bootstrap->get_version($drupal_root);
50 function drush_drupal_cache_clear_all() {
51 if (drush_drupal_major_version() >= 8) {
52 drush_invoke_process('@self', 'cache-rebuild');
55 drush_invoke_process('@self', 'cache-clear', array('all'));
60 * Returns the Drupal major version number (6, 7, 8 ...)
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];
70 return $major_version;
74 * Log Drupal watchdog() calls.
76 * A sneaky implementation of hook_watchdog(), for D6/D7.
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) {
85 drush_include_engine('drupal', 'environment');
86 $levels = drush_watchdog_severity_levels();
87 $severity = $levels[$log_entry['severity']];
89 // Format the message.
90 if (is_array($log_entry['variables'])) {
91 $message = strtr($log_entry['message'], $log_entry['variables']);
94 $message = $log_entry['message'];
97 // decode_entities() only loaded after FULL bootstrap.
98 if (function_exists('decode_entities')) {
99 $message = decode_entities($message);
101 $message = strip_tags($message);
103 // Log or print or ignore. Just printing saves memory but thats rarely needed.
104 switch (drush_get_option('watchdog', 'log')) {
106 drush_log('WD '. $log_entry['type'] . ': ' . $message, $severity);
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);
121 * Log the return value of Drupal hook_update_n functions.
123 * This is used during install and update to log the output
124 * of the update process to the logging system.
126 function _drush_log_update_sql($ret) {
128 foreach ($ret as $info) {
129 if (is_array($info)) {
130 if (!$info['success']) {
131 drush_set_error('DRUPAL_UPDATE_FAILED', $info['query']);
134 drush_log($info['query'], ($info['success']) ? LogLevel::SUCCESS : LogLevel::ERROR);
141 function drush_find_profiles($drupal_root , $key = 'name') {
142 return drush_scan_directory($drupal_root . '/profiles', "/.*\.profile$/", array('.', '..', 'CVS', 'tests'), 0, 2, $key);
146 * Parse Drupal info file format.
148 * Copied with modifications from includes/common.inc.
150 * @see drupal_parse_info_file
152 function drush_drupal_parse_info_file($filename) {
153 if (!file_exists($filename)) {
157 $data = file_get_contents($filename);
158 return _drush_drupal_parse_info_file($data);
162 * Parse the info file.
164 function _drush_drupal_parse_info_file($data, $merge_item = NULL) {
170 @^\s* # Start at the beginning of a line, ignoring leading whitespace
172 [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
173 \[[^\[\]]*\] # unless they are balanced and not nested
175 \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
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)) {
183 foreach ($matches as $match) {
184 // Fetch the key and value string.
186 foreach (array('key', 'value1', 'value2', 'value3') as $var) {
187 $$var = isset($match[++$i]) ? $match[$i] : '';
189 $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
191 // Parse array syntax.
192 $keys = preg_split('/\]?\[/', rtrim($key, ']'));
193 $last = array_pop($keys);
196 // Create nested arrays.
197 foreach ($keys as $key) {
199 $key = count($parent);
201 if (isset($merge_item) && isset($parent[$key]) && !is_array($parent[$key])) {
202 $parent[$key] = array($merge_item => $parent[$key]);
204 if (!isset($parent[$key]) || !is_array($parent[$key])) {
205 $parent[$key] = array();
207 $parent = &$parent[$key];
210 // Handle PHP constants.
211 if (defined($value)) {
212 $value = constant($value);
215 // Insert actual value.
217 $last = count($parent);
219 if (isset($merge_item) && isset($parent[$last]) && is_array($parent[$last])) {
220 $parent[$last][$merge_item] = $value;
223 $parent[$last] = $value;
232 * Build a cache id to store the install_profile for a given site.
234 function drush_cid_install_profile() {
235 return drush_get_cid('install_profile', array(), array(drush_get_context('DRUSH_SELECTED_DRUPAL_SITE_CONF_PATH')));
239 * An array of options shared by sql-sanitize and sql-sync commands.
241 function drupal_sanitize_options() {
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',
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',