Created
October 28, 2014 03:21
-
-
Save shawnhooper/45634ac1d1eebe74a2e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Plugin Name: CLI Export Multisite | |
*/ | |
if ( ! defined( 'WP_CLI' ) ) return; | |
use \WP_CLI\Utils; | |
/** | |
* Export SQL data for selected sites in a multisite network | |
* | |
* @author shooper | |
*/ | |
class CLI_ExportMultisite extends WP_CLI_Command { | |
/** | |
* Exports the database to a file or to STDOUT. | |
* | |
* ## OPTIONS | |
* | |
* | |
* [<range>] | |
* : The comma separated list of site IDs to export | |
* | |
* [<file>] | |
* : The name of the SQL file to export. If '-', then outputs to STDOUT. If omitted, it will be '{dbname}.sql'. | |
* | |
* [--<field>=<value>] | |
* : Extra arguments to pass to mysqldump | |
* | |
* ## EXAMPLES | |
* | |
* wp multisite export --add-drop-table | |
* wp multisite export --tables=wp_options,wp_users | |
* | |
* @alias dump | |
*/ | |
function export($args, $assoc_args) { | |
if (!is_multisite()) { | |
WP_CLI::error('This command can only be used on a multisite installation'); | |
exit; | |
} | |
$result_file = $this->get_file_name( $args ); | |
$stdout = ( '-' === $result_file ); | |
if ( ! $stdout ) { | |
$assoc_args['result-file'] = $result_file; | |
} | |
$command = 'mysqldump --no-defaults %s'; | |
$command_esc_args = array( DB_NAME ); | |
$tables = self::get_table_list($args[0]); | |
if ( $tables ) { | |
$command .= ' --tables'; | |
foreach ( $tables as $table ) { | |
$command .= ' %s'; | |
$command_esc_args[] = trim( $table ); | |
} | |
} | |
$escaped_command = call_user_func_array( '\WP_CLI\Utils\esc_cmd', array_merge( array( $command ), $command_esc_args ) ); | |
self::run( $escaped_command, $assoc_args ); | |
if ( ! $stdout ) { | |
WP_CLI::success( sprintf( 'Exported to %s', $result_file ) ); | |
} | |
} | |
private static function get_table_list ($range) { | |
$siteIds = self::parse_siteid_ranges($range); | |
$tables = array(); | |
global $wpdb; | |
foreach ($siteIds as $site) { | |
array_push($tables, $wpdb->prefix . $site . '_commentmeta'); | |
array_push($tables, $wpdb->prefix . $site . '_comments'); | |
array_push($tables, $wpdb->prefix . $site . '_links'); | |
array_push($tables, $wpdb->prefix . $site . '_options'); | |
array_push($tables, $wpdb->prefix . $site . '_postmeta'); | |
array_push($tables, $wpdb->prefix . $site . '_posts'); | |
array_push($tables, $wpdb->prefix . $site . '_term_relationships'); | |
array_push($tables, $wpdb->prefix . $site . '_term_taxonomy'); | |
array_push($tables, $wpdb->prefix . $site . '_terms'); | |
} | |
return $tables; | |
} | |
public static function parse_siteid_ranges($rangeString) { | |
$output = array(); | |
foreach (explode(',', $rangeString) as $nums) { | |
if (strpos($nums, '-') !== false) { | |
list($from, $to) = explode('-', $nums); | |
$output = array_merge($output, range($from, $to)); | |
} else { | |
$output[] = (int)$nums; | |
} | |
} | |
$output = array_unique($output, SORT_NUMERIC); | |
return $output; | |
} | |
private static function run_query( $query ) { | |
self::run( 'mysql --no-defaults', array( 'execute' => $query ) ); | |
} | |
private function get_file_name( $args ) { | |
if ( ! isset( $args[1] ) ) | |
return sprintf( '%s.sql', DB_NAME ); | |
return $args[1]; | |
} | |
private static function run( $cmd, $assoc_args = array(), $descriptors = null ) { | |
$required = array( | |
'host' => DB_HOST, | |
'user' => DB_USER, | |
'pass' => DB_PASSWORD, | |
); | |
if ( defined( 'DB_CHARSET' ) && constant( 'DB_CHARSET' ) ) { | |
$required['default-character-set'] = constant( 'DB_CHARSET' ); | |
} | |
$final_args = array_merge( $assoc_args, $required ); | |
Utils\run_mysql_command( $cmd, $final_args, $descriptors ); | |
} | |
} | |
WP_CLI::add_command( 'multisite', 'CLI_ExportMultisite' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment