Skip to content

Commit 6003c54

Browse files
swissspidyclaude
andcommitted
Fix --where, --no-revisions and --callback edge cases
--no-revisions: - Exclude posts of the `revision` post type (and their meta) instead of everything that is not published, which also skipped drafts, private posts and attachments. - Do not restrict the run to the posts and postmeta tables. - Apply to the posts and postmeta tables of every site, for --network. --where: - Parenthesize each clause, so an OR cannot escape the AND with the search condition or the primary key paging (which looped forever). - Append the raw clauses after $wpdb->prepare(), so a LIKE "%s..." pattern is not taken as a placeholder. - Apply the clauses to the dry-run count for JSON-escaped search strings. - Restrict columns per table, instead of adding the columns of one specification to --include-columns for all tables. - Refuse --where and --no-revisions with --export, which ignored them. - Rebuild the primary key paging condition per chunk instead of appending one more condition for every chunk. --callback: - Only refuse an explicit --no-precise; the callback failed whenever --precise was not passed. - Pass the table, column and primary key values to the callback for regular (non-export) runs too. - With --regex, only call the callback for strings that match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent a3176e6 commit 6003c54

5 files changed

Lines changed: 300 additions & 69 deletions

File tree

‎features/search-replace-callback.feature‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ Feature: Test search-replace --callback option
150150
"""
151151
<?php
152152
function opts_callback( $data, $replacement, $search_regex, $opts ) {
153+
if ( empty( $opts['table'] ) || empty( $opts['col'] ) || empty( $opts['key']['ID'] ) ) {
154+
throw new Exception( 'Expected $opts to have the table, the column and the primary key values, got: ' . var_export( $opts, true ) );
155+
}
153156
// Only verify opts when we have a match for content columns
154157
if ( strpos( $data, 'foo' ) !== false && isset( $opts['col'] ) && $opts['col'] === 'post_content' ) {
155158
// Verify opts parameter exists and has expected structure
@@ -185,3 +188,28 @@ Feature: Test search-replace --callback option
185188
"""
186189
verified content here
187190
"""
191+
192+
@require-mysql
193+
Scenario: Callback infers --precise when it is not passed
194+
Given a WP install
195+
And a callback-function.php file:
196+
"""
197+
<?php
198+
function test_callback( $data, $replacement ) {
199+
return str_replace( 'foo', strtoupper( $replacement ), $data );
200+
}
201+
"""
202+
And I run `wp post create --post_title='foo bar' --porcelain`
203+
And save STDOUT as {POST_ID}
204+
205+
When I run `wp search-replace 'foo' 'baz' wp_posts --include-columns=post_title --callback='test_callback' --require=callback-function.php`
206+
Then STDOUT should contain:
207+
"""
208+
Success: Made 1 replacement.
209+
"""
210+
211+
When I run `wp post get {POST_ID} --field=title`
212+
Then STDOUT should be:
213+
"""
214+
BAZ bar
215+
"""

‎features/search-replace-revisions.feature‎

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,66 @@ Feature: Test search-replace --revisions option
1010
When I run `wp post create --post_title='Draft foo' --post_status='draft' --porcelain`
1111
Then save STDOUT as {DRAFT_ID}
1212

13+
When I run `wp post create --post_title='Revision foo' --post_type='revision' --post_status='inherit' --post_parent={PUBLISHED_ID} --post_name='{PUBLISHED_ID}-revision-v1' --porcelain`
14+
Then save STDOUT as {REVISION_ID}
15+
1316
When I run `wp post meta add {PUBLISHED_ID} test_key 'published_foo_meta'`
1417
Then STDOUT should not be empty
1518

1619
When I run `wp post meta add {DRAFT_ID} test_key 'draft_foo_meta'`
1720
Then STDOUT should not be empty
1821

19-
When I run `wp search-replace 'foo' 'bar' --no-revisions`
22+
# add_post_meta() would store the meta on the parent post, not the revision.
23+
When I run `wp eval 'add_metadata( "post", {REVISION_ID}, "test_key", "revision_foo_meta" );'`
24+
And I run `wp option add test_option 'option_foo'`
25+
And I run `wp search-replace 'foo' 'bar' --no-revisions`
2026
Then STDOUT should contain:
2127
"""
22-
Success: Made 2 replacements.
28+
Success: Made
2329
"""
2430

25-
# Verify published post was changed
2631
When I run `wp post get {PUBLISHED_ID} --field=title`
2732
Then STDOUT should be:
2833
"""
2934
Published bar
3035
"""
3136

32-
# Verify draft post was NOT changed
37+
# Drafts are not revisions
3338
When I run `wp post get {DRAFT_ID} --field=title`
3439
Then STDOUT should be:
3540
"""
36-
Draft foo
41+
Draft bar
42+
"""
43+
44+
When I run `wp post get {REVISION_ID} --field=title`
45+
Then STDOUT should be:
46+
"""
47+
Revision foo
3748
"""
3849

39-
# Verify published post meta was changed
4050
When I run `wp post meta get {PUBLISHED_ID} test_key`
4151
Then STDOUT should be:
4252
"""
4353
published_bar_meta
4454
"""
4555

46-
# Verify draft post meta was NOT changed
4756
When I run `wp post meta get {DRAFT_ID} test_key`
4857
Then STDOUT should be:
4958
"""
50-
draft_foo_meta
59+
draft_bar_meta
60+
"""
61+
62+
When I run `wp eval 'echo get_metadata( "post", {REVISION_ID}, "test_key", true );'`
63+
Then STDOUT should be:
64+
"""
65+
revision_foo_meta
66+
"""
67+
68+
# Tables other than posts and postmeta are still processed
69+
When I run `wp option get test_option`
70+
Then STDOUT should be:
71+
"""
72+
option_bar
5173
"""
5274

5375
@require-mysql
@@ -57,8 +79,8 @@ Feature: Test search-replace --revisions option
5779
When I run `wp post create --post_title='Published fooooo' --post_name=1 --post_status='publish' --porcelain`
5880
Then save STDOUT as {PUBLISHED_ID}
5981

60-
When I run `wp post create --post_title='Draft fooooo' --post_name=2 --post_status='draft' --porcelain`
61-
Then save STDOUT as {DRAFT_ID}
82+
When I run `wp post create --post_title='Revision fooooo' --post_type='revision' --post_status='inherit' --post_parent={PUBLISHED_ID} --post_name='{PUBLISHED_ID}-revision-v1' --porcelain`
83+
Then save STDOUT as {REVISION_ID}
6284

6385
# With default behavior (--revisions=true), both should be changed
6486
When I run `wp search-replace 'fooooo' 'baz'`
@@ -73,19 +95,19 @@ Feature: Test search-replace --revisions option
7395
Published baz
7496
"""
7597

