Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompting doesn't properly set config values #805

Merged
merged 8 commits into from Nov 22, 2013
23 changes: 23 additions & 0 deletions features/core.feature
Expand Up @@ -106,6 +106,29 @@ Feature: Manage WordPress installation
http://localhost:8001
"""

Scenario: Install WordPress by prompting
Given an empty directory
And WP files
And wp-config.php
And a database
And a session file:
"""
localhost:8001
Test
wpcli
wpcli
admin@example.com
"""

When I run `wp core install --prompt < session`
Then STDOUT should not be empty

When I run `wp eval 'echo home_url();'`
Then STDOUT should be:
"""
http://localhost:8001
"""

Scenario: Full install
Given a WP install

Expand Down
47 changes: 9 additions & 38 deletions php/WP_CLI/Runner.php
Expand Up @@ -147,28 +147,6 @@ private static function guess_url( $assoc_args ) {
return false;
}

private static function set_url_params( $url_parts ) {
$f = function( $key ) use ( $url_parts ) {
return isset( $url_parts[ $key ] ) ? $url_parts[ $key ] : '';
};

if ( isset( $url_parts['host'] ) ) {
$_SERVER['HTTP_HOST'] = $url_parts['host'];
if ( isset( $url_parts['port'] ) ) {
$_SERVER['HTTP_HOST'] .= ':' . $url_parts['port'];
}

$_SERVER['SERVER_NAME'] = $url_parts['host'];
}

$_SERVER['REQUEST_URI'] = $f('path') . ( isset( $url_parts['query'] ) ? '?' . $url_parts['query'] : '' );
$_SERVER['SERVER_PORT'] = isset( $url_parts['port'] ) ? $url_parts['port'] : '80';
$_SERVER['QUERY_STRING'] = $f('query');
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
$_SERVER['HTTP_USER_AGENT'] = '';
$_SERVER['REQUEST_METHOD'] = 'GET';
}

private function cmd_starts_with( $prefix ) {
return $prefix == array_slice( $this->arguments, 0, count( $prefix ) );
}
Expand Down Expand Up @@ -395,6 +373,10 @@ private function init_config() {
$configurator->merge_config( $runtime_config );
$this->config = $configurator->to_array();

// --prompt can't be set in file, but is still a valid runtime arg
if ( ! empty( $runtime_config['prompt'] ) )
$this->config['prompt'] = true;

if ( !isset( $this->config['path'] ) ) {
$this->config['path'] = dirname( Utils\find_file_upward( 'wp-load.php' ) );
}
Expand Down Expand Up @@ -476,10 +458,8 @@ public function before_wp_load() {

// Handle --url parameter
$url = self::guess_url( $this->config );
if ( $url ) {
$url_parts = self::parse_url( $url );
self::set_url_params( $url_parts );
}
if ( $url )
\WP_CLI::set_url( $url );

$this->do_early_invoke( 'before_wp_load' );

Expand Down Expand Up @@ -511,12 +491,13 @@ public function before_wp_load() {

// We really need a URL here
if ( !isset( $_SERVER['HTTP_HOST'] ) ) {
$url_parts = self::parse_url( 'http://example.com' );
self::set_url_params( $url_parts );
$url = 'http://example.com';
\WP_CLI::set_url( $url );
}

if ( 'multisite-install' == $this->arguments[1] ) {
// need to fake some globals to skip the checks in wp-includes/ms-settings.php
$url_parts = Utils\parse_url( $url );
self::fake_current_site_blog( $url_parts );

if ( !defined( 'COOKIEHASH' ) ) {
Expand All @@ -531,16 +512,6 @@ public function before_wp_load() {
}
}

private static function parse_url( $url ) {
$url_parts = parse_url( $url );

if ( !isset( $url_parts['scheme'] ) ) {
$url_parts = parse_url( 'http://' . $url );
}

return $url_parts;
}

private static function fake_current_site_blog( $url_parts ) {
global $current_site, $current_blog;

Expand Down
30 changes: 30 additions & 0 deletions php/class-wp-cli.php
Expand Up @@ -83,6 +83,36 @@ private static function get_cache() {
return $cache;
}

/**
* Set the context in which WP-CLI should be run
*/
static function set_url( $url ) {
$url_parts = Utils\parse_url( $url );
self::set_url_params( $url_parts );
}

private static function set_url_params( $url_parts ) {
$f = function( $key ) use ( $url_parts ) {
return isset( $url_parts[ $key ] ) ? $url_parts[ $key ] : '';
};

if ( isset( $url_parts['host'] ) ) {
$_SERVER['HTTP_HOST'] = $url_parts['host'];
if ( isset( $url_parts['port'] ) ) {
$_SERVER['HTTP_HOST'] .= ':' . $url_parts['port'];
}

$_SERVER['SERVER_NAME'] = $url_parts['host'];
}

$_SERVER['REQUEST_URI'] = $f('path') . ( isset( $url_parts['query'] ) ? '?' . $url_parts['query'] : '' );
$_SERVER['SERVER_PORT'] = isset( $url_parts['port'] ) ? $url_parts['port'] : '80';
$_SERVER['QUERY_STRING'] = $f('query');
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
$_SERVER['HTTP_USER_AGENT'] = '';
$_SERVER['REQUEST_METHOD'] = 'GET';
}

/**
* @return WpHttpCacheManager
*/
Expand Down
5 changes: 5 additions & 0 deletions php/commands/core.php
Expand Up @@ -444,6 +444,11 @@ private function _install( $assoc_args ) {
'admin_password' => ''
) ), EXTR_SKIP );

// Support prompting for the `--url=<url>`,
// which is normally a runtime argument
if ( isset( $assoc_args['url'] ) )
$url_parts = WP_CLI::set_url( $assoc_args['url'] );

$public = true;

// @codingStandardsIgnoreStart
Expand Down
10 changes: 10 additions & 0 deletions php/utils.php
Expand Up @@ -358,3 +358,13 @@ function make_progress_bar( $message, $count ) {
return new \cli\progress\Bar( $message, $count );
}

function parse_url( $url ) {
$url_parts = \parse_url( $url );

if ( !isset( $url_parts['scheme'] ) ) {
$url_parts = parse_url( 'http://' . $url );
}

return $url_parts;
}