| 1 |
<?php |
| 2 |
/** |
| 3 |
* Table-name parsing and metadata probes for database error handling. |
| 4 |
*/ |
| 5 |
|
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
// allow-no-test-found: covered through DatabaseErrorClassifier facade by tests/MissingTableErrorDoubleReportingTest.php and tests/DataAccessRetrySemanticsTest.php |
| 11 |
class ABJ_404_Solution_DatabaseErrorTableInspector { |
| 12 |
|
| 13 |
/** @var ABJ_404_Solution_Logging */ |
| 14 |
private $logger; |
| 15 |
|
| 16 |
/** |
| 17 |
* @param ABJ_404_Solution_Logging $logger |
| 18 |
*/ |
| 19 |
public function __construct($logger) { |
| 20 |
$this->logger = $logger; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Extract a table name from a MySQL "table is full" error message. |
| 25 |
* |
| 26 |
* @param string $errorText |
| 27 |
* @return string|null |
| 28 |
*/ |
| 29 |
public function extractTableNameFromFullError(string $errorText): ?string { |
| 30 |
if (preg_match("/table '([^']+)' is full/i", $errorText, $m)) { |
| 31 |
return $m[1]; |
| 32 |
} |
| 33 |
return null; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Extract the table name from a MySQL "doesn't exist" error message. |
| 38 |
* |
| 39 |
* @param string $errorText |
| 40 |
* @return string |
| 41 |
*/ |
| 42 |
public function extractMissingTableNameFromError(string $errorText): string { |
| 43 |
if ($errorText === '') { |
| 44 |
return ''; |
| 45 |
} |
| 46 |
if (!preg_match("/Table '([^']+)' doesn't exist/i", $errorText, $matches)) { |
| 47 |
return ''; |
| 48 |
} |
| 49 |
$fullName = $matches[1]; |
| 50 |
$dotPos = strrpos($fullName, '.'); |
| 51 |
return $dotPos !== false ? substr($fullName, $dotPos + 1) : $fullName; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Check if a given table uses the InnoDB storage engine. |
| 56 |
* |
| 57 |
* @param string $tableName |
| 58 |
* @return bool |
| 59 |
*/ |
| 60 |
public function isInnoDBTable(string $tableName): bool { |
| 61 |
global $wpdb; |
| 62 |
/** @var wpdb $wpdb */ |
| 63 |
if (!is_object($wpdb) || !method_exists($wpdb, 'get_var') || !method_exists($wpdb, 'prepare')) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
if (defined('DB_NAME')) { |
| 67 |
$dbName = (string)DB_NAME; |
| 68 |
} else { |
| 69 |
static $warnedNoDbName = false; |
| 70 |
if (!$warnedNoDbName) { |
| 71 |
$warnedNoDbName = true; |
| 72 |
$this->logger->warn(__METHOD__ . ': DB_NAME undefined; using empty schema in InnoDB probe'); |
| 73 |
} |
| 74 |
$dbName = ''; |
| 75 |
} |
| 76 |
// DAO-bypass-approved: read-only information_schema engine probe for classifying table-full DB errors. |
| 77 |
$engine = $wpdb->get_var( |
| 78 |
// DAO-bypass-approved: prepare call for read-only information_schema engine probe placeholders. |
| 79 |
$wpdb->prepare( |
| 80 |
"SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s", |
| 81 |
$dbName, |
| 82 |
$tableName |
| 83 |
) |
| 84 |
); |
| 85 |
return is_string($engine) && strtolower($engine) === 'innodb'; |
| 86 |
} |
| 87 |
} |
| 88 |
|