diff --git a/.editorconfig b/.editorconfig index e09b844..011343d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,4 +9,10 @@ charset = utf-8 insert_final_newline = true trim_trailing_whitespace = true indent_style = space +indent_size = 4 + +[{*.md,*.json,*.sh,*.js,*.css}] indent_size = 2 + +[{*.md,*.json}] +max_line_length = null diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e675067 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +*.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ae426f5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: php +php: + - '5.6' + - nightly +install: composer install +script: composer test diff --git a/README.md b/README.md index 6618001..393bb01 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # WP Sync DB CLI -An addon for [WP Sync DB](https://github.com/slang800/wp-sync-db) that allows you to execute migrations using a function call or via WP-CLI + +An addon for [WP Sync DB](https://github.com/slang800/wp-sync-db) that allows you to execute migrations using a function call or via WP-CLI. diff --git a/class/command.php b/class/command.php index cee217e..c27c5a2 100644 --- a/class/command.php +++ b/class/command.php @@ -3,38 +3,40 @@ /** * Migrate your DB using WP Sync DB. */ -class WPSDBCLI extends WP_CLI_Command { +class WPSDBCLI extends WP_CLI_Command +{ - /** - * Run a migration. - * - * ## OPTIONS - * - * - * : ID of the profile to use for the migration. - * - * ## EXAMPLES - * - * wp wpsdb migrate 1 - * - * @synopsis - * - * @since 1.0 - */ - public function migrate( $args, $assoc_args ) { - $profile = $args[0]; + /** + * Run a migration. + * + * ## OPTIONS + * + * + * : ID of the profile to use for the migration. + * + * ## EXAMPLES + * + * wp wpsdb migrate 1 + * + * @synopsis + * + * @since 1.0 + */ + public function migrate($args, $assoc_args) + { + $profile = $args[0]; - $result = wpsdb_migrate( $profile ); + $result = wpsdb_migrate($profile); - if ( true === $result ) { - WP_CLI::success( __( 'Migration successful.', 'wp-sync-db-cli' ) ); - return; - } + if (true === $result) { + WP_CLI::success(__('Migration successful.', 'wp-sync-db-cli')); + return; + } - WP_CLI::warning( $result->get_error_message() ); - return; - } + WP_CLI::warning($result->get_error_message()); + return; + } } -WP_CLI::add_command( 'wpsdb', 'WPSDBCLI' ); +WP_CLI::add_command('wpsdb', 'WPSDBCLI'); diff --git a/class/wpsdb-cli.php b/class/wpsdb-cli.php index ab59116..0f0c2c8 100644 --- a/class/wpsdb-cli.php +++ b/class/wpsdb-cli.php @@ -1,169 +1,198 @@ plugin_slug = 'wp-sync-db-cli'; - $this->plugin_version = $GLOBALS['wpsdb_meta']['wp-sync-db-cli']['version']; - - if( ! $this->meets_version_requirements( '1.4b1' ) ) return; - } - - function cli_migration( $profile ) { - global $wpsdb; - $wpsdb_settings = get_option( 'wpsdb_settings' ); - --$profile; - if( ! $this->meets_version_requirements( '1.4b1' ) ) return $this->cli_error( __( 'Please update WP Sync DB.', 'wp-sync-db-cli' ) ); - if( ! isset( $profile ) ) return $this->cli_error( __( 'Profile ID missing.', 'wp-sync-db-cli' ) ); - if( ! isset( $wpsdb_settings['profiles'][$profile] ) ) return $this->cli_error( __( 'Profile ID not found.', 'wp-sync-db-cli' ) ); - - $this->set_time_limit(); - $wpsdb->set_cli_migration(); - - $profile = apply_filters( 'wpsdb_cli_profile_before_migration', $wpsdb_settings['profiles'][$profile] ); - $connection_info = explode( "\n", $profile['connection_info'] ); - $form_data = http_build_query( $profile ); - - if( 'savefile' == $profile['action'] ) return $this->cli_error( __( 'Exports not supported for CLI migrations. Please instead select push or pull instead.', 'wp-sync-db-cli' ) ); - - do_action( 'wpsdb_cli_before_verify_connection_to_remote_site', $profile ); - - // this request returns certain information from the remote machine, e.g. tables, prefix, bottleneck, gzip, etc - $_POST['intent'] = $profile['action']; - $_POST['url'] = trim( $connection_info[0] ); - $_POST['key'] = trim( $connection_info[1] ); - $_POST = apply_filters( 'wpsdb_cli_verify_connection_to_remote_site_args', $_POST, $profile ); - $response = $wpsdb->ajax_verify_connection_to_remote_site(); - if( is_wp_error( $verify_connection_response = $this->verify_cli_response( $response, 'ajax_verify_connection_to_remote_site()' ) ) ) return $verify_connection_response; - - $verify_connection_response = apply_filters( 'wpsdb_cli_verify_connection_response', $verify_connection_response ); - do_action( 'wpsdb_cli_before_initiate_migration', $profile, $verify_connection_response ); - - // this request does one last verification check and creates export / backup files (if required) - $_POST['form_data'] = $form_data; - $_POST['stage'] = ( '0' == $profile['create_backup'] ) ? 'migrate' : 'backup'; - $_POST = apply_filters( 'wpsdb_cli_initiate_migration_args', $_POST, $profile, $verify_connection_response ); - $response = $wpsdb->ajax_initiate_migration(); - if( is_wp_error( $initiate_migration_response = $this->verify_cli_response( $response, 'ajax_initiate_migration()' ) ) ) return $initiate_migration_response; - - $initiate_migration_response = apply_filters( 'wpsdb_cli_initiate_migration_response', $initiate_migration_response ); - - // determine which tables to backup (if required) - $tables_to_backup = array(); - if( 'backup' == $_POST['stage'] ) { - if( 'push' == $profile['action'] ) { - if( 'backup_only_with_prefix' == $profile['backup_option'] ) { - $tables_to_backup = $verify_connection_response['prefixed_tables']; - } elseif( 'backup_selected' == $profile['backup_option'] ) { - $tables_to_backup = array_intersect( $profile['select_backup'], $verify_connection_response['tables'] ); - } elseif( 'backup_manual_select' == $profile['backup_option'] ) { - $tables_to_backup = array_intersect( $profile['select_backup'], $verify_connection_response['tables'] ); - } - } else { - if( 'backup_only_with_prefix' == $profile['backup_option'] ) { - $tables_to_backup = $this->get_tables( 'prefix' ); - } elseif( 'backup_selected' == $profile['backup_option'] ) { - $tables_to_backup = array_intersect( $profile['select_backup'], $this->get_tables() ); - } elseif( 'backup_manual_select' == $profile['backup_option'] ) { - $tables_to_backup = array_intersect( $profile['select_backup'], $this->get_tables() ); - } - } - } - $tables_to_backup = apply_filters( 'wpsdb_cli_tables_to_backup', $tables_to_backup, $profile, $verify_connection_response, $initiate_migration_response ); - - // determine which tables to migrate - $tables_to_migrate = array(); - if( 'push' == $profile['action'] ) { - if( 'migrate_only_with_prefix' == $profile['table_migrate_option'] ) { - $tables_to_migrate = $this->get_tables( 'prefix' ); - } elseif( 'migrate_select' == $profile['table_migrate_option'] ) { - $tables_to_migrate = array_intersect( $profile['select_tables'], $this->get_tables() ); - } - } elseif( 'pull' == $profile['action'] ) { - if( 'migrate_only_with_prefix' == $profile['table_migrate_option'] ) { - $tables_to_migrate = $verify_connection_response['prefixed_tables']; - } elseif( 'migrate_select' == $profile['table_migrate_option'] ) { - $tables_to_migrate = array_intersect( $profile['select_tables'], $verify_connection_response['tables'] ); - } - } - $tables_to_migrate = apply_filters( 'wpsdb_cli_tables_to_migrate', $tables_to_migrate, $profile, $verify_connection_response, $initiate_migration_response ); - - $_POST['dump_filename'] = $initiate_migration_response['dump_filename']; - $_POST['gzip'] = ( '1' == $verify_connection_response['gzip'] ) ? 1 : 0; - $_POST['bottleneck'] = $verify_connection_response['bottleneck']; - $_POST['prefix'] = $verify_connection_response['prefix']; - - $tables_to_process = ( 'backup' == $_POST['stage'] ) ? $tables_to_backup : $tables_to_migrate; - $stage_interator = ( 'backup' == $_POST['stage'] ) ? 1 : 2; - - do_action( 'wpsdb_cli_before_migrate_tables', $profile, $verify_connection_response, $initiate_migration_response ); - - do { - foreach( $tables_to_process as $key => $table ) { - $current_row = -1; - $primary_keys = ''; - $_POST['table'] = $table; - $_POST['last_table'] = ( $key == count( $tables_to_process ) - 1 ) ? '1' : '0'; - - do { - // reset the current chunk - $wpsdb->empty_current_chunk(); - - $_POST['current_row'] = $current_row; - $_POST['primary_keys'] = $primary_keys; - $_POST = apply_filters( 'wpsdb_cli_migrate_table_args', $_POST, $profile, $verify_connection_response, $initiate_migration_response ); - $response = $wpsdb->ajax_migrate_table(); - if( is_wp_error( $migrate_table_response = $this->verify_cli_response( $response, 'ajax_migrate_table()' ) ) ) return $migrate_table_response; - $migrate_table_response = apply_filters( 'wpsdb_cli_migrate_table_response', $migrate_table_response, $_POST, $profile, $verify_connection_response, $initiate_migration_response ); - - $current_row = $migrate_table_response['current_row']; - $primary_keys = $migrate_table_response['primary_keys']; - - } while ( -1 != $current_row ); - } - ++$stage_interator; - $_POST['stage'] = 'migrate'; - $tables_to_process = $tables_to_migrate; - - } while ( $stage_interator < 3 ); - - do_action( 'wpsdb_cli_before_finalize_migration', $profile, $verify_connection_response, $initiate_migration_response ); - - $finalize_migration_response = apply_filters( 'wpsdb_cli_finalize_migration', true, $profile, $verify_connection_response, $initiate_migration_response ); - if( is_wp_error( $finalize_migration_response ) ) return $finalize_migration_response; - - $_POST['tables'] = implode( ',', $tables_to_process ); - $_POST['temp_prefix'] = $verify_connection_response['temp_prefix']; - $_POST = apply_filters( 'wpsdb_cli_finalize_migration_args', $_POST, $profile, $verify_connection_response, $initiate_migration_response ); - // don't send redundant POST variables - $_POST = $this->filter_post_elements( $_POST, array( 'action', 'intent', 'url', 'key', 'form_data', 'prefix', 'type', 'location', 'tables', 'temp_prefix' ) ); - $response = trim( $wpsdb->ajax_finalize_migration() ); - if( ! empty( $response ) ) return $this->cli_error( $response ); - - do_action( 'wpsdb_cli_after_finalize_migration', $profile, $verify_connection_response, $initiate_migration_response ); - - return true; - } - - function verify_cli_response( $response, $function_name ) { - global $wpsdb; - $response = trim( $response ); - if( false === $response ) { - return $this->cli_error( $this->error ); - } - if( false === $wpsdb->is_json( $response ) ) { - return $this->cli_error( sprintf( __( '%1$s was expecting a JSON response, instead we received: %2$s', 'wp-sync-db-cli' ), $function_name, $response ) ); - } - $response = json_decode( $response, true ); - if( isset( $response['wpsdb_error'] ) ) { - return $this->cli_error( $response['body'] ); - } - return $response; - } - - function cli_error( $message ){ - return new WP_Error( 'wpsdb_cli_error', $message ); - } +class WPSDB_CLI extends WPSDB_Addon +{ + + public function __construct($plugin_file_path) + { + parent::__construct($plugin_file_path); + + $this->plugin_slug = 'wp-sync-db-cli'; + $this->plugin_version = $GLOBALS['wpsdb_meta']['wp-sync-db-cli']['version']; + + if (!$this->meets_version_requirements('1.4b1')) { + return; + } + + } + + public function cli_migration($profile) + { + global $wpsdb; + $wpsdb_settings = get_option('wpsdb_settings'); + --$profile; + if (!$this->meets_version_requirements('1.4b1')) { + return $this->cli_error(__('Please update WP Sync DB.', 'wp-sync-db-cli')); + } + + if (!isset($profile)) { + return $this->cli_error(__('Profile ID missing.', 'wp-sync-db-cli')); + } + + if (!isset($wpsdb_settings['profiles'][$profile])) { + return $this->cli_error(__('Profile ID not found.', 'wp-sync-db-cli')); + } + + $this->set_time_limit(); + $wpsdb->set_cli_migration(); + + $profile = apply_filters('wpsdb_cli_profile_before_migration', $wpsdb_settings['profiles'][$profile]); + $connection_info = explode("\n", $profile['connection_info']); + $form_data = http_build_query($profile); + + if ('savefile' == $profile['action']) { + return $this->cli_error(__('Exports not supported for CLI migrations. Please instead select push or pull instead.', 'wp-sync-db-cli')); + } + + do_action('wpsdb_cli_before_verify_connection_to_remote_site', $profile); + + // this request returns certain information from the remote machine, e.g. tables, prefix, bottleneck, gzip, etc + $_POST['intent'] = $profile['action']; + $_POST['url'] = trim($connection_info[0]); + $_POST['key'] = trim($connection_info[1]); + $_POST = apply_filters('wpsdb_cli_verify_connection_to_remote_site_args', $_POST, $profile); + $response = $wpsdb->ajax_verify_connection_to_remote_site(); + if (is_wp_error($verify_connection_response = $this->verify_cli_response($response, 'ajax_verify_connection_to_remote_site()'))) { + return $verify_connection_response; + } + + $verify_connection_response = apply_filters('wpsdb_cli_verify_connection_response', $verify_connection_response); + do_action('wpsdb_cli_before_initiate_migration', $profile, $verify_connection_response); + + // this request does one last verification check and creates export / backup files (if required) + $_POST['form_data'] = $form_data; + $_POST['stage'] = ('0' == $profile['create_backup']) ? 'migrate' : 'backup'; + $_POST = apply_filters('wpsdb_cli_initiate_migration_args', $_POST, $profile, $verify_connection_response); + $response = $wpsdb->ajax_initiate_migration(); + if (is_wp_error($initiate_migration_response = $this->verify_cli_response($response, 'ajax_initiate_migration()'))) { + return $initiate_migration_response; + } + + $initiate_migration_response = apply_filters('wpsdb_cli_initiate_migration_response', $initiate_migration_response); + + // determine which tables to backup (if required) + $tables_to_backup = array(); + if ('backup' == $_POST['stage']) { + if ('push' == $profile['action']) { + if ('backup_only_with_prefix' == $profile['backup_option']) { + $tables_to_backup = $verify_connection_response['prefixed_tables']; + } elseif ('backup_selected' == $profile['backup_option']) { + $tables_to_backup = array_intersect($profile['select_backup'], $verify_connection_response['tables']); + } elseif ('backup_manual_select' == $profile['backup_option']) { + $tables_to_backup = array_intersect($profile['select_backup'], $verify_connection_response['tables']); + } + } else { + if ('backup_only_with_prefix' == $profile['backup_option']) { + $tables_to_backup = $this->get_tables('prefix'); + } elseif ('backup_selected' == $profile['backup_option']) { + $tables_to_backup = array_intersect($profile['select_backup'], $this->get_tables()); + } elseif ('backup_manual_select' == $profile['backup_option']) { + $tables_to_backup = array_intersect($profile['select_backup'], $this->get_tables()); + } + } + } + $tables_to_backup = apply_filters('wpsdb_cli_tables_to_backup', $tables_to_backup, $profile, $verify_connection_response, $initiate_migration_response); + + // determine which tables to migrate + $tables_to_migrate = array(); + if ('push' == $profile['action']) { + if ('migrate_only_with_prefix' == $profile['table_migrate_option']) { + $tables_to_migrate = $this->get_tables('prefix'); + } elseif ('migrate_select' == $profile['table_migrate_option']) { + $tables_to_migrate = array_intersect($profile['select_tables'], $this->get_tables()); + } + } elseif ('pull' == $profile['action']) { + if ('migrate_only_with_prefix' == $profile['table_migrate_option']) { + $tables_to_migrate = $verify_connection_response['prefixed_tables']; + } elseif ('migrate_select' == $profile['table_migrate_option']) { + $tables_to_migrate = array_intersect($profile['select_tables'], $verify_connection_response['tables']); + } + } + $tables_to_migrate = apply_filters('wpsdb_cli_tables_to_migrate', $tables_to_migrate, $profile, $verify_connection_response, $initiate_migration_response); + + $_POST['dump_filename'] = $initiate_migration_response['dump_filename']; + $_POST['gzip'] = ('1' == $verify_connection_response['gzip']) ? 1 : 0; + $_POST['bottleneck'] = $verify_connection_response['bottleneck']; + $_POST['prefix'] = $verify_connection_response['prefix']; + + $tables_to_process = ('backup' == $_POST['stage']) ? $tables_to_backup : $tables_to_migrate; + $stage_interator = ('backup' == $_POST['stage']) ? 1 : 2; + + do_action('wpsdb_cli_before_migrate_tables', $profile, $verify_connection_response, $initiate_migration_response); + + do { + foreach ($tables_to_process as $key => $table) { + $current_row = -1; + $primary_keys = ''; + $_POST['table'] = $table; + $_POST['last_table'] = ($key == count($tables_to_process) - 1) ? '1' : '0'; + + do { + // reset the current chunk + $wpsdb->empty_current_chunk(); + + $_POST['current_row'] = $current_row; + $_POST['primary_keys'] = $primary_keys; + $_POST = apply_filters('wpsdb_cli_migrate_table_args', $_POST, $profile, $verify_connection_response, $initiate_migration_response); + $response = $wpsdb->ajax_migrate_table(); + if (is_wp_error($migrate_table_response = $this->verify_cli_response($response, 'ajax_migrate_table()'))) { + return $migrate_table_response; + } + + $migrate_table_response = apply_filters('wpsdb_cli_migrate_table_response', $migrate_table_response, $_POST, $profile, $verify_connection_response, $initiate_migration_response); + + $current_row = $migrate_table_response['current_row']; + $primary_keys = $migrate_table_response['primary_keys']; + + } while (-1 != $current_row); + } + ++$stage_interator; + $_POST['stage'] = 'migrate'; + $tables_to_process = $tables_to_migrate; + + } while ($stage_interator < 3); + + do_action('wpsdb_cli_before_finalize_migration', $profile, $verify_connection_response, $initiate_migration_response); + + $finalize_migration_response = apply_filters('wpsdb_cli_finalize_migration', true, $profile, $verify_connection_response, $initiate_migration_response); + if (is_wp_error($finalize_migration_response)) { + return $finalize_migration_response; + } + + $_POST['tables'] = implode(',', $tables_to_process); + $_POST['temp_prefix'] = $verify_connection_response['temp_prefix']; + $_POST = apply_filters('wpsdb_cli_finalize_migration_args', $_POST, $profile, $verify_connection_response, $initiate_migration_response); + // don't send redundant POST variables + $_POST = $this->filter_post_elements($_POST, array('action', 'intent', 'url', 'key', 'form_data', 'prefix', 'type', 'location', 'tables', 'temp_prefix')); + $response = trim($wpsdb->ajax_finalize_migration()); + if (!empty($response)) { + return $this->cli_error($response); + } + + do_action('wpsdb_cli_after_finalize_migration', $profile, $verify_connection_response, $initiate_migration_response); + + return true; + } + + public function verify_cli_response($response, $function_name) + { + global $wpsdb; + $response = trim($response); + if (false === $response) { + return $this->cli_error($this->error); + } + if (false === $wpsdb->is_json($response)) { + return $this->cli_error(sprintf(__('%1$s was expecting a JSON response, instead we received: %2$s', 'wp-sync-db-cli'), $function_name, $response)); + } + $response = json_decode($response, true); + if (isset($response['wpsdb_error'])) { + return $this->cli_error($response['body']); + } + return $response; + } + + public function cli_error($message) + { + return new WP_Error('wpsdb_cli_error', $message); + } } diff --git a/composer.json b/composer.json index 7232579..ccc970e 100644 --- a/composer.json +++ b/composer.json @@ -7,5 +7,11 @@ "keywords": ["plugin","wordpress","wp-sync-db","cli"], "require": { "composer/installers": "~1.0.6" + }, + "require-dev": { + "phpfmt/fmt": "^19.6" + }, + "scripts": { + "test": "./test.sh" } } diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..6d19f2e --- /dev/null +++ b/test.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e +set -o pipefail + +files=(*.php class/*.php); +for file in "${files[@]}" +do + php -l "$file"; + php vendor/phpfmt/fmt/fmt.phar --psr1 --psr2 -o=- "$file" | diff -U3 \ + --label "$file (correct formatting)" --label "$file (original)" - "$file"; +done diff --git a/wp-sync-db-cli.php b/wp-sync-db-cli.php index e356bd0..317a91c 100644 --- a/wp-sync-db-cli.php +++ b/wp-sync-db-cli.php @@ -7,32 +7,36 @@ Version: 1.0b1 Author URI: http://slang.cx Network: True -*/ + */ require_once 'version.php'; -$GLOBALS['wpsdb_meta']['wp-sync-db-cli']['folder'] = basename( plugin_dir_path( __FILE__ ) ); +$GLOBALS['wpsdb_meta']['wp-sync-db-cli']['folder'] = basename(plugin_dir_path(__FILE__)); -function wp_sync_db_cli_loaded() { - if ( ! class_exists( 'WPSDB_Addon' ) ) return; +function wp_sync_db_cli_loaded() +{ + if (!class_exists('WPSDB_Addon')) { + return; + } - require_once __DIR__ . '/class/wpsdb-cli.php'; + require_once __DIR__ . '/class/wpsdb-cli.php'; - // register with wp-cli if it's running, and command hasn't already been defined elsewhere - if ( defined( 'WP_CLI' ) && WP_CLI && ! class_exists( 'WPSDBCLI' ) ) { - require_once __DIR__ . '/class/command.php'; - } + // register with wp-cli if it's running, and command hasn't already been defined elsewhere + if (defined('WP_CLI') && WP_CLI && !class_exists('WPSDBCLI')) { + require_once __DIR__ . '/class/command.php'; + } - load_plugin_textdomain( 'wp-sync-db-cli', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); + load_plugin_textdomain('wp-sync-db-cli', false, dirname(plugin_basename(__FILE__)) . '/languages/'); - global $wpsdb_cli; - $wpsdb_cli = new WPSDB_CLI( __FILE__ ); + global $wpsdb_cli; + $wpsdb_cli = new WPSDB_CLI(__FILE__); } -add_action( 'plugins_loaded', 'wp_sync_db_cli_loaded', 20 ); +add_action('plugins_loaded', 'wp_sync_db_cli_loaded', 20); -function wpsdb_migrate( $profile ) { - global $wpsdb_cli; - if( empty( $wpsdb_cli ) ) { - return new WP_Error( 'wpsdb_cli_error', __( 'WP Sync DB CLI class not available', 'wp-sync-db-cli' ) ); - } - return $wpsdb_cli->cli_migration( $profile ); +function wpsdb_migrate($profile) +{ + global $wpsdb_cli; + if (empty($wpsdb_cli)) { + return new WP_Error('wpsdb_cli_error', __('WP Sync DB CLI class not available', 'wp-sync-db-cli')); + } + return $wpsdb_cli->cli_migration($profile); }