Files
2meet-data-optimizer/includes/migration/class-tmdo-hot-migration.php
T
wpdev 76c01e44df refactor: 全部 128 個生產檔加入 declare(strict_types=1)(PR-H)
對齊 A v3.2.0。型別強制會把隱式轉換變成 TypeError,所以一次全檔加入
並跑完整測試(unit 451 / integration 398 全綠,無迴歸)。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
2026-07-31 06:13:33 +08:00

194 lines
5.8 KiB
PHP

<?php
/**
* Zone A (Hot) migration for postmeta to flat-column hot table.
*
* @package WP_Data_Optimizer
*/
declare(strict_types=1);
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Zone A (Hot) migration — postmeta → flat-column hot table.
*
* Reads registered hot fields from Schema Registry for a specific post type,
* bulk-copies their postmeta values into wpdo_hot_{post_type}.
*/
class TMDO_Hot_Migration extends TMDO_Migration_Base {
/**
* Post type being migrated.
*
* @var string
*/
private string $post_type;
/**
* Constructor.
*
* @param string $post_type Post type slug to migrate.
*/
public function __construct( string $post_type ) {
$this->post_type = $post_type;
}
/**
* Returns the module identifier.
*
* @return string Module name.
*/
public function get_module(): string {
return TMDO_Zone_Router::module_name( 'hot', $this->post_type );
}
/**
* Returns the zone identifier.
*
* @return string Zone name.
*/
public function get_zone(): string {
return 'hot';
}
/**
* Returns the total number of posts to migrate.
*
* @return int Total post count.
*/
protected function count_source(): int {
global $wpdb;
$meta_keys = $this->get_meta_keys();
if ( empty( $meta_keys ) ) {
return 0;
}
$placeholders = implode( ',', array_fill( 0, count( $meta_keys ), '%s' ) );
$args = array_merge( array( $this->post_type ), $meta_keys );
// Count distinct posts that have at least one hot field.
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- $placeholders built from array_fill with %s; $wpdb->postmeta/$wpdb->posts are core properties.
return (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT pm.post_id)
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE p.post_type = %s AND pm.meta_key IN ({$placeholders})",
...$args
)
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
}
/**
* Migrates one batch of posts to the hot table.
*
* @param int $offset Starting post offset.
* @return int Number of posts processed.
*/
protected function migrate_batch( int $offset ): int {
global $wpdb;
$meta_keys = $this->get_meta_keys();
if ( empty( $meta_keys ) ) {
return 0;
}
$columns = TMDO_Schema_Registry::instance()->get_hot_columns( $this->post_type );
$key_map = $this->build_key_to_column_map();
$placeholders = implode( ',', array_fill( 0, count( $meta_keys ), '%s' ) );
// Get batch of distinct post IDs.
$args = array_merge( array( $this->post_type ), $meta_keys, array( self::BATCH_SIZE, $offset ) );
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- $placeholders/$id_placeholders built from array_fill; $wpdb->postmeta/$wpdb->posts are core properties.
$post_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT DISTINCT pm.post_id
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE p.post_type = %s AND pm.meta_key IN ({$placeholders})
ORDER BY pm.post_id ASC
LIMIT %d OFFSET %d",
...$args
)
);
if ( empty( $post_ids ) ) {
return 0;
}
// Ensure hot table exists.
TMDO_Zone_Hot::ensure_table( $this->post_type );
// For each post, gather all hot meta and upsert.
$id_placeholders = implode( ',', array_fill( 0, count( $post_ids ), '%d' ) );
$meta_rows = $wpdb->get_results(
$wpdb->prepare(
"SELECT post_id, meta_key, meta_value
FROM {$wpdb->postmeta}
WHERE post_id IN ({$id_placeholders}) AND meta_key IN ({$placeholders})",
...array_merge( array_map( 'intval', $post_ids ), $meta_keys )
),
ARRAY_A
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
// Group by post_id.
$grouped = array();
foreach ( $meta_rows ?: array() as $row ) {
$col = $key_map[ $row['meta_key'] ] ?? null;
if ( $col ) {
$grouped[ $row['post_id'] ][ $col ] = $row['meta_value'];
}
}
// Upsert each post's hot data.
foreach ( $grouped as $pid => $data ) {
TMDO_Zone_Hot::set_many( (int) $pid, $this->post_type, $data );
}
return count( $post_ids );
}
/**
* Verifies that the hot table row count is at least as large as the source.
*
* @return bool True if verification passes.
*/
public function verify_counts(): bool {
global $wpdb;
$source = $this->count_source();
$table = TMDO_Zone_Hot::table( $this->post_type );
$target = (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$table}`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from TMDO_Zone_Hot::table()
return $target >= $source;
}
// ── Private helpers ───────────────────────────────────────────────────
/**
* Get all registered hot meta_keys for this post type.
*/
private function get_meta_keys(): array {
$fields = TMDO_Schema_Registry::instance()->get_zone_fields_for_type( 'hot', $this->post_type );
return array_column( $fields, 'meta_key' );
}
/**
* Build meta_key → column_name map.
*/
private function build_key_to_column_map(): array {
$fields = TMDO_Schema_Registry::instance()->get_zone_fields_for_type( 'hot', $this->post_type );
$map = array();
foreach ( $fields as $field ) {
$map[ $field['meta_key'] ] = $field['column'];
}
return $map;
}
}