OSDN Git Service

Refactor MySQL utilities:
authorscribu <mail@scribu.net>
Fri, 16 Aug 2013 20:24:15 +0000 (23:24 +0300)
committerscribu <mail@scribu.net>
Fri, 16 Aug 2013 20:24:15 +0000 (23:24 +0300)
* extract mysql_host_to_cli_args() helper
* make run_mysql_command() accept an associative array of args
* remove redundant run_mysql_query() utility

features/bootstrap/FeatureContext.php
php/commands/core.php
php/commands/db.php
php/utils.php

index 4c5def0..fc3a15c 100644 (file)
@@ -113,7 +113,8 @@ class FeatureContext extends BehatContext implements ClosuredContextInterface {
        }
 
        private static function run_sql( $sql ) {
-               Utils\run_mysql_query( $sql, array(
+               Utils\run_mysql_command( 'mysql --no-defaults', array(
+                       'execute' => $sql,
                        'host' => 'localhost',
                        'user' => self::$db_settings['dbuser'],
                        'pass' => self::$db_settings['dbpass'],
index 81762a6..5dc62c9 100644 (file)
@@ -184,7 +184,8 @@ class Core_Command extends WP_CLI_Command {
                        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'],
@@ -699,7 +700,8 @@ define('BLOG_ID_CURRENT_SITE', 1);
                // 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'],
index e126ec8..0f2be91 100644 (file)
@@ -63,9 +63,8 @@ class DB_Command extends WP_CLI_Command {
         * 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." );
@@ -75,9 +74,9 @@ class DB_Command extends WP_CLI_Command {
         * 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." );
        }
@@ -88,9 +87,9 @@ class DB_Command extends WP_CLI_Command {
         * @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
+               ) );
        }
 
        /**
@@ -99,14 +98,16 @@ class DB_Command extends WP_CLI_Command {
         * @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 );
        }
 
        /**
@@ -119,29 +120,9 @@ class DB_Command extends WP_CLI_Command {
        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 ) );
        }
@@ -153,10 +134,19 @@ class DB_Command extends WP_CLI_Command {
         */
        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 ) );
        }
@@ -169,15 +159,17 @@ class DB_Command extends WP_CLI_Command {
        }
 
        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 );
        }
 }
 
index b3e02b3..5606b02 100644 (file)
@@ -370,38 +370,50 @@ function launch_editor_for_input( $input, $title = 'WP-CLI' ) {
        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 );
 }