@@ -529,51 +529,66 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
529529 $ count = 0 ;
530530 $ 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 );
531531
532- $ table_sql = self ::esc_sql_ident ( $ table );
533- $ col_sql = self ::esc_sql_ident ( $ col );
534- $ where = $ this ->regex ? '' : " WHERE $ col_sql " . $ wpdb ->prepare ( ' LIKE BINARY %s ' , '% ' . self ::esc_like ( $ old ) . '% ' );
535- $ primary_keys_sql = implode ( ', ' , self ::esc_sql_ident ( $ primary_keys ) );
532+ $ table_sql = self ::esc_sql_ident ( $ table );
533+ $ col_sql = self ::esc_sql_ident ( $ col );
534+ $ where = $ this ->regex ? '' : " WHERE $ col_sql " . $ wpdb ->prepare ( ' LIKE BINARY %s ' , '% ' . self ::esc_like ( $ old ) . '% ' );
535+ $ escaped_primary_keys = self ::esc_sql_ident ( $ primary_keys );
536+ $ primary_keys_sql = implode ( ', ' , $ escaped_primary_keys );
537+ $ order_by_keys = array_map (
538+ function ( $ key ) {
539+ return "{$ key } ASC " ;
540+ },
541+ $ escaped_primary_keys
542+ );
543+ $ order_by_sql = 'ORDER BY ' . implode ( ', ' , $ order_by_keys );
544+ $ limit = 1000 ;
545+ $ offset = 0 ;
546+
547+ // 2 errors:
548+ // - WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
549+ // - WordPress.CodeAnalysis.AssignmentInCondition -- no reason to do copy-paste for a single valid assignment in while
550+ // phpcs:ignore
551+ while ( $ rows = $ wpdb ->get_results ( "SELECT {$ primary_keys_sql } FROM {$ table_sql } {$ where } {$ order_by_sql } LIMIT {$ limit } OFFSET {$ offset }" ) ) {
552+ foreach ( $ rows as $ keys ) {
553+ $ where_sql = '' ;
554+ foreach ( (array ) $ keys as $ k => $ v ) {
555+ if ( strlen ( $ where_sql ) ) {
556+ $ where_sql .= ' AND ' ;
557+ }
558+ $ where_sql .= self ::esc_sql_ident ( $ k ) . ' = ' . self ::esc_sql_value ( $ v );
559+ }
536560
537- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
538- $ rows = $ wpdb ->get_results ( "SELECT {$ primary_keys_sql } FROM {$ table_sql } { $ where }" );
561+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
562+ $ col_value = $ wpdb ->get_var ( "SELECT {$ col_sql } FROM {$ table_sql } WHERE { $ where_sql }" );
539563
540- foreach ( $ rows as $ keys ) {
541- $ where_sql = '' ;
542- foreach ( (array ) $ keys as $ k => $ v ) {
543- if ( strlen ( $ where_sql ) ) {
544- $ where_sql .= ' AND ' ;
564+ if ( '' === $ col_value ) {
565+ continue ;
545566 }
546- $ where_sql .= self ::esc_sql_ident ( $ k ) . ' = ' . self ::esc_sql_value ( $ v );
547- }
548567
549- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
550- $ col_value = $ wpdb ->get_var ( "SELECT {$ col_sql } FROM {$ table_sql } WHERE {$ where_sql }" );
568+ $ value = $ replacer ->run ( $ col_value );
551569
552- if ( '' === $ col_value ) {
553- continue ;
554- }
555-
556- $ value = $ replacer ->run ( $ col_value );
570+ if ( $ value === $ col_value ) {
571+ continue ;
572+ }
557573
558- if ( $ value === $ col_value ) {
559- continue ;
560- }
574+ if ( $ this ->log_handle ) {
575+ $ this ->log_php_diff ( $ col , $ keys , $ table , $ old , $ new , $ replacer ->get_log_data () );
576+ $ replacer ->clear_log_data ();
577+ }
561578
562- if ( $ this ->log_handle ) {
563- $ this ->log_php_diff ( $ col , $ keys , $ table , $ old , $ new , $ replacer ->get_log_data () );
564- $ replacer ->clear_log_data ();
565- }
579+ if ( $ this ->dry_run ) {
580+ $ count ++;
581+ } else {
582+ $ update_where = array ();
583+ foreach ( (array ) $ keys as $ k => $ v ) {
584+ $ update_where [ $ k ] = $ v ;
585+ }
566586
567- if ( $ this ->dry_run ) {
568- $ count ++;
569- } else {
570- $ where = array ();
571- foreach ( (array ) $ keys as $ k => $ v ) {
572- $ where [ $ k ] = $ v ;
587+ $ count += $ wpdb ->update ( $ table , array ( $ col => $ value ), $ update_where );
573588 }
574-
575- $ count += $ wpdb ->update ( $ table , array ( $ col => $ value ), $ where );
576589 }
590+
591+ $ offset += $ limit ;
577592 }
578593
579594 if ( $ this ->verbose && 'table ' === $ this ->format ) {
0 commit comments