コード例 #1
0
ファイル: db.php プロジェクト: wp-cli/wp-cli
 /**
  * List the database tables.
  *
  * Defaults to all tables registered to the $wpdb database handler.
  *
  * ## OPTIONS
  *
  * [<table>...]
  * : List tables based on wildcard search, e.g. 'wp_*_options' or 'wp_post?'.
  *
  * [--scope=<scope>]
  * : Can be all, global, ms_global, blog, or old tables. Defaults to all.
  *
  * [--network]
  * : List all the tables in a multisite install. Overrides --scope=<scope>.
  *
  * [--all-tables-with-prefix]
  * : List all tables that match the table prefix even if not registered on $wpdb. Overrides --network.
  *
  * [--all-tables]
  * : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix.
  *
  * [--format=<format>]
  * : Render output in a particular format.
  * ---
  * default: list
  * options:
  *   - list
  *   - csv
  * ---
  *
  * ## EXAMPLES
  *
  *     # Export only tables for a single site
  *     $ wp db export --tables=$(wp db tables --url=sub.example.com --format=csv)
  *     Success: Exported to wordpress_dbase.sql
  */
 function tables($args, $assoc_args)
 {
     $format = WP_CLI\Utils\get_flag_value($assoc_args, 'format');
     unset($assoc_args['format']);
     if (empty($args) && empty($assoc_args)) {
         $assoc_args['scope'] = 'all';
     }
     $tables = WP_CLI\Utils\wp_get_table_names($args, $assoc_args);
     if ('csv' === $format) {
         WP_CLI::line(implode(',', $tables));
     } else {
         foreach ($tables as $table) {
             WP_CLI::line($table);
         }
     }
 }
コード例 #2
0
ファイル: db.php プロジェクト: voldemortensen/wp-cli
 /**
  * List the MySQL database tables.
  *
  * Defaults to all tables registered to $wpdb.
  *
  * ## OPTIONS
  *
  * [<table>...]
  * : List tables based on wildcard search, e.g. 'wp_*_options' or 'wp_post?'.
  *
  * [--scope=<scope>]
  * : Can be all, global, ms_global, blog, or old tables. Defaults to all.
  *
  * [--network]
  * : List all the tables in a multisite install. Overrides --scope=<scope>.
  *
  * [--all-tables-with-prefix]
  * : List all tables that match the table prefix even if not registered on $wpdb. Overrides --network.
  *
  * [--all-tables]
  * : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix.
  *
  * [--format=<format>]
  * : Accepted values: list, csv. Default: list
  *
  * ## EXAMPLES
  *
  *     # Export only tables for a single site
  *     $ wp db export --tables=$(wp db tables --url=sub.example.com --format=csv)
  *     Success: Exported to wordpress_dbase.sql
  */
 function tables($args, $assoc_args)
 {
     if (empty($args) && empty($assoc_args)) {
         $assoc_args['scope'] = 'all';
     }
     $tables = WP_CLI\Utils\wp_get_table_names($args, $assoc_args);
     if (!empty($assoc_args['format']) && 'csv' === $assoc_args['format']) {
         WP_CLI::line(implode(',', $tables));
     } else {
         foreach ($tables as $table) {
             WP_CLI::line($table);
         }
     }
 }
