PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / database / DatabaseErrorTableInspector.php

DatabaseErrorTableInspector.php in 404 Solution 4.3.0, at includes/database/DatabaseErrorTableInspector.php

88 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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