WP_CLI::error( '--dbprefix can only contain numbers, letters, and underscores.' );
// Check DB connection
- Utils\run_mysql_query( ';', array(
+ Utils\run_mysql_command( 'mysql --no-defaults', array(
+ 'execute' => ';',
'host' => $assoc_args['dbhost'],
'user' => $assoc_args['dbuser'],
'pass' => $assoc_args['dbpass'],
// Create the database
$query = sprintf( 'CREATE DATABASE IF NOT EXISTS `%s`', $assoc_args['dbname'] );
- Utils\run_mysql_query( $query, array(
+ Utils\run_mysql_command( 'mysql --no-defaults', array(
+ 'execute' => $query,
'host' => $assoc_args['dbhost'],
'user' => $assoc_args['dbuser'],
'pass' => $assoc_args['dbpass'],
* Optimize the database.
*/
function optimize() {
- self::run( 'mysqlcheck', Utils\esc_cmd(
- '--optimize --host=%s --user=%s %s',
- DB_HOST, DB_USER, DB_NAME
+ self::run( Utils\esc_cmd( 'mysqlcheck %s', DB_NAME ), array(
+ 'optimize' => true,
) );
WP_CLI::success( "Database optimized." );
* Repair the database.
*/
function repair() {
- self::run( 'mysqlcheck', Utils\esc_cmd(
- '--repair --host=%s --user=%s %s',
- DB_HOST, DB_USER, DB_NAME ) );
+ self::run( Utils\esc_cmd( 'mysqlcheck %s', DB_NAME ), array(
+ 'repair' => true,
+ ) );
WP_CLI::success( "Database repaired." );
}
* @alias connect
*/
function cli() {
- self::run( 'mysql', Utils\esc_cmd(
- '--host=%s --user=%s --database=%s',
- DB_HOST, DB_USER, DB_NAME ) );
+ self::run( 'mysql --no-defaults', array(
+ 'database' => DB_NAME
+ ) );
}
/**
* @synopsis [<sql>]
*/
function query( $args ) {
- $cmd = '--host=%s --user=%s --database=%s';
- $cmd = Utils\esc_cmd( $cmd, DB_HOST, DB_USER, DB_NAME );
+ $assoc_args = array(
+ 'database' => DB_NAME
+ );
+ // The query might come from STDIN
if ( !empty( $args ) ) {
- $cmd .= Utils\esc_cmd( ' --execute=%s', $args[0] );
+ $assoc_args['execute'] = $args[0];
}
- self::run( 'mysql', $cmd );
+ self::run( 'mysql --no-defaults', $assoc_args );
}
/**
function export( $args, $assoc_args ) {
$result_file = $this->get_file_name( $args );
- $host_parts = explode( ':', DB_HOST );
- if ( count( $host_parts ) == 2 ) {
- list( $host, $extra ) = $host_parts;
- } else {
- $host = DB_HOST;
- }
-
- $arg_str = '';
-
- if ( isset( $extra ) ) {
- if ( is_numeric($extra) ) {
- $arg_str .= Utils\esc_cmd(
- '--port=%s --protocol=%s', intval( $extra ), 'tcp' );
- } else if ( trim($extra) !== '' ) {
- $arg_str .= Utils\esc_cmd(
- '--socket=%s', trim( $extra ) );
- }
- }
-
- $arg_str .= Utils\esc_cmd( ' --host=%s --user=%s --result-file=%s %s',
- $host, DB_USER, $result_file, DB_NAME );
-
- self::run( 'mysqldump', $arg_str );
+ self::run( Utils\esc_cmd( 'mysqldump %s', DB_NAME ), array(
+ 'result-file' => $result_file
+ ) );
WP_CLI::success( sprintf( 'Exported to %s', $result_file ) );
}
*/
function import( $args, $assoc_args ) {
$result_file = $this->get_file_name( $args );
+ if ( !file_exists( $result_file ) ) {
+ WP_CLI::error( sprintf( 'Import file missing: %s', $result_file ) );
+ }
+
+ $descriptors = array(
+ array( 'file', $result_file, 'r' ),
+ STDOUT,
+ STDERR,
+ );
- self::run( 'mysql', Utils\esc_cmd(
- '%s --user=%s --host=%s < %s',
- DB_NAME, DB_USER, DB_HOST, $result_file ) );
+ self::run( 'mysql --no-defaults', array(
+ 'database' => DB_NAME
+ ), $descriptors );
WP_CLI::success( sprintf( 'Imported from %s', $result_file ) );
}
}
private static function run_query( $query ) {
- Utils\run_mysql_query( $query, array(
+ self::run( 'mysql --no-defaults', array( 'execute' => $query ) );
+ }
+
+ private static function run( $cmd, $assoc_args = array(), $descriptors = null ) {
+ $final_args = array_merge( $assoc_args, array(
'host' => DB_HOST,
'user' => DB_USER,
'pass' => DB_PASSWORD,
) );
- }
- private static function run( $cmd, $args ) {
- Utils\run_mysql_command( $cmd, $args, DB_PASSWORD );
+ Utils\run_mysql_command( $cmd, $final_args, $descriptors );
}
}
return $output;
}
-function run_mysql_query( $query, $args ) {
- // TODO: use PDO?
-
- $host_parts = explode( ':', $args['host'] );
- if ( count( $host_parts ) == 2 ) {
- list( $host, $extra ) = $host_parts;
- } else {
- $host = $args['host'];
- }
-
- $arg_str = esc_cmd( '--host=%s --user=%s --execute=%s',
- $host, $args['user'], $query );
-
- if ( isset( $extra ) ) {
- if ( is_numeric($extra) ) {
- $arg_str .= esc_cmd( ' --port=%s --protocol=%s', intval( $extra ), 'tcp' );
- } else if ( trim($extra) !== '' ) {
- $arg_str .= esc_cmd( ' --socket=%s', trim( $extra ) );
- }
- }
-
- run_mysql_command( 'mysql', $arg_str, $args['pass'] );
+/**
+ * @param string MySQL host string, as defined in wp-config.php
+ * @return array
+ */
+function mysql_host_to_cli_args( $raw_host ) {
+ $assoc_args = array();
+
+ $host_parts = explode( ':', $raw_host );
+ if ( count( $host_parts ) == 2 ) {
+ list( $assoc_args['host'], $extra ) = $host_parts;
+ if ( is_numeric($extra) ) {
+ $assoc_args['port'] = intval( $extra );
+ $assoc_args['protocol'] = 'tcp';
+ } else if ( trim($extra) !== '' ) {
+ $assoc_args['socket'] = trim( $extra );
+ }
+ } else {
+ $assoc_args['host'] = $raw_host;
+ }
+
+ return $assoc_args;
}
-function run_mysql_command( $cmd, $arg_str, $pass ) {
- $final_cmd = "$cmd --no-defaults $arg_str";
+function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) {
+ if ( !$descriptors )
+ $descriptors = array( STDIN, STDOUT, STDERR );
+
+ if ( isset( $assoc_args['host'] ) ) {
+ $assoc_args = array_merge( $assoc_args, mysql_host_to_cli_args( $assoc_args['host'] ) );
+ }
+
+ $env = array();
+ if ( isset( $assoc_args['pass'] ) ) {
+ $env['MYSQL_PWD'] = $assoc_args['pass'];
+ unset( $assoc_args['pass'] );
+ }
+
+ $final_cmd = $cmd . assoc_args_to_str( $assoc_args );
- $descriptors = array( STDIN, STDOUT, STDERR );
+ $proc = proc_open( $final_cmd, $descriptors, $pipes, null, $env );
+ if ( !$proc )
+ exit(1);
- $r = proc_close( proc_open( $final_cmd, $descriptors, $pipes, null, array(
- 'MYSQL_PWD' => $pass
- ) ) );
+ $r = proc_close( $proc );
if ( $r ) exit( $r );
}