drush.php

  1. 8.0.x drush.php
  2. 6.x drush.php
  3. 7.x drush.php
  4. 3.x drush.php
  5. 4.x drush.php
  6. 5.x drush.php
  7. master lib/Drush.php
  8. master drush.php

drush is a PHP script implementing a command line shell for Drupal.

@requires PHP CLI 5.2.0, or newer.

Functions

Namesort descending Description
drush_drupal_login Log the given user in to a bootstrapped Drupal site.
drush_main The main Drush function.
drush_return_status
drush_shutdown Shutdown function for use while Drupal is bootstrapping and to return any registered errors.
drush_verify_cli Verify that we are running PHP through the command line interface.

Constants

File

drush.php
View source
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * @file
  5. * drush is a PHP script implementing a command line shell for Drupal.
  6. *
  7. * @requires PHP CLI 5.2.0, or newer.
  8. */
  9. // Terminate immediately unless invoked as a command line script
  10. if (!drush_verify_cli()) {
  11. die('drush.php is designed to run via the command line.');
  12. }
  13. // Check supported version of PHP.
  14. define('DRUSH_MINIMUM_PHP', '5.2.0');
  15. if (version_compare(phpversion(), DRUSH_MINIMUM_PHP) < 0) {
  16. die('Your command line PHP installation is too old. Drush requires at least PHP ' . DRUSH_MINIMUM_PHP . "\n");
  17. }
  18. define('DRUSH_BASE_PATH', dirname(__FILE__));
  19. define('DRUSH_REQUEST_TIME', microtime(TRUE));
  20. require_once DRUSH_BASE_PATH . '/includes/environment.inc';
  21. require_once DRUSH_BASE_PATH . '/includes/command.inc';
  22. require_once DRUSH_BASE_PATH . '/includes/drush.inc';
  23. require_once DRUSH_BASE_PATH . '/includes/backend.inc';
  24. require_once DRUSH_BASE_PATH . '/includes/batch.inc';
  25. require_once DRUSH_BASE_PATH . '/includes/context.inc';
  26. require_once DRUSH_BASE_PATH . '/includes/sitealias.inc';
  27. drush_set_context('argc', $GLOBALS['argc']);
  28. drush_set_context('argv', $GLOBALS['argv']);
  29. set_error_handler('drush_error_handler');
  30. exit(drush_main());
  31. /**
  32. * Verify that we are running PHP through the command line interface.
  33. *
  34. * This function is useful for making sure that code cannot be run via the web server,
  35. * such as a function that needs to write files to which the web server should not have
  36. * access to.
  37. *
  38. * @return
  39. * A boolean value that is true when PHP is being run through the command line,
  40. * and false if being run through cgi or mod_php.
  41. */
  42. function drush_verify_cli() {
  43. return (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0));
  44. }
  45. /**
  46. * The main Drush function.
  47. *
  48. * - Parses the command line arguments, configuration files and environment.
  49. * - Prepares and executes a Drupal bootstrap, if possible,
  50. * - Dispatches the given command.
  51. *
  52. * @return
  53. * Whatever the given command returns.
  54. */
  55. function drush_main() {
  56. $phases = _drush_bootstrap_phases();
  57. $completed_phases = array();
  58. $return = '';
  59. $command_found = FALSE;
  60. foreach ($phases as $phase) {
  61. if (drush_bootstrap($phase)) {
  62. $completed_phases[$phase] = TRUE;
  63. $command = drush_parse_command();
  64. // Process a remote command if 'remote-host' option is set.
  65. if (drush_remote_command()) {
  66. $command_found = TRUE;
  67. break;
  68. }
  69. if (is_array($command)) {
  70. if (array_key_exists($command['bootstrap'], $completed_phases) && empty($command['bootstrap_errors'])) {
  71. drush_log(dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])), 'bootstrap');
  72. $command_found = TRUE;
  73. // Dispatch the command(s).
  74. $return = drush_dispatch($command);
  75. if (drush_get_context('DRUSH_DEBUG')) {
  76. drush_print_timers();
  77. }
  78. drush_log(dt('Peak memory usage was !peak', array('!peak' => drush_format_size(memory_get_peak_usage()))), 'memory');
  79. break;
  80. }
  81. }
  82. }
  83. else {
  84. break;
  85. }
  86. }
  87. if (!$command_found) {
  88. // If we reach this point, we have not found either a valid or matching command.
  89. $args = implode(' ', drush_get_arguments());
  90. if (isset($command) && is_array($command)) {
  91. foreach ($command['bootstrap_errors'] as $key => $error) {
  92. drush_set_error($key, $error);
  93. }
  94. drush_set_error('DRUSH_COMMAND_NOT_EXECUTABLE', dt("The drush command '!args' could not be executed.", array('!args' => $args)));
  95. }
  96. elseif (!empty($args)) {
  97. drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt("The drush command '!args' could not be found.", array('!args' => $args)));
  98. }
  99. else {
  100. // This can occur if we get an error during _drush_bootstrap_drush_validate();
  101. drush_set_error('DRUSH_COULD_NOT_EXECUTE', dt("Drush could not execute."));
  102. }
  103. }
  104. // We set this context to let the shutdown function know we reached the end of drush_main();
  105. drush_set_context("DRUSH_EXECUTION_COMPLETED", TRUE);
  106. // After this point the drush_shutdown function will run,
  107. // exiting with the correct exit code.
  108. return $return;
  109. }
  110. /**
  111. * Shutdown function for use while Drupal is bootstrapping and to return any
  112. * registered errors.
  113. *
  114. * The shutdown command checks whether certain options are set to reliably
  115. * detect and log some common Drupal initialization errors.
  116. *
  117. * If the command is being executed with the --backend option, the script
  118. * will return a json string containing the options and log information
  119. * used by the script.
  120. *
  121. * The command will exit with '1' if it was successfully executed, and the
  122. * result of drush_get_error() if it wasn't.
  123. */
  124. function drush_shutdown() {
  125. // Mysteriously make $user available during sess_write(). Avoids a NOTICE.
  126. global $user;
  127. if (!drush_get_context('DRUSH_EXECUTION_COMPLETED', FALSE)) {
  128. // We did not reach the end of the drush_main function,
  129. // this generally means somewhere in the code a call to exit(),
  130. // was made. We catch this, so that we can trigger an error in
  131. // those cases.
  132. drush_set_error("DRUSH_NOT_COMPLETED", dt("Drush command could not be completed."));
  133. }
  134. $phase = drush_get_context('DRUSH_BOOTSTRAP_PHASE');
  135. if (drush_get_context('DRUSH_BOOTSTRAPPING')) {
  136. switch ($phase) {
  137. case DRUSH_BOOTSTRAP_DRUPAL_FULL :
  138. ob_end_clean();
  139. _drush_log_drupal_messages();
  140. drush_set_error('DRUSH_DRUPAL_BOOTSTRAP_ERROR');
  141. break;
  142. }
  143. }
  144. if (drush_get_context('DRUSH_BACKEND')) {
  145. drush_backend_output();
  146. }
  147. elseif (drush_get_context('DRUSH_QUIET')) {
  148. ob_end_clean();
  149. }
  150. // If we are in pipe mode, emit the compact representation of the command, if available.
  151. if (drush_get_context('DRUSH_PIPE')) {
  152. drush_pipe_output();
  153. }
  154. // this way drush_return_status will always be the last shutdown function (unless other shutdown functions register shutdown functions...)
  155. // and won't prevent other registered shutdown functions (IE from numerous cron methods) from running by calling exit() before they get a chance.
  156. register_shutdown_function('drush_return_status');
  157. }
  158. function drush_return_status() {
  159. exit((drush_get_error()) ? DRUSH_FRAMEWORK_ERROR : DRUSH_SUCCESS);
  160. }
  161. /**
  162. * Log the given user in to a bootstrapped Drupal site.
  163. *
  164. * @param mixed
  165. * Numeric user id or user name.
  166. *
  167. * @return boolean
  168. * TRUE if user was logged in, otherwise FALSE.
  169. */
  170. function drush_drupal_login($drush_user) {
  171. global $user;
  172. if (drush_drupal_major_version() >= 7) {
  173. $user = is_numeric($drush_user) ? user_load($drush_user) : user_load_by_name($drush_user);
  174. }
  175. else {
  176. $user = user_load(is_numeric($drush_user) ? array('uid' => $drush_user) : array('name' => $drush_user));
  177. }
  178. if (empty($user)) {
  179. if (is_numeric($drush_user)) {
  180. $message = dt('Could not login with user ID #%user.', array('%user' => $drush_user));
  181. }
  182. else {
  183. $message = dt('Could not login with user account `%user\'.', array('%user' => $drush_user));
  184. }
  185. return drush_set_error('DRUPAL_USER_LOGIN_FAILED', $message);
  186. }
  187. return TRUE;
  188. }