Skip to content

Commit 244e895

Browse files
committed
fix(search-replace): preserve references and skip on key collisions
1 parent e9440f3 commit 244e895

2 files changed

Lines changed: 232 additions & 28 deletions

File tree

‎features/search-replace.feature‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,3 +1714,112 @@ Feature: Do global search/replace
17141714
"""
17151715
old.example.com
17161716
"""
1717+
1718+
@require-mysql
1719+
Scenario: Refuse to rename colliding array keys with --recurse-keys
1720+
Given a WP install
1721+
And a setup-recurse-keys-collision.php file:
1722+
"""
1723+
<?php
1724+
update_option( 'recurse_keys_collision_test', array( 'https://old.example.com/key' => 'v1', 'https://new.example.com/key' => 'v2' ), false );
1725+
"""
1726+
And a check-recurse-keys-collision.php file:
1727+
"""
1728+
<?php
1729+
$value = get_option( 'recurse_keys_collision_test' );
1730+
echo json_encode( $value, JSON_UNESCAPED_SLASHES );
1731+
"""
1732+
And I run `wp eval-file setup-recurse-keys-collision.php`
1733+
1734+
When I try `wp search-replace 'https://old.example.com' 'https://new.example.com' --recurse-keys`
1735+
Then the return code should be 0
1736+
And STDOUT should contain:
1737+
"""
1738+
Success:
1739+
"""
1740+
And STDERR should contain:
1741+
"""
1742+
two keys would map onto the same key
1743+
"""
1744+
1745+
When I run `wp eval-file check-recurse-keys-collision.php`
1746+
Then STDOUT should contain:
1747+
"""
1748+
"https://old.example.com/key":"v1"
1749+
"""
1750+
And STDOUT should contain:
1751+
"""
1752+
"https://new.example.com/key":"v2"
1753+
"""
1754+
1755+
@require-mysql
1756+
Scenario: Refuse to rename colliding object property names with --recurse-keys
1757+
Given a WP install
1758+
And a setup-recurse-keys-object-collision.php file:
1759+
"""
1760+
<?php
1761+
update_option( 'recurse_keys_object_collision_test', (object) array( 'https://old.example.com/prop' => 'v1', 'https://new.example.com/prop' => 'v2' ), false );
1762+
"""
1763+
And a check-recurse-keys-object-collision.php file:
1764+
"""
1765+
<?php
1766+
$value = get_option( 'recurse_keys_object_collision_test' );
1767+
echo json_encode( $value, JSON_UNESCAPED_SLASHES );
1768+
"""
1769+
And I run `wp eval-file setup-recurse-keys-object-collision.php`
1770+
1771+
When I try `wp search-replace 'https://old.example.com' 'https://new.example.com' --recurse-keys`
1772+
Then the return code should be 0
1773+
And STDOUT should contain:
1774+
"""
1775+
Success:
1776+
"""
1777+
And STDERR should contain:
1778+
"""
1779+
two property names would map onto the same name
1780+
"""
1781+
1782+
When I run `wp eval-file check-recurse-keys-object-collision.php`
1783+
Then STDOUT should contain:
1784+
"""
1785+
"https://old.example.com/prop":"v1"
1786+
"""
1787+
And STDOUT should contain:
1788+
"""
1789+
"https://new.example.com/prop":"v2"
1790+
"""
1791+
1792+
@require-mysql
1793+
Scenario: Preserve shared references when renaming keys with --recurse-keys
1794+
Given a WP install
1795+
And a setup-recurse-keys-refs.php file:
1796+
"""
1797+
<?php
1798+
$shared = 'https://old.example.com/shared';
1799+
update_option( 'recurse_keys_ref_test', array( 'https://old.example.com/a' => &$shared, 'b' => &$shared ), false );
1800+
"""
1801+
And a check-recurse-keys-refs.php file:
1802+
"""
1803+
<?php
1804+
$raw = $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_ref_test'" );
1805+
$value = get_option( 'recurse_keys_ref_test' );
1806+
echo json_encode( $value, JSON_UNESCAPED_SLASHES ), "\n";
1807+
echo false !== strpos( $raw, 'R:2;' ) ? 'HASREF' : 'NOREF';
1808+
"""
1809+
And I run `wp eval-file setup-recurse-keys-refs.php`
1810+
1811+
When I run `wp search-replace 'https://old.example.com' 'https://new.example.com' --recurse-keys`
1812+
Then STDOUT should contain:
1813+
"""
1814+
Success:
1815+
"""
1816+
1817+
When I run `wp eval-file check-recurse-keys-refs.php`
1818+
Then STDOUT should contain:
1819+
"""
1820+
"https://new.example.com/a":"https://new.example.com/shared"
1821+
"""
1822+
And STDOUT should contain:
1823+
"""
1824+
HASREF
1825+
"""

