? .drush.php.swp ? .svn ? drush_backend.diff ? drushrc.php ? nonbe.deff ? z ? includes/.backend.inc.swp ? includes/.context.inc.swp ? includes/.drush.inc.swp ? includes/.environment.inc.swp Index: drush.php =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/drush.php,v retrieving revision 1.49 diff -u -r1.49 drush.php --- drush.php 20 Mar 2009 16:10:54 -0000 1.49 +++ drush.php 24 Mar 2009 03:08:35 -0000 @@ -20,7 +20,9 @@ require_once dirname(__FILE__) . '/includes/backend.inc'; require_once dirname(__FILE__) . '/includes/context.inc'; -exit(drush_bootstrap($GLOBALS['argc'], $GLOBALS['argv'])); +drush_set_context('argc', $GLOBALS['argc']); +drush_set_context('argv', $GLOBALS['argv']); +exit(drush_main()); /** * Verify that we are running PHP through the command line interface. @@ -51,112 +53,24 @@ * @return * Whatever the given command returns. */ -function drush_bootstrap($argc, $argv) { - // The bootstrap can fail silently, so we catch that in a shutdown function. - register_shutdown_function('drush_shutdown'); - - // Parse command line options and arguments. - // This will poplate the 'arguments', 'cli' and 'stdin' contexts. - drush_parse_args($argv, array('c', 'h', 'u', 'r', 'l', 'i')); - - $path = drush_get_option(array('r', 'root'), drush_cwd()); - $drupal_root = drush_locate_root($path); - - // Load available .drushrc file(s). - // Allows you to provide defaults for options and variables. - drush_load_config(); - define('DRUSH_BACKEND', drush_get_option(array('b', 'backend'), FALSE)); - define('DRUSH_QUIET', drush_get_option(array('q', 'quiet'), FALSE)); - // When running in backend mode, all output is buffered, and returned - // as a property of a JSON encoded associative array. - if (DRUSH_BACKEND || DRUSH_QUIET) { - ob_start(); - } - // Quickly attempt to find the command. - // A second attempt is performed in drush_dispatch(). - drush_parse_command(); - $command = drush_get_command(); - // Define basic options as constants. - define('DRUSH_VERBOSE', drush_get_option(array('v', 'verbose'), FALSE)); - define('DRUSH_AFFIRMATIVE', drush_get_option(array('y', 'yes'), FALSE)); - define('DRUSH_SIMULATE', drush_get_option(array('s', 'simulate'), FALSE)); - - define('DRUSH_URI', drush_get_option(array('l', 'uri'), drush_site_uri($drupal_root))); - define('DRUSH_USER', drush_get_option(array('u', 'user'), 0)); - - if ($drupal_root) { - - drush_drupal_set_environment($drupal_root); - - // Bootstrap Drupal. - if (drush_drupal_bootstrap($drupal_root, $command['bootstrap'])) { - /** - * Allow the drushrc.php file to override $conf settings. - * This is a separate variable because the $conf array gets - * initialized to an empty array, in the drupal bootstrap process, - * and changes in settings.php would wipe out the drushrc.php settings. - */ - if (is_array($override)) { - $conf = array_merge($conf, $override); - } +function drush_main() { + $phases = _drush_bootstrap_phases(); - // We have changed bootstrap level, so re-detect command files. - drush_commandfile_cache_flush(); + foreach ($phases as $phase) { + drush_bootstrap($phase); - // Login the specified user (if given). - if (DRUSH_USER) { - drush_drupal_login(DRUSH_USER); + $command = drush_parse_command(); + if (is_array($command)) { + if ($command['bootstrap'] == $phase) { + // Dispatch the command(s). + // After this point the drush_shutdown function will run, + // exiting with the correct exit code. + return drush_dispatch($command); } } } - - if (DRUSH_SIMULATE) { - drush_print('SIMULATION MODE IS ENABLED. NO ACTUAL ACTION WILL BE TAKEN. SYSTEM WILL REMAIN UNCHANGED.'); - } - - // Dispatch the command(s). - $output = drush_dispatch(); - - // prevent a '1' at the end of the outputs - if ($output === TRUE) { - $output = ''; - } - - // After this point the drush_shutdown function will run, - // exiting with the correct exit code. - return $output; -} - -/** - * Sets up various constants and $_SERVER entries used by - * Drupal, essentially mimicking a webserver environment. - * - * @param string - * Path to Drupal installation root. - */ -function drush_drupal_set_environment($drupal_root) { - define('DRUSH_DRUPAL_ROOT', $drupal_root); - define('DRUSH_DRUPAL_VERSION', drush_drupal_version()); - define('DRUSH_DRUPAL_MAJOR_VERSION', drush_drupal_major_version()); - - // Possibly temporary. See http://drupal.org/node/312421. - define('DRUPAL_ROOT', DRUSH_DRUPAL_ROOT); - - // Fake the necessary HTTP headers that Drupal needs: - if (DRUSH_URI) { - $drupal_base_url = parse_url(DRUSH_URI); - $_SERVER['HTTP_HOST'] = $drupal_base_url['host']; - $_SERVER['PHP_SELF'] = $drupal_base_url['path'] . '/index.php'; - } - else { - $_SERVER['HTTP_HOST'] = 'default'; - $_SERVER['PHP_SELF'] = '/index.php'; - } - $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF']; - $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; - $_SERVER['REQUEST_METHOD'] = NULL; - $_SERVER['SERVER_SOFTWARE'] = NULL; - $_SERVER['HTTP_USER_AGENT'] = NULL; + // If we reach this point, we have not found a valid command. + drush_set_error('DRUSH_COMMAND_NOT_FOUND'); } /** @@ -174,19 +88,23 @@ * result of drush_get_error() if it wasn't. */ function drush_shutdown() { - $handle_bootstrap = drush_get_option('handle_bootstrap', FALSE, 'process'); - if ($handle_bootstrap && DRUSH_URI) { - if (!drush_get_option('handle_bootstrap_database', FALSE, 'process')) { - ob_end_clean(); - drush_set_error(DRUSH_DRUPAL_DB_ERROR); + $phase = drush_get_context('DRUSH_BOOTSTRAP_PHASE'); + if (drush_get_context('DRUSH_BOOTSTRAPPING')) { + switch ($phase) { + case DRUSH_BOOTSTRAP_DRUPAL_DATABASE : + ob_end_clean(); + drush_set_error(DRUSH_DRUPAL_DB_ERROR); + break; + case DRUSH_BOOTSTRAP_DRUPAL_FULL : + ob_end_clean(); + drush_set_error(DRUSH_DRUPAL_BOOTSTRAP_ERROR); + break; } - elseif (!drush_get_option('handle_bootstrap_database', FALSE, 'process')) { - ob_end_clean(); - drush_set_error(DRUSH_DRUPAL_BOOTSTRAP_ERROR); - } - _drush_log_drupal_messages(); } - if (DRUSH_BACKEND) { + + + + if (drush_get_context('DRUSH_BACKEND')) { drush_backend_output(); } @@ -196,68 +114,6 @@ } /** - * Bootstrap Drupal. - * - * @param string - * path to Drupal installation root. - * - * @param mixed - * NULL for a full bootstrap or any of the Drupal bootstrap sequence constants. - * These depend on the Drupal major version. - * - * @return - * TRUE if Drupal successfully bootstrapped to the given state. - */ -function drush_drupal_bootstrap($drupal_root, $bootstrap = NULL) { - // Change to Drupal root dir. - chdir($drupal_root); - - if ($bootstrap != -1) { - require_once DRUSH_DRUPAL_BOOTSTRAP; - - if (($conf_path = conf_path()) && !file_exists("./$conf_path/settings.php")) { - return FALSE; - } - - // Enable error reporting if the bootstrap fails silently. - drush_set_option('handle_bootstrap', TRUE); - - if (is_null($bootstrap)) { - drush_drupal_bootstrap_db(); - drush_drupal_bootstrap_full(); - - // Set this constant when we are fully bootstrapped. - define('DRUSH_DRUPAL_BOOTSTRAPPED', TRUE); - } - - // If we are still running here then we don't need to worry about handling bootstrap failures. - drush_set_option('handle_bootstrap', FALSE); - } - - return TRUE; -} - -/** - * Bootstrap the Drupal database. - */ -function drush_drupal_bootstrap_db() { - ob_start(); - drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE); - ob_end_clean(); - drush_set_option('handle_bootstrap_database', TRUE); -} - -/** - * Fully bootstrap Drupal. - */ -function drush_drupal_bootstrap_full() { - ob_start(); - drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); - ob_end_clean(); - drush_set_option('handle_bootstrap_full', TRUE); -} - -/** * Log the given user in to a bootstrapped Drupal site. * * @param mixed @@ -272,13 +128,14 @@ if (empty($user)) { if (is_numeric($drush_user)) { - drush_die(dt('Could not login with user ID #%user.', array('%user' => $drush_user))); + $message = dt('Could not login with user ID #%user.', array('%user' => $drush_user)); } else { - drush_die(dt('Could not login with user account `%user\'.', array('%user' => $drush_user))); + $message = dt('Could not login with user account `%user\'.', array('%user' => $drush_user)); } - return FALSE; + return drush_set_error(DRUPAL_USER_LOGIN_FAILED, $message); } return TRUE; } + Index: includes/backend.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/backend.inc,v retrieving revision 1.2 diff -u -r1.2 backend.inc --- includes/backend.inc 20 Mar 2009 16:10:54 -0000 1.2 +++ includes/backend.inc 24 Mar 2009 03:08:36 -0000 @@ -72,7 +72,7 @@ // Return the options that were set at the end of the process. $data['context'] = drush_get_merged_options(); - if (!DRUSH_QUIET) { + if (!drush_get_context('DRUSH_QUIET')) { printf(DRUSH_BACKEND_OUTPUT_DELIMITER, json_encode($data)); } } @@ -167,7 +167,7 @@ $process = proc_open($cmd, $descriptorspec, $pipes, null, null, array('context' => $context)); if (is_resource($process)) { if ($data) { - fwrite($pipes[0], json_encode($data, TRUE)); // pass the data array in a JSON encoded string + fwrite($pipes[0], json_encode($data)); // pass the data array in a JSON encoded string } $info = stream_get_meta_data($pipes[1]); @@ -291,8 +291,10 @@ function _drush_backend_generate_command($command, &$data, $method = 'GET', $drush_path = null, $hostname = null, $username = null) { $drush_path = !is_null($drush_path) ? $drush_path : $_SERVER['argv'][0]; // Call own drush.php file. - $data['root'] = ($data['root']) ? $data['root'] : drush_get_option(array('r', 'root'), drush_cwd()); - $data['uri'] = ($data['uri']) ? $data['uri'] : DRUSH_URI; + $data['root'] = ($data['root']) ? $data['root'] : drush_get_context('DRUSH_DRUPAL_ROOT'); + if (!is_null($data['uri'])) { + $data['uri'] = ($data['uri']) ? $data['uri'] : drush_get_context('DRUSH_URI'); + } $option_str = _drush_backend_argument_string($data, $method); $args = explode(" ", $command); Index: includes/command.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/command.inc,v retrieving revision 1.17 diff -u -r1.17 command.inc --- includes/command.inc 20 Mar 2009 16:10:54 -0000 1.17 +++ includes/command.inc 24 Mar 2009 03:08:37 -0000 @@ -15,6 +15,65 @@ */ /** + * Parse console arguments. + */ +function drush_parse_args() { + $args = drush_get_context('argv'); + + static $default_options = array('c', 'h', 'u', 'r', 'l', 'i'); + + $arguments = array(); + + for ($i = 1; $i < count($args); $i++) { + $opt = $args[$i]; + // Is the arg an option (starting with '-')? + if ($opt{0} == "-" && strlen($opt) != 1) { + // Do we have multiple options behind one '-'? + if (strlen($opt) > 2 && $opt{1} != "-") { + // Each char becomes a key of its own. + for ($j = 1; $j < strlen($opt); $j++) { + $options[substr($opt, $j, 1)] = true; + } + } + // Do we have a longopt (starting with '--')? + elseif ($opt{1} == "-") { + if ($pos = strpos($opt, '=')) { + $options[substr($opt, 2, $pos - 2)] = substr($opt, $pos + 1); + } + else { + $options[substr($opt, 2)] = true; + } + } + else { + $opt = substr($opt, 1); + // Check if the current opt is in $arg_opts (= has to be followed by an argument). + if ((in_array($opt, $arg_opts))) { + if (($args[$i+1] == NULL) || ($args[$i+1] == "") || ($args[$i + 1]{0} == "-")) { + exit("Invalid input: -$opt needs to be followed by an argument."); + } + $options[$opt] = $args[$i + 1]; + $i++; + } + else { + $options[$opt] = true; + } + } + } + // If it's not an option, it's a command. + else { + $arguments[] = $opt; + } + } + + // If arguments are specified, print the help screen. + $arguments = sizeof($arguments) ? $arguments : array('help'); + + drush_set_arguments($arguments); + drush_set_context('options', $options); +} + + +/** * Get a list of all implemented commands. * This invokes hook_drush_command(). * @@ -27,14 +86,17 @@ */ function drush_get_commands() { $commands = drush_command_invoke_all('drush_command'); - + + // Filter out commands which are invalid for this bootstrap phase. + $commands = drush_enforce_requirement_bootstrap_phase($commands); + // Filter out commands which are invalid for the current version of core Drupal. $commands = drush_enforce_requirement_core($commands); // Filter out commands which are missing their dependant Drupal modules. $commands = drush_enforce_requirement_drupal_dependencies($commands); - - return $commands; + + return drush_set_context('DRUSH_COMMANDS', $commands); } /** @@ -110,10 +172,10 @@ if (isset($command['callback arguments']) && is_array($command['callback arguments'])) { $arguments = array_merge($command['callback arguments'], $arguments); } - + $command['arguments'] = $arguments; drush_set_command($command); - drush_set_arguments($arguments); } + return $command; } /** @@ -174,7 +236,6 @@ */ function drush_is_command($command) { $commands = drush_get_commands(); - // print_r($commands); return isset($commands[$command]); } @@ -197,34 +258,41 @@ * command files. */ function drush_commandfile_list() { - $cache = _drush_commandfile_cache(); + return drush_get_context('DRUSH_COMMAND_FILES', array()); +} - $list = NULL; +function _drush_find_commandfiles($phase) { + $cache =& drush_get_context('DRUSH_COMMAND_FILES', array()); - if (!$cache) { - $searchpath = array(); + $searchpath = array(); + switch ($phase) { + case DRUSH_BOOTSTRAP_DRUSH : + // Core commands shipping with drush + $searchpath[] = dirname(__FILE__); + + // User commands, specified by 'include' option + if ($include = drush_get_option(array('i', 'include'), FALSE)) { + foreach (explode(":", $include) as $path) { + $searchpath[] = $path; + } + } - // Core commands shipping with drush - $searchpath[] = dirname(__FILE__); + // User commands, residing i ~/.drush + if (!empty($_SERVER['HOME'])) { + $searchpath[] = $_SERVER['HOME'] . '/.drush'; + } + break; + case DRUSH_BOOTSTRAP_DRUPAL_FULL : + // Add enabled module paths. Since we are bootstrapped, + // we can use the Drupal API. + foreach (array_keys(module_rebuild_cache()) as $module) { + $searchpath[] = drupal_get_path('module', $module); + } + break; - // User commands, residing i ~/.drush - if (!empty($_SERVER['HOME'])) { - $searchpath[] = $_SERVER['HOME'] . '/.drush'; - } + } - // User commands, specified by 'include' option - if ($include = drush_get_option(array('i', 'include'), FALSE)) { - $searchpath = array_merge($searchpath, explode(":", $include)); - } - - // Site commands, found in current Drupal site. - if (defined('DRUSH_DRUPAL_BOOTSTRAPPED')) { - // Add enabled module paths. Since we are bootstrapped, - // we can use the Drupal API. - foreach (array_keys(module_rebuild_cache()) as $module) { - $searchpath[] = drupal_get_path('module', $module); - } - } + if (sizeof($searchpath)) { $list = array(); // Scan for drush command files, load if found @@ -237,14 +305,13 @@ } } } - } - return _drush_commandfile_cache($list); + if (sizeof($list)) { + $cache = array_merge($cache, $list); + } + } } -// This constant, when passed to _drush_commandfile_cache, will wipe out the command file cache -define('DRUSH_FLUSH_COMMANDFILE_CACHE', 1); - /** * Storage mechanism for the cached commandfile list. * @@ -362,6 +429,21 @@ return $files; } +// Return commands that are valid for the current bootstrap phase. +function drush_enforce_requirement_bootstrap_phase($commands) { + $valid = array(); + $current_phase = drush_get_context('DRUSH_BOOTSTRAP_PHASE'); + if (!empty($commands)) { + foreach ($commands as $key => $command) { + $command += array('bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL); + if ($command['bootstrap'] == $current_phase) { + $valid[$key] = $command; + } + } + } + return $valid; +} + // Return commands which have their declared dependencies or have no dependencies. function drush_enforce_requirement_drupal_dependencies($commands) { $valid = array(); @@ -393,3 +475,4 @@ return $valid; } + Index: includes/context.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/context.inc,v retrieving revision 1.1 diff -u -r1.1 context.inc --- includes/context.inc 20 Mar 2009 16:10:54 -0000 1.1 +++ includes/context.inc 24 Mar 2009 03:08:37 -0000 @@ -47,7 +47,7 @@ * Specified by config files : * custom : Loaded from the config file specified by --config or -c * site : Loaded from the drushrc.php file in the Drupal sites directory. - * platform : Loaded from the drushrc.php file in the Drupal root directory. + * drupal : Loaded from the drushrc.php file in the Drupal root directory. * user : Loaded from the drushrc.php file in the user's home directory. * drush : Loaded from the drushrc.php file in the same directory as drush.php. * @@ -67,19 +67,21 @@ * The keys are the 'context' of the files, the values are the file * system locations. */ -function _drush_config_files() { +function _drush_config_file($context) { $configs = array(); // Did the user explicitly specify a config file? - if ($config = drush_get_option(array('c', 'config'), FALSE)) { + if ($config = drush_get_option(array('c', 'config'))) { $configs['custom'] = $config; } - // in the current site directory - $configs['site'] = drush_site_path() . "/drushrc.php"; + if ($site_path = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) { + $configs['site'] = $site_path . "/drushrc.php"; + } - // in the located drupal root - $configs['platform'] = drush_get_option(array('r', 'root'), drush_locate_root()). '/drushrc.php'; + if ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT')) { + $configs['drupal'] = $drupal_root . '/drushrc.php'; + } // in the user home directory $configs['user'] = $_SERVER['HOME'] . '/.drushrc.php'; @@ -88,23 +90,30 @@ // TODO: This really should be a proper, system-wide location. $configs['drush'] = dirname(__FILE__) . '/../drushrc.php'; - return $configs; + return $configs[$context]; } /** * Load drushrc files (if available) from several possible locations. */ -function drush_load_config() { - global $override; +function drush_load_config($context) { + global $conf; - $configs = _drush_config_files(); - foreach ($configs as $context => $config) { - $options = array(); - if (file_exists($config)) { - // Sets all the default options for drush - require_once($config); - drush_set_context($context, $options); + $config = _drush_config_file($context); + if (file_exists($config)) { + drush_log(dt('Loading drushrc "!config" into "!context" scope.', array('!config' => realpath($config), '!context' => $context)), 'bootstrap'); + require_once($config); + drush_set_context($context, $options); + + /** + * Allow the drushrc.php file to override $conf settings. + * This is a separate variable because the $conf array gets + * initialized to an empty array, in the drupal bootstrap process, + * and changes in settings.php would wipe out the drushrc.php settings. + */ + if (is_array($override)) { + $conf = array_merge($conf, $override); } } } @@ -114,28 +123,16 @@ * * @param context * Any of the default defined contexts. - * @param options + * @param value * The value to store in the context - * @param merge - * Optional. Should the pervious options be merged, retaining any settings not defined in options. * * @return * An associative array of the settings specified in the request context. */ -function drush_set_context($context, $options, $merge = FALSE) { - - $options = (array) $options; - - $cache =& _drush_get_context($context); - - if ($merge) { - $cache = array_merge($cache, $options); - } - else { - $cache = $options; - } - - return $cache; +function drush_set_context($context, $value) { + $cache =& drush_get_context($context); + $cache = $value; + return $value; } @@ -149,7 +146,7 @@ * * Functions that operate on the context cache, can retrieve a reference * to the context cache using : - * $cache = &_drush_get_context($context); + * $cache = &drush_get_context($context); * * This is a private function, because it is meant as an internal * generalized API for writing static cache functions, not as a general @@ -167,7 +164,7 @@ * Otherwise only the requested context will be returned. * If the context does not exist yet, it will be initialied to an empty array. */ -function &_drush_get_context($context = null) { +function &drush_get_context($context = null) { static $cache = array(); if (!is_null($context)) { if (!isset($cache[$context])) { @@ -203,7 +200,7 @@ * arguments to the command. */ function drush_get_arguments() { - return _drush_get_context('arguments'); + return drush_get_context('arguments'); } /** @@ -226,82 +223,8 @@ * */ function drush_get_command() { - return _drush_get_context('command'); + return drush_get_context('command'); } - -/** - * Parse console arguments. - * - * @param $args - * The console argument array (usually $argv) - * @param $arg_opts - * An array of options that are followed by an argument. - * e.g. shell.php -u admin -v --> $arg_opts = array('u') - * @param $default_options - * @return - * A associative array: - * $return['commands'] ia a numeric array of all commands, - * $return['options'] contains the options. The option keys - * are always noted without - or -- and are set to TRUE if they were - * invoked, to the argument if followed by an argument, and if not present - * to their default value or FALSE if no default value was specified. - */ -function drush_parse_args($args = array(), $arg_opts = array(), $default_options = array()) { - $options = $default_options; - $arguments = array(); - - for ($i = 1; $i < count($args); $i++) { - $opt = $args[$i]; - // Is the arg an option (starting with '-')? - if ($opt{0} == "-" && strlen($opt) != 1) { - // Do we have multiple options behind one '-'? - if (strlen($opt) > 2 && $opt{1} != "-") { - // Each char becomes a key of its own. - for ($j = 1; $j < strlen($opt); $j++) { - $options[substr($opt, $j, 1)] = true; - } - } - // Do we have a longopt (starting with '--')? - elseif ($opt{1} == "-") { - if ($pos = strpos($opt, '=')) { - $options[substr($opt, 2, $pos - 2)] = substr($opt, $pos + 1); - } - else { - $options[substr($opt, 2)] = true; - } - } - else { - $opt = substr($opt, 1); - // Check if the current opt is in $arg_opts (= has to be followed by an argument). - if ((in_array($opt, $arg_opts))) { - if (($args[$i+1] == NULL) || ($args[$i+1] == "") || ($args[$i + 1]{0} == "-")) { - exit("Invalid input: -$opt needs to be followed by an argument."); - } - $options[$opt] = $args[$i + 1]; - $i++; - } - else { - $options[$opt] = true; - } - } - } - // If it's not an option, it's a command. - else { - $arguments[] = $opt; - } - } - drush_set_arguments($arguments); - drush_set_context('options', $options); - - if (DRUSH_BACKEND) { - // Load options passed as a JSON encoded string through STDIN. - $stdin_options = _drush_backend_get_stdin(); - if (is_array($stdin_options)) { - drush_set_context('stdin', $stdin_options); - } - } -} - /** * Get the value for an option. * @@ -321,17 +244,17 @@ if ($context) { // We have a definite context to check for the presence of an option. - $value = _drush_get_option($option, _drush_get_context($context)); + $value = _drush_get_option($option, drush_get_context($context)); } else { // We are not checking a specific context, so check them in a predefined order of precedence. static $contexts = array( 'process', 'options', 'stdin', - 'custom', 'site', 'platform', 'user', + 'custom', 'site', 'drupal', 'user', 'drush', 'default'); foreach ($contexts as $context) { - $value = _drush_get_option($option, _drush_get_context($context)); + $value = _drush_get_option($option, drush_get_context($context)); if ($value !== null) { return $value; @@ -352,9 +275,9 @@ function drush_get_merged_options() { static $contexts = array( 'process', 'options', 'stdin', - 'custom', 'site', 'platform', 'user', + 'custom', 'site', 'drupal', 'user', 'drush', 'default'); - $cache = _drush_get_context(); + $cache = drush_get_context(); $result = array(); foreach (array_reverse($contexts) as $context) { if (array_key_exists($context, $cache)) { @@ -398,7 +321,7 @@ * Without having to constantly type out the value parameter. */ function drush_set_option($option, $value, $context = 'process') { - $cache =& _drush_get_context($context); + $cache =& drush_get_context($context); $cache[$option] = $value; return $value; } @@ -419,7 +342,7 @@ * Context in which to unset the value in. */ function drush_unset_option($option, $context) { - $cache =& _drush_get_context($context); + $cache =& drush_get_context($context); if (array_key_exists($option, $cache)) { unset($cache[$option]); } @@ -438,7 +361,7 @@ if (array_key_exists($context, $files)) { $filename = $files[$context]; - $cache = _drush_get_context($context); + $cache = drush_get_context($context); $fp = fopen($filename, "w+"); if (!$fp) { @@ -453,6 +376,7 @@ } fwrite($fp, "\n"); fclose($fp); + drush_log(dt('Config file (!filename) written', array('!filename' => $filename), 'success')); return true; } Index: includes/core_commands.drush.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/core_commands.drush.inc,v retrieving revision 1.29 diff -u -r1.29 core_commands.drush.inc --- includes/core_commands.drush.inc 13 Mar 2009 10:13:48 -0000 1.29 +++ includes/core_commands.drush.inc 24 Mar 2009 03:08:38 -0000 @@ -22,6 +22,11 @@ function core_commands_drush_command() { $items = array(); + $items['help'] = array( + 'callback' => 'drush_usage', + 'description' => 'Print this help message.', + 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap. + ); $items['cron'] = array( 'callback' => 'core_commands_cron_run', 'description' => 'Run all cron hooks.', @@ -59,7 +64,7 @@ 'options' => array( '--destination' => 'Path to which the project will be copied.', ), - 'bootstrap' => -1, // No bootstrap at all. + 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all. ); return $items; } @@ -98,15 +103,6 @@ $message .= dt("\nDrush was attempting to connect to : \n!credentials\n", array('!credentials' => _core_commands_site_credentials())); $message .= dt("You can select another site with a working database setup by specifying the URI to use with the --uri parameter on the command line or \$options['uri'] in your drushrc.php file.\n"); return $message; - - $site_path = drush_site_path(); - $message = "Drush was not able to start (bootstrap) Drupal.\n"; - $message .= "Hint: This error often occurs when Drush is trying to bootstrap a site\n"; - $message .= "that has not been installed or does not have a configured database.\n"; - $message .= "Drush was looking for the site in '$site_path'. You can select another site\n"; - $message .= "with a working database setup by specifying the URI to use with the --uri\n"; - $message .= "parameter on the command line or \$options['uri'] in your drushrc.php file.\n"; - return $message; break; } } @@ -177,15 +173,15 @@ * installation. */ function core_commands_status() { - if (defined('DRUSH_DRUPAL_ROOT')) { - drush_print(dt("Drupal !version is installed at !DRUSH_DRUPAL_ROOT.", array('!version' => drush_drupal_version(), '!DRUSH_DRUPAL_ROOT' => DRUSH_DRUPAL_ROOT))); - if (DRUSH_URI != '') { - drush_print(dt("Current site: !DRUSH_URI", array('!DRUSH_URI' => DRUSH_URI))); + if ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT')) { + drush_print(dt("Drupal !version is installed at !DRUSH_DRUPAL_ROOT.", array('!version' => drush_drupal_version(), '!DRUSH_DRUPAL_ROOT' => $drupal_root))); + if ($drush_uri = drush_get_context('DRUSH_URI')) { + drush_print(dt("Current site: !DRUSH_URI", array('!DRUSH_URI' => $drush_uri))); } else { drush_print(dt("No site uri was determined. Using default site path.")); } - if (defined('DRUSH_DRUPAL_BOOTSTRAPPED')) { + if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_DATABASE) { switch (drush_drupal_major_version()) { case 5: case 6: @@ -410,4 +406,4 @@ drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', dt('Could not download project status information from !url', array('!url' => $url))); } } -} \ No newline at end of file +} Index: includes/drush.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/drush.inc,v retrieving revision 1.15 diff -u -r1.15 drush.inc --- includes/drush.inc 20 Mar 2009 16:10:54 -0000 1.15 +++ includes/drush.inc 24 Mar 2009 03:08:38 -0000 @@ -14,41 +14,18 @@ * @param * */ -function drush_dispatch() { - if (!drush_get_command()) { - $arguments = drush_get_arguments(); - - // Special case for help command - $show_help = FALSE; - if (isset($arguments[0]) && $arguments[0] == 'help') { - $show_help = TRUE; - array_shift($arguments); - (empty($arguments)) ? drush_usage() : drush_show_help($arguments); - return TRUE; - } - - drush_parse_command(); - } - - return _drush_dispatch_cmd(); -} - -/** - * Dispatch a command if one has been found. - */ -function _drush_dispatch_cmd() { +function drush_dispatch($command = NULL) { + $command = ($command) ? $command : drush_get_command(); $return = FALSE; - $command = drush_get_command(); - $arguments = drush_get_arguments(); - if ($command) { // Call the callback function of the active command. - $return = call_user_func_array($command['callback'], $arguments); + $return = call_user_func_array($command['callback'], $command['arguments']); } - else { - // Special case: If no command is found, display usage info. - $return = call_user_func_array('drush_usage', $arguments); + + // prevent a '1' at the end of the output + if ($return === TRUE) { + $return = ''; } return $return; @@ -70,10 +47,10 @@ static $version = FALSE; if (!$version) { - if (defined('DRUSH_DRUPAL_ROOT')) { - if (file_exists(DRUSH_DRUPAL_ROOT . '/modules/system/system.module')) { + if ($drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT')) { + if (file_exists($drupal_root . '/modules/system/system.module')) { // We can safely include system.module as it contains defines and functions only. - require_once(DRUSH_DRUPAL_ROOT . '/modules/system/system.module'); + require_once($drupal_root . '/modules/system/system.module'); // We just might be dealing with an early Drupal version (pre 4.7) if (defined('VERSION')) { $version = VERSION; @@ -131,11 +108,11 @@ $args = func_get_args(); array_shift($args); // Skip function name - if (DRUSH_VERBOSE || DRUSH_SIMULATE) { + if (drush_get_context('DRUSH_VERBOSE') || drush_get_context('DRUSH_SIMULATE')) { drush_print("Calling $function(". implode(", ", $args) .')'); } - if (DRUSH_SIMULATE) { + if (drush_get_context('DRUSH_SIMULATE')) { return true; } @@ -174,8 +151,10 @@ */ function drush_usage() { $commands = func_get_args(); - - if (drush_get_option('help') || empty($commands)) { + drush_set_context('DRUSH_SHOWING_HELP', TRUE); + + if (empty($commands)) { + $phases = _drush_bootstrap_phases(); drush_print(dt('Usage: drush.php [options] ...')); drush_print(); drush_print('Run "drush help [command]" to view command-specific help.'); @@ -188,14 +167,33 @@ drush_print(); drush_print('Commands: '); - $commands = drush_get_commands(); - $rows = array(); - foreach($commands as $key => $command) { - $rows[] = array($key, $commands[$key]['description']); + $printed_rows = array(); + $phase_index = DRUSH_BOOTSTRAP_DRUSH; + + foreach ($phases as $phase_index) { + if ($phase_index > drush_get_context('DRUSH_BOOTSTRAP_PHASE')) { + drush_bootstrap($phase_index); + } + if (!drush_get_error()) { + $commands = drush_get_commands(); + $rows = array(); + foreach($commands as $key => $command) { + if (!array_key_exists($key, $printed_rows)) { + $rows[$key] = array(sprintf("%-20s", $key), $commands[$key]['description']); + } + } + drush_print_table($rows, 2); + $printed_rows = array_merge($printed_rows, $rows); + } + else { + break; + } } - drush_print_table($rows, 2); return; } + else { + return drush_show_help($commands); + } drush_set_error(DRUSH_COMMAND_NOT_FOUND, dt('Invalid command !command.', array('!command' => implode(" ", $commands)))); } @@ -226,45 +224,55 @@ * Prints out the default drush help page. */ function drush_show_help($commands) { - $commandstring = implode(" ", $commands); + $phases = _drush_bootstrap_phases(); - if (!drush_is_command($commandstring)) { - return drush_set_error(DRUSH_COMMAND_NOT_FOUND, dt('Invalid command !command.', array('!command' => $commandstring))); - } - else { - // Get the full command array - $all = drush_get_commands(); - $command = $all[$commandstring]; - } + $commandstring = implode(" ", $commands); - $help = drush_command_invoke_all('drush_help', 'drush:'. $commandstring); - if (!empty($help)) { - drush_print(wordwrap(implode("\n", $help), 80)); - drush_print(); - -// TODO: Just let commands define their own sections? Standardization need not be mandated? - $sections = array( - 'examples' => 'Examples', - 'arguments' => 'Arguments', - 'options' => 'Options', - ); - - foreach ($sections as $key => $value) { - if ($command[$key]) { - drush_print(dt($value) . ':'); - foreach ($command[$key] as $name => $description) { - $rows[] = array($name, dt($description)); + foreach ($phases as $phase_index) { + if ($phase_index > drush_get_context('DRUSH_BOOTSTRAP_PHASE')) { + drush_bootstrap($phase_index); + } + if (!drush_get_error()) { + $commands = drush_get_commands(); + if (array_key_exists($commandstring, $commands)) { + $command = $commands[$commandstring]; + $help = drush_command_invoke_all('drush_help', 'drush:'. $commandstring); + if (!empty($help)) { + drush_print(wordwrap(implode("\n", $help), 80)); + drush_print(); + + // TODO: Just let commands define their own sections? + // Standardization need not be mandated? + $sections = array( + 'examples' => 'Examples', + 'arguments' => 'Arguments', + 'options' => 'Options', + ); + + foreach ($sections as $key => $value) { + if ($command[$key]) { + drush_print(dt($value) . ':'); + foreach ($command[$key] as $name => $description) { + $rows[] = array($name, dt($description)); + } + drush_print_table($rows, 2); + unset($rows); + drush_print(); + } + } + return TRUE; + } + else { + drush_print("No help available for command 'drush $commandstring'."); + return TRUE; } - drush_print_table($rows, 2); - unset($rows); - drush_print(); } } - - } - else { - drush_print("No help available for command 'drush $commandstring'."); + else { + break; + } } + return drush_set_error(DRUSH_COMMAND_NOT_FOUND, dt('Invalid command !command.', array('!command' => $commandstring))); } /** @@ -279,11 +287,11 @@ * Indentation for the output (only for verbose mode). */ function drush_shell_exec($cmd, $indent = 0) { - if (DRUSH_VERBOSE || DRUSH_SIMULATE) { + if (drush_get_context('DRUSH_VERBOSE') || drush_get_context('DRUSH_SIMULATE')) { drush_print('Executing: ' . $cmd, $indent); } - if (DRUSH_SIMULATE) { + if (drush_get_context('DRUSH_SIMULATE')) { return true; } @@ -291,7 +299,7 @@ _drush_shell_exec_output_set($output); - if (DRUSH_VERBOSE) { + if (drush_get_context('DRUSH_VERBOSE')) { foreach ($output as $line) { drush_print($line, $indent + 2); } @@ -346,10 +354,10 @@ * Returns TRUE if in verbose mode, otherwise FALSE. */ function drush_verbose($msg = FALSE, $indent = 0) { - if (!DRUSH_VERBOSE) { + if (!drush_get_context('DRUSH_VERBOSE')) { return FALSE; } - if (DRUSH_VERBOSE && $msg === FALSE) { + if (drush_get_context('DRUSH_VERBOSE') && $msg === FALSE) { return TRUE; } @@ -366,7 +374,7 @@ function drush_confirm($msg, $indent = 0) { print str_repeat(' ', $indent) . (string)$msg . " (y/n): "; - if (DRUSH_AFFIRMATIVE) { + if (drush_get_context('DRUSH_AFFIRMATIVE')) { print "y\n"; return TRUE; } @@ -531,7 +539,7 @@ $type_msg = sprintf($green, $entry['type']); break; default : - if (!defined('DRUSH_VERBOSE')) { + if (!drush_get_context('DRUSH_VERBOSE')) { // print nothing. exit cleanly. return TRUE; } @@ -539,8 +547,8 @@ break; } // When running in backend mode, log messages are not displayed, as they will - // be returned in the JSON encoded associative array. - if (DRUSH_BACKEND) { + // be returned in the JSON encoded associative array. + if (drush_get_context('DRUSH_BACKEND') || drush_get_context('DRUSH_SHOWING_HELP')) { return $return; } // Place the status message right aligned with the top line of the error message. @@ -570,7 +578,7 @@ drush_log(ereg_replace('/^user warning: /', '', $error), 'error'); } else { - drush_log($error, 'error'); + drush_set_error('DRUSH_DRUPAL_ERROR_MESSAGE', $error); } } } Index: includes/environment.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/environment.inc,v retrieving revision 1.5 diff -u -r1.5 environment.inc --- includes/environment.inc 20 Mar 2009 16:10:54 -0000 1.5 +++ includes/environment.inc 24 Mar 2009 03:08:39 -0000 @@ -13,6 +13,343 @@ define('DRUSH_DRUPAL_BOOTSTRAP', 'includes/bootstrap.inc'); /** + * @name Drush bootstrap phases + * @{ + * Sequential Drush bootstrapping phases. + */ + +/** + * Only bootstrap Drush, without any Drupal specific code. + * + * Any code that operates on the Drush installation, and not specifically + * any Drupal directory, should bootstrap to this phase. + */ +define('DRUSH_BOOTSTRAP_DRUSH', 0); + +/** + * Set up and test for a valid drupal root, either through the -r/--root options, + * or evaluated based on the current working directory. + * + * Any code that interacts with an entire Drupal installation, and not a specific + * site on the Drupal installation should use this bootstrap phase. + */ +define('DRUSH_BOOTSTRAP_DRUPAL_ROOT', 1); + +/** + * Set up a Drupal site directory and the correct environment variables to + * allow Drupal to find the configuration file. + * + * If no site is specified with the -l / --uri options, Drush will assume the + * site is 'default', which mimics Drupal's behaviour. + * + * If you want to avoid this behaviour, it is recommended that you use the + * DRUSH_BOOTSTRAP_DRUPAL_ROOT bootstrap phase instead. + * + * Any code that needs to modify or interact with a specific Drupal site's + * settings.php file should bootstrap to this phase. + */ +define('DRUSH_BOOTSTRAP_DRUPAL_SITE', 2); + +/** + * Load the settings from the Drupal sites directory. + * + * This phase is analagous to the DRUPAL_BOOTSTRAP_CONFIGURATION bootstrap phase in Drupal + * itself, and this is also the first step where Drupal specific code is included. + * + * This phase is commonly used for code that interacts with the Drupal install API, + * as both install.php and update.php start at this phase. + */ +define('DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION', 3); + +/** + * Connect to the Drupal database using the database credentials loaded + * during the previous bootstrap phase. + * + * This phase is analogous to the DRUPAL_BOOTSTRAP_DATABASE bootstrap phase in + * Drupal. + * + * Any code that needs to interact with the Drupal database API needs to + * be bootstrapped to at least this phase. + */ +define('DRUSH_BOOTSTRAP_DRUPAL_DATABASE', 4); + +/** + * Fully initialize Drupal. + * + * This is the default bootstrap phase all commands will try to reach, + * unless otherwise specified. + * This is analogous to the DRUPAL_BOOTSTRAP_FULL bootstrap phase in + * Drupal. + * + * Any code that interacts with the general Drupal API should be + * bootstrapped to this phase. + */ +define('DRUSH_BOOTSTRAP_DRUPAL_FULL', 5); + +/** + * Log in to the initialiased Drupal site. + * + * This bootstrap phase is used after the site has been + * fully bootstrapped. + * + * This phase will log you in to the drupal site with the username + * or user ID specified by the --user/ -u option. + * + * Use this bootstrap phase for your command if you need to have access + * to information for a specific user, such as listing nodes that might + * be different based on who is logged in. + */ +define('DRUSH_BOOTSTRAP_DRUPAL_LOGIN', 6); + + +/** + * Helper function listing phases. + * + * For commands that need to iterate through the phases, such as help + */ +function _drush_bootstrap_phases() { + static $phases = array( + DRUSH_BOOTSTRAP_DRUSH, DRUSH_BOOTSTRAP_DRUPAL_ROOT, + DRUSH_BOOTSTRAP_DRUPAL_SITE, DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION, + DRUSH_BOOTSTRAP_DRUPAL_DATABASE, DRUSH_BOOTSTRAP_DRUPAL_FULL, + DRUSH_BOOTSTRAP_DRUPAL_LOGIN, + ); + + return $phases; +} + +/** + * @} End of Drush bootstrap phases. + */ + +/** + * Bootstrap Drush to the desired phase. + * + * This function will sequentially bootstrap each + * lower phase up to the phase that has been requested. + * + * @param phase + * The bootstrap phase to bootstrap to. + * Any of the following constants : + * DRUSH_BOOTSTRAP_DRUSH = Only Drush. + * DRUSH_BOOTSTRAP_DRUPAL_ROOT = Find a valid Drupal root. + * DRUSH_BOOTSTRAP_DRUPAL_SITE = Find a valid Drupal site. + * DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION = Load the site's settings. + * DRUSH_BOOTSTRAP_DRUPAL_DATABASE = Initialize the database. + * DRUSH_BOOTSTRAP_DRUPAL_FULL = Initialize Drupal fully. + * DRUSH_BOOTSTRAP_DRUPAL_LOGIN = Log into Drupal with a valid user. + */ + +function drush_bootstrap($phase) { + static $phases = array( + DRUSH_BOOTSTRAP_DRUSH => '_drush_bootstrap_drush', + DRUSH_BOOTSTRAP_DRUPAL_ROOT => '_drush_bootstrap_drupal_root', + DRUSH_BOOTSTRAP_DRUPAL_SITE => '_drush_bootstrap_drupal_site', + DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION => '_drush_bootstrap_drupal_configuration', + DRUSH_BOOTSTRAP_DRUPAL_DATABASE => '_drush_bootstrap_drupal_database', + DRUSH_BOOTSTRAP_DRUPAL_FULL => '_drush_bootstrap_drupal_full', + DRUSH_BOOTSTRAP_DRUPAL_LOGIN => '_drush_bootstrap_drupal_login'); + + static $phase_index = 0; + + while ($phase >= $phase_index && isset($phases[$phase_index])) { + drush_set_context('DRUSH_BOOTSTRAP_PHASE', $phase_index); + $current_phase = $phases[$phase_index]; + if (function_exists($current_phase) && !drush_get_error()) { + drush_log(dt("Drush bootstrap phase : !function()", array('!function' => $current_phase)), 'bootstrap'); + drush_set_context('DRUSH_BOOTSTRAPPING', TRUE); + ob_start(); + $current_phase(); + ob_end_clean(); + drush_set_context('DRUSH_BOOSTRAPPING', FALSE); + + if ($phase >= DRUSH_DRUPAL_BOOTSTRAP_FULL) { + // We have enough of the system bootstrapped now to + // capture and integrate and drupal messages. + _drush_log_drupal_messages(); + } + } + unset($phases[$phase_index++]); + } +} + +/** + * Initial Drush bootstrap phase. + * + * During the initialization of Drush, + * this is the first step where all we are + * aware of is Drush itself. + * + * In this step we will register the shutdown function, + * parse the command line arguments and store them in their + * related contexts. + * + * Configuration files (drushrc.php) that are + * a) Specified on the command line + * b) Stored in the root directory of drush.php + * c) Stored in the home directory of the system user. + * + * Additionally the DRUSH_QUIET and DRUSH_BACKEND contexts, + * will be evaluated now, as they need to be set very early in + * the execution flow to be able to take affect/ + */ +function _drush_bootstrap_drush() { + // The bootstrap can fail silently, so we catch that in a shutdown function. + register_shutdown_function('drush_shutdown'); + + // parse the command line arguments. + drush_parse_args(); + + // Load a custom config specified with the --config option. + drush_load_config('custom'); + + // Load a drushrc.php file in the drush.php's directory. + drush_load_config('drush'); + + // Load a drushrc.php file in the ~/.drush directory. + drush_load_config('user'); + + $backend = drush_set_context('DRUSH_BACKEND', drush_get_option(array('b', 'backend'))); + + if ($backend) { + // Load options passed as a JSON encoded string through STDIN. + $stdin_options = _drush_backend_get_stdin(); + if (is_array($stdin_options)) { + drush_set_context('stdin', $stdin_options); + } + } + $quiet = drush_set_context('DRUSH_QUIET', drush_get_option(array('q', 'quiet'))); + + // When running in backend mode, all output is buffered, and returned + // as a property of a JSON encoded associative array. + if ($backend || $quiet) { + ob_start(); + } + + drush_set_context('DRUSH_VERBOSE', drush_get_option(array('v', 'verbose'), FALSE)); + drush_set_context('DRUSH_AFFIRMATIVE', drush_get_option(array('y', 'yes'), FALSE)); + drush_set_context('DRUSH_SIMULATE', drush_get_option(array('s', 'simulate'), FALSE)); + + // Find any command files that are available during this bootstrap phase. + _drush_find_commandfiles(DRUSH_BOOTSTRAP_DRUSH); +} + +/** + * Bootstrap Drush with a valid Drupal Directory. + * + * In this function, the pwd will be moved to the root + * of the Drupal installation. + * + * We also now load the drushrc.php for this specific platform. + * We can now include files from the Drupal Tree, and figure + * out more context about the platform, such as the version of Drupal. + */ +function _drush_bootstrap_drupal_root() { + $drupal_root = drush_get_option(array('r', 'root'), drush_locate_root()); + + if (!drush_valid_drupal_root($drupal_root)) { + drush_set_error('DRUSH_INVALID_DRUPAL_ROOT', dt("The directory !drupal_root does not contain a valid Drupal installation", array('!drupal_root' => $drupal_root))); + } + else { + drush_set_context('DRUSH_DRUPAL_ROOT', $drupal_root); + chdir($drupal_root); + + define('DRUPAL_ROOT', $drupal_root); + + drush_load_config('drupal'); + + require_once(DRUSH_DRUPAL_BOOTSTRAP); + + $version = drush_set_context('DRUSH_DRUPAL_VERSION', drush_drupal_version()); + $major_version = drush_set_context('DRUSH_DRUPAL_MAJOR_VERSION', drush_drupal_major_version()); + + drush_log(dt("Initialized Drupal !version root directory at !drupal_root", array("!version" => $version, '!drupal_root' => $drupal_root))); + } +} + +/** + * Initialize a site on the Drupal root. + * + * We will now check if we have a valid site that is specified for + * this command. + * + * Sets up various constants and $_SERVER entries used by + * Drupal, essentially mimicking a webserver environment. + */ +function _drush_bootstrap_drupal_site() { + $drush_uri = drush_set_context('DRUSH_URI', + drush_get_option(array('l', 'uri'), drush_site_uri())); + + // Fake the necessary HTTP headers that Drupal needs: + if ($drush_uri) { + $drupal_base_url = parse_url($drush_uri); + $_SERVER['HTTP_HOST'] = $drupal_base_url['host']; + $_SERVER['PHP_SELF'] = $drupal_base_url['path'] . '/index.php'; + } + else { + $_SERVER['HTTP_HOST'] = 'default'; + $_SERVER['PHP_SELF'] = '/index.php'; + } + + $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF']; + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + $_SERVER['REQUEST_METHOD'] = NULL; + $_SERVER['SERVER_SOFTWARE'] = NULL; + $_SERVER['HTTP_USER_AGENT'] = NULL; + + $site = drush_set_context('DRUSH_DRUPAL_SITE', $_SERVER['HTTP_HOST']); + if (($conf_path = conf_path()) && !file_exists("./$conf_path/settings.php")) { + drush_set_error(DRUPAL_SITE_SETTINGS_NOT_FOUND); + } + else { + drush_set_context('DRUSH_DRUPAL_SITE_ROOT', $conf_path); + + drush_log(dt("Initialized Drupal site !site at !site_root", array('!site' => $site, '!site_root' => $conf_path))); + } +} + +/** + * Initialize and load the Drupal configuration files. + */ +function _drush_bootstrap_drupal_configuration() { + drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION); +} + +/** + * Attempt to load the drupal database connection. + */ +function _drush_bootstrap_drupal_database() { + drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE); +} + +/** + * Attempt to load the full Drupal system + */ +function _drush_bootstrap_drupal_full() { + drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); + + // Find any command files that are available during this bootstrap phase. + _drush_find_commandfiles(DRUSH_BOOTSTRAP_DRUPAL_FULL); + +} + +/** + * Log into the bootstrapped Drupal site with a specific + * username or user id. + */ +function _drush_bootstrap_drupal_login() { + $drush_user = drush_set_context('DRUSH_USER', drush_get_option(array('u', 'user'), 0)); + + if (!drush_drupal_login($drush_user)) { + exit(); + } +} + + + + + +/** * Returns the current working directory. * * TODO: Could cache result, but it isn't really expensive. ? drush_pm/.svn ? drush_sql/.svn ? drush_tools/.svn