true, 'version' => self::VERSION, 'ran_at' => gmdate('Y-m-d H:i:s'), 'dry_run' => (bool) $dry_run, 'renamed_leaves' => 0, 'widened_leaves' => 0, 'rows_updated' => 0, 'audit' => array(), 'notes' => array(), ); self::load_schema_helpers(); self::migrate_audiences($report, $dry_run); self::migrate_proposals($report, $dry_run); if (!$dry_run && class_exists('AIEO_Audience_Rules')) { AIEO_Audience_Rules::flush_segment_slugs(); } self::write_summary($report); return $report; } private static function load_schema_helpers() { if (function_exists('aieo_audiences_table') && function_exists('aieo_proposals_table')) { return; } $schema = __DIR__ . '/../dmm/sql/subjects-schema.php'; if (file_exists($schema)) { require_once $schema; } } private static function migrate_audiences(array &$report, $dry_run) { global $wpdb; if (!function_exists('aieo_audiences_table')) { $report['ok'] = false; $report['notes'][] = 'wp_aieo_audiences: schema helper unavailable — pass abandoned, will retry'; return; } $table = aieo_audiences_table(); if (!self::table_exists($table)) { $report['notes'][] = 'wp_aieo_audiences: table absent (fresh install), nothing to migrate'; return; } $after = 0; while (true) { $rows = $wpdb->get_results($wpdb->prepare( "SELECT id, name, rule_json FROM `{$table}` WHERE id > %d ORDER BY id ASC LIMIT %d", $after, self::BATCH_SIZE ), ARRAY_A); if ($wpdb->last_error) { $report['ok'] = false; $report['notes'][] = 'wp_aieo_audiences: read failed — ' . $wpdb->last_error; return; } if (empty($rows)) { break; } foreach ($rows as $row) { $after = (int) $row['id']; $rule = json_decode((string) $row['rule_json'], true); if (!is_array($rule)) { continue; } $renamed = 0; $widened = 0; $new = self::rewrite($rule, $renamed, $widened); if (0 === $renamed && 0 === $widened) { continue; } if (!$dry_run) { $updated = $wpdb->update( $table, array( 'rule_json' => wp_json_encode($new), 'count_cached' => -1, 'count_at' => null, 'updated' => gmdate('Y-m-d H:i:s'), ), array('id' => (int) $row['id']) ); if (false === $updated || $wpdb->last_error) { $report['ok'] = false; $report['notes'][] = 'wp_aieo_audiences: update failed on id ' . (int) $row['id'] . ' — ' . ($wpdb->last_error ?: 'wpdb::update() refused the row'); return; } $report['rows_updated']++; } $report['renamed_leaves'] += $renamed; $report['widened_leaves'] += $widened; self::record($report, array( 'table' => 'audiences', 'id' => (int) $row['id'], 'label' => (string) $row['name'], 'renamed' => $renamed, 'widened' => $widened, )); } } } private static function migrate_proposals(array &$report, $dry_run) { global $wpdb; if (!function_exists('aieo_proposals_table')) { $report['ok'] = false; $report['notes'][] = 'wp_aieo_proposals: schema helper unavailable — pass abandoned, will retry'; return; } $table = aieo_proposals_table(); if (!self::table_exists($table)) { $report['notes'][] = 'wp_aieo_proposals: table absent, nothing to migrate'; return; } $after = 0; $expired = 0; while (true) { $rows = $wpdb->get_results($wpdb->prepare( "SELECT id, title, rule_json FROM `{$table}` WHERE status = 'new' AND id > %d ORDER BY id ASC LIMIT %d", $after, self::BATCH_SIZE ), ARRAY_A); if ($wpdb->last_error) { $report['ok'] = false; $report['notes'][] = 'wp_aieo_proposals: read failed — ' . $wpdb->last_error; return; } if (empty($rows)) { break; } foreach ($rows as $row) { $after = (int) $row['id']; $rule = json_decode((string) $row['rule_json'], true); if (!is_array($rule)) { continue; } $renamed = 0; $widened = 0; self::rewrite($rule, $renamed, $widened); if (0 === $renamed && 0 === $widened) { continue; } if (!$dry_run) { $updated = $wpdb->update( $table, array('status' => 'expired', 'updated' => gmdate('Y-m-d H:i:s')), array('id' => (int) $row['id']) ); if (false === $updated || $wpdb->last_error) { $report['ok'] = false; $report['notes'][] = 'wp_aieo_proposals: update failed on id ' . (int) $row['id'] . ' — ' . ($wpdb->last_error ?: 'wpdb::update() refused the row'); return; } } $expired++; self::record($report, array( 'table' => 'proposals', 'id' => (int) $row['id'], 'label' => (string) $row['title'], 'renamed' => $renamed, 'widened' => $widened, 'action' => 'expired for re-mint', )); } } if ($expired > 0) { $report['proposals_expired'] = $expired; $report['notes'][] = sprintf( '%d open proposal(s) expired so the nightly scanner re-mints them with the new vocabulary and a matching signature.', $expired ); } } private static function rewrite($node, &$renamed, &$widened) { if (!is_array($node)) { return $node; } if (isset($node['conditions'])) { if (is_array($node['conditions'])) { foreach ($node['conditions'] as $i => $child) { $node['conditions'][$i] = self::rewrite($child, $renamed, $widened); } } return $node; } if ('segments' !== (string) ($node['field'] ?? '')) { return $node; } if (!isset($node['value']) || is_array($node['value'])) { return $node; } $value = strtolower(trim((string) $node['value'])); if (self::OLD_OVERDUE === $value) { $node['value'] = self::NEW_OVERDUE; $renamed++; return $node; } if (self::OLD_DORMANT === $value) { $node['field'] = 'dormancy_cohort'; $node['op'] = '!='; $node['value'] = 'active'; $widened++; return $node; } return $node; } private static function record(array &$report, array $entry) { if (count($report['audit']) < self::MAX_AUDIT_ROWS) { $report['audit'][] = $entry; } if (!empty($entry['widened'])) { error_log(sprintf( '[AIEO audience-vocabulary] WIDENED %s #%d "%s": %d dormant leaf/leaves → dormancy_cohort <> \'\' (13,883 → 33,222 on the reference bed, 2.4x).%s', $entry['table'], $entry['id'], $entry['label'], (int) $entry['widened'], isset($entry['action']) ? ' Action: ' . $entry['action'] . '.' : '' )); } if (!empty($entry['renamed'])) { error_log(sprintf( '[AIEO audience-vocabulary] RENAMED %s #%d "%s": %d lapsed leaf/leaves → overdue (same predicate, same people).%s', $entry['table'], $entry['id'], $entry['label'], (int) $entry['renamed'], isset($entry['action']) ? ' Action: ' . $entry['action'] . '.' : '' )); } } private static function write_summary(array $report) { if (empty($report['dry_run'])) { update_option(self::REPORT_OPTION, $report, false); } error_log(sprintf( '[AIEO audience-vocabulary] %s v%s: %d rule(s) renamed lapsed→overdue, %d widened dormant→dormancy_cohort<>\'\', %d row(s) rewritten%s. %s', empty($report['dry_run']) ? 'run' : 'DRY RUN', self::VERSION, (int) $report['renamed_leaves'], (int) $report['widened_leaves'], (int) $report['rows_updated'], isset($report['proposals_expired']) ? ', ' . (int) $report['proposals_expired'] . ' proposal(s) expired' : '', empty($report['ok']) ? 'INCOMPLETE — will retry.' : 'OK.' )); } private static function table_exists($table) { global $wpdb; return (string) $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) === $table; } public static function cli_run($args, $assoc_args) { $dry = !empty($assoc_args['dry-run']); $force = !empty($assoc_args['force']); if (!$dry && !$force && get_option(self::VERSION_OPTION) === self::VERSION) { WP_CLI::success('Already migrated at ' . self::VERSION . ' — pass --force to re-run (it is a no-op by design).'); return; } $report = self::run($dry); foreach ($report['audit'] as $e) { WP_CLI::log(sprintf( ' %s #%d "%s" — renamed %d, widened %d%s', $e['table'], $e['id'], $e['label'], (int) $e['renamed'], (int) $e['widened'], isset($e['action']) ? ' (' . $e['action'] . ')' : '' )); } foreach ($report['notes'] as $n) { WP_CLI::log(' note: ' . $n); } $line = sprintf( '%d renamed lapsed→overdue, %d widened dormant→dormancy_cohort<>\'\', %d rows rewritten.', (int) $report['renamed_leaves'], (int) $report['widened_leaves'], (int) $report['rows_updated'] ); if (empty($report['ok'])) { WP_CLI::warning('Incomplete: ' . $line); return; } if (!$dry) { update_option(self::VERSION_OPTION, self::VERSION, false); } WP_CLI::success(($dry ? 'DRY RUN — ' : '') . $line); } }