PluginProbe
404 Solution / 4.3.3
404 Solution v4.3.3
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 / DatabaseRecoveryServices.php

DatabaseRecoveryServices.php in 404 Solution 4.3.3, at includes/database/DatabaseRecoveryServices.php

130 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 require_once __DIR__ . '/DatabaseErrorClassifier.php';
8 require_once __DIR__ . '/DatabaseRepairPolicy.php';
9 require_once __DIR__ . '/DatabaseSqlErrorReporter.php';
10 require_once __DIR__ . '/DatabaseCollationHelper.php';
11 require_once __DIR__ . '/DatabaseTableRepairer.php';
12
13 /**
14 * Sub-composition-root for the cohesive "recover and repair from DB failures"
15 * cluster: classify a query error, decide whether to repair, run the repair,
16 * recover from collation mismatches, and report SQL errors to the debug log.
17 *
18 * Owns five collaborators that previously lived as separate fields on
19 * DatabaseCore. They are grouped here because they share one cohesive
20 * concern (what to do when a query fails) and because each one's wiring
21 * needs the same back-references (queryAndGetResults, harvest result,
22 * runtime flags, plugin notices). Concentrating that wiring in one place
23 * shrinks DatabaseCore's field count and centralizes recovery wiring.
24 *
25 * DatabaseCore exposes each of the five components via its existing
26 * per-component accessor methods (errorClassifier(), repairPolicy(),
27 * sqlErrorReporter(), collationHelper(), tableRepairer()); those
28 * accessors now delegate here. The public contract from
29 * DatabaseCoreInterface is unchanged.
30 */
31 class ABJ_404_Solution_DatabaseRecoveryServices {
32
33 /** @var ABJ_404_Solution_DatabaseErrorClassifier */
34 private $errorClassifier;
35
36 /** @var ABJ_404_Solution_DatabaseRepairPolicy */
37 private $repairPolicy;
38
39 /** @var ABJ_404_Solution_DatabaseSqlErrorReporter */
40 private $sqlErrorReporter;
41
42 /** @var ABJ_404_Solution_DatabaseCollationHelper */
43 private $collationHelper;
44
45 /** @var ABJ_404_Solution_DatabaseTableRepairer */
46 private $tableRepairer;
47
48 /**
49 * @param ABJ_404_Solution_DatabaseCore $core
50 * @param ABJ_404_Solution_Functions $functions
51 * @param ABJ_404_Solution_Logging $logger
52 * @param ABJ_404_Solution_DatabaseWpdbResultHarvester $resultHarvester
53 * @param ABJ_404_Solution_DatabaseNoticeStateHolder $noticeState
54 * @param ABJ_404_Solution_DatabaseQueryExecutor $queryExecutor
55 */
56 public function __construct(
57 ABJ_404_Solution_DatabaseCore $core,
58 $functions,
59 $logger,
60 ABJ_404_Solution_DatabaseWpdbResultHarvester $resultHarvester,
61 ABJ_404_Solution_DatabaseNoticeStateHolder $noticeState,
62 ABJ_404_Solution_DatabaseQueryExecutor $queryExecutor
63 ) {
64 $this->errorClassifier = new ABJ_404_Solution_DatabaseErrorClassifier($core, $functions, $logger);
65 $this->repairPolicy = new ABJ_404_Solution_DatabaseRepairPolicy($core, $this->errorClassifier, $functions, $logger);
66 $this->sqlErrorReporter = new ABJ_404_Solution_DatabaseSqlErrorReporter($core, $logger);
67 $this->collationHelper = new ABJ_404_Solution_DatabaseCollationHelper(
68 function (string $query, array $options) use ($core): array {
69 return $core->queryAndGetResults($query, $options);
70 },
71 function (string $tableName) use ($core): string {
72 // Call through $core so test-stub subclass overrides of
73 // getCreateTableDDL are honored, matching pre-extraction behavior.
74 return $core->tableNameResolver()->getCreateTableDDL($tableName);
75 },
76 function (string $key) use ($noticeState) {
77 return $noticeState->getRuntimeFlag($key);
78 },
79 function (string $key, $value, int $ttl) use ($noticeState): void {
80 $noticeState->setRuntimeFlag($key, $value, $ttl);
81 },
82 $logger
83 );
84 $this->tableRepairer = new ABJ_404_Solution_DatabaseTableRepairer(
85 function (string $query, array $options) use ($core): array {
86 return $core->queryAndGetResults($query, $options);
87 },
88 function (array &$result) use ($resultHarvester): void {
89 $resultHarvester->harvestWpdbResult($result);
90 },
91 function () use ($queryExecutor): string {
92 return $queryExecutor->getCurrentResultType();
93 },
94 function (string $type, string $message, string $guidance, string $errorString) use ($noticeState): void {
95 $noticeState->setPluginDbNotice($type, $message, $guidance, $errorString);
96 },
97 function (string $errorText) use ($core): bool {
98 return $core->connectionManager()->resetForRetry($errorText);
99 },
100 $functions,
101 $logger
102 );
103 }
104
105 /** @return ABJ_404_Solution_DatabaseErrorClassifier */
106 public function errorClassifier(): ABJ_404_Solution_DatabaseErrorClassifier {
107 return $this->errorClassifier;
108 }
109
110 /** @return ABJ_404_Solution_DatabaseRepairPolicy */
111 public function repairPolicy(): ABJ_404_Solution_DatabaseRepairPolicy {
112 return $this->repairPolicy;
113 }
114
115 /** @return ABJ_404_Solution_DatabaseSqlErrorReporter */
116 public function sqlErrorReporter(): ABJ_404_Solution_DatabaseSqlErrorReporter {
117 return $this->sqlErrorReporter;
118 }
119
120 /** @return ABJ_404_Solution_DatabaseCollationHelper */
121 public function collationHelper(): ABJ_404_Solution_DatabaseCollationHelper {
122 return $this->collationHelper;
123 }
124
125 /** @return ABJ_404_Solution_DatabaseTableRepairer */
126 public function tableRepairer(): ABJ_404_Solution_DatabaseTableRepairer {
127 return $this->tableRepairer;
128 }
129 }
130