wp-cli icon indicating copy to clipboard operation
wp-cli copied to clipboard

Multiple substrings are not getting replaced while doing search-replace

Open Kladizkov opened this issue 9 years ago • 8 comments

Today, I tried to do a search and replace on an email. There were a total of 10 occurrences in wp database. One row in wp_options table alone had two occurrence of the old email. Only the first one got replaced during the first run of wp-cli. So only 9 occurrence in total got replaced. I had to run search-replace again in order to replace the second occurrence in that row in wp_options table.

Kladizkov avatar Dec 10 '16 07:12 Kladizkov

Can you share steps to reproduce, please?

danielbachhuber avatar Dec 10 '16 16:12 danielbachhuber

I tried to reproduce the error but could not. It is working as expected.

wp option update my_opt_one [email protected] wp option update my_opt_two [email protected] wp post meta update 1 my_post_meta [email protected] wp search-replace '[email protected]' '[email protected]'

Success: Made 3 replacements.

I tested with v1.0.0 and also with latest master branch.

ernilambar avatar Dec 12 '16 10:12 ernilambar

This is the step to recreate.

I have option_value like this

a:24:{s:18:"notification_email";a:1:{i:0;s:16:"[email protected]";}s:12:"backup_email";a:1:{i:0;s:16:"[email protected]";}s:15:"lockout_message";s:5:"error";s:20:"user_lockout_message";s:64:"You have been locked out due to too many invalid login attempts.";s:25:"community_lockout_message";s:77:"Your IP address has been flagged as a threat by the iThemes Security network.";s:9:"blacklist";b:1;s:15:"blacklist_count";i:3;s:16:"blacklist_period";i:7;s:19:"email_notifications";b:1;s:14:"lockout_period";i:15;s:18:"lockout_white_list";a:0:{}s:12:"log_rotation";i:14;s:8:"log_type";i:0;s:12:"log_location";s:68:"/home/xxxxxxxxx/public_html/wp-content/uploads/ithemes-security/logs";s:14:"allow_tracking";b:0;s:11:"write_files";b:0;s:10:"nginx_file";s:38:"/home/xxxxxxxxx/public_html/nginx.conf";s:24:"infinitewp_compatibility";b:0;s:11:"did_upgrade";b:0;s:9:"lock_file";b:0;s:12:"digest_email";b:0;s:14:"proxy_override";b:0;s:14:"hide_admin_bar";b:0;s:8:"log_info";s:40:"xxxxxxxxx-EVruqkpMtfJbhKHYq8LGqjExCZ6ujc";}

That is a single string with two occurrence of [email protected].

I'm trying to change the email address [email protected] to [email protected]. I did

./wp-cli search-replace [email protected] [email protected]

It changed the first occurrence, but second occurrence remained unchanged. Then I ran the same command again and it changed the other occurrence. So i realize that it doesn't do the search-replace if there is multiple occurrence in a single string ( as if we are not using 'g' ( global ) option in sed )

Kladizkov avatar Dec 12 '16 11:12 Kladizkov

It changed the first occurrence, but second occurrence remained unchanged. Then I ran the same command again and it changed the other occurrence. So i realize that it doesn't do the search-replace if there is multiple occurrence in a single string ( as if we are not using 'g' ( global ) option in sed )

I'd consider this a bug in wp search-replace — and I'm surprised it took us this long to discover it.

We just added passing flags (#3639), so we should use g as the default flag.

danielbachhuber avatar Dec 12 '16 13:12 danielbachhuber

@Kladizkov I tried reproducing this bug, but couldn't :(

$ wp option update test_option '["dxample.org","example.org","dxample.org"]' --format=json
Success: Updated 'test_option' option.
$ wp option get test_option
array (
  0 => 'dxample.org',
  1 => 'example.org',
  2 => 'dxample.org',
)
$ wp search-replace 'dxample\.org' 'example.org' wp_options --regex
+------------+--------------+--------------+------+
| Table      | Column       | Replacements | Type |
+------------+--------------+--------------+------+
| wp_options | option_name  | 0            | PHP  |
| wp_options | option_value | 1            | PHP  |
| wp_options | autoload     | 0            | PHP  |
+------------+--------------+--------------+------+
Success: Made 1 replacements. Please remember to flush your persistent object cache with `wp cache flush`.
$ wp option get test_option
array (
  0 => 'example.org',
  1 => 'example.org',
  2 => 'example.org',
)

The default behavior for preg_replace() (doc) is to replace all matching instances, so we don't actually need the g flag.

Are you able to reproduce this issue on a stock WordPress install? If so, can you send those reproduction steps?

danielbachhuber avatar Dec 15 '16 13:12 danielbachhuber

You can do it in a stock wordpress. ( The video recording https://youtu.be/lu4IRjWZmiA will make it more clear for you )

Lets add a row to wp_options.

INSERT INTO wp_options (option_id, option_name, option_value, autoload) VALUES (NULL, 'itsec_global', 'a:24:{s:18:"notification_email";a:1:{i:0;s:20:"[email protected]";}s:12:"backup_email";a:1:{i:0;s:20:"[email protected]";}s:15:"lockout_message";s:5:"error";s:20:"user_lockout_message";s:64:"You have been locked out due to too many invalid login attempts.";s:25:"community_lockout_message";s:77:"Your IP address has been flagged as a threat by the iThemes Security network.";s:9:"blacklist";b:1;s:15:"blacklist_count";i:3;s:16:"blacklist_period";i:7;s:19:"email_notifications";b:1;s:14:"lockout_period";i:15;s:18:"lockout_white_list";a:0:{}s:12:"log_rotation";i:14;s:8:"log_type";i:0;s:12:"log_location";s:68:"/home/xxxxxxxxx/public_html/wp-content/uploads/ithemes-security/logs";s:14:"allow_tracking";b:0;s:11:"write_files";b:0;s:10:"nginx_file";s:38:"/home/xxxxxxxxx/public_html/nginx.conf";s:24:"infinitewp_compatibility";b:0;s:11:"did_upgrade";b:0;s:9:"lock_file";b:0;s:12:"digest_email";b:0;s:14:"proxy_override";b:0;s:14:"hide_admin_bar";b:0;s:8:"log_info";s:40:"xxxxxxxxx-EVruqkpMtfJbhKHYq8LGqjExCZ6ujc";}', 'no');

Here I'm going to replace two occurrence of [email protected] to example.com

Only one occurrence will change at first execution, then I have to run it again in order to change the second occurrence.

I have a video recording of this issue at https://youtu.be/lu4IRjWZmiA

Kladizkov avatar Dec 16 '16 05:12 Kladizkov

Thanks for the additional details, @Kladizkov.

I've been able to replicate the issue. The cause of the problem is this logic:

if ( is_array( $data ) || is_object( $data ) ) {
	// If we've seen this exact object or array before, short circuit
	if ( in_array( $data, $visited_data, true ) ) {
		return $data; // Avoid infinite loops when there's a cycle
	}
	// Add this data to the list of
	$visited_data[] = $data;
}

This logic was introduced in 29454e3b568a1d9d21e0487ab88579fde94d3b36 (#951)

danielbachhuber avatar Dec 23 '16 23:12 danielbachhuber

@itsananderson Not sure if you're still working with PHP anymore, but thoughts on how you'd approach this problem ^?

Essentially, there are two identical arrays:

array (
  0 => '[email protected]',
)

One isn't a reference to another though, so when the first array is replaced, the second array is skipped. It seems like the problem you were solving is exclusive to referenced variables.

danielbachhuber avatar Dec 23 '16 23:12 danielbachhuber