private function php_handle_col($col, $primary_keys, $table, $old, $new) { global $wpdb; $count = 0; $replacer = new \WP_CLI\SearchReplacer($old, $new, $this->recurse_objects, $this->regex); $where = $this->regex ? '' : " WHERE `{$col}`" . $wpdb->prepare(' LIKE %s', '%' . self::esc_like($old) . '%'); $primary_keys_sql = esc_sql(implode(',', $primary_keys)); $col_sql = esc_sql($col); $table_sql = esc_sql($table); $rows = $wpdb->get_results("SELECT {$primary_keys_sql} FROM {$table_sql}{$where}"); foreach ($rows as $keys) { $where_sql = ''; foreach ((array) $keys as $k => $v) { $where_sql .= "{$k}={$v}"; } $col_value = $wpdb->get_var("SELECT {$col_sql} FROM {$table_sql} WHERE {$where_sql}"); if ('' === $col_value) { continue; } $value = $replacer->run($col_value); if ($value === $col_value) { continue; } if ($this->dry_run) { if ($value != $col_value) { $count++; } } else { $where = array(); foreach ((array) $keys as $k => $v) { $where[$k] = $v; } $count += $wpdb->update($table, array($col => $value), $where); } } if ($this->verbose) { $time = round(microtime(true) - $this->start_time, 3); WP_CLI::log(sprintf('%d rows affected using PHP (in %ss)', $count, $time)); } return $count; }
private static function php_handle_col($col, $primary_keys, $table, $old, $new, $dry_run, $recurse_objects, $verbose, $regex) { global $wpdb; // We don't want to have to generate thousands of rows when running the test suite $chunk_size = getenv('BEHAT_RUN') ? 10 : 1000; $fields = $primary_keys; $fields[] = $col; $args = array('table' => $table, 'fields' => $fields, 'where' => $regex ? '' : "`{$col}`" . ' LIKE "%' . self::esc_like($old) . '%"', 'chunk_size' => $chunk_size); $it = new \WP_CLI\Iterators\Table($args); $count = 0; $replacer = new \WP_CLI\SearchReplacer($old, $new, $recurse_objects, $regex); foreach ($it as $row) { if ('' === $row->{$col}) { continue; } $value = $replacer->run($row->{$col}); if ($dry_run) { if ($value != $row->{$col}) { $count++; } } else { $where = array(); foreach ($primary_keys as $primary_key) { $where[$primary_key] = $row->{$primary_key}; } $count += $wpdb->update($table, array($col => $value), $where); } } if ($verbose) { self::log_verbose_details($table, $col, $count); } return $count; }
private static function recursive_unserialize_replace($from, $to, $data, $serialised = false, $recurse_objects = false) { $replacer = new \WP_CLI\SearchReplacer($from, $to, $recurse_objects); return $replacer->run($data, $serialised); }
private static function handle_col($col, $primary_key, $table, $old, $new, $dry_run, $recurse_objects) { global $wpdb; // We don't want to have to generate thousands of rows when running the test suite $chunk_size = getenv('BEHAT_RUN') ? 10 : 1000; $fields = array($primary_key, $col); $args = array('table' => $table, 'fields' => $fields, 'where' => "`{$col}`" . ' LIKE "%' . like_escape(esc_sql($old)) . '%"', 'chunk_size' => $chunk_size); $it = new \WP_CLI\Iterators\Table($args); $count = 0; $replacer = new \WP_CLI\SearchReplacer($old, $new, $recurse_objects); foreach ($it as $row) { if ('' === $row->{$col}) { continue; } $value = $replacer->run($row->{$col}); if ($dry_run) { if ($value != $row->{$col}) { $count++; } } else { $count += $wpdb->update($table, array($col => $value), array($primary_key => $row->{$primary_key})); } } return $count; }
private function php_handle_col($col, $primary_keys, $table, $old, $new, $dry_run, $recurse_objects, $verbose, $regex) { global $wpdb; // We don't want to have to generate thousands of rows when running the test suite $chunk_size = getenv('BEHAT_RUN') ? 10 : 1000; $fields = $primary_keys; $fields[] = $col; $args = array('table' => $table, 'fields' => $fields, 'where' => $regex ? '' : "`{$col}`" . $wpdb->prepare(' LIKE %s', '%' . self::esc_like($old) . '%'), 'chunk_size' => $chunk_size); $it = new \WP_CLI\Iterators\Table($args); $count = 0; $replacer = new \WP_CLI\SearchReplacer($old, $new, $recurse_objects, $regex); foreach ($it as $row) { if ('' === $row->{$col}) { continue; } $value = $replacer->run($row->{$col}); if ($value === $row->{$col}) { continue; } if ($dry_run) { if ($value != $row->{$col}) { $count++; } } else { $where = array(); foreach ($primary_keys as $primary_key) { $where[$primary_key] = $row->{$primary_key}; } $count += $wpdb->update($table, array($col => $value), $where); } } if ($verbose) { $time = round(microtime(true) - $this->start_time, 3); WP_CLI::log(sprintf('%d rows affected using PHP (in %ss)', $count, $time)); } return $count; }