Skip to content

Commit

Permalink
Merge pull request #59 from wp-cli/57-strip-wp-content
Browse files Browse the repository at this point in the history
Strip `wp-content/` using `ZipArchive` to allow `--skip-content` always
  • Loading branch information
danielbachhuber authored Dec 11, 2017
2 parents 661a58c + 9ef518a commit 2ef05ac
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Subsequent uses of command will use the local cache if it still exists.
Select which version you want to download. Accepts a version number, 'latest' or 'nightly'

[--skip-content]
Download the latest version of WP without the default themes and plugins (en_US locale only)
Download WP without the default themes and plugins.

[--force]
Overwrites existing files, if present.
Expand Down
20 changes: 11 additions & 9 deletions features/core-download.feature
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,24 @@ Feature: Download WordPress
And the wp-includes directory should exist
And the wp-content directory should not exist

Scenario: Core download without the wp-content dir should error for non US locale
Scenario: Core download without the wp-content dir should work non US locale
Given an empty directory

When I try `wp core download --skip-content --locale=nl_NL`
Then STDERR should be:
When I run `wp core download --skip-content --locale=nl_NL`
Then STDOUT should contain:
"""
Error: Skip content build is only available for the en_US locale.
Success: WordPress downloaded.
"""
And the return code should be 1
And the wp-includes directory should exist
And the wp-content directory should not exist

Scenario: Core download without the wp-content dir should error if a version is set
Scenario: Core download without the wp-content dir should work if a version is set
Given an empty directory

