Repository navigation
feat: add flag --replace-keys to handle replacing string array key values #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,11 @@ class Search_Replace_Command extends WP_CLI_Command { | |
| */ | ||
| private $recurse_objects; | ||
|
|
||
| /** | ||
| * @var bool | ||
| */ | ||
| private $replace_keys; | ||
|
|
||
| /** | ||
| * @var bool | ||
| */ | ||
|
|
@@ -214,6 +219,9 @@ class Search_Replace_Command extends WP_CLI_Command { | |
| * : Enable recursing into objects to replace strings. Defaults to true; | ||
| * pass --no-recurse-objects to disable. | ||
| * | ||
| * [--replace-keys] | ||
| * : Enable replacing string keys in serialized arrays. | ||
| * | ||
| * [--verbose] | ||
| * : Prints rows to the console as they're updated. | ||
| * | ||
|
|
@@ -287,7 +295,7 @@ class Search_Replace_Command extends WP_CLI_Command { | |
| * fi | ||
| * | ||
| * @param array<string> $args Positional arguments. | ||
| * @param array{'old'?: string, 'new'?: string, 'dry-run'?: bool, 'network'?: bool, 'all-tables-with-prefix'?: bool, 'all-tables'?: bool, 'export'?: string, 'export_insert_size'?: string, 'skip-tables'?: string, 'skip-columns'?: string, 'include-columns'?: string, 'precise'?: bool, 'recurse-objects'?: bool, 'verbose'?: bool, 'regex'?: bool, 'regex-flags'?: string, 'regex-delimiter'?: string, 'regex-limit'?: string, 'format': string, 'report'?: bool, 'report-changed-only'?: bool, 'log'?: string, 'before_context'?: string, 'after_context'?: string} $assoc_args Associative arguments. | ||
| * @param array{'old'?: string, 'new'?: string, 'dry-run'?: bool, 'network'?: bool, 'all-tables-with-prefix'?: bool, 'all-tables'?: bool, 'export'?: string, 'export_insert_size'?: string, 'skip-tables'?: string, 'skip-columns'?: string, 'include-columns'?: string, 'precise'?: bool, 'recurse-objects'?: bool, 'replace-keys'?: bool, 'verbose'?: bool, 'regex'?: bool, 'regex-flags'?: string, 'regex-delimiter'?: string, 'regex-limit'?: string, 'format': string, 'report'?: bool, 'report-changed-only'?: bool, 'log'?: string, 'before_context'?: string, 'after_context'?: string} $assoc_args Associative arguments. | ||
| */ | ||
| public function __invoke( $args, $assoc_args ) { | ||
| global $wpdb; | ||
|
|
@@ -333,6 +341,7 @@ public function __invoke( $args, $assoc_args ) { | |
| $this->dry_run = Utils\get_flag_value( $assoc_args, 'dry-run', false ); | ||
| $php_only = Utils\get_flag_value( $assoc_args, 'precise', false ); | ||
| $this->recurse_objects = Utils\get_flag_value( $assoc_args, 'recurse-objects', true ); | ||
| $this->replace_keys = Utils\get_flag_value( $assoc_args, 'replace-keys', false ); | ||
| $this->verbose = Utils\get_flag_value( $assoc_args, 'verbose', false ); | ||
| $this->format = Utils\get_flag_value( $assoc_args, 'format' ); | ||
| $this->regex = Utils\get_flag_value( $assoc_args, 'regex', false ); | ||
|
|
@@ -638,7 +647,7 @@ private function php_export_table( $table, $old, $new ) { | |
| 'chunk_size' => $chunk_size, | ||
| ); | ||
|
|
||
| $replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, false, $this->regex_limit ); | ||
| $replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->replace_keys, $this->regex, $this->regex_flags, $this->regex_delimiter, false, $this->regex_limit ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the $replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, false, $this->regex_limit, $this->replace_keys ); |
||
| $col_counts = array_fill_keys( $all_columns, 0 ); | ||
| if ( $this->verbose && 'table' === $this->format ) { | ||
| $this->start_time = microtime( true ); | ||
|
|
@@ -743,7 +752,7 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) { | |
| global $wpdb; | ||
|
|
||
| $count = 0; | ||
| $replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, null !== $this->log_handle, $this->regex_limit ); | ||
| $replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->replace_keys, $this->regex, $this->regex_flags, $this->regex_delimiter, null !== $this->log_handle, $this->regex_limit ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the $replacer = new SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, null !== $this->log_handle, $this->regex_limit, $this->replace_keys ); |
||
|
|
||
| $table_sql = self::esc_sql_ident( $table ); | ||
| $col_sql = self::esc_sql_ident( $col ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -32,6 +32,11 @@ class SearchReplacer { | |||||
| */ | ||||||
| private $recurse_objects; | ||||||
|
|
||||||
| /** | ||||||
| * @var bool | ||||||
| */ | ||||||
| private $replace_keys; | ||||||
|
|
||||||
| /** | ||||||
| * @var bool | ||||||
| */ | ||||||
|
|
@@ -71,16 +76,18 @@ class SearchReplacer { | |||||
| * @param string $from String we're looking to replace. | ||||||
| * @param string $to What we want it to be replaced with. | ||||||
| * @param bool $recurse_objects Should objects be recursively replaced? | ||||||
| * @param bool $replace_keys Should array keys be replaced? | ||||||
| * @param bool $regex Whether `$from` is a regular expression. | ||||||
| * @param string $regex_flags Flags for regular expression. | ||||||
| * @param string $regex_delimiter Delimiter for regular expression. | ||||||
| * @param bool $logging Whether logging. | ||||||
| * @param integer $regex_limit The maximum possible replacements for each pattern in each subject string. | ||||||
| */ | ||||||
| public function __construct( $from, $to, $recurse_objects = false, $regex = false, $regex_flags = '', $regex_delimiter = '/', $logging = false, $regex_limit = -1 ) { | ||||||
| public function __construct( $from, $to, $recurse_objects = false, $replace_keys = false, $regex = false, $regex_flags = '', $regex_delimiter = '/', $logging = false, $regex_limit = -1 ) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The addition of the public function __construct( $from, $to, $recurse_objects = false, $regex = false, $regex_flags = '', $regex_delimiter = '/', $logging = false, $regex_limit = -1, $replace_keys = false ) {There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With
Suggested change
|
||||||
| $this->from = $from; | ||||||
| $this->to = $to; | ||||||
| $this->recurse_objects = $recurse_objects; | ||||||
| $this->replace_keys = $replace_keys; | ||||||
| $this->regex = $regex; | ||||||
| $this->regex_flags = $regex_flags; | ||||||
| $this->regex_delimiter = $regex_delimiter; | ||||||
|
|
@@ -165,7 +172,17 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis | |||||
| } elseif ( is_array( $data ) ) { | ||||||
| $keys = array_keys( $data ); | ||||||
| foreach ( $keys as $key ) { | ||||||
| $data[ $key ] = $this->run_recursively( $data[ $key ], false, $recursion_level + 1, $visited_data ); | ||||||
| $value = $this->run_recursively( $data[ $key ], false, $recursion_level + 1, $visited_data ); | ||||||
| if ( $this->replace_keys && is_string( $key ) ) { | ||||||
| $replaced_key = $this->run_recursively( $key, false, $recursion_level + 1, $visited_data ); | ||||||
| if ( $replaced_key !== $key ) { | ||||||
| unset( $data[ $key ] ); | ||||||
| $data[ $replaced_key ] = $value; | ||||||
| continue; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| $data[ $key ] = $value; | ||||||
| } | ||||||
|
Comment on lines
+175
to
186
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modifying the array in-place while iterating over its keys can lead to incorrect results and data loss. If a key is replaced with a name that exists later in the original array, the value associated with that new key will be overwritten before it is processed. Furthermore, the loop will eventually reach that new key and process the already-replaced value a second time. To fix this, consider building a new array for the transformed data instead of modifying Example fix: $new_data = [];
foreach ( $data as $key => $value ) {
$new_value = $this->run_recursively( $value, false, $recursion_level + 1, $visited_data );
$new_key = $key;
if ( $this->replace_keys && is_string( $key ) ) {
$new_key = $this->run_recursively( $key, false, $recursion_level + 1, $visited_data );
}
$new_data[ $new_key ] = $new_value;
}
$data = $new_data; |
||||||
| } elseif ( $this->recurse_objects && ( is_object( $data ) || $data instanceof \__PHP_Incomplete_Class ) ) { | ||||||
| if ( $data instanceof \__PHP_Incomplete_Class ) { | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the SQLite object cache,
wp option getreturns the cached value from before the replacement, becausewp search-replacewrites to the table directly.wp cache flushis not available in these tests (cache-command is not a dev dependency), but this works. The same line is needed at lines 353, 374 and 396: