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 ascending Description
_drush_log_update_sql Log the return value of Drupal hook_update_n functions.
_drush_drupal_parse_info_file Parse the info file.
system_watchdog Log Drupal watchdog() calls.
drush_find_profiles
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_drupal_parse_info_file Parse Drupal info file format.
drush_drupal_major_version Returns the Drupal major version number (6, 7, 8 ...)
drush_drupal_load_autoloader Loads the Drupal autoloader and returns the instance.
drush_drupal_cache_clear_all
drush_cid_install_profile Build a cache id to store the install_profile for a given site.
drupal_sanitize_options

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. // Try and find D8.
  36. if (file_exists($drupal_root . '/autoload.php')) {
  37. // Load the autoloader so we can access the class constants.
  38. drush_drupal_load_autoloader($drupal_root);
  39. // Drush depends on bootstrap being loaded at this point.
  40. require_once $drupal_root .'/core/includes/bootstrap.inc';
  41. if (defined('Drupal::VERSION')) {
  42. $version = Drupal::VERSION;
  43. }
  44. }
  45. else {
  46. // D7 stores VERSION in bootstrap.inc.
  47. // D6 and below does it in system.module.
  48. $version_constant_paths = array('/includes/bootstrap.inc', '/modules/system/system.module');
  49. foreach ($version_constant_paths as $path) {
  50. if (file_exists($drupal_root . $path)) {
  51. require_once $drupal_root . $path;
  52. if (defined('VERSION')) {
  53. $version = VERSION;
  54. break;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. return $version;
  62. }
  63. function drush_drupal_cache_clear_all() {
  64. if (drush_drupal_major_version() >= 8) {
  65. drush_invoke_process('@self', 'cache-rebuild');
  66. }
  67. else {
  68. drush_invoke_process('@self', 'cache-clear', array('all'));
  69. }
  70. }
  71. /**
  72. * Returns the Drupal major version number (6, 7, 8 ...)
  73. */
  74. function drush_drupal_major_version($drupal_root = NULL) {
  75. $major_version = FALSE;
  76. if ($version = drush_drupal_version($drupal_root)) {
  77. $version_parts = explode('.', $version);
  78. if (is_numeric($version_parts[0])) {
  79. $major_version = (integer)$version_parts[0];
  80. }
  81. }
  82. return $major_version;
  83. }
  84. /**
  85. * Log Drupal watchdog() calls.
  86. *
  87. * A sneaky implementation of hook_watchdog(), for D6/D7.
  88. */
  89. function system_watchdog($log_entry) {
  90. // Transform non informative severity levels to 'error' for compatibility with _drush_print_log.
  91. // Other severity levels are coincident with the ones we use in drush.
  92. if (drush_drupal_major_version() >= 6 && $log_entry['severity'] <= 2) {
  93. $severity = 'error';
  94. }
  95. else {
  96. drush_include_engine('drupal', 'environment');
  97. $levels = drush_watchdog_severity_levels();
  98. $severity = $levels[$log_entry['severity']];
  99. }
  100. // Format the message.
  101. if (is_array($log_entry['variables'])) {
  102. $message = strtr($log_entry['message'], $log_entry['variables']);
  103. }
  104. else {
  105. $message = $log_entry['message'];
  106. }
  107. // decode_entities() only loaded after FULL bootstrap.
  108. if (function_exists('decode_entities')) {
  109. $message = decode_entities($message);
  110. }
  111. $message = strip_tags($message);
  112. // Log or print or ignore. Just printing saves memory but thats rarely needed.
  113. switch (drush_get_option('watchdog', 'log')) {
  114. case 'log':
  115. drush_log('WD '. $log_entry['type'] . ': ' . $message, $severity);
  116. break;
  117. case 'print':
  118. // Disable in backend mode since it logs output and the goal is to conserve memory.
  119. // @see _drush_bootstrap_drush().
  120. if (ob_get_length() === FALSE) {
  121. drush_print('WD '. $severity . ' ' . $log_entry['type'] . ': ' . $message);
  122. }
  123. break;
  124. default:
  125. // Do nothing.
  126. }
  127. }
  128. /**
  129. * Log the return value of Drupal hook_update_n functions.
  130. *
  131. * This is used during install and update to log the output
  132. * of the update process to the logging system.
  133. */
  134. function _drush_log_update_sql($ret) {
  135. if (count($ret)) {
  136. foreach ($ret as $info) {
  137. if (is_array($info)) {
  138. if (!$info['success']) {
  139. drush_set_error('DRUPAL_UPDATE_FAILED', $info['query']);
  140. }
  141. else {
  142. drush_log($info['query'], ($info['success']) ? LogLevel::SUCCESS : LogLevel::ERROR);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. function drush_find_profiles($drupal_root , $key = 'name') {
  149. return drush_scan_directory($drupal_root . '/profiles', "/.*\.profile$/", array('.', '..', 'CVS', 'tests'), 0, 2, $key);
  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, $merge_item = NULL) {
  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($merge_item) && isset($parent[$key]) && !is_array($parent[$key])) {
  203. $parent[$key] = array($merge_item => $parent[$key]);
  204. }
  205. if (!isset($parent[$key]) || !is_array($parent[$key])) {
  206. $parent[$key] = array();
  207. }
  208. $parent = &$parent[$key];
  209. }
  210. // Handle PHP constants.
  211. if (defined($value)) {
  212. $value = constant($value);
  213. }
  214. // Insert actual value.
  215. if ($last == '') {
  216. $last = count($parent);
  217. }
  218. if (isset($merge_item) && isset($parent[$last]) && is_array($parent[$last])) {
  219. $parent[$last][$merge_item] = $value;
  220. }
  221. else {
  222. $parent[$last] = $value;
  223. }
  224. }
  225. return $info;
  226. }
  227. return FALSE;
  228. }
  229. /**
  230. * Build a cache id to store the install_profile for a given site.
  231. */
  232. function drush_cid_install_profile() {
  233. return drush_get_cid('install_profile', array(), array(drush_get_context('DRUSH_SELECTED_DRUPAL_SITE_CONF_PATH')));
  234. }
  235. /*
  236. * An array of options shared by sql-sanitize and sql-sync commands.
  237. */
  238. function drupal_sanitize_options() {
  239. return array(
  240. 'sanitize-password' => array(
  241. 'description' => 'The password to assign to all accounts in the sanitization operation, or "no" to keep passwords unchanged.',
  242. 'example-value' => 'password',
  243. 'value' => 'required',
  244. ),
  245. 'sanitize-email' => array(
  246. '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.',
  247. 'example-value' => 'user+%uid@localhost',
  248. 'value' => 'required',
  249. ),
  250. );
  251. }