コード例 #3
0
ファイル: search-replace.php プロジェクト: mattclegg/wp-cli
 /**
  * Search/replace strings in the database.
  *
  * ## DESCRIPTION
  *
  * This command will go through all rows in a selection of tables
  * and will replace all appearances of the old string with the new one.  The
  * default tables are those registered on the $wpdb object (usually
  * just WordPress core tables).
  *
  * It will correctly handle serialized values, and will not change primary key values.
  *
  * ## OPTIONS
  *
  * <old>
  * : The old string.
  *
  * <new>
  * : The new string.
  *
  * [<table>...]
  * : List of database tables to restrict the replacement to. Wildcards are supported, e.g. wp_\*_options or wp_post\?.
  *
  * [--network]
  * : Search/replace through all the tables in a multisite install.
  *
  * [--skip-columns=<columns>]
  * : Do not perform the replacement in the comma-separated columns.
  *
  * [--dry-run]
  * : Show report, but don't perform the changes.
  *
  * [--precise]
  * : Force the use of PHP (instead of SQL) which is more thorough, but slower. Use if you see issues with serialized data.
  *
  * [--recurse-objects]
  * : Enable recursing into objects to replace strings. Defaults to true; pass --no-recurse-objects to disable.
  *
  * [--all-tables-with-prefix]
  * : Enable replacement on any tables that match the table prefix even if not registered on wpdb
  *
  * [--all-tables]
  * : Enable replacement on ALL tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --network and --all-tables-with-prefix.
  *
  * [--verbose]
  * : Prints rows to the console as they're updated.
  *
  * [--regex]
  * : Runs the search using a regular expression. Warning: search-replace will take about 15-20x longer when using --regex.
  *
  * [--export[=<file>]]
  * : Write transformed data as SQL file instead of performing in-place replacements. If <file> is not supplied, will output to STDOUT.
  *
  * ## EXAMPLES
  *
  *     wp search-replace 'http://example.dev' 'http://example.com' --skip-columns=guid
  *
  *     wp search-replace 'foo' 'bar' wp_posts wp_postmeta wp_terms --dry-run
  *
  *     # Turn your production database into a local database
  *     wp search-replace --url=example.com example.com example.dev wp_\*_options
  *
  *     # Search/replace to a SQL file without transforming the database
  *     wp search-replace foo bar --export=database.sql
  */
 public function __invoke($args, $assoc_args)
 {
     global $wpdb;
     $old = array_shift($args);
     $new = array_shift($args);
     $total = 0;
     $report = array();
     $this->dry_run = \WP_CLI\Utils\get_flag_value($assoc_args, 'dry-run');
     $php_only = \WP_CLI\Utils\get_flag_value($assoc_args, 'precise');
     $this->recurse_objects = \WP_CLI\Utils\get_flag_value($assoc_args, 'recurse-objects', true);
     $this->verbose = \WP_CLI\Utils\get_flag_value($assoc_args, 'verbose');
     $this->regex = \WP_CLI\Utils\get_flag_value($assoc_args, 'regex');
     $this->skip_columns = explode(',', \WP_CLI\Utils\get_flag_value($assoc_args, 'skip-columns'));
     if ($old === $new && !$this->regex) {
         WP_CLI::warning("Replacement value '{$old}' is identical to search value '{$new}'. Skipping operation.");
         exit;
     }
     if (null !== ($export = \WP_CLI\Utils\get_flag_value($assoc_args, 'export'))) {
         if ($this->dry_run) {
             WP_CLI::error('You cannot supply --dry-run and --export at the same time.');
         }
         if (true === $export) {
             $this->export_handle = STDOUT;
             $this->verbose = false;
         } else {
             $this->export_handle = fopen($assoc_args['export'], 'w');
             if (false === $this->export_handle) {
                 WP_CLI::error(sprintf('Unable to open "%s" for writing.', $assoc_args['export']));
             }
         }
         $php_only = true;
     }
     // never mess with hashed passwords
     $this->skip_columns[] = 'user_pass';
     // Get table names based on leftover $args or supplied $assoc_args
     $tables = \WP_CLI\Utils\wp_get_table_names($args, $assoc_args);
     foreach ($tables as $table) {
         if ($this->export_handle) {
             fwrite($this->export_handle, "\nDROP TABLE IF EXISTS `{$table}`;\n");
             $row = $wpdb->get_row("SHOW CREATE TABLE `{$table}`", ARRAY_N);
             fwrite($this->export_handle, $row[1] . ";\n");
             list($table_report, $total_rows) = $this->php_export_table($table, $old, $new);
             $report = array_merge($report, $table_report);
             $total += $total_rows;
             // Don't perform replacements on the actual database
             continue;
         }
         list($primary_keys, $columns, $all_columns) = self::get_columns($table);
         // since we'll be updating one row at a time,
         // we need a primary key to identify the row
         if (empty($primary_keys)) {
             $report[] = array($table, '', 'skipped');
             continue;
         }
         foreach ($columns as $col) {
             if (in_array($col, $this->skip_columns)) {
                 continue;
             }
             if ($this->verbose) {
                 $this->start_time = microtime(true);
                 WP_CLI::log(sprintf('Checking: %s.%s', $table, $col));
             }
             if (!$php_only && !$this->regex) {
                 $serialRow = $wpdb->get_row("SELECT * FROM `{$table}` WHERE `{$col}` REGEXP '^[aiO]:[1-9]' LIMIT 1");
             }
             if ($php_only || $this->regex || NULL !== $serialRow) {
                 $type = 'PHP';
                 $count = $this->php_handle_col($col, $primary_keys, $table, $old, $new);
             } else {
                 $type = 'SQL';
                 $count = $this->sql_handle_col($col, $table, $old, $new);
             }
             $report[] = array($table, $col, $count, $type);
             $total += $count;
         }
     }
     if ($this->export_handle && STDOUT !== $this->export_handle) {
         fclose($this->export_handle);
     }
     // Only informational output after this point
     if (WP_CLI::get_config('quiet') || STDOUT === $this->export_handle) {
         return;
     }
     $table = new \cli\Table();
     $table->setHeaders(array('Table', 'Column', 'Replacements', 'Type'));
     $table->setRows($report);
     $table->display();
     if (!$this->dry_run) {
         if (!empty($assoc_args['export'])) {
             $success_message = "Made {$total} replacements and exported to {$assoc_args['export']}.";
         } else {
             $success_message = "Made {$total} replacements.";
             if ($total && 'Default' !== WP_CLI\Utils\wp_get_cache_type()) {
                 $success_message .= ' Please remember to flush your persistent object cache with `wp cache flush`.';
             }
         }
         WP_CLI::success($success_message);
     }
 }
