drupal.inc

  1. 8.0.x includes/drupal.inc
  2. 6.x commands/pm/update_info/drupal.inc
  3. 6.x includes/drupal.inc
  4. 7.x includes/drupal.inc
  5. 4.x commands/pm/update_info/drupal.inc
  6. 5.x commands/pm/update_info/drupal.inc
  7. 5.x includes/drupal.inc
  8. master includes/drupal.inc

Utility functions related to Drupal.

Functions

Namesort descending Description
drush_cid_install_profile Build a cache id to store the install_profile for a given site.
drush_drupal_cache_clear_all
drush_drupal_login Log the given user in to a bootstrapped Drupal site.
drush_drupal_major_version Returns the Drupal major version number (6, 7, 8 ...)
drush_drupal_parse_info_file Parse Drupal info file format.
drush_drupal_version Detects the version number of the current Drupal installation, if any. Returns FALSE if there is no current Drupal installation, or it is somehow broken.
drush_find_profiles
system_watchdog Log Drupal watchdog() calls.
_drush_drupal_parse_info_file Parse the info file.
_drush_log_update_sql Log the return value of Drupal hook_update_n functions.

File

includes/drupal.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Utility functions related to Drupal.
  5. */
  6. /**
  7. * Detects the version number of the current Drupal installation,
  8. * if any. Returns FALSE if there is no current Drupal installation,
  9. * or it is somehow broken.
  10. *
  11. * @return
  12. * A string containing the version number of the current
  13. * Drupal installation, if any. Otherwise, return FALSE.
  14. */
  15. function drush_drupal_version($drupal_root = NULL) {
  16. static $version = FALSE;
  17. if (!$version) {
  18. if (($drupal_root != NULL) || ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT'))) {
  19. // D7 stores VERSION in bootstrap.inc. D8 moved that to /core/includes (see below).
  20. $version_constant_paths = array('/modules/system/system.module', '/includes/bootstrap.inc');
  21. foreach ($version_constant_paths as $path) {
  22. if (file_exists($drupal_root . $path)) {
  23. require_once $drupal_root . $path;
  24. }
  25. }
  26. // We just might be dealing with an early Drupal version (pre 4.7)
  27. if (defined('VERSION')) {
  28. $version = VERSION;
  29. }
  30. elseif (file_exists($drupal_root . '/core/vendor/autoload.php')) {
  31. // Recognize D8 so we can give a good error message.
  32. // Load the autoloader so we can access the class constants.
  33. require_once $drupal_root . '/core/vendor/autoload.php';
  34. // Drush depends on bootstrap being loaded at this point;
  35. require_once $drupal_root . '/core/includes/bootstrap.inc';
  36. if (defined('Drupal::VERSION')) {
  37. $version = Drupal::VERSION;
  38. }
  39. }
  40. }
  41. }
  42. return $version;
  43. }
  44. function drush_drupal_cache_clear_all() {
  45. $prior = drush_get_context('DRUSH_AFFIRMATIVE');
  46. drush_set_context('DRUSH_AFFIRMATIVE', TRUE);
  47. drush_invoke('cache-clear', 'all');
  48. drush_set_context('DRUSH_AFFIRMATIVE', $prior);
  49. }
  50. /**
  51. * Returns the Drupal major version number (6, 7, 8 ...)
  52. */
  53. function drush_drupal_major_version($drupal_root = NULL) {
  54. $major_version = FALSE;
  55. if ($version = drush_drupal_version($drupal_root)) {
  56. $version_parts = explode('.', $version);
  57. if (is_numeric($version_parts[0])) {
  58. $major_version = (integer)$version_parts[0];
  59. }
  60. }
  61. return $major_version;
  62. }
  63. /**
  64. * Log Drupal watchdog() calls.
  65. *
  66. * A sneaky implementation of hook_watchdog().
  67. */
  68. function system_watchdog($log_entry) {
  69. // Transform non informative severity levels to 'error' for compatibility with _drush_print_log.
  70. // Other severity levels are coincident with the ones we use in drush.
  71. if (drush_drupal_major_version() >= 6 && $log_entry['severity'] <= 2) {
  72. $severity = 'error';
  73. }
  74. else {
  75. drush_include_engine('drupal', 'environment');
  76. $levels = core_watchdog_severity_levels();
  77. $severity = $levels[$log_entry['severity']];
  78. }
  79. // Format the message.
  80. if (is_array($log_entry['variables'])) {
  81. $message = strtr($log_entry['message'], $log_entry['variables']);
  82. }
  83. else {
  84. $message = $log_entry['message'];
  85. }
  86. // decode_entities() only loaded after FULL bootstrap.
  87. if (function_exists('decode_entities')) {
  88. $message = decode_entities($message);
  89. }
  90. $message = strip_tags($message);
  91. // Log or print or ignore. Just printing saves memory but thats rarely needed.
  92. switch (drush_get_option('watchdog', 'log')) {
  93. case 'log':
  94. drush_log('WD '. $log_entry['type'] . ': ' . $message, $severity);
  95. break;
  96. case 'print':
  97. // Disable in backend mode since it logs output and the goal is to conserve memory.
  98. // @see _drush_bootstrap_drush().
  99. if (ob_get_length() === FALSE) {
  100. drush_print('WD '. $severity . ' ' . $log_entry['type'] . ': ' . $message);
  101. }
  102. break;
  103. default:
  104. // Do nothing.
  105. }
  106. }
  107. /**
  108. * Log the return value of Drupal hook_update_n functions.
  109. *
  110. * This is used during install and update to log the output
  111. * of the update process to the logging system.
  112. */
  113. function _drush_log_update_sql($ret) {
  114. if (count($ret)) {
  115. foreach ($ret as $info) {
  116. if (is_array($info)) {
  117. if (!$info['success']) {
  118. drush_set_error('DRUPAL_UPDATE_FAILED', $info['query']);
  119. }
  120. else {
  121. drush_log($info['query'], ($info['success']) ? 'success' : 'error');
  122. }
  123. }
  124. }
  125. }
  126. }
  127. function drush_find_profiles($drupal_root , $key = 'name') {
  128. return drush_scan_directory($drupal_root . '/profiles', "/.*\.profile$/", array('.', '..', 'CVS', 'tests'), 0, 2, $key);
  129. }
  130. /**
  131. * Log the given user in to a bootstrapped Drupal site.
  132. *
  133. * @param mixed
  134. * Numeric user id or user name.
  135. *
  136. * @return boolean
  137. * TRUE if user was logged in, otherwise FALSE.
  138. */
  139. function drush_drupal_login($drush_user) {
  140. global $user;
  141. if (drush_drupal_major_version() >= 7) {
  142. $user = is_numeric($drush_user) ? user_load($drush_user) : user_load_by_name($drush_user);
  143. }
  144. else {
  145. $user = user_load(is_numeric($drush_user) ? array('uid' => $drush_user) : array('name' => $drush_user));
  146. }
  147. if (empty($user)) {
  148. if (is_numeric($drush_user)) {
  149. $message = dt('Could not login with user ID #!user.', array('!user' => $drush_user));
  150. if ($drush_user === 0) {
  151. $message .= ' ' . dt('This is typically caused by importing a MySQL database dump from a faulty tool which re-numbered the anonymous user ID in the users table. See !link for help recovering from this situation.', array('!link' => 'http://drupal.org/node/1029506'));
  152. }
  153. }
  154. else {
  155. $message = dt('Could not login with user account `!user\'.', array('!user' => $drush_user));
  156. }
  157. return drush_set_error('DRUPAL_USER_LOGIN_FAILED', $message);
  158. }
  159. else {
  160. $name = $user->name ? $user->name : variable_get('anonymous', t('Anonymous'));
  161. drush_log(dt('Successfully logged into Drupal as !name', array('!name' => $name . " (uid=$user->uid)")), 'bootstrap');
  162. }
  163. return TRUE;
  164. }
  165. /**
  166. * Parse Drupal info file format.
  167. *
  168. * Copied with modifications from includes/common.inc.
  169. *
  170. * @see drupal_parse_info_file
  171. */
  172. function drush_drupal_parse_info_file($filename) {
  173. if (!file_exists($filename)) {
  174. return array();
  175. }
  176. $data = file_get_contents($filename);
  177. return _drush_drupal_parse_info_file($data);
  178. }
  179. /**
  180. * Parse the info file.
  181. */
  182. function _drush_drupal_parse_info_file($data, $merge_item = NULL) {
  183. if (!$data) {
  184. return FALSE;
  185. }
  186. if (preg_match_all('
  187. @^\s* # Start at the beginning of a line, ignoring leading whitespace
  188. ((?:
  189. [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
  190. \[[^\[\]]*\] # unless they are balanced and not nested
  191. )+?)
  192. \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
  193. (?:
  194. ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
  195. (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
  196. ([^\r\n]*?) # Non-quoted string
  197. )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
  198. @msx', $data, $matches, PREG_SET_ORDER)) {
  199. $info = array();
  200. foreach ($matches as $match) {
  201. // Fetch the key and value string.
  202. $i = 0;
  203. foreach (array('key', 'value1', 'value2', 'value3') as $var) {
  204. $$var = isset($match[++$i]) ? $match[$i] : '';
  205. }
  206. $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
  207. // Parse array syntax.
  208. $keys = preg_split('/\]?\[/', rtrim($key, ']'));
  209. $last = array_pop($keys);
  210. $parent = &$info;
  211. // Create nested arrays.
  212. foreach ($keys as $key) {
  213. if ($key == '') {
  214. $key = count($parent);
  215. }
  216. if (isset($merge_item) && isset($parent[$key]) && !is_array($parent[$key])) {
  217. $parent[$key] = array($merge_item => $parent[$key]);
  218. }
  219. if (!isset($parent[$key]) || !is_array($parent[$key])) {
  220. $parent[$key] = array();
  221. }
  222. $parent = &$parent[$key];
  223. }
  224. // Handle PHP constants.
  225. if (defined($value)) {
  226. $value = constant($value);
  227. }
  228. // Insert actual value.
  229. if ($last == '') {
  230. $last = count($parent);
  231. }
  232. if (isset($merge_item) && isset($parent[$last]) && is_array($parent[$last])) {
  233. $parent[$last][$merge_item] = $value;
  234. }
  235. else {
  236. $parent[$last] = $value;
  237. }
  238. }
  239. return $info;
  240. }
  241. return FALSE;
  242. }
  243. /**
  244. * Build a cache id to store the install_profile for a given site.
  245. */
  246. function drush_cid_install_profile() {
  247. return drush_get_cid('install_profile', array(), array(drush_get_context('DRUSH_SELECTED_DRUPAL_SITE_CONF_PATH')));
  248. }