76-
When I run `wp post get {DRAFT_ID} --field=title`
98+
When I run `wp post get {REVISION_ID} --field=title`
7799
Then STDOUT should be:
78100
"""
79-
Draft baz
101+
Revision baz
80102
"""
81103

82104
@require-mysql
83105
Scenario: Combining no-revisions with regex
84106
Given a WP install
85107
And I run `wp post create --post_title='Test foo123' --post_name='pubslug' --post_status='publish' --porcelain`
86108
And save STDOUT as {PUB_ID}
87-
And I run `wp post create --post_title='Test foo456' --post_name='draftslug' --post_status='draft' --porcelain`
88-
And save STDOUT as {DRAFT_ID}
109+
And I run `wp post create --post_title='Test foo456' --post_type='revision' --post_status='inherit' --post_parent={PUB_ID} --post_name='{PUB_ID}-revision-v1' --porcelain`
110+
And save STDOUT as {REVISION_ID}
89111

90112
When I run `wp search-replace 'foo[0-9]+' 'bar999' --regex --no-revisions`
91113
Then STDOUT should contain:
@@ -99,8 +121,45 @@ Feature: Test search-replace --revisions option
99121
Test bar999
100122
"""
101123

102-
When I run `wp post get {DRAFT_ID} --field=title`
124+
When I run `wp post get {REVISION_ID} --field=title`
103125
Then STDOUT should be:
104126
"""
105127
Test foo456
106128
"""
129+
130+
@require-mysql
131+
Scenario: Excluding revisions on all sites of a network
132+
Given a WP multisite install
133+
And I run `wp site create --slug=second --porcelain`
134+
And I run `wp post create --url=example.com/second --post_title='Second foo' --post_name='secondslug' --post_status='publish' --porcelain`
135+
And save STDOUT as {POST_ID}
136+
And I run `wp post create --url=example.com/second --post_title='Second revision foo' --post_type='revision' --post_status='inherit' --post_parent={POST_ID} --post_name='{POST_ID}-revision-v1' --porcelain`
137+
And save STDOUT as {REVISION_ID}
138+
139+
When I run `wp search-replace 'foo' 'bar' --network --no-revisions`
140+
Then STDOUT should contain:
141+
"""
142+
Success:
143+
"""
144+
145+
When I run `wp post get {POST_ID} --url=example.com/second --field=title`
146+
Then STDOUT should be:
147+
"""
148+
Second bar
149+
"""
150+
151+
When I run `wp post get {REVISION_ID} --url=example.com/second --field=title`
152+
Then STDOUT should be:
153+
"""
154+
Second revision foo
155+
"""
156+
157+
Scenario: Excluding revisions cannot be combined with an export
158+
Given a WP install
159+
160+
When I try `wp search-replace 'foo' 'bar' --no-revisions --export=out.sql`
161+
Then STDERR should be:
162+
"""
163+
Error: You cannot supply --where or --no-revisions together with --export.
164+
"""
165+
And the return code should be 1

‎features/search-replace-where.feature‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,89 @@ Feature: Test search-replace --where option
278278
"""
279279
rows affected
280280
"""
281+
282+
@require-mysql
283+
Scenario: Where specifications with and without columns for different tables
284+
Given a WP install
285+
And I run `wp post create --post_title='Title foo' --post_content='Content foo' --post_status='publish' --porcelain`
286+
And save STDOUT as {POST_ID}
287+
And I run `wp option add test_option 'option_foo'`
288+
289+
When I run `wp search-replace 'foo' 'bar' --where='posts:post_title:post_status="publish";options::option_name="test_option"'`
290+
Then STDOUT should contain:
291+
"""
292+
Success: Made 2 replacements.
293+
"""
294+
295+
When I run `wp post get {POST_ID} --field=title`
296+
Then STDOUT should be:
297+
"""
298+
Title bar
299+
"""
300+
301+
When I run `wp post get {POST_ID} --field=content`
302+
Then STDOUT should be:
303+
"""
304+
Content foo
305+
"""
306+
307+
When I run `wp option get test_option`
308+
Then STDOUT should be:
309+
"""
310+
option_bar
311+
"""
312+
313+
@require-mysql
314+
Scenario: Where conditions with OR and with LIKE patterns
315+
Given a WP install
316+
And I run `wp post create --post_title='Foo sale' --post_status='publish' --porcelain`
317+
And save STDOUT as {SALE_ID}
318+
And I run `wp post create --post_title='Foo other' --post_status='publish' --porcelain`
319+
And save STDOUT as {OTHER_ID}
320+
And I run `wp post create --post_title='nothing here' --post_status='private' --porcelain`
321+
322+
When I run `wp search-replace 'Foo' 'Bar' --where='posts::post_status="publish" OR post_status="private"' --dry-run`
323+
Then STDOUT should contain:
324+
"""
325+
Success: 2 replacements to be made.
326+
"""
327+
328+
When I run `wp search-replace 'Foo' 'Bar' --where='posts::post_title LIKE "%sale%"'`
329+
Then STDOUT should contain:
330+
"""
331+
Success: Made 1 replacement.
332+
"""
333+
334+
When I run `wp post get {SALE_ID} --field=title`
335+
Then STDOUT should be:
336+
"""
337+
Bar sale
338+
"""
339+
340+
When I run `wp post get {OTHER_ID} --field=title`
341+
Then STDOUT should be:
342+
"""
343+
Foo other
344+
"""
345+
346+
@require-mysql
347+
Scenario: Where clause with dry-run and a search string that differs when JSON-encoded
348+
Given a WP install
349+
And I run `wp post create --post_title='http://Foo.test published' --post_status='publish' --porcelain`
350+
And I run `wp post create --post_title='http://Foo.test draft' --post_status='draft' --porcelain`
351+
352+
When I run `wp search-replace 'http://Foo.test' 'http://bar.test' wp_posts --where='posts::post_status="publish"' --dry-run`
353+
Then STDOUT should contain:
354+
"""
355+
Success: 1 replacement to be made.
356+
"""
357+
358+
Scenario: Where clause cannot be combined with an export
359+
Given a WP install
360+
361+
When I try `wp search-replace 'foo' 'bar' --where='posts::post_status="publish"' --export=out.sql`
362+
Then STDERR should be:
363+
"""
364+
Error: You cannot supply --where or --no-revisions together with --export.
365+
"""
366+
And the return code should be 1

0 commit comments

Comments
 (0)