Skip to content

Commit 5f3581a

Browse files
swissspidyclaude
andcommitted
Split --where specifications only on semicolons outside quoted literals
Semicolons inside SQL string literals (with '' or backslash-escaped quotes) no longer cut a condition in two. An unterminated quote is an error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 358342a commit 5f3581a

2 files changed

Lines changed: 104 additions & 2 deletions

File tree

‎features/search-replace-where.feature‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,55 @@ Feature: Test search-replace --where option
355355
Success: 1 replacement to be made.
356356
"""
357357

358+
@require-mysql
359+
Scenario: Where conditions with semicolons inside quoted literals
360+
Given a WP install
361+
And I run `wp option update blogname 'Site foo'`
362+
And I run `wp post create --post_title='a;b foo' --porcelain`
363+
And save STDOUT as {POST1_ID}
364+
And I run `wp post create --post_title="it's;c foo" --porcelain`
365+
And save STDOUT as {POST2_ID}
366+
And I run `wp post create --post_title="it's;d foo" --porcelain`
367+
And save STDOUT as {POST3_ID}
368+
And I run `wp post create --post_title='other foo' --porcelain`
369+
And save STDOUT as {POST4_ID}
370+
371+
When I run `wp search-replace foo bar --where="posts:post_title:post_title IN ('a;b foo', 'it\'s;c foo', 'it''s;d foo');options:option_value:option_name = 'blogname'"`
372+
Then STDOUT should contain:
373+
"""
374+
Success: Made 4 replacements.
375+
"""
376+
377+
When I run `wp post get {POST1_ID} --field=title`
378+
Then STDOUT should be:
379+
"""
380+
a;b bar
381+
"""
382+
383+
When I run `wp post get {POST2_ID} --field=title`
384+
Then STDOUT should be:
385+
"""
386+
it's;c bar
387+
"""
388+
389+
When I run `wp post get {POST3_ID} --field=title`
390+
Then STDOUT should be:
391+
"""
392+
it's;d bar
393+
"""
394+
395+
When I run `wp post get {POST4_ID} --field=title`
396+
Then STDOUT should be:
397+
"""
398+
other foo
399+
"""
400+
401+
When I run `wp option get blogname`
402+
Then STDOUT should be:
403+
"""
404+
Site bar
405+
"""
406+
358407
@require-mysql
359408
Scenario: Malformed where specifications are rejected before any table is processed
360409
Given a WP install
@@ -382,6 +431,13 @@ Feature: Test search-replace --where option
382431
"""
383432
And the return code should be 1
384433

434+
When I try `wp search-replace foo bar --where="posts::post_title = 'Post foo"`
435+
Then STDERR should be:
436+
"""
437+
Error: Invalid --where specification 'posts::post_title = 'Post foo': unterminated ' quote.
438+
"""
439+
And the return code should be 1
440+
385441
When I run `wp post get {POST_ID} --field=title`
386442
Then STDOUT should be:
387443
"""

‎src/Search_Replace_Command.php‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,9 +1581,8 @@ private function log_write( $col, $keys, $table, $old_bits, $new_bits ) {
15811581
*/
15821582
private function develop_where_specs( $str_specs ) {
15831583
global $wpdb;
1584-
$specs = array_filter( explode( ';', (string) $str_specs ) );
15851584
$clauses = [];
1586-
foreach ( $specs as $spec ) {
1585+
foreach ( self::split_where_specs( (string) $str_specs ) as $spec ) {
15871586
$parts = array_map( 'trim', explode( ':', $spec, 3 ) );
15881587
if ( count( $parts ) < 3 ) {
15891588
WP_CLI::error( "Invalid --where specification '{$spec}'. Expected format: <table>[,table]:[column,...]:<SQL-condition>." );
@@ -1604,6 +1603,53 @@ private function develop_where_specs( $str_specs ) {
16041603
return $clauses;
16051604
}
16061605

1606+
/**
1607+
* Splits the --where option into its specifications, on the semicolons outside quoted SQL literals.
1608+
*
1609+
* Inside quotes, a backslash escapes the next character, and a doubled quote character
1610+
* closes and reopens the literal, so neither ends it.
1611+
*
1612+
* @param string $str_specs The --where option value.
1613+
* @return string[] The non-empty specifications.
1614+
*/
1615+
private static function split_where_specs( $str_specs ) {
1616+
$specs = [];
1617+
$current = '';
1618+
$quote = null;
1619+
$length = strlen( $str_specs );
1620+
for ( $i = 0; $i < $length; $i++ ) {
1621+
$char = $str_specs[ $i ];
1622+
if ( null !== $quote ) {
1623+
if ( '\\' === $char && $i + 1 < $length ) {
1624+
$current .= $char . $str_specs[ ++$i ];
1625+
continue;
1626+
}
1627+
if ( $char === $quote ) {
1628+
$quote = null;
1629+
}
1630+
} elseif ( "'" === $char || '"' === $char || '`' === $char ) {
1631+
$quote = $char;
1632+
} elseif ( ';' === $char ) {
1633+
$specs[] = $current;
1634+
$current = '';
1635+
continue;
1636+
}
1637+
$current .= $char;
1638+
}
1639+
1640+
if ( null !== $quote ) {
1641+
WP_CLI::error( "Invalid --where specification '{$current}': unterminated {$quote} quote." );
1642+
}
1643+
$specs[] = $current;
1644+
1645+
return array_values(
1646+
array_filter(
1647+
array_map( 'trim', $specs ),
1648+
'strlen'
1649+
)
1650+
);
1651+
}
1652+
16071653
/**
16081654
* Gets the clause that excludes revisions for a posts or postmeta table, with --no-revisions.
16091655
*

0 commit comments

Comments
 (0)