drush.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 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 an error handler and a shutdown function
  30. set_error_handler('drush_error_handler');
  31. register_shutdown_function('drush_shutdown');
  32. exit(drush_main());
  33. /**
  34. * Verify that we are running PHP through the command line interface.
  35. *
  36. * This function is useful for making sure that code cannot be run via the web server,
  37. * such as a function that needs to write files to which the web server should not have
  38. * access to.
  39. *
  40. * @return
  41. * A boolean value that is true when PHP is being run through the command line,
  42. * and false if being run through cgi or mod_php.
  43. */
  44. function drush_verify_cli() {
  45. return (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0));
  46. }
  47. /**
  48. * The main Drush function.
  49. *
  50. * - Parses the command line arguments, configuration files and environment.
  51. * - Prepares and executes a Drupal bootstrap, if possible,
  52. * - Dispatches the given command.
  53. *
  54. * @return
  55. * Whatever the given command returns.
  56. */
  57. function drush_main() {
  58. $phases = _drush_bootstrap_phases(FALSE, TRUE);
  59. drush_set_context('DRUSH_BOOTSTRAP_PHASE', DRUSH_BOOTSTRAP_NONE);
  60. // We need some global options processed at this early stage. Namely --debug.
  61. drush_parse_args();
  62. _drush_bootstrap_global_options();
  63. $return = '';
  64. $command_found = FALSE;
  65. foreach ($phases as $phase) {
  66. if (drush_bootstrap_to_phase($phase)) {
  67. $command = drush_parse_command();
  68. // Process a remote command if 'remote-host' option is set.
  69. if (drush_remote_command()) {
  70. $command_found = TRUE;
  71. break;
  72. }
  73. if (is_array($command)) {
  74. $bootstrap_result = drush_bootstrap_to_phase($command['bootstrap']);
  75. drush_enforce_requirement_bootstrap_phase($command);
  76. drush_enforce_requirement_core($command);
  77. drush_enforce_requirement_drupal_dependencies($command);
  78. drush_enforce_requirement_drush_dependencies($command);
  79. if ($bootstrap_result && empty($command['bootstrap_errors'])) {
  80. drush_log(dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])), 'bootstrap');
  81. $command_found = TRUE;
  82. // Dispatch the command(s).
  83. $return = drush_dispatch($command);
  84. // prevent a '1' at the end of the output
  85. if ($return === TRUE) {
  86. $return = '';
  87. }
  88. if (drush_get_context('DRUSH_DEBUG') && !drush_get_context('DRUSH_QUIET')) {
  89. drush_print_timers();
  90. }
  91. drush_log(dt('Peak memory usage was !peak', array('!peak' => drush_format_size(memory_get_peak_usage()))), 'memory');
  92. break;
  93. }
  94. }
  95. }
  96. else {
  97. break;
  98. }
  99. }
  100. if (!$command_found) {
  101. // If we reach this point, we have not found either a valid or matching command.
  102. $args = implode(' ', drush_get_arguments());
  103. if (isset($command) && is_array($command)) {
  104. foreach ($command['bootstrap_errors'] as $key => $error) {
  105. drush_set_error($key, $error);
  106. }
  107. drush_set_error('DRUSH_COMMAND_NOT_EXECUTABLE', dt("The drush command '!args' could not be executed.", array('!args' => $args)));
  108. }
  109. elseif (!empty($args)) {
  110. drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt("The drush command '!args' could not be found.", array('!args' => $args)));
  111. }
  112. // Set errors that ocurred in the bootstrap phases.
  113. $errors = drush_get_context('DRUSH_BOOTSTRAP_ERRORS', array());
  114. foreach ($errors as $code => $message) {
  115. drush_set_error($code, $message);
  116. }
  117. }
  118. // We set this context to let the shutdown function know we reached the end of drush_main();
  119. drush_set_context("DRUSH_EXECUTION_COMPLETED", TRUE);
  120. // After this point the drush_shutdown function will run,
  121. // exiting with the correct exit code.
  122. return $return;
  123. }
  124. /**
  125. * Shutdown function for use while Drupal is bootstrapping and to return any
  126. * registered errors.
  127. *
  128. * The shutdown command checks whether certain options are set to reliably
  129. * detect and log some common Drupal initialization errors.
  130. *
  131. * If the command is being executed with the --backend option, the script
  132. * will return a json string containing the options and log information
  133. * used by the script.
  134. *
  135. * The command will exit with '1' if it was successfully executed, and the
  136. * result of drush_get_error() if it wasn't.
  137. */
  138. function drush_shutdown() {
  139. // Mysteriously make $user available during sess_write(). Avoids a NOTICE.
  140. global $user;
  141. if (!drush_get_context('DRUSH_EXECUTION_COMPLETED', FALSE) && !drush_get_context('DRUSH_USER_ABORT', FALSE)) {
  142. $php_error_message = '';
  143. if ($error = error_get_last()) {
  144. $php_error_message = "\n" . dt('Error: !message in !file, line !line', array('!message' => $error['message'], '!file' => $error['file'], '!line' => $error['line']));
  145. }
  146. // We did not reach the end of the drush_main function,
  147. // this generally means somewhere in the code a call to exit(),
  148. // was made. We catch this, so that we can trigger an error in
  149. // those cases.
  150. drush_set_error("DRUSH_NOT_COMPLETED", dt("Drush command terminated abnormally due to an unrecoverable error.!message", array('!message' => $php_error_message)));
  151. // Attempt to give the user some advice about how to fix the problem
  152. _drush_postmortem();
  153. }
  154. $phase = drush_get_context('DRUSH_BOOTSTRAP_PHASE');
  155. if (drush_get_context('DRUSH_BOOTSTRAPPING')) {
  156. switch ($phase) {
  157. case DRUSH_BOOTSTRAP_DRUPAL_FULL :
  158. ob_end_clean();
  159. _drush_log_drupal_messages();
  160. drush_set_error('DRUSH_DRUPAL_BOOTSTRAP_ERROR');
  161. break;
  162. }
  163. }
  164. if (drush_get_context('DRUSH_BACKEND')) {
  165. drush_backend_output();
  166. }
  167. elseif (drush_get_context('DRUSH_QUIET')) {
  168. ob_end_clean();
  169. }
  170. // If we are in pipe mode, emit the compact representation of the command, if available.
  171. if (drush_get_context('DRUSH_PIPE')) {
  172. drush_pipe_output();
  173. }
  174. /**
  175. * For now, drush skips end of page processing on D7. Doing so could write
  176. * cache entries to module_implements and lookup_cache that don't match web requests.
  177. */
  178. // if (drush_drupal_major_version() >= 7 && function_exists('drupal_page_footer')) {
  179. // drupal_page_footer();
  180. // }
  181. // this way drush_return_status will always be the last shutdown function (unless other shutdown functions register shutdown functions...)
  182. // and won't prevent other registered shutdown functions (IE from numerous cron methods) from running by calling exit() before they get a chance.
  183. register_shutdown_function('drush_return_status');
  184. }
  185. function drush_return_status() {
  186. exit((drush_get_error()) ? DRUSH_FRAMEWORK_ERROR : DRUSH_SUCCESS);
  187. }
  188. /**
  189. * Log the given user in to a bootstrapped Drupal site.
  190. *
  191. * @param mixed
  192. * Numeric user id or user name.
  193. *
  194. * @return boolean
  195. * TRUE if user was logged in, otherwise FALSE.
  196. */
  197. function drush_drupal_login($drush_user) {
  198. global $user;
  199. if (drush_drupal_major_version() >= 7) {
  200. $user = is_numeric($drush_user) ? user_load($drush_user) : user_load_by_name($drush_user);
  201. }
  202. else {
  203. $user = user_load(is_numeric($drush_user) ? array('uid' => $drush_user) : array('name' => $drush_user));
  204. }
  205. if (empty($user)) {
  206. if (is_numeric($drush_user)) {
  207. $message = dt('Could not login with user ID #!user.', array('!user' => $drush_user));
  208. if ($drush_user === 0) {
  209. $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'));
  210. }
  211. }
  212. else {
  213. $message = dt('Could not login with user account `!user\'.', array('!user' => $drush_user));
  214. }
  215. return drush_set_error('DRUPAL_USER_LOGIN_FAILED', $message);
  216. }
  217. else {
  218. $name = $user->name ? $user->name : variable_get('anonymous', t('Anonymous'));
  219. drush_log(dt('Successfully logged into Drupal as !name', array('!name' => $name . " (uid=$user->uid)")), 'bootstrap');
  220. }
  221. return TRUE;
  222. }