Beispiel #1
0
/**
 * The main Drush function.
 *
 * - Parses the command line arguments, configuration files and environment.
 * - Prepares and executes a Drupal bootstrap, if possible,
 * - Dispatches the given command.
 *
 * @return
 *   Whatever the given command returns.
 */
function drush_main()
{
    $phases = _drush_bootstrap_phases(FALSE, TRUE);
    drush_set_context('DRUSH_BOOTSTRAP_PHASE', DRUSH_BOOTSTRAP_NONE);
    // We need some global options processed at this early stage. Namely --debug.
    drush_parse_args();
    _drush_bootstrap_global_options();
    $return = '';
    $command_found = FALSE;
    foreach ($phases as $phase) {
        if (drush_bootstrap_to_phase($phase)) {
            $command = drush_parse_command();
            // Process a remote command if 'remote-host' option is set.
            if (drush_remote_command()) {
                $command_found = TRUE;
                break;
            }
            if (is_array($command)) {
                $bootstrap_result = drush_bootstrap_to_phase($command['bootstrap']);
                drush_enforce_requirement_bootstrap_phase($command);
                drush_enforce_requirement_core($command);
                drush_enforce_requirement_drupal_dependencies($command);
                drush_enforce_requirement_drush_dependencies($command);
                if ($bootstrap_result && empty($command['bootstrap_errors'])) {
                    drush_log(dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])), 'bootstrap');
                    $command_found = TRUE;
                    // Dispatch the command(s).
                    $return = drush_dispatch($command);
                    // prevent a '1' at the end of the output
                    if ($return === TRUE) {
                        $return = '';
                    }
                    if (drush_get_context('DRUSH_DEBUG')) {
                        drush_print_timers();
                    }
                    drush_log(dt('Peak memory usage was !peak', array('!peak' => drush_format_size(memory_get_peak_usage()))), 'memory');
                    break;
                }
            }
        } else {
            break;
        }
    }
    if (!$command_found) {
        // If we reach this point, we have not found either a valid or matching command.
        $args = implode(' ', drush_get_arguments());
        if (isset($command) && is_array($command)) {
            foreach ($command['bootstrap_errors'] as $key => $error) {
                drush_set_error($key, $error);
            }
            drush_set_error('DRUSH_COMMAND_NOT_EXECUTABLE', dt("The drush command '!args' could not be executed.", array('!args' => $args)));
        } elseif (!empty($args)) {
            drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt("The drush command '!args' could not be found.", array('!args' => $args)));
        }
        // Set errors that ocurred in the bootstrap phases.
        $errors = drush_get_context('DRUSH_BOOTSTRAP_ERRORS', array());
        foreach ($errors as $code => $message) {
            drush_set_error($code, $message);
        }
    }
    // We set this context to let the shutdown function know we reached the end of drush_main();
    drush_set_context("DRUSH_EXECUTION_COMPLETED", TRUE);
    // After this point the drush_shutdown function will run,
    // exiting with the correct exit code.
    return $return;
}
Beispiel #2
0
function drush_bootstrap($argc, $argv)
{
    global $args, $override, $conf;
    // Parse command line options and arguments.
    $args = drush_parse_args($argv, array('h', 'u', 'r', 'l'));
    // We use PWD if available because getcwd() resolves symlinks, which
    // could take us outside of the Drupal root, making it impossible to find.
    $path = $_SERVER['PWD'];
    if (empty($path)) {
        $path = getcwd();
    }
    // Convert windows paths.
    $path = drush_convert_path($path);
    // Try and locate the Drupal root directory
    $root = _drush_locate_root($path);
    // Load .drushrc file if available. Allows you to provide defaults for options and variables.
    drush_load_rc($root);
    $uri = FALSE;
    // If the current directory contains a settings.php we assume that is the desired site URI.
    if (file_exists('./settings.php')) {
        // Export the following settings.php variables to the global namespace.
        global $db_url, $db_prefix, $cookie_domain, $conf, $installed_profile, $update_free_access;
        // If the settings.php has a defined path we use the URI from that.
        include_once './settings.php';
        if (isset($base_url)) {
            $uri = $base_url;
        } else {
            // Alternatively we default to the name of the current directory, if it is not 'default'.
            $elements = explode('/', $path);
            $current = array_pop($elements);
            if ($current != 'default') {
                $uri = 'http://' . $current;
            }
        }
    }
    // Define basic options as constants.
    define('DRUSH_URI', drush_get_option(array('l', 'uri'), $uri));
    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));
    // TODO: Make use of this as soon as the external
    define('DRUSH_USER', drush_get_option(array('u', 'user'), 0));
    // If no root is defined, we try to guess from the current directory.
    define('DRUSH_DRUPAL_ROOT', drush_get_option(array('r', 'root'), $root));
    // If the Drupal directory can't be found, and no -r option was specified,
    // or the path specified in -r does not point to a Drupal directory,
    // we have no alternative but to give up the ghost at this point.
    // (NOTE: t() is not available yet.)
    if (!DRUSH_DRUPAL_ROOT || !is_dir(DRUSH_DRUPAL_ROOT) || !file_exists(DRUSH_DRUPAL_ROOT . '/' . DRUSH_DRUPAL_BOOTSTRAP)) {
        $dir = '';
        if (DRUSH_DRUPAL_ROOT) {
            $dir = ' in ' . DRUSH_DRUPAL_ROOT;
        }
        // Provide a helpful exit message, letting the user know where we looked
        // (if we looked at all) and a hint on how to specify the directory manually.
        $message = "E: Could not locate a Drupal installation directory{$dir}. Aborting.\n";
        $message .= "Hint: You can specify your Drupal installation directory with the --root\n";
        $message .= "parameter on the command line or \$options['root'] in your drushrc.php file.\n";
        die($message);
    }
    // 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'] = NULL;
        $_SERVER['PHP_SELF'] = NULL;
    }
    $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
    $_SERVER['REMOTE_ADDR'] = '';
    $_SERVER['REQUEST_METHOD'] = NULL;
    $_SERVER['SERVER_SOFTWARE'] = NULL;
    // Change to Drupal root dir.
    chdir(DRUSH_DRUPAL_ROOT);
    // Bootstrap Drupal.
    _drush_bootstrap_drupal();
    /**
     * 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);
    }
    // Login the specified user (if given).
    if (DRUSH_USER) {
        _drush_login(DRUSH_USER);
    }
    // Now we can use all of Drupal.
    if (DRUSH_SIMULATE) {
        drush_print(t('SIMULATION MODE IS ENABLED. NO ACTUAL ACTION WILL BE TAKEN. SYSTEM WILL REMAIN UNCHANGED.'));
    }
    // Dispatch the command.
    $output = drush_dispatch($args['commands']);
    // prevent a '1' at the end of the outputs
    if ($output === true) {
        $output = '';
    }
    // TODO: Terminate with the correct exit status.
    return $output;
}
Beispiel #3
0
function drush_bootstrap($argc, $argv)
{
    global $args, $override, $conf;
    // Parse command line options and arguments.
    $args = drush_parse_args($argv, array('h', 'u', 'r', 'l'));
    // Load .drushrc file if available. Allows you to provide defaults for options and variables.
    drush_load_rc();
    // Define basic options as constants.
    define('DRUSH_URI', drush_get_option(array('l', 'uri'), FALSE));
    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));
    // TODO: Make use of this as soon as the external
    define('DRUSH_USER', drush_get_option(array('u', 'user'), 0));
    // If no root is defined, we try to guess from the current directory.
    define('DRUSH_DRUPAL_ROOT', drush_get_option(array('r', 'root'), _drush_locate_root()));
    // If the Drupal directory can't be found, and no -r option was specified,
    // or the path specified in -r does not point to a Drupal directory,
    // we have no alternative but to give up the ghost at this point.
    // (NOTE: t() is not available yet.)
    if (!DRUSH_DRUPAL_ROOT || !is_dir(DRUSH_DRUPAL_ROOT) || !file_exists(DRUSH_DRUPAL_ROOT . '/' . DRUSH_DRUPAL_BOOTSTRAP)) {
        exit("E: Could not locate the Drupal installation directory. Aborting.\n");
    }
    // Fake the necessary HTTP headers that Drupal needs:
    $drupal_base_url = parse_url(DRUSH_URI);
    $_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
    $_SERVER['PHP_SELF'] = $drupal_base_url['path'] . '/index.php';
    $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
    $_SERVER['REMOTE_ADDR'] = NULL;
    $_SERVER['REQUEST_METHOD'] = NULL;
    $_SERVER['SERVER_SOFTWARE'] = NULL;
    // Change to Drupal root dir.
    chdir(DRUSH_DRUPAL_ROOT);
    // Bootstrap Drupal.
    _drush_bootstrap_drupal();
    /**
     * 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);
    }
    // Login the specified user (if given).
    if (DRUSH_USER) {
        _drush_login(DRUSH_USER);
    }
    // Now we can use all of Drupal.
    if (DRUSH_SIMULATE) {
        drush_print(t('SIMULATION MODE IS ENABLED. NO ACTUAL ACTION WILL BE TAKEN. SYSTEM WILL REMAIN UNCHANGED.'));
    }
    // Dispatch the command.
    $output = drush_dispatch($args['commands']);
    // prevent a '1' at the end of the outputs
    if ($output === true) {
        $output = '';
    }
    // TODO: Terminate with the correct exit status.
    return $output;
}