When I try `wp core download --skip-content --version=4.7`
Then STDERR should contain:
Then STDOUT should contain:
"""
Skip content build is only available for the latest version.
Success: WordPress downloaded.
"""
And the return code should be 1
And the wp-includes directory should exist
And the wp-content directory should not exist
78 changes: 52 additions & 26 deletions src/Core_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function check_update( $_, $assoc_args ) {
* : Select which version you want to download. Accepts a version number, 'latest' or 'nightly'
*
* [--skip-content]
* : Download the latest version of WP without the default themes and plugins (en_US locale only)
* : Download WP without the default themes and plugins.
*
* [--force]
* : Overwrites existing files, if present.
Expand Down Expand Up @@ -145,38 +145,26 @@ public function download( $args, $assoc_args ) {
}

$locale = \WP_CLI\Utils\get_flag_value( $assoc_args, 'locale', 'en_US' );
$skip_content = \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-content' );

if ( true === \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-content' ) && 'en_US' !== $locale ) {
WP_CLI::error( 'Skip content build is only available for the en_US locale.' );
}

if ( true === \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-content' ) && isset( $assoc_args['version'] ) ) {
WP_CLI::error( 'Skip content build is only available for the latest version.' );
}

$no_content = '';
if ( true === \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-content' ) ) {
$response = \WP_CLI\Utils\http_request( 'GET', 'https://api.wordpress.org/core/version-check/1.7/' );
if ( 200 === $response->status_code && ( $body = json_decode( $response->body ) ) && is_object( $body ) && isset( $body->offers[0]->packages->no_content ) && is_array( $body->offers ) ) {
$download_url = $body->offers[0]->packages->no_content;
$version = $body->offers[0]->version;
$no_content = 'no-content-';
} else {
WP_CLI::error( 'Skip content build is not available.' );
}
} elseif ( isset( $assoc_args['version'] ) && 'latest' !== $assoc_args['version'] ) {
if ( isset( $assoc_args['version'] ) && 'latest' !== $assoc_args['version'] ) {
$version = $assoc_args['version'];
$version = ( in_array( strtolower( $version ), array( 'trunk', 'nightly' ) ) ? 'nightly' : $version );
//nightly builds are only available in .zip format
// nightly builds are only available in .zip format
$ext = ( 'nightly' === $version ? 'zip' : 'tar.gz' );
// Skip content requires ZIP.
$ext = ( $skip_content ) ? 'zip' : $ext;
$download_url = $this->get_download_url( $version, $locale, $ext );
} else {
$offer = $this->get_download_offer( $locale );
if ( !$offer ) {
WP_CLI::error( "The requested locale ($locale) was not found." );
}
$version = $offer['current'];
$download_url = str_replace( '.zip', '.tar.gz', $offer['download'] );
$download_url = $offer['download'];
if ( ! $skip_content ) {
$download_url = str_replace( '.zip', '.tar.gz', $download_url );
}
}

if ( 'nightly' === $version && 'en_US' !== $locale ) {
Expand All @@ -200,15 +188,20 @@ public function download( $args, $assoc_args ) {
}
}

if ( $skip_content && 'zip' !== $extension ) {
WP_CLI::error( 'Skip content is only available for ZIP files.' );
}

$cache = WP_CLI::get_cache();
$cache_key = "core/wordpress-{$version}-{$no_content}{$locale}.{$extension}";
$cache_key = "core/wordpress-{$version}-{$locale}.{$extension}";
$cache_file = $cache->has($cache_key);

$bad_cache = false;
if ( $cache_file ) {
WP_CLI::log( "Using cached file '$cache_file'..." );
$skip_content_cache_file = $skip_content ? self::strip_content_dir( $cache_file ) : null;
try{
Extractor::extract( $cache_file, $download_dir );
Extractor::extract( $skip_content_cache_file ? $skip_content_cache_file : $cache_file, $download_dir );
} catch ( Exception $e ) {
WP_CLI::warning( "Extraction failed, downloading a new copy..." );
$bad_cache = true;
Expand All @@ -219,6 +212,11 @@ public function download( $args, $assoc_args ) {
// We need to use a temporary file because piping from cURL to tar is flaky
// on MinGW (and probably in other environments too).
$temp = \WP_CLI\Utils\get_temp_dir() . uniqid('wp_') . ".{$extension}";
register_shutdown_function( function () use ( $temp ) {
if ( file_exists( $temp ) ) {
unlink( $temp );
}
} );

$headers = array('Accept' => 'application/json');
$options = array(
Expand Down Expand Up @@ -250,16 +248,17 @@ public function download( $args, $assoc_args ) {
WP_CLI::warning( 'md5 hash checks are not available for nightly downloads.' );
}

$skip_content_temp = $skip_content ? self::strip_content_dir( $temp ): null;

try {
Extractor::extract( $temp, $download_dir );
Extractor::extract( $skip_content_temp ? $skip_content_temp : $temp, $download_dir );
} catch ( Exception $e ) {
WP_CLI::error( "Couldn't extract WordPress archive. " . $e->getMessage() );
}

if ( 'nightly' !== $version ) {
$cache->import( $cache_key, $temp );
}
unlink( $temp );
}

if ( $wordpress_present ) {
Expand Down Expand Up @@ -1312,4 +1311,31 @@ private function cleanup_extra_files( $version_from, $version_to, $locale ) {
}
}

private static function strip_content_dir( $zip_file ) {
$new_zip_file = \WP_CLI\Utils\get_temp_dir() . uniqid( 'wp_' ) . '.zip';
register_shutdown_function( function () use ( $new_zip_file ) {
if ( file_exists( $new_zip_file ) ) {
unlink( $new_zip_file );
}
} );
// Duplicate file to avoid modifying the original, which could be cache.
if ( ! copy( $zip_file, $new_zip_file ) ) {
WP_CLI::error( 'Failed to copy ZIP file.' );
}
$zip = new ZipArchive();
$res = $zip->open( $new_zip_file );
if ( true === $res ) {
for ( $i = 0; $i < $zip->numFiles; $i++ ) {
$info = $zip->statIndex( $i );
if ( false !== stripos( $info['name'], 'wp-content/' ) ) {
$zip->deleteIndex( $i );
}
}
$zip->close();
return $new_zip_file;
} else {
WP_CLI::error( 'ZipArchive failed to open ZIP file.' );
}
}

}
5 changes: 5 additions & 0 deletions src/WP_CLI/CoreUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function download_package( $package ) {
$ext = pathinfo( $package, PATHINFO_EXTENSION );

$temp = \WP_CLI\Utils\get_temp_dir() . uniqid( 'wp_' ) . '.' . $ext;
register_shutdown_function( function () use ( $temp ) {
if ( file_exists( $temp ) ) {
unlink( $temp );
}
} );

$cache = WP_CLI::get_cache();
$update = $GLOBALS['wp_cli_update_obj'];
Expand Down

0 comments on commit 2ef05ac

Please sign in to comment.