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