Skip to content

Commit 93dc73a

Browse files
committed
inital commit
0 parents  commit 93dc73a

File tree

5 files changed

+310
-0
lines changed

5 files changed

+310
-0
lines changed

class/command.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Migrate your DB using WP Migrate DB Pro.
5+
*/
6+
class WPMDBCLI extends WP_CLI_Command {
7+
8+
/**
9+
* Run a migration.
10+
*
11+
* ## OPTIONS
12+
*
13+
* <profile>
14+
* : ID of the profile to use for the migration.
15+
*
16+
* ## EXAMPLES
17+
*
18+
* wp wpmdb migrate 1
19+
*
20+
* @synopsis <profile>
21+
*
22+
* @since 1.0
23+
*/
24+
public function migrate( $args, $assoc_args ) {
25+
$profile = $args[0];
26+
27+
$result = wpmdb_migrate( $profile );
28+
29+
if ( true === $result ) {
30+
WP_CLI::success( __( 'Migration successful.', 'wp-migrate-db-pro-cli' ) );
31+
return;
32+
}
33+
34+
WP_CLI::warning( $result->get_error_message() );
35+
return;
36+
}
37+
38+
}
39+
40+
WP_CLI::add_command( 'wpmdb', 'WPMDBCLI' );

class/wpmdbpro-cli.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
class WPMDBPro_CLI extends WPMDBPro_Addon {
3+
4+
function __construct( $plugin_file_path ) {
5+
parent::__construct( $plugin_file_path );
6+
7+
$this->plugin_slug = 'wp-migrate-db-pro-cli';
8+
$this->plugin_version = $GLOBALS['wpmdb_meta']['wp-migrate-db-pro-cli']['version'];
9+
10+
if( ! $this->meets_version_requirements( '1.4b1' ) ) return;
11+
}
12+
13+
function cli_migration( $profile ) {
14+
global $wpmdbpro;
15+
$wpmdb_settings = get_option( 'wpmdb_settings' );
16+
--$profile;
17+
if( ! $this->meets_version_requirements( '1.4b1' ) ) return $this->cli_error( __( 'Please update WP Migrate DB Pro.', 'wp-migrate-db-pro-cli' ) );
18+
if( ! isset( $profile ) ) return $this->cli_error( __( 'Profile ID missing.', 'wp-migrate-db-pro-cli' ) );
19+
if( ! isset( $wpmdb_settings['profiles'][$profile] ) ) return $this->cli_error( __( 'Profile ID not found.', 'wp-migrate-db-pro-cli' ) );
20+
21+
$this->set_time_limit();
22+
$wpmdbpro->set_cli_migration();
23+
24+
$profile = apply_filters( 'wpmdb_cli_profile_before_migration', $wpmdb_settings['profiles'][$profile] );
25+
$connection_info = explode( "\n", $profile['connection_info'] );
26+
$form_data = http_build_query( $profile );
27+
28+
if( 'savefile' == $profile['action'] ) return $this->cli_error( __( 'Exports not supported for CLI migrations. Please instead select push or pull instead.', 'wp-migrate-db-pro-cli' ) );
29+
30+
do_action( 'wpmdb_cli_before_verify_connection_to_remote_site', $profile );
31+
32+
// this request returns certain information from the remote machine, e.g. tables, prefix, bottleneck, gzip, etc
33+
$_POST['intent'] = $profile['action'];
34+
$_POST['url'] = trim( $connection_info[0] );
35+
$_POST['key'] = trim( $connection_info[1] );
36+
$_POST = apply_filters( 'wpmdb_cli_verify_connection_to_remote_site_args', $_POST, $profile );
37+
$response = $wpmdbpro->ajax_verify_connection_to_remote_site();
38+
if( is_wp_error( $verify_connection_response = $this->verify_cli_response( $response, 'ajax_verify_connection_to_remote_site()' ) ) ) return $verify_connection_response;
39+
40+
$verify_connection_response = apply_filters( 'wpmdb_cli_verify_connection_response', $verify_connection_response );
41+
do_action( 'wpmdb_cli_before_initiate_migration', $profile, $verify_connection_response );
42+
43+
// this request does one last verification check and creates export / backup files (if required)
44+
$_POST['form_data'] = $form_data;
45+
$_POST['stage'] = ( '0' == $profile['create_backup'] ) ? 'migrate' : 'backup';
46+
$_POST = apply_filters( 'wpmdb_cli_initiate_migration_args', $_POST, $profile, $verify_connection_response );
47+
$response = $wpmdbpro->ajax_initiate_migration();
48+
if( is_wp_error( $initiate_migration_response = $this->verify_cli_response( $response, 'ajax_initiate_migration()' ) ) ) return $initiate_migration_response;
49+
50+
$initiate_migration_response = apply_filters( 'wpmdb_cli_initiate_migration_response', $initiate_migration_response );
51+
52+
// determine which tables to backup (if required)
53+
$tables_to_backup = array();
54+
if( 'backup' == $_POST['stage'] ) {
55+
if( 'push' == $profile['action'] ) {
56+
if( 'backup_only_with_prefix' == $profile['backup_option'] ) {
57+
$tables_to_backup = $verify_connection_response['prefixed_tables'];
58+
} elseif( 'backup_selected' == $profile['backup_option'] ) {
59+
$tables_to_backup = array_intersect( $profile['select_backup'], $verify_connection_response['tables'] );
60+
} elseif( 'backup_manual_select' == $profile['backup_option'] ) {
61+
$tables_to_backup = array_intersect( $profile['select_backup'], $verify_connection_response['tables'] );
62+
}
63+
} else {
64+
if( 'backup_only_with_prefix' == $profile['backup_option'] ) {
65+
$tables_to_backup = $this->get_tables( 'prefix' );
66+
} elseif( 'backup_selected' == $profile['backup_option'] ) {
67+
$tables_to_backup = array_intersect( $profile['select_backup'], $this->get_tables() );
68+
} elseif( 'backup_manual_select' == $profile['backup_option'] ) {
69+
$tables_to_backup = array_intersect( $profile['select_backup'], $this->get_tables() );
70+
}
71+
}
72+
}
73+
$tables_to_backup = apply_filters( 'wpmdb_cli_tables_to_backup', $tables_to_backup, $profile, $verify_connection_response, $initiate_migration_response );
74+
75+
// determine which tables to migrate
76+
$tables_to_migrate = array();
77+
if( 'push' == $profile['action'] ) {
78+
if( 'migrate_only_with_prefix' == $profile['table_migrate_option'] ) {
79+
$tables_to_migrate = $this->get_tables( 'prefix' );
80+
} elseif( 'migrate_select' == $profile['table_migrate_option'] ) {
81+
$tables_to_migrate = array_intersect( $profile['select_tables'], $this->get_tables() );
82+
}
83+
} elseif( 'pull' == $profile['action'] ) {
84+
if( 'migrate_only_with_prefix' == $profile['table_migrate_option'] ) {
85+
$tables_to_migrate = $verify_connection_response['prefixed_tables'];
86+
} elseif( 'migrate_select' == $profile['table_migrate_option'] ) {
87+
$tables_to_migrate = array_intersect( $profile['select_tables'], $verify_connection_response['tables'] );
88+
}
89+
}
90+
$tables_to_migrate = apply_filters( 'wpmdb_cli_tables_to_migrate', $tables_to_migrate, $profile, $verify_connection_response, $initiate_migration_response );
91+
92+
$_POST['dump_filename'] = $initiate_migration_response['dump_filename'];
93+
$_POST['gzip'] = ( '1' == $verify_connection_response['gzip'] ) ? 1 : 0;
94+
$_POST['bottleneck'] = $verify_connection_response['bottleneck'];
95+
$_POST['prefix'] = $verify_connection_response['prefix'];
96+
97+
$tables_to_process = ( 'backup' == $_POST['stage'] ) ? $tables_to_backup : $tables_to_migrate;
98+
$stage_interator = ( 'backup' == $_POST['stage'] ) ? 1 : 2;
99+
100+
do_action( 'wpmdb_cli_before_migrate_tables', $profile, $verify_connection_response, $initiate_migration_response );
101+
102+
do {
103+
foreach( $tables_to_process as $key => $table ) {
104+
$current_row = -1;
105+
$primary_keys = '';
106+
$_POST['table'] = $table;
107+
$_POST['last_table'] = ( $key == count( $tables_to_process ) - 1 ) ? '1' : '0';
108+
109+
do {
110+
// reset the current chunk
111+
$wpmdbpro->empty_current_chunk();
112+
113+
$_POST['current_row'] = $current_row;
114+
$_POST['primary_keys'] = $primary_keys;
115+
$_POST = apply_filters( 'wpmdb_cli_migrate_table_args', $_POST, $profile, $verify_connection_response, $initiate_migration_response );
116+
$response = $wpmdbpro->ajax_migrate_table();
117+
if( is_wp_error( $migrate_table_response = $this->verify_cli_response( $response, 'ajax_migrate_table()' ) ) ) return $migrate_table_response;
118+
$migrate_table_response = apply_filters( 'wpmdb_cli_migrate_table_response', $migrate_table_response, $_POST, $profile, $verify_connection_response, $initiate_migration_response );
119+
120+
$current_row = $migrate_table_response['current_row'];
121+
$primary_keys = $migrate_table_response['primary_keys'];
122+
123+
} while ( -1 != $current_row );
124+
}
125+
++$stage_interator;
126+
$_POST['stage'] = 'migrate';
127+
$tables_to_process = $tables_to_migrate;
128+
129+
} while ( $stage_interator < 3 );
130+
131+
do_action( 'wpmdb_cli_before_finalize_migration', $profile, $verify_connection_response, $initiate_migration_response );
132+
133+
$finalize_migration_response = apply_filters( 'wpmdb_cli_finalize_migration', true, $profile, $verify_connection_response, $initiate_migration_response );
134+
if( is_wp_error( $finalize_migration_response ) ) return $finalize_migration_response;
135+
136+
$_POST['tables'] = implode( ',', $tables_to_process );
137+
$_POST['temp_prefix'] = $verify_connection_response['temp_prefix'];
138+
$_POST = apply_filters( 'wpmdb_cli_finalize_migration_args', $_POST, $profile, $verify_connection_response, $initiate_migration_response );
139+
// don't send redundant POST variables
140+
$_POST = $this->filter_post_elements( $_POST, array( 'action', 'intent', 'url', 'key', 'form_data', 'prefix', 'type', 'location', 'tables', 'temp_prefix' ) );
141+
$response = trim( $wpmdbpro->ajax_finalize_migration() );
142+
if( ! empty( $response ) ) return $this->cli_error( $response );
143+
144+
do_action( 'wpmdb_cli_after_finalize_migration', $profile, $verify_connection_response, $initiate_migration_response );
145+
146+
return true;
147+
}
148+
149+
function verify_cli_response( $response, $function_name ) {
150+
global $wpmdbpro;
151+
$response = trim( $response );
152+
if( false === $response ) {
153+
return $this->cli_error( $this->error );
154+
}
155+
if( false === $wpmdbpro->is_json( $response ) ) {
156+
return $this->cli_error( sprintf( __( '%1$s was expecting a JSON response, instead we received: %2$s', 'wp-migrate-db-pro-cli' ), $function_name, $response ) );
157+
}
158+
$response = json_decode( $response, true );
159+
if( isset( $response['wpmdb_error'] ) ) {
160+
return $this->cli_error( $response['body'] );
161+
}
162+
return $response;
163+
}
164+
165+
function cli_error( $message ){
166+
return new WP_Error( 'wpmdb_cli_error', $message );
167+
}
168+
169+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
msgid ""
2+
msgstr ""
3+
"Project-Id-Version: WP Migrate DB Pro CLI\n"
4+
"POT-Creation-Date: 2014-05-27 11:40+1000\n"
5+
"PO-Revision-Date: 2014-05-27 11:41+1000\n"
6+
"Last-Translator: Delicious Brains <[email protected]>\n"
7+
"Language-Team: Delicious Brains <[email protected]>\n"
8+
"Language: en\n"
9+
"MIME-Version: 1.0\n"
10+
"Content-Type: text/plain; charset=UTF-8\n"
11+
"Content-Transfer-Encoding: 8bit\n"
12+
"X-Generator: Poedit 1.6.5\n"
13+
"X-Poedit-Basepath: ..\n"
14+
"X-Poedit-SourceCharset: UTF-8\n"
15+
"X-Poedit-KeywordsList: __;_e;_n:1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;esc_attr__;"
16+
"esc_attr_e;esc_attr_x:1,2c;esc_html__;esc_html_e;esc_html_x:1,2c;_n_noop:1,2;"
17+
"_nx_noop:3c,1,2;__ngettext_noop:1,2\n"
18+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
19+
"X-Poedit-SearchPath-0: .\n"
20+
21+
#: class/command.php:30
22+
msgid "Migration successful."
23+
msgstr ""
24+
25+
#: class/wpmdbpro-cli.php:19
26+
msgid "Please update WP Migrate DB Pro."
27+
msgstr ""
28+
29+
#: class/wpmdbpro-cli.php:20
30+
msgid "Profile ID missing."
31+
msgstr ""
32+
33+
#: class/wpmdbpro-cli.php:21
34+
msgid "Profile ID not found."
35+
msgstr ""
36+
37+
#: class/wpmdbpro-cli.php:30
38+
msgid ""
39+
"Exports not supported for CLI migrations. Please instead select push or pull "
40+
"instead."
41+
msgstr ""
42+
43+
#: class/wpmdbpro-cli.php:158
44+
#, php-format
45+
msgid "%1$s was expecting a JSON response, instead we received: %2$s"
46+
msgstr ""
47+
48+
#: wp-migrate-db-pro-cli.php:44
49+
msgid "WP Migrate DB Pro CLI class not available"
50+
msgstr ""

version.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
$GLOBALS['wpmdb_meta']['wp-migrate-db-pro-cli']['version'] = '1.0b1';

wp-migrate-db-pro-cli.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*
3+
Plugin Name: WP Migrate DB Pro CLI
4+
Plugin URI: http://deliciousbrains.com/wp-migrate-db-pro/
5+
Description: An extension to WP Migrate DB Pro, allows you to execute migrations using a function call or via WP-CLI
6+
Author: Delicious Brains
7+
Version: 1.0b1
8+
Author URI: http://deliciousbrains.com
9+
Network: True
10+
*/
11+
12+
// Copyright (c) 2013 Delicious Brains. All rights reserved.
13+
//
14+
// Released under the GPL license
15+
// http://www.opensource.org/licenses/gpl-license.php
16+
//
17+
// **********************************************************************
18+
// This program is distributed in the hope that it will be useful, but
19+
// WITHOUT ANY WARRANTY; without even the implied warranty of
20+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21+
// **********************************************************************
22+
23+
require_once 'version.php';
24+
$GLOBALS['wpmdb_meta']['wp-migrate-db-pro-cli']['folder'] = basename( plugin_dir_path( __FILE__ ) );
25+
26+
function wp_migrate_db_pro_cli_loaded() {
27+
if ( ! class_exists( 'WPMDBPro_Addon' ) ) return;
28+
29+
require_once __DIR__ . '/class/wpmdbpro-cli.php';
30+
31+
// register with wp-cli if it's running, and command hasn't already been defined elsewhere
32+
if ( defined( 'WP_CLI' ) && WP_CLI && ! class_exists( 'WPMDBCLI' ) ) {
33+
require_once __DIR__ . '/class/command.php';
34+
}
35+
36+
load_plugin_textdomain( 'wp-migrate-db-pro-cli', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
37+
38+
global $wpmdbpro_cli;
39+
$wpmdbpro_cli = new WPMDBPro_CLI( __FILE__ );
40+
}
41+
add_action( 'plugins_loaded', 'wp_migrate_db_pro_cli_loaded', 20 );
42+
43+
function wpmdb_migrate( $profile ) {
44+
global $wpmdbpro_cli;
45+
if( empty( $wpmdbpro_cli ) ) {
46+
return new WP_Error( 'wpmdb_cli_error', __( 'WP Migrate DB Pro CLI class not available', 'wp-migrate-db-pro-cli' ) );
47+
}
48+
return $wpmdbpro_cli->cli_migration( $profile );
49+
}

0 commit comments

Comments
 (0)