Skip to content

Commit d5408f3

Browse files
committed
Add allowed mysql flag and behat test
1 parent 0cc54a7 commit d5408f3

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

features/db-import.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ Feature: Import a WordPress database
4444
"""
4545
And STDOUT should be empty
4646

47+
Scenario: Import database with passed-in options
48+
Given a WP install
49+
50+
Given a debug.sql file:
51+
"""
52+
INSERT INTO `wp_options` (`option_id`, `option_name`, `option_value`, `autoload`) VALUES (999, 'testoption', 'testval', 'yes'),(999, 'testoption', 'testval', 'yes');
53+
"""
54+
55+
When I try `wp db import debug.sql --force`
56+
Then STDOUT should be:
57+
"""
58+
Success: Imported from 'debug.sql'.
59+
"""
60+
4761
Scenario: Help runs properly at various points of a functional WP install
4862
Given an empty directory
4963

src/DB_Command.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,10 @@ public function import( $args, $assoc_args ) {
558558
$result_file = sprintf( '%s.sql', DB_NAME );
559559
}
560560

561-
$assoc_args['database'] = DB_NAME;
561+
$mysql_args = array(
562+
'database' => DB_NAME,
563+
);
564+
$mysql_args = array_merge( self::get_dbuser_dbpass_args( $assoc_args ), $mysql_args );
562565

563566
if ( '-' !== $result_file ) {
564567
if ( ! is_readable( $result_file ) ) {
@@ -569,14 +572,12 @@ public function import( $args, $assoc_args ) {
569572
? 'SOURCE %s;'
570573
: 'SET autocommit = 0; SET unique_checks = 0; SET foreign_key_checks = 0; SOURCE %s; COMMIT;';
571574

572-
if ( isset( $assoc_args['skip-optimization'] ) ) {
573-
unset( $assoc_args['skip-optimization'] );
574-
}
575-
576-
$assoc_args['execute'] = sprintf( $query, $result_file );
575+
$mysql_args['execute'] = sprintf( $query, $result_file );
577576
}
577+
// Check if any mysql option pass.
578+
$mysql_args = array_merge( $mysql_args, self::get_mysql_args( $assoc_args ) );
578579

579-
self::run( '/usr/bin/env mysql --no-defaults --no-auto-rehash', $assoc_args );
580+
self::run( '/usr/bin/env mysql --no-defaults --no-auto-rehash', $mysql_args );
580581

581582
WP_CLI::success( sprintf( "Imported from '%s'.", $result_file ) );
582583
}
@@ -1433,4 +1434,27 @@ private function get_colors( $assoc_args, $colors ) {
14331434

14341435
return $colors;
14351436
}
1437+
1438+
/**
1439+
* Helper to pluck `mysql` opitons from associative args array.
1440+
*
1441+
* @param array $assoc_args Associative args array.
1442+
* @return array Array with `mysql` opitons set if in passed-in associative args array.
1443+
*/
1444+
private static function get_mysql_args( $assoc_args ) {
1445+
1446+
$allowed_mysql_option = [ 'auto-rehash', 'auto-vertical-output', 'batch', 'binary-as-hex', 'binary-mode', 'bind-address', 'character-sets-dir', 'column-names', 'column-type-info', 'comments', 'compress', 'connect-expired-password', 'connect_timeout', 'database', 'debug', 'debug-check', 'debug-info', 'default-auth', 'default-character-set', 'defaults-extra-file', 'defaults-file', 'defaults-group-suffix', 'delimiter', 'enable-cleartext-plugin', 'execute', 'force', 'get-server-public-key', 'help', 'histignore', 'host', 'html', 'ignore-spaces', 'init-command', 'line-numbers', 'local-infile', 'login-path', 'max_allowed_packet', 'max_join_size', 'named-commands', 'net_buffer_length', 'no-beep', 'one-database', 'pager', 'pipe', 'plugin-dir', 'port', 'print-defaults', 'protocol', 'quick', 'raw', 'reconnect', 'i-am-a-dummy', 'safe-updates', 'secure-auth', 'select_limit', 'server-public-key-path', 'shared-memory-base-name', 'show-warnings', 'sigint-ignore', 'silent', 'skip-auto-rehash', 'skip-column-names', 'skip-line-numbers', 'skip-named-commands', 'skip-pager', 'skip-reconnect', 'socket', 'ssl-ca', 'ssl-capath', 'ssl-cert', 'ssl-cipher', 'ssl-crl', 'ssl-crlpath', 'ssl-fips-mode', 'ssl-key', 'ssl-mode', 'syslog', 'table', 'tee', 'tls-version', 'unbuffered', 'verbose', 'version', 'vertical', 'wait', 'xml' ];
1447+
1448+
$mysql_args = array();
1449+
1450+
foreach ( $assoc_args as $mysql_option_key => $mysql_option_value ) {
1451+
// check flags is valid flag or not.
1452+
if ( in_array( $mysql_option_key, $allowed_mysql_option, true ) && ! empty( $mysql_option_value ) ) {
1453+
$mysql_args[$mysql_option_key] = $mysql_option_value;
1454+
1455+
}
1456+
}
1457+
1458+
return $mysql_args;
1459+
}
14361460
}

0 commit comments

Comments
 (0)