‎src/WP_CLI/SearchReplacer.php‎

Lines changed: 123 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,14 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
187187
if ( false !== $unserialized ) {
188188
$data = $this->run_recursively( $unserialized, true, $recursion_level + 1 );
189189
} elseif ( is_array( $data ) ) {
190-
$keys = array_keys( $data );
191-
$replaced = array();
192-
foreach ( $keys as $key ) {
193-
$value = $this->run_recursively( $data[ $key ], false, $recursion_level + 1, $visited_data );
194-
$new_key = $key;
195-
if ( $this->recurse_keys && is_string( $key ) ) {
196-
$new_key = $this->replace_string( $key );
190+
if ( $this->recurse_keys ) {
191+
$data = $this->replace_array_keys( $data, $recursion_level, $visited_data );
192+
} else {
193+
$keys = array_keys( $data );
194+
foreach ( $keys as $key ) {
195+
$data[ $key ] = $this->run_recursively( $data[ $key ], false, $recursion_level + 1, $visited_data );
197196
}
198-
$replaced[ $new_key ] = $value;
199197
}
200-
$data = $replaced;
201198
} elseif ( $this->recurse_objects && ( is_object( $data ) || $data instanceof \__PHP_Incomplete_Class ) ) {
202199
if ( $data instanceof \__PHP_Incomplete_Class ) {
203200
$array = new ArrayObject( $data );
@@ -210,26 +207,16 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
210207
} else {
211208
try {
212209
$is_array_access = $data instanceof \ArrayAccess;
213-
$properties = array();
214-
foreach ( $data as $key => $value ) {
215-
$properties[ $key ] = $value;
216-
}
217-
foreach ( $properties as $key => $value ) {
218-
$value = $this->run_recursively( $value, false, $recursion_level + 1, $visited_data );
219-
$new_key = $key;
220-
if ( $this->recurse_keys && is_string( $key ) ) {
221-
$new_key = $this->replace_string( $key );
222-
}
223-
if ( $is_array_access ) {
224-
if ( $new_key !== $key ) {
225-
unset( $data[ $key ] );
210+
if ( $this->recurse_keys ) {
211+
$data = $this->replace_object_property_names( $data, $is_array_access, $recursion_level, $visited_data );
212+
} else {
213+
foreach ( $data as $key => $value ) {
214+
$value = $this->run_recursively( $value, false, $recursion_level + 1, $visited_data );
215+
if ( $is_array_access ) {
216+
$data[ $key ] = $value;
217+
} else {
218+
$data->$key = $value;
226219
}
227-
$data[ $new_key ] = $value;
228-
} else {
229-
if ( $new_key !== $key ) {
230-
unset( $data->$key );
231-
}
232-
$data->$new_key = $value;
233220
}
234221
}
235222
} catch ( \Error $exception ) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
@@ -303,6 +290,114 @@ private function replace_string( $data ) {
303290
return $data;
304291
}
305292

293+
/**
294+
* Recurse into an array, replacing string keys as well as values.
295+
*
296+
* Renamed keys keep their original position. Shared references between
297+
* elements are preserved by iterating by reference. When two keys would
298+
* map onto the same key, the array is left unchanged and a warning is
299+
* emitted instead of silently discarding a value.
300+
*
301+
* @param array $data The array to process.
302+
* @param int $recursion_level Current recursion depth.
303+
* @param array $visited_data Previously seen arrays and objects.
304+
* @return array The processed array.
305+
*/
306+
private function replace_array_keys( $data, $recursion_level, $visited_data ) {
307+
$renames = array();
308+
foreach ( array_keys( $data ) as $key ) {
309+
$renames[ $key ] = is_string( $key ) ? $this->replace_string( $key ) : $key;
310+
}
311+
if ( count( array_unique( $renames, SORT_REGULAR ) ) !== count( $renames ) ) {
312+
\WP_CLI::warning( 'Skipping key replacement for an array: two keys would map onto the same key and a value would be lost.' );
313+
return $data;
314+
}
315+
$replaced = array();
316+
foreach ( $data as $key => &$value ) {
317+
$value = $this->run_recursively( $value, false, $recursion_level + 1, $visited_data );
318+
$replaced[ $renames[ $key ] ] =& $value;
319+
}
320+
unset( $value );
321+
return $replaced;
322+
}
323+
324+
/**
325+
* Recurse into an object, replacing string property names as well as values.
326+
*
327+
* When two property names would map onto the same name, the object is
328+
* left unchanged and a warning is emitted instead of silently
329+
* discarding a value.
330+
*
331+
* @param object $data The object to process.
332+
* @param bool $is_array_access Whether the object implements ArrayAccess.
333+
* @param int $recursion_level Current recursion depth.
334+
* @param array $visited_data Previously seen arrays and objects.
335+
* @return object The processed object.
336+
*/
337+
private function replace_object_property_names( $data, $is_array_access, $recursion_level, $visited_data ) {
338+
$renames = array();
339+
foreach ( $data as $key => $value ) {
340+
$renames[ $key ] = is_string( $key ) ? $this->replace_string( $key ) : $key;
341+
}
342+
if ( count( array_unique( $renames, SORT_REGULAR ) ) !== count( $renames ) ) {
343+
\WP_CLI::warning( 'Skipping key replacement for an object: two property names would map onto the same name and a value would be lost.' );
344+
return $data;
345+
}
346+
foreach ( $renames as $key => $new_key ) {
347+
$value = $this->run_recursively( $this->read_property( $data, $is_array_access, $key ), false, $recursion_level + 1, $visited_data );
348+
if ( $new_key !== $key ) {
349+
$this->remove_property( $data, $is_array_access, $key );
350+
}
351+
$this->write_property( $data, $is_array_access, $new_key, $value );
352+
}
353+
return $data;
354+
}
355+
356+
/**
357+
* Read one property or offset from an object or ArrayAccess instance.
358+
*
359+
* @param object $data The object to read from.
360+
* @param bool $is_array_access Whether the object implements ArrayAccess.
361+
* @param string $key The property name or offset.
362+
* @return mixed The property value.
363+
*/
364+
private function read_property( $data, $is_array_access, $key ) {
365+
return $is_array_access ? $data[ $key ] : $data->$key;
366+
}
367+
368+
/**
369+
* Remove one property or offset from an object or ArrayAccess instance.
370+
*
371+
* @param object $data The object to modify, by reference.
372+
* @param bool $is_array_access Whether the object implements ArrayAccess.
373+
* @param string $key The property name or offset.
374+
* @return void
375+
*/
376+
private function remove_property( &$data, $is_array_access, $key ) {
377+
if ( $is_array_access ) {
378+
unset( $data[ $key ] );
379+
} else {
380+
unset( $data->$key );
381+
}
382+
}
383+
384+
/**
385+
* Write one property or offset on an object or ArrayAccess instance.
386+
*
387+
* @param object $data The object to modify, by reference.
388+
* @param bool $is_array_access Whether the object implements ArrayAccess.
389+
* @param string $key The property name or offset.
390+
* @param mixed $value The value to write.
391+
* @return void
392+
*/
393+
private function write_property( &$data, $is_array_access, $key, $value ) {
394+
if ( $is_array_access ) {
395+
$data[ $key ] = $value;
396+
} else {
397+
$data->$key = $value;
398+
}
399+
}
400+
306401
/**
307402
* Gets existing data saved for this run when logging.
308403
* @return string[] Array of data strings, prior to replacements.

0 commit comments

Comments
 (0)