コード例 #4
0
ファイル: search-replace.php プロジェクト: Jaace/wp-cli
 /**
  * Search/replace strings in the database.
  *
  * ## DESCRIPTION
  *
  * This command will go through all rows in a selection of tables
  * and will replace all appearances of the old string with the new one.  The
  * default tables are those registered on the $wpdb object (usually
  * just WordPress core tables).
  *
  * It will correctly handle serialized values, and will not change primary key values.
  *
  * ## OPTIONS
  *
  * <old>
  * : The old string.
  *
  * <new>
  * : The new string.
  *
  * [<table>...]
  * : List of database tables to restrict the replacement to. Wildcards are supported, e.g. wp_\*_options or wp_post\?.
  *
  * [--network]
  * : Search/replace through all the tables in a multisite install.
  *
  * [--skip-columns=<columns>]
  * : Do not perform the replacement in the comma-separated columns.
  *
  * [--dry-run]
  * : Show report, but don't perform the changes.
  *
  * [--precise]
  * : Force the use of PHP (instead of SQL) which is more thorough, but slower. Use if you see issues with serialized data.
  *
  * [--recurse-objects]
  * : Enable recursing into objects to replace strings. Defaults to true; pass --no-recurse-objects to disable.
  *
  * [--all-tables-with-prefix]
  * : Enable replacement on any tables that match the table prefix even if not registered on wpdb
  *
  * [--all-tables]
  * : Enable replacement on ALL tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --network and --all-tables-with-prefix.
  *
  * [--verbose]
  * : Prints rows to the console as they're updated.
  *
  * [--regex]
  * : Runs the search using a regular expression. Warning: search-replace will take about 15-20x longer when using --regex.
  *
  * ## EXAMPLES
  *
  *     wp search-replace 'http://example.dev' 'http://example.com' --skip-columns=guid
  *
  *     wp search-replace 'foo' 'bar' wp_posts wp_postmeta wp_terms --dry-run
  *
  *     # Turn your production database into a local database
  *     wp search-replace --url=example.com example.com example.dev wp_\*_options
  */
 public function __invoke($args, $assoc_args)
 {
     global $wpdb;
     $old = array_shift($args);
     $new = array_shift($args);
     $total = 0;
     $report = array();
     $dry_run = \WP_CLI\Utils\get_flag_value($assoc_args, 'dry-run');
     $php_only = \WP_CLI\Utils\get_flag_value($assoc_args, 'precise');
     $recurse_objects = \WP_CLI\Utils\get_flag_value($assoc_args, 'recurse-objects', true);
     $verbose = \WP_CLI\Utils\get_flag_value($assoc_args, 'verbose');
     $regex = \WP_CLI\Utils\get_flag_value($assoc_args, 'regex');
     $skip_columns = explode(',', \WP_CLI\Utils\get_flag_value($assoc_args, 'skip-columns'));
     if ($old === $new && !$regex) {
         WP_CLI::warning("Replacement value '{$old}' is identical to search value '{$new}'. Skipping operation.");
         exit;
     }
     // never mess with hashed passwords
     $skip_columns[] = 'user_pass';
     // Get table names based on leftover $args or supplied $assoc_args
     $tables = \WP_CLI\Utils\wp_get_table_names($args, $assoc_args);
     foreach ($tables as $table) {
         list($primary_keys, $columns) = self::get_columns($table);
         // since we'll be updating one row at a time,
         // we need a primary key to identify the row
         if (empty($primary_keys)) {
             $report[] = array($table, '', 'skipped');
             continue;
         }
         foreach ($columns as $col) {
             if (in_array($col, $skip_columns)) {
                 continue;
             }
             if ($verbose) {
                 $this->start_time = microtime(true);
                 WP_CLI::log(sprintf('Checking: %s.%s', $table, $col));
             }
             if (!$php_only) {
                 $serialRow = $wpdb->get_row("SELECT * FROM `{$table}` WHERE `{$col}` REGEXP '^[aiO]:[1-9]' LIMIT 1");
             }
             if ($php_only || $regex || NULL !== $serialRow) {
                 $type = 'PHP';
                 $count = $this->php_handle_col($col, $primary_keys, $table, $old, $new, $dry_run, $recurse_objects, $verbose, $regex);
             } else {
                 $type = 'SQL';
                 $count = $this->sql_handle_col($col, $table, $old, $new, $dry_run, $verbose);
             }
             $report[] = array($table, $col, $count, $type);
             $total += $count;
         }
     }
     if (!WP_CLI::get_config('quiet')) {
         $table = new \cli\Table();
         $table->setHeaders(array('Table', 'Column', 'Replacements', 'Type'));
         $table->setRows($report);
         $table->display();
         if (!$dry_run) {
             $success_message = "Made {$total} replacements.";
             if ($total && 'Default' !== WP_CLI\Utils\wp_get_cache_type()) {
                 $success_message .= ' Please remember to flush your persistent object cache with `wp cache flush`.';
             }
             WP_CLI::success($success_message);
         }
     }
 }