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

Functions

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