Skip to content

Commit

Permalink
Merge pull request #153 from aidvu/update/chunk-php-handle-col
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera authored Jan 14, 2021
2 parents e53777b + bba9be5 commit cd03d7e
Showing 1 changed file with 50 additions and 35 deletions.
85 changes: 50 additions & 35 deletions src/Search_Replace_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,51 +529,66 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
$count = 0;
$replacer = new \WP_CLI\SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, null !== $this->log_handle, $this->regex_limit );

$table_sql = self::esc_sql_ident( $table );
$col_sql = self::esc_sql_ident( $col );
$where = $this->regex ? '' : " WHERE $col_sql" . $wpdb->prepare( ' LIKE BINARY %s', '%' . self::esc_like( $old ) . '%' );
$primary_keys_sql = implode( ',', self::esc_sql_ident( $primary_keys ) );
$table_sql = self::esc_sql_ident( $table );
$col_sql = self::esc_sql_ident( $col );
$where = $this->regex ? '' : " WHERE $col_sql" . $wpdb->prepare( ' LIKE BINARY %s', '%' . self::esc_like( $old ) . '%' );
$escaped_primary_keys = self::esc_sql_ident( $primary_keys );
$primary_keys_sql = implode( ',', $escaped_primary_keys );
$order_by_keys = array_map(
function( $key ) {
return "{$key} ASC";
},
$escaped_primary_keys
);
$order_by_sql = 'ORDER BY ' . implode( ',', $order_by_keys );
$limit = 1000;
$offset = 0;

// 2 errors:
// - WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
// - WordPress.CodeAnalysis.AssignmentInCondition -- no reason to do copy-paste for a single valid assignment in while
// phpcs:ignore
while ( $rows = $wpdb->get_results( "SELECT {$primary_keys_sql} FROM {$table_sql} {$where} {$order_by_sql} LIMIT {$limit} OFFSET {$offset}" ) ) {
foreach ( $rows as $keys ) {
$where_sql = '';
foreach ( (array) $keys as $k => $v ) {
if ( strlen( $where_sql ) ) {
$where_sql .= ' AND ';
}
$where_sql .= self::esc_sql_ident( $k ) . ' = ' . self::esc_sql_value( $v );
}

// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
$rows = $wpdb->get_results( "SELECT {$primary_keys_sql} FROM {$table_sql} {$where}" );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
$col_value = $wpdb->get_var( "SELECT {$col_sql} FROM {$table_sql} WHERE {$where_sql}" );

foreach ( $rows as $keys ) {
$where_sql = '';
foreach ( (array) $keys as $k => $v ) {
if ( strlen( $where_sql ) ) {
$where_sql .= ' AND ';
if ( '' === $col_value ) {
continue;
}
$where_sql .= self::esc_sql_ident( $k ) . ' = ' . self::esc_sql_value( $v );
}

// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
$col_value = $wpdb->get_var( "SELECT {$col_sql} FROM {$table_sql} WHERE {$where_sql}" );
$value = $replacer->run( $col_value );

if ( '' === $col_value ) {
continue;
}

$value = $replacer->run( $col_value );
if ( $value === $col_value ) {
continue;
}

if ( $value === $col_value ) {
continue;
}
if ( $this->log_handle ) {
$this->log_php_diff( $col, $keys, $table, $old, $new, $replacer->get_log_data() );
$replacer->clear_log_data();
}

if ( $this->log_handle ) {
$this->log_php_diff( $col, $keys, $table, $old, $new, $replacer->get_log_data() );
$replacer->clear_log_data();
}
if ( $this->dry_run ) {
$count++;
} else {
$update_where = array();
foreach ( (array) $keys as $k => $v ) {
$update_where[ $k ] = $v;
}

if ( $this->dry_run ) {
$count++;
} else {
$where = array();
foreach ( (array) $keys as $k => $v ) {
$where[ $k ] = $v;
$count += $wpdb->update( $table, array( $col => $value ), $update_where );
}

$count += $wpdb->update( $table, array( $col => $value ), $where );
}

$offset += $limit;
}

if ( $this->verbose && 'table' === $this->format ) {
Expand Down

0 comments on commit cd03d7e

Please sign in to comment.