Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions php/commands/src/CLI_Command.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Composer\Semver\Comparator;
use WP_CLI\Formatter;
use WP_CLI\Process;
use WP_CLI\Utils;

/**
Expand Down Expand Up @@ -32,11 +34,11 @@
class CLI_Command extends WP_CLI_Command {

private function command_to_array( $command ) {
$dump = array(
$dump = [
'name' => $command->get_name(),
'description' => $command->get_shortdesc(),
'longdesc' => $command->get_longdesc(),
);
];

foreach ( $command->get_subcommands() as $subcommand ) {
$dump['subcommands'][] = $this->command_to_array( $subcommand );
Expand Down Expand Up @@ -107,8 +109,6 @@ public function version() {
* WP-CLI version: 1.5.0
*/
public function info( $_, $assoc_args ) {
$php_bin = Utils\get_php_binary();

// php_uname() $mode argument was only added with PHP 7.0+. Fall back to
// entire string for older versions.
$system_os = PHP_MAJOR_VERSION < 7
Expand All @@ -126,23 +126,33 @@ public function info( $_, $assoc_args ) {
$shell = getenv( 'ComSpec' );
}

$php_bin = Utils\get_php_binary();

$runner = WP_CLI::get_runner();

$packages_dir = $runner->get_packages_dir_path();
if ( ! is_dir( $packages_dir ) ) {
$packages_dir = null;
}
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) {
$info = array(

if ( Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) {
$info = [
'system_os' => $system_os,
'shell' => $shell,
'php_binary_path' => $php_bin,
'global_config_path' => $runner->global_config_path,
'project_config_path' => $runner->project_config_path,
'php_version' => PHP_VERSION,
'php_ini_used' => get_cfg_var( 'cfg_file_path' ),
'mysql_binary_path' => Utils\get_mysql_binary_path(),
'mysql_version' => Utils\get_mysql_version(),
'sql_modes' => Utils\get_sql_modes(),
'wp_cli_dir_path' => WP_CLI_ROOT,
'wp_cli_vendor_path' => WP_CLI_VENDOR_DIR,
'wp_cli_phar_path' => defined( 'WP_CLI_PHAR_PATH' ) ? WP_CLI_PHAR_PATH : '',
'wp_cli_packages_dir_path' => $packages_dir,
'global_config_path' => $runner->global_config_path,
'project_config_path' => $runner->project_config_path,
'wp_cli_version' => WP_CLI_VERSION,
'system_os' => $system_os,
'shell' => $shell,
);
];

WP_CLI::line( json_encode( $info ) );
} else {
Expand All @@ -151,6 +161,9 @@ public function info( $_, $assoc_args ) {
WP_CLI::line( "PHP binary:\t" . $php_bin );
WP_CLI::line( "PHP version:\t" . PHP_VERSION );
WP_CLI::line( "php.ini used:\t" . get_cfg_var( 'cfg_file_path' ) );
WP_CLI::line( "MySQL binary:\t" . Utils\get_mysql_binary_path() );
WP_CLI::line( "MySQL version:\t" . Utils\get_mysql_version() );
WP_CLI::line( "SQL modes:\t" . implode( ',', Utils\get_sql_modes() ) );
WP_CLI::line( "WP-CLI root dir:\t" . WP_CLI_ROOT );
WP_CLI::line( "WP-CLI vendor dir:\t" . WP_CLI_VENDOR_DIR );
WP_CLI::line( "WP_CLI phar path:\t" . ( defined( 'WP_CLI_PHAR_PATH' ) ? WP_CLI_PHAR_PATH : '' ) );
Expand Down Expand Up @@ -216,9 +229,9 @@ public function check_update( $_, $assoc_args ) {
$updates = $this->get_updates( $assoc_args );

if ( $updates ) {
$formatter = new \WP_CLI\Formatter(
$formatter = new Formatter(
$assoc_args,
array( 'version', 'update_type', 'package_url' )
[ 'version', 'update_type', 'package_url' ]
);
$formatter->display_items( $updates );
} elseif ( empty( $assoc_args['format'] ) || 'table' === $assoc_args['format'] ) {
Expand Down Expand Up @@ -312,13 +325,13 @@ public function update( $_, $assoc_args ) {

WP_CLI::log( sprintf( 'Downloading from %s...', $download_url ) );

$temp = \WP_CLI\Utils\get_temp_dir() . uniqid( 'wp_', true ) . '.phar';
$temp = Utils\get_temp_dir() . uniqid( 'wp_', true ) . '.phar';

$headers = array();
$options = array(
$headers = [];
$options = [
'timeout' => 600, // 10 minutes ought to be enough for everybody.
'filename' => $temp,
);
];

Utils\http_request( 'GET', $download_url, null, $headers, $options );

Expand All @@ -336,7 +349,7 @@ public function update( $_, $assoc_args ) {

$allow_root = WP_CLI::get_runner()->config['allow-root'] ? '--allow-root' : '';
$php_binary = Utils\get_php_binary();
$process = WP_CLI\Process::create( "{$php_binary} $temp --info {$allow_root}" );
$process = Process::create( "{$php_binary} $temp --info {$allow_root}" );
$result = $process->run();
if ( 0 !== $result->return_code || false === stripos( $result->stdout, 'WP-CLI version' ) ) {
$multi_line = explode( PHP_EOL, $result->stderr );
Expand Down Expand Up @@ -374,13 +387,13 @@ class_exists( '\cli\Colors' ); // This autoloads \cli\Colors - after we move the
private function get_updates( $assoc_args ) {
$url = 'https://api.github.com/repos/wp-cli/wp-cli/releases?per_page=100';

$options = array(
$options = [
'timeout' => 30,
);
];

$headers = array(
$headers = [
'Accept' => 'application/json',
);
];

$github_token = getenv( 'GITHUB_TOKEN' );
if ( false !== $github_token ) {
Expand All @@ -395,11 +408,11 @@ private function get_updates( $assoc_args ) {

$release_data = json_decode( $response->body );

$updates = array(
$updates = [
'major' => false,
'minor' => false,
'patch' => false,
);
];
foreach ( $release_data as $release ) {

// Get rid of leading "v" if there is one set.
Expand All @@ -421,11 +434,11 @@ private function get_updates( $assoc_args ) {
continue;
}

$updates[ $update_type ] = array(
$updates[ $update_type ] = [
'version' => $release_version,
'update_type' => $update_type,
'package_url' => $release->assets[0]->browser_download_url,
);
];
}

foreach ( $updates as $type => $value ) {
Expand All @@ -434,9 +447,9 @@ private function get_updates( $assoc_args ) {
}
}

foreach ( array( 'major', 'minor', 'patch' ) as $type ) {
foreach ( [ 'major', 'minor', 'patch' ] as $type ) {
if ( true === \WP_CLI\Utils\get_flag_value( $assoc_args, $type ) ) {
return ! empty( $updates[ $type ] ) ? array( $updates[ $type ] ) : false;
return ! empty( $updates[ $type ] ) ? [ $updates[ $type ] ] : false;
}
}

Expand All @@ -448,11 +461,11 @@ private function get_updates( $assoc_args ) {
}
$nightly_version = trim( $response->body );
if ( WP_CLI_VERSION !== $nightly_version ) {
$updates['nightly'] = array(
$updates['nightly'] = [
'version' => $nightly_version,
'update_type' => 'nightly',
'package_url' => 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.phar',
);
];
}
}

Expand Down Expand Up @@ -561,7 +574,7 @@ public function completions( $_, $assoc_args ) {
*/
private function get_update_type_str( $assoc_args ) {
$update_type = ' ';
foreach ( array( 'major', 'minor', 'patch' ) as $type ) {
foreach ( [ 'major', 'minor', 'patch' ] as $type ) {
if ( true === \WP_CLI\Utils\get_flag_value( $assoc_args, $type ) ) {
$update_type = ' ' . $type . ' ';
break;
Expand Down
71 changes: 71 additions & 0 deletions php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use WP_CLI\Formatter;
use WP_CLI\Inflector;
use WP_CLI\Iterators\Transform;
use WP_CLI\Process;

const PHAR_STREAM_PREFIX = 'phar://';

Expand Down Expand Up @@ -1603,3 +1604,73 @@ function pluralize( $noun, $count = null ) {

return Inflector::pluralize( $noun );
}

/**
* Get the path to the mysql binary.
*
* @return string Path to the mysql binary, or an empty string if not found.
*/
function get_mysql_binary_path() {
static $path = null;

if ( null === $path ) {
$result = Process::create( '/usr/bin/env which mysql', null, null )->run();

if ( 0 !== $result->return_code ) {
$path = '';
} else {
$path = trim( $result->stdout );
}
}

return $path;
}

/**
* Get the version of the MySQL database.
*
* @return string Version of the MySQL database, or an empty string if not
* found.
*/
function get_mysql_version() {
static $version = null;

if ( null === $version ) {
$result = Process::create( '/usr/bin/env mysql --version', null, null )->run();

if ( 0 !== $result->return_code ) {
$version = '';
} else {
$version = trim( $result->stdout );
}
}

return $version;
}

/**
* Get the SQL modes of the MySQL session.
*
* @return string[] Array of SQL modes, or an empty array if they couldn't be
* read.
*/
function get_sql_modes() {
static $sql_modes = null;

if ( null === $sql_modes ) {
$result = Process::create( '/usr/bin/env mysql --no-auto-rehash --batch --skip-column-names --execute="SELECT @@SESSION.sql_mode"', null, null )->run();

if ( 0 !== $result->return_code ) {
$sql_modes = [];
} else {
$sql_modes = array_filter(
array_map(
'trim',
preg_split( "/\r\n|\n|\r/", $result->stdout )
)
);
}
}

return $sql_modes;
}