Skip to content

Commit 42c9851

Browse files
swissspidyclaude
andcommitted
Log the value --callback returned instead of an ordinary replacement
The log (and the dry-run preview) simulated replacing the matches with <new>. Keep the before/after strings of the actual callback run and log the part that changed, without calling the callback again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 471c42b commit 42c9851

2 files changed

Lines changed: 110 additions & 2 deletions

File tree

‎features/search-replace-callback.feature‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,40 @@ Feature: Test search-replace --callback option
268268
"""
269269
foo bar
270270
"""
271+
272+
@require-mysql
273+
Scenario: Log shows the value the callback returned
274+
Given a WP install
275+
And a callback-function.php file:
276+
"""
277+
<?php
278+
function test_callback( $data, $replacement ) {
279+
return str_replace( 'foo', strtoupper( $replacement ), $data );
280+
}
281+
"""
282+
And I run `wp post create --post_title='foo bar' --porcelain`
283+
And save STDOUT as {POST_ID}
284+
285+
When I run `wp search-replace 'foo' 'baz' wp_posts --include-columns=post_title --callback='test_callback' --require=callback-function.php --dry-run --log`
286+
Then STDOUT should contain:
287+
"""
288+
Success: 1 replacement to be made.
289+
"""
290+
And STDOUT should contain:
291+
"""
292+
wp_posts.post_title:{POST_ID}
293+
< foo bar
294+
> BAZ bar
295+
"""
296+
297+
When I run `wp search-replace 'foo' 'baz' wp_posts --include-columns=post_title --callback='test_callback' --require=callback-function.php --log=- --before_context=2 --after_context=2`
298+
Then STDOUT should contain:
299+
"""
300+
Success: Made 1 replacement.
301+
"""
302+
And STDOUT should contain:
303+
"""
304+
wp_posts.post_title:{POST_ID}
305+
< foo b
306+
> BAZ b
307+
"""

‎src/Search_Replace_Command.php‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ static function ( $key ) {
979979
}
980980

981981
if ( $this->log_handle ) {
982-
$this->log_php_diff( $col, $keys, $table, $old, $new, $replacer->get_log_data() );
982+
$this->log_php_diff( $col, $keys, $table, $old, $new, $replacer->get_log_data(), $replacer->get_log_new_data() );
983983
$replacer->clear_log_data();
984984
}
985985

@@ -1418,8 +1418,25 @@ private function log_sql_diff( $col, $primary_keys, $table, $old, $new, $clauses
14181418
* @param string $old Old value to match.
14191419
* @param string $new New value to replace the old value with.
14201420
* @param string[] $log_data Array of data strings before replacements.
1421+
* @param string[] $log_new_data Array of the same data strings after replacements.
14211422
*/
1422-
private function log_php_diff( $col, $keys, $table, $old, $new, $log_data ) {
1423+
private function log_php_diff( $col, $keys, $table, $old, $new, $log_data, $log_new_data = array() ) {
1424+
if ( $this->callback ) {
1425+
// The callback may return anything, so log what it actually returned
1426+
// instead of simulating an ordinary replacement (and never call it again).
1427+
$old_bits = array();
1428+
$new_bits = array();
1429+
foreach ( $log_data as $i => $data ) {
1430+
if ( isset( $log_new_data[ $i ] ) ) {
1431+
list( $old_bits[], $new_bits[] ) = $this->log_changed_bits( $data, $log_new_data[ $i ] );
1432+
}
1433+
}
1434+
if ( $old_bits ) {
1435+
$this->log_write( $col, $keys, $table, $old_bits, $new_bits );
1436+
}
1437+
return;
1438+
}
1439+
14231440
if ( $this->regex ) {
14241441
$search_regex = $this->regex_delimiter . $old . $this->regex_delimiter . $this->regex_flags;
14251442
} else {
@@ -1543,6 +1560,60 @@ static function ( $m ) use ( $matches ) {
15431560
return array( $old_bits, $new_bits );
15441561
}
15451562

1563+
/**
1564+
* Returns the log strings of the part that differs between two data strings, with context.
1565+
*
1566+
* Used for callback replacements, whose result cannot be derived from the matches.
1567+
*
1568+
* @param string $old_data Data before the replacement.
1569+
* @param string $new_data Data after the replacement.
1570+
* @return string[] Two element array containing the old and the new log string.
1571+
*/
1572+
private function log_changed_bits( $old_data, $new_data ) {
1573+
$encoding = $this->log_encoding;
1574+
if ( ! $encoding && ( $this->log_before_context || $this->log_after_context ) && function_exists( 'mb_detect_encoding' ) ) {
1575+
$encoding = mb_detect_encoding( $old_data, null, true /*strict*/ );
1576+
}
1577+
1578+
$old_len = strlen( $old_data );
1579+
$new_len = strlen( $new_data );
1580+
$max = min( $old_len, $new_len );
1581+
1582+
$prefix = 0;
1583+
while ( $prefix < $max && $old_data[ $prefix ] === $new_data[ $prefix ] ) {
1584+
++$prefix;
1585+
}
1586+
$suffix = 0;
1587+
while ( $suffix < $max - $prefix && $old_data[ $old_len - 1 - $suffix ] === $new_data[ $new_len - 1 - $suffix ] ) {
1588+
++$suffix;
1589+
}
1590+
1591+
// Do not cut a UTF-8 multibyte character in two.
1592+
$is_continuation = static function ( $str, $pos ) {
1593+
return $pos < strlen( $str ) && 0x80 === ( ord( $str[ $pos ] ) & 0xC0 );
1594+
};
1595+
while ( $prefix > 0 && ( $is_continuation( $old_data, $prefix ) || $is_continuation( $new_data, $prefix ) ) ) {
1596+
--$prefix;
1597+
}
1598+
while ( $suffix > 0 && ( $is_continuation( $old_data, $old_len - $suffix ) || $is_continuation( $new_data, $new_len - $suffix ) ) ) {
1599+
--$suffix;
1600+
}
1601+
1602+
$before = '';
1603+
$after = '';
1604+
if ( $this->log_before_context && $prefix ) {
1605+
$before = safe_substr( substr( $old_data, 0, $prefix ), -$this->log_before_context, null /*length*/, false /*is_width*/, $encoding );
1606+
}
1607+
if ( $this->log_after_context && $suffix ) {
1608+
$after = safe_substr( substr( $old_data, $old_len - $suffix ), 0, $this->log_after_context, false /*is_width*/, $encoding );
1609+
}
1610+
1611+
$old_log = $this->log_colors['log_old'][0] . substr( $old_data, $prefix, $old_len - $prefix - $suffix ) . $this->log_colors['log_old'][1];
1612+
$new_log = $this->log_colors['log_new'][0] . substr( $new_data, $prefix, $new_len - $prefix - $suffix ) . $this->log_colors['log_new'][1];
1613+
1614+
return array( $before . $old_log . $after, $before . $new_log . $after );
1615+
}
1616+
15461617
/**
15471618
* Outputs the log strings.
15481619
*

0 commit comments

Comments
 (0)