| 1 |
<?php |
| 2 |
/** |
| 3 |
* SQL error reporting helpers for DataAccess. |
| 4 |
* |
| 5 |
* Extracted from DataAccess.php to keep the main class under the file-size |
| 6 |
* limit. All methods are called via $this-> from DataAccess (trait context) |
| 7 |
* and rely on sibling traits: ErrorClassificationTrait for the isXxxError() |
| 8 |
* predicates, plus the host class's $this->logger and extractSqlFilename(). |
| 9 |
* |
| 10 |
* @since 4.1.8 |
| 11 |
*/ |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
class ABJ_404_Solution_DatabaseSqlErrorReporter { |
| 18 |
|
| 19 |
/** @var ABJ_404_Solution_DatabaseCore */ |
| 20 |
private $core; |
| 21 |
|
| 22 |
/** @var ABJ_404_Solution_Logging */ |
| 23 |
private $logger; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param ABJ_404_Solution_DatabaseCore $core |
| 27 |
* @param ABJ_404_Solution_Logging $logger |
| 28 |
*/ |
| 29 |
public function __construct(ABJ_404_Solution_DatabaseCore $core, $logger) { |
| 30 |
$this->core = $core; |
| 31 |
$this->logger = $logger; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Forward DatabaseCore infrastructure calls that remain owned by the core. |
| 36 |
* |
| 37 |
* @param string $name |
| 38 |
* @param array<int, mixed> $arguments |
| 39 |
* @return mixed |
| 40 |
*/ |
| 41 |
public function __call(string $name, array $arguments) { |
| 42 |
return $this->core->$name(...$arguments); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Log the first observed database error for every query, before retry and |
| 47 |
* recovery paths can mutate or clear wpdb::last_error. |
| 48 |
* |
| 49 |
* @param string $query |
| 50 |
* @param array<string, mixed> $result |
| 51 |
* @param array<string, mixed> $options |
| 52 |
* @param bool $producesRows |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function logObservedSqlError(string $query, array $result, array $options, bool $producesRows): void { |
| 56 |
$lastError = isset($result['last_error']) && is_string($result['last_error']) |
| 57 |
? trim($result['last_error']) : ''; |
| 58 |
if ($lastError === '') { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
// Honor an explicit log_errors=false: the caller has accepted that |
| 63 |
// this query may fail and does not want the failure routed through |
| 64 |
// Logging::errorMessage() (which can email the developer and surface |
| 65 |
// admin notices). The error is still returned in $result['last_error'] |
| 66 |
// so callers can react to it. May 2026: a regression in this function |
| 67 |
// was emailing 35 of 38 4.1.15 sites about benign SHOW CREATE TABLE |
| 68 |
// probes of the transient view_build table. log_errors=false must |
| 69 |
// mean "do not log". |
| 70 |
$logErrors = !array_key_exists('log_errors', $options) || (bool)$options['log_errors']; |
| 71 |
if (!$logErrors) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
// Honor ignore_errors: callers pass substring patterns for errors |
| 76 |
// that are expected and benign for that query (e.g. RENAME TABLE |
| 77 |
// with ignore_errors=["already exists"] in renameAbj404TablesToLowerCase |
| 78 |
// when the lowercase target name pre-exists). Without this check, |
| 79 |
// the observation logger fires ERROR before the downstream |
| 80 |
// ignore_errors branch can suppress, which emails the developer. |
| 81 |
// May 2026: 16+ sites in the email-flood cohort hit this path. |
| 82 |
$ignoreErrorStrings = isset($options['ignore_errors']) && is_array($options['ignore_errors']) |
| 83 |
? $options['ignore_errors'] : array(); |
| 84 |
foreach ($ignoreErrorStrings as $needle) { |
| 85 |
if (is_string($needle) && $needle !== '' && strpos($lastError, $needle) !== false) { |
| 86 |
return; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$sqlInfo = (defined('WP_DEBUG') && WP_DEBUG) ? $query : $this->core->extractSqlFilename($query); |
| 91 |
$elapsed = isset($result['elapsed_time']) && is_numeric($result['elapsed_time']) |
| 92 |
? round((float)$result['elapsed_time'], 4) : 0; |
| 93 |
$message = 'SQL query error observed: ' . $lastError |
| 94 |
. ', SQL: ' . $sqlInfo |
| 95 |
. ', source: ' . $this->core->extractSqlFilename($query) |
| 96 |
. ', route: ' . ($producesRows ? 'get_results' : 'query') |
| 97 |
. ', log_errors_option: ' . ($logErrors ? 'true' : 'false') /** @phpstan-ignore ternary.alwaysTrue */ |
| 98 |
. ', execution_time: ' . $elapsed; |
| 99 |
|
| 100 |
// Infrastructure errors (collation mismatch, disk full, read-only, |
| 101 |
// host quota, deadlock, transient connection drop, etc.) are server |
| 102 |
// and host issues, not plugin bugs. Log as WARN so they appear in |
| 103 |
// the debug log without triggering Logging::errorMessage() email |
| 104 |
// reports. The downstream code at queryAndGetResults() lines 1067+ |
| 105 |
// already classifies these for its own reporting branch; doing the |
| 106 |
// same classification here keeps the two layers consistent. |
| 107 |
// May 2026: 4 of 38 4.1.13 sites in the email-flood cohort were |
| 108 |
// "Illegal mix of collations" reports that should have been WARN. |
| 109 |
if ($this->isInfrastructureSqlError($lastError)) { |
| 110 |
$this->logger->warn($message); |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
$this->logger->errorMessage($message); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Pure classifier: true if the SQL error string matches a known |
| 119 |
* infrastructure / host / hosting-environment failure pattern. These |
| 120 |
* are conditions the plugin can detect and degrade past, not plugin |
| 121 |
* bugs. Centralizing the union here keeps logObservedSqlError() and |
| 122 |
* the downstream reporting branch in queryAndGetResults() consistent. |
| 123 |
* |
| 124 |
* @param string $errorText |
| 125 |
* @return bool |
| 126 |
*/ |
| 127 |
public function isInfrastructureSqlError(string $errorText): bool { |
| 128 |
if ($errorText === '') { |
| 129 |
return false; |
| 130 |
} |
| 131 |
return $this->core->isDiskFullError($errorText) |
| 132 |
|| $this->core->isReadOnlyError($errorText) |
| 133 |
|| $this->core->isQuotaLimitError($errorText) |
| 134 |
|| $this->core->isInvalidDataError($errorText) |
| 135 |
|| $this->core->isCollationError($errorText) |
| 136 |
|| $this->core->isMissingPluginTableError($errorText) |
| 137 |
|| $this->core->isIncorrectKeyFileError($errorText) |
| 138 |
|| $this->core->isCrashedTableError($errorText) |
| 139 |
|| $this->core->isDeadlockOrLockTimeoutError($errorText) |
| 140 |
|| $this->core->isGaleraConflictError($errorText) |
| 141 |
|| $this->core->isTransientConnectionError($errorText) |
| 142 |
|| $this->core->isQueryTimeoutError($errorText) |
| 143 |
|| $this->core->isAccessDeniedError($errorText); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @param string $query |
| 148 |
* @param Throwable $e |
| 149 |
* @param array<string, mixed> $options |
| 150 |
* @param bool $producesRows |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public function logSqlThrowable(string $query, Throwable $e, array $options, bool $producesRows): void { |
| 154 |
$sqlInfo = (defined('WP_DEBUG') && WP_DEBUG) ? $query : $this->core->extractSqlFilename($query); |
| 155 |
$logErrors = !array_key_exists('log_errors', $options) || (bool)$options['log_errors']; |
| 156 |
$message = 'SQL query threw exception: ' . $e->getMessage() |
| 157 |
. ', SQL: ' . $sqlInfo |
| 158 |
. ', source: ' . $this->core->extractSqlFilename($query) |
| 159 |
. ', route: ' . ($producesRows ? 'get_results' : 'query') |
| 160 |
. ', log_errors_option: ' . ($logErrors ? 'true' : 'false'); |
| 161 |
|
| 162 |
$exception = $e instanceof Exception ? $e : new Exception($e->getMessage(), (int)$e->getCode(), $e); |
| 163 |
$this->logger->errorMessage($message, $exception); |
| 164 |
} |
| 165 |
} |
| 166 |
|