public function download($args, $assoc_args)
{
$download_dir = !empty($assoc_args['path']) ? $assoc_args['path'] : ABSPATH;
$wordpress_present = is_readable($download_dir . 'wp-load.php');
if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'force') && $wordpress_present) {
WP_CLI::error('WordPress files seem to already be present here.');
}
if (!is_dir($download_dir)) {
if (!is_writable(dirname($download_dir))) {
WP_CLI::error(sprintf("Insufficient permission to create directory '%s'.", $download_dir));
}
WP_CLI::log(sprintf("Creating directory '%s'.", $download_dir));
$mkdir = \WP_CLI\Utils\is_windows() ? 'mkdir %s' : 'mkdir -p %s';
WP_CLI::launch(Utils\esc_cmd($mkdir, $download_dir));
}
if (!is_writable($download_dir)) {
WP_CLI::error(sprintf("'%s' is not writable by current user.", $download_dir));
}
$locale = \WP_CLI\Utils\get_flag_value($assoc_args, 'locale', 'en_US');
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
$ext = 'nightly' === $version ? 'zip' : 'tar.gz';
$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']);
}
if ('nightly' === $version && 'en_US' !== $locale) {
WP_CLI::error('Nightly builds are only available for the en_US locale.');
}
$from_version = '';
if (file_exists($download_dir . 'wp-includes/version.php')) {
global $wp_version;
require_once $download_dir . 'wp-includes/version.php';
$from_version = $wp_version;
}
WP_CLI::log(sprintf('Downloading WordPress %s (%s)...', $version, $locale));
$path_parts = pathinfo($download_url);
$extension = 'tar.gz';
if ('zip' === $path_parts['extension']) {
$extension = 'zip';
if (!class_exists('ZipArchive')) {
WP_CLI::error('Extracting a zip file requires ZipArchive.');
}
}
$cache = WP_CLI::get_cache();
$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}'...");
try {
Extractor::extract($cache_file, $download_dir);
} catch (Exception $e) {
WP_CLI::warning("Extraction failed, downloading a new copy...");
$bad_cache = true;
}
}
if (!$cache_file || $bad_cache) {
// 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}";
$headers = array('Accept' => 'application/json');
$options = array('timeout' => 600, 'filename' => $temp);
$response = Utils\http_request('GET', $download_url, null, $headers, $options);
if (404 == $response->status_code) {
WP_CLI::error("Release not found. Double-check locale or version.");
} else {
if (20 != substr($response->status_code, 0, 2)) {
WP_CLI::error("Couldn't access download URL (HTTP code {$response->status_code}).");
}
}
if ('nightly' !== $version) {
$md5_response = Utils\http_request('GET', $download_url . '.md5');
if (20 != substr($md5_response->status_code, 0, 2)) {
WP_CLI::error("Couldn't access md5 hash for release (HTTP code {$response->status_code}).");
}
$md5_file = md5_file($temp);
if ($md5_file === $md5_response->body) {
WP_CLI::log('md5 hash verified: ' . $md5_file);
} else {
WP_CLI::error("md5 hash for download ({$md5_file}) is different than the release hash ({$md5_response->body}).");
}
} else {
WP_CLI::warning('md5 hash checks are not available for nightly downloads.');
}
try {
Extractor::extract($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) {
$this->cleanup_extra_files($from_version, $version, $locale);
}
WP_CLI::success('WordPress downloaded.');
}