*/ private $columns = array(); /** @var bool Whether the live schema probe has already been attempted. */ private $loaded = false; /** * @param array{ * tableMetadata: ABJ_404_Solution_DatabaseTableNameResolver, * logger: ABJ_404_Solution_Logging * } $dependencies */ public function __construct(array $dependencies) { $this->tableMetadata = $dependencies['tableMetadata']; $this->logger = $dependencies['logger']; } /** * Return one optional INSERT candidate only when the live table can accept * it. The named request prevents column/value/format argument swaps. * * @param array{columnName: string, value: mixed, format: string} $candidate * @return array{columnName: string, value: mixed, format: string}|null */ public function candidateIfPresent(array $candidate): ?array { return $this->has($candidate['columnName']) ? $candidate : null; } /** * @param string $columnName * @return bool True only when the live table positively reports the column. */ public function has(string $columnName): bool { $key = strtolower($columnName); if ($this->loaded) { return isset($this->columns[$key]); } $this->loaded = true; $redirectsTable = $this->tableMetadata->getPrefixedTableName('abj404_redirects'); $columns = $this->tableMetadata->getTableColumnNames($redirectsTable); if ($columns === array()) { global $wpdb; $lastError = isset($wpdb) && isset($wpdb->last_error) && is_string($wpdb->last_error) ? trim($wpdb->last_error) : ''; $this->logger->warn( 'Could not read the live redirects-table columns; optional redirect fields are omitted' . ' so the base row can still be inserted.' . ($lastError === '' ? '' : ' Database error: ' . $lastError) ); return false; } $this->columns = array_fill_keys(array_map('strtolower', $columns), true); return isset($this->columns[$key]); } }