Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix #377 #379

Merged
merged 3 commits into from

1 participant

This page is out of date. Refresh to see the latest.
View
21 features/bootstrap/FeatureContext.php
@@ -5,6 +5,8 @@
Behat\Behat\Context\BehatContext,
Behat\Behat\Event\SuiteEvent;
+use \WP_CLI\Utils;
+
require_once 'PHPUnit/Framework/Assert/Functions.php';
require_once __DIR__ . '/../../php/utils.php';
@@ -92,26 +94,27 @@ public function get_cache_path( $file ) {
if ( !$path ) {
$path = sys_get_temp_dir() . '/wp-cli-test-cache';
- system( \WP_CLI\Utils\create_cmd( 'mkdir -p %s', $path ) );
+ system( Utils\create_cmd( 'mkdir -p %s', $path ) );
}
return $path . '/' . $file;
}
public function download_file( $url, $path ) {
- system( \WP_CLI\Utils\create_cmd( 'curl -sSL %s > %s', $url, $path ) );
+ system( Utils\create_cmd( 'curl -sSL %s > %s', $url, $path ) );
}
private static function run_sql( $sql ) {
- putenv( 'MYSQL_PWD=' . self::$db_settings['dbpass'] );
-
- system( \WP_CLI\Utils\create_cmd( 'mysql -u%s -e %s',
- self::$db_settings['dbuser'], $sql ) );
+ Utils\run_mysql_query( $sql, array(
+ 'host' => 'localhost',
+ 'user' => self::$db_settings['dbuser'],
+ 'pass' => self::$db_settings['dbpass'],
+ ) );
}
public function create_db() {
$dbname = self::$db_settings['dbname'];
- self::run_sql( "CREATE DATABASE $dbname" );
+ self::run_sql( "CREATE DATABASE IF NOT EXISTS $dbname" );
}
public function drop_db() {
@@ -121,7 +124,7 @@ public function drop_db() {
private function _run( $command, $assoc_args, $subdir = '' ) {
if ( !empty( $assoc_args ) )
- $command .= \WP_CLI\Utils\assoc_args_to_str( $assoc_args );
+ $command .= Utils\assoc_args_to_str( $assoc_args );
$subdir = $this->get_path( $subdir );
@@ -184,7 +187,7 @@ public function download_wordpress_files( $subdir = '' ) {
if ( $subdir ) mkdir( $dest_dir );
- $cmd = \WP_CLI\Utils\create_cmd( "cp -r %s/* %s", $cache_dir, $dest_dir );
+ $cmd = Utils\create_cmd( "cp -r %s/* %s", $cache_dir, $dest_dir );
system( $cmd );
}
View
33 php/commands/db.php
@@ -1,5 +1,7 @@
<?php
+use \WP_CLI\Utils;
+
/**
* Perform basic database operations.
*
@@ -47,8 +49,8 @@ function reset( $_, $assoc_args ) {
* Optimize the database.
*/
function optimize() {
- self::run( \WP_CLI\Utils\create_cmd(
- 'mysqlcheck --optimize --host=%s --user=%s %s',
+ self::run( 'mysqlcheck', Utils\create_cmd(
+ '--optimize --host=%s --user=%s %s',
DB_HOST, DB_USER, DB_NAME
) );
@@ -59,8 +61,8 @@ function optimize() {
* Repair the database.
*/
function repair() {
- self::run( \WP_CLI\Utils\create_cmd(
- 'mysqlcheck --repair --host=%s --user=%s %s',
+ self::run( 'mysqlcheck', Utils\create_cmd(
+ '--repair --host=%s --user=%s %s',
DB_HOST, DB_USER, DB_NAME ) );
WP_CLI::success( "Database repaired." );
@@ -72,8 +74,8 @@ function repair() {
* @alias cli
*/
function connect() {
- self::run( \WP_CLI\Utils\create_cmd(
- 'mysql --host=%s --user=%s --database=%s',
+ self::run( 'mysql', Utils\create_cmd(
+ '--host=%s --user=%s --database=%s',
DB_HOST, DB_USER, DB_NAME ) );
}
@@ -98,8 +100,8 @@ function query( $args ) {
function export( $args, $assoc_args ) {
$result_file = $this->get_file_name( $args );
- self::run( \WP_CLI\Utils\create_cmd(
- 'mysqldump %s --user=%s --host=%s --result-file %s',
+ self::run( 'mysqldump', Utils\create_cmd(
+ '%s --user=%s --host=%s --result-file %s',
DB_NAME, DB_USER, DB_HOST, $result_file ) );
WP_CLI::success( sprintf( 'Exported to %s', $result_file ) );
@@ -113,8 +115,8 @@ function export( $args, $assoc_args ) {
function import( $args, $assoc_args ) {
$result_file = $this->get_file_name( $args );
- self::run( \WP_CLI\Utils\create_cmd(
- 'mysql %s --user=%s --host=%s < %s',
+ self::run( 'mysql', Utils\create_cmd(
+ '%s --user=%s --host=%s < %s',
DB_NAME, DB_USER, DB_HOST, $result_file ) );
WP_CLI::success( sprintf( 'Imported from %s', $result_file ) );
@@ -128,20 +130,17 @@ private function get_file_name( $args ) {
}
private static function run_query( $query ) {
- return \WP_CLI\Utils\run_mysql_query( $query, array(
+ Utils\run_mysql_query( $query, array(
'host' => DB_HOST,
'user' => DB_USER,
'pass' => DB_PASSWORD,
) );
}
- private static function run( $cmd ) {
- $old_val = getenv( 'MYSQL_PWD' );
-
- putenv( 'MYSQL_PWD=' . DB_PASSWORD );
- WP_CLI::launch( $cmd );
- putenv( 'MYSQL_PWD=' . $old_val );
+ private static function run( $cmd, $args ) {
+ Utils\run_mysql_command( $cmd, $args, DB_PASSWORD );
}
}
WP_CLI::add_command( 'db', 'DB_Command' );
+
View
19 php/utils.php
@@ -362,17 +362,24 @@ function find_subcommand( $args ) {
return $command;
}
-function run_mysql_query( $query, $args, $dry_run = false ) {
+function run_mysql_query( $query, $args ) {
// TODO: use PDO?
- $cmd = \WP_CLI\Utils\create_cmd( 'mysql --host=%s --user=%s --execute=%s',
- $args['host'], $args['user'], $query
- );
+ $arg_str = create_cmd( '--host=%s --user=%s --execute=%s',
+ $args['host'], $args['user'], $query );
+
+ run_mysql_command( 'mysql', $arg_str, $args['pass'] );
+}
+function run_mysql_command( $cmd, $arg_str, $pass ) {
$old_val = getenv( 'MYSQL_PWD' );
- putenv( 'MYSQL_PWD=' . $args['pass'] );
- \WP_CLI::launch( $cmd );
+ $final_cmd = "$cmd --defaults-file=/dev/null $arg_str";
+
+ putenv( 'MYSQL_PWD=' . $pass );
+ $r = proc_close( proc_open( $final_cmd, array( STDIN, STDOUT, STDERR ), $pipes ) );
putenv( 'MYSQL_PWD=' . $old_val );
+
+ if ( $r ) exit( $r );
}
Something went wrong with that request. Please try again.