PluginProbe
User Access Manager / 2.2.21
User Access Manager v2.2.21
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/Setup/Database/DatabaseHandler.php +152 -34 2.3.152.2.21 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * DatabaseHandler.php
4 + *
5 + * The DatabaseHandler class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Setup\Database;
@@ -9,36 +22,82 @@
9 22 use UserAccessManager\Setup\Update\UpdateInterface;
10 23 use UserAccessManager\UserAccessManager;
11 24 use UserAccessManager\Wrapper\Wordpress;
12 25
26 +/**
27 + * Class DatabaseHandler
28 + *
29 + * @package UserAccessManager\Setup\Database
30 + */
13 31 class DatabaseHandler
14 32 {
15 - public const MISSING_TABLES = 'MISSING_TABLE';
16 - public const MISSING_COLUMNS = 'MISSING_COLUMNS';
17 - public const MODIFIED_COLUMNS = 'MODIFIED_COLUMNS';
18 - public const EXTRA_COLUMNS = 'EXTRA_COLUMNS';
33 + const MISSING_TABLES = 'MISSING_TABLE';
34 + const MISSING_COLUMNS = 'MISSING_COLUMNS';
35 + const MODIFIED_COLUMNS = 'MODIFIED_COLUMNS';
36 + const EXTRA_COLUMNS = 'EXTRA_COLUMNS';
19 37
38 + /**
39 + * @var Wordpress
40 + */
41 + private $wordpress;
42 +
43 + /**
44 + * @var Database
45 + */
46 + private $database;
47 +
48 + /**
49 + * @var DatabaseObjectFactory
50 + */
51 + private $databaseObjectFactory;
52 +
53 + /**
54 + * @var UpdateFactory
55 + */
56 + private $updateFactory;
57 +
58 + /**
59 + * DatabaseHandler constructor.
60 + * @param Wordpress $wordpress
61 + * @param Database $database
62 + * @param DatabaseObjectFactory $databaseObjectFactory
63 + * @param UpdateFactory $updateFactory
64 + */
20 65 public function __construct(
21 - private Wordpress $wordpress,
22 - private Database $database,
23 - private DatabaseObjectFactory $databaseObjectFactory,
24 - private UpdateFactory $updateFactory
66 + Wordpress $wordpress,
67 + Database $database,
68 + DatabaseObjectFactory $databaseObjectFactory,
69 + UpdateFactory $updateFactory
25 70 ) {
71 + $this->wordpress = $wordpress;
72 + $this->database = $database;
73 + $this->databaseObjectFactory = $databaseObjectFactory;
74 + $this->updateFactory = $updateFactory;
26 75 }
27 76
77 + /**
78 + * Checks if the table exists.
79 + * @param string $table
80 + * @return bool
81 + */
28 82 private function tableExists(string $table): bool
29 83 {
30 - $dbTable = $this->database->getVariable("SHOW TABLES LIKE '$table'");
84 + $dbTable = $this->database->getVariable("SHOW TABLES LIKE '{$table}'");
31 85
32 86 return ($table === $dbTable);
33 87 }
34 88
35 - private function addTable(Table $table): void
89 + /**
90 + * Adds a table.
91 + * @param Table $table
92 + */
93 + private function addTable(Table $table)
36 94 {
37 95 $this->database->dbDelta((string) $table);
38 96 }
39 97
40 98 /**
99 + * Returns all tables.
41 100 * @return Table[]
42 101 * @throws MissingColumnsException
43 102 */
44 103 private function getTables(): array
@@ -76,11 +135,12 @@
76 135 return $tables;
77 136 }
78 137
79 138 /**
139 + * Adds the tables to the database.
80 140 * @throws MissingColumnsException
81 141 */
82 - public function install(): void
142 + public function install()
83 143 {
84 144 foreach ($this->getTables() as $table) {
85 145 if ($this->tableExists($table->getName()) === false) {
86 146 $this->addTable($table);
@@ -90,8 +150,10 @@
90 150 $this->wordpress->addOption('uam_db_version', UserAccessManager::DB_VERSION);
91 151 }
92 152
93 153 /**
154 + * Returns the existing columns for a table.
155 + * @param Table $table
94 156 * @return Column[]
95 157 */
96 158 private function getExistingColumns(Table $table): array
97 159 {
@@ -112,9 +174,14 @@
112 174
113 175 return $existingColumns;
114 176 }
115 177
116 - private function addCorruptedRows(Table $table, array &$information): void
178 + /**
179 + * Add corrupted columns to the information array if the are some.
180 + * @param Table $table
181 + * @param array $information
182 + */
183 + private function addCorruptedRows(Table $table, array &$information)
117 184 {
118 185 $existingColumns = $this->getExistingColumns($table);
119 186
120 187 foreach ($table->getColumns() as $column) {
@@ -136,8 +203,10 @@
136 203 }
137 204 }
138 205
139 206 /**
207 + * Returns corrupted database information.
208 + * @return array
140 209 * @throws MissingColumnsException
141 210 */
142 211 public function getCorruptedDatabaseInformation(): array
143 212 {
@@ -159,18 +228,36 @@
159 228
160 229 return $information;
161 230 }
162 231
232 + /**
233 + * Adds a new column.
234 + * @param Table $table
235 + * @param Column $column
236 + * @return bool
237 + */
163 238 private function addColumn(Table $table, Column $column): bool
164 239 {
165 240 return $this->database->query("ALTER TABLE `{$table->getName()}` ADD $column;") !== false;
166 241 }
167 242
243 + /**
244 + * Modify an existing column.
245 + * @param Table $table
246 + * @param Column $column
247 + * @return bool
248 + */
168 249 private function modifyColumn(Table $table, Column $column): bool
169 250 {
170 251 return $this->database->query("ALTER TABLE `{$table->getName()}` MODIFY $column;") !== false;
171 252 }
172 253
254 + /**
255 + * Drops an existing column.
256 + * @param Table $table
257 + * @param Column $column
258 + * @return bool
259 + */
173 260 private function dropColumn(Table $table, Column $column): bool
174 261 {
175 262 return $this->database->query("ALTER TABLE `{$table->getName()}` DROP `{$column->getName()}`;") !== false;
176 263 }
@@ -175,8 +262,11 @@
175 262 return $this->database->query("ALTER TABLE `{$table->getName()}` DROP `{$column->getName()}`;") !== false;
176 263 }
177 264
178 265 /**
266 + * Repairs a corrupt database.
267 + * @param array $information
268 + * @return bool
179 269 * @throws MissingColumnsException
180 270 */
181 271 public function repairDatabase(array $information = []): bool
182 272 {
@@ -201,8 +291,12 @@
201 291
202 292 return $success;
203 293 }
204 294
295 + /**
296 + * Returns all sites where the user access manager is active.
297 + * @return array
298 + */
205 299 private function getActivePluginSites(): array
206 300 {
207 301 $activeSites = [];
208 302
@@ -221,9 +315,10 @@
221 315 return $activeSites;
222 316 }
223 317
224 318 /**
225 - * @throws MissingColumnsException
319 + * Checks if a database update is necessary.
320 + * @return bool
226 321 */
227 322 public function isDatabaseUpdateNecessary(): bool
228 323 {
229 324 if ($this->wordpress->isSuperAdmin() === true) {
@@ -228,14 +323,14 @@
228 323 {
229 324 if ($this->wordpress->isSuperAdmin() === true) {
230 325 foreach ($this->getActivePluginSites() as $siteId) {
231 326 $table = $this->database->getBlogPrefix($siteId) . 'options';
232 - $select = "SELECT option_value FROM $table WHERE option_name = '%s' LIMIT 1";
327 + $select = "SELECT option_value FROM {$table} WHERE option_name = '%s' LIMIT 1";
233 328 $select = $this->database->prepare($select, 'uam_db_version');
234 329 $currentDbVersion = $this->database->getVariable($select);
235 330
236 331 if ($currentDbVersion !== null
237 - && version_compare((string) $currentDbVersion, UserAccessManager::DB_VERSION, '<') === true
332 + && version_compare($currentDbVersion, UserAccessManager::DB_VERSION, '<') === true
238 333 ) {
239 334 return true;
240 335 }
241 336 }
@@ -240,21 +335,19 @@
240 335 }
241 336 }
242 337 }
243 338
244 - $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version');
245 -
246 - if (empty($currentDbVersion) === true) {
247 - $this->install();
248 - $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version');
249 - }
250 -
339 + $currentDbVersion = $this->wordpress->getOption('uam_db_version');
251 340 return version_compare($currentDbVersion, UserAccessManager::DB_VERSION, '<');
252 341 }
253 342
343 + /**
344 + * Creates a database backup.
345 + * @return bool
346 + */
254 347 public function backupDatabase(): bool
255 348 {
256 - $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version');
349 + $currentDbVersion = $this->wordpress->getOption('uam_db_version');
257 350
258 351 if (empty($currentDbVersion) === true
259 352 || version_compare($currentDbVersion, '1.2', '<') === true
260 353 ) {
@@ -269,11 +362,11 @@
269 362 $currentDbVersion = str_replace('.', '-', $currentDbVersion);
270 363 $success = true;
271 364
272 365 foreach ($tables as $table) {
273 - $createQuery = "CREATE TABLE `{$table}_$currentDbVersion` LIKE `$table`";
366 + $createQuery = "CREATE TABLE `{$table}_{$currentDbVersion}` LIKE `{$table}`";
274 367 $success = $success && ($this->database->query($createQuery) !== false);
275 - $insertQuery = "INSERT `{$table}_$currentDbVersion` SELECT * FROM `$table`";
368 + $insertQuery = "INSERT `{$table}_{$currentDbVersion}` SELECT * FROM `{$table}`";
276 369 $success = $success && ($this->database->query($insertQuery) !== false);
277 370 }
278 371
279 372 return $success;
@@ -278,17 +371,21 @@
278 371
279 372 return $success;
280 373 }
281 374
375 + /**
376 + * Returns the version for which a backup was created.
377 + * @return array
378 + */
282 379 public function getBackups(): array
283 380 {
284 381 $versions = [];
285 - $tables = $this->database->getColumn(
382 + $tables = (array) $this->database->getColumn(
286 383 "SHOW TABLES LIKE '{$this->database->getPrefix()}uam_%'"
287 384 );
288 385
289 386 foreach ($tables as $table) {
290 - if (preg_match('/.*_([0-9\-]+)/', $table, $matches) === 1) {
387 + if (preg_match('/.*_([0-9\-]+)/i', $table, $matches) === 1) {
291 388 $version = str_replace('-', '.', $matches[1]);
292 389 $versions[$version] = $version;
293 390 }
294 391 }
@@ -295,8 +392,13 @@
295 392
296 393 return $versions;
297 394 }
298 395
396 + /**
397 + * Returns the backup tables for the given version.
398 + * @param string $version
399 + * @return array
400 + */
299 401 private function getBackupTables(string $version): array
300 402 {
301 403 $backupTables = [];
302 404 $tables = [
@@ -307,9 +409,9 @@
307 409 $versionForDb = str_replace('.', '-', $version);
308 410
309 411 foreach ($tables as $table) {
310 412 $backupTable = (string) $this->database->getVariable(
311 - "SHOW TABLES LIKE '{$table}_$versionForDb'"
413 + "SHOW TABLES LIKE '{$table}_{$versionForDb}'"
312 414 );
313 415
314 416 if ($backupTable !== '') {
315 417 $backupTables[$table] = $backupTable;
@@ -318,8 +420,13 @@
318 420
319 421 return $backupTables;
320 422 }
321 423
424 + /**
425 + * Reverts the database to the given version.
426 + * @param string $version
427 + * @return bool
428 + */
322 429 public function revertDatabase(string $version): bool
323 430 {
324 431 $success = true;
325 432 $tables = $this->getBackupTables($version);
@@ -324,11 +431,11 @@
324 431 $success = true;
325 432 $tables = $this->getBackupTables($version);
326 433
327 434 foreach ($tables as $table => $backupTable) {
328 - $dropQuery = "DROP TABLE IF EXISTS `$table`";
435 + $dropQuery = "DROP TABLE IF EXISTS `{$table}`";
329 436 $success = $success && ($this->database->query($dropQuery) !== false);
330 - $renameQuery = "RENAME TABLE `$backupTable` TO `$table`";
437 + $renameQuery = "RENAME TABLE `{$backupTable}` TO `{$table}`";
331 438 $success = $success && ($this->database->query($renameQuery) !== false);
332 439 }
333 440
334 441 if ($success === true) {
@@ -337,15 +444,20 @@
337 444
338 445 return $success;
339 446 }
340 447
448 + /**
449 + * Deletes the given database backup.
450 + * @param string $version
451 + * @return bool
452 + */
341 453 public function deleteBackup(string $version): bool
342 454 {
343 455 $success = true;
344 456 $tables = $this->getBackupTables($version);
345 457
346 - foreach ($tables as $backupTable) {
347 - $dropQuery = "DROP TABLE IF EXISTS `$backupTable`";
458 + foreach ($tables as $table => $backupTable) {
459 + $dropQuery = "DROP TABLE IF EXISTS `{$backupTable}`";
348 460 $success = $success && ($this->database->query($dropQuery) !== false);
349 461 }
350 462
351 463 return $success;
@@ -351,8 +463,9 @@
351 463 return $success;
352 464 }
353 465
354 466 /**
467 + * Returns the ordered updates.
355 468 * @return UpdateInterface[]
356 469 */
357 470 private function getOrderedDatabaseUpdates(): array
358 471 {
@@ -366,11 +479,15 @@
366 479 uksort($updates, 'version_compare');
367 480 return $updates;
368 481 }
369 482
483 + /**
484 + * Updates the database.
485 + * @return bool
486 + */
370 487 public function updateDatabase(): bool
371 488 {
372 - $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version');
489 + $currentDbVersion = $this->wordpress->getOption('uam_db_version');
373 490
374 491 if (empty($currentDbVersion) === true) {
375 492 return false;
376 493 }
@@ -390,11 +507,12 @@
390 507 return $success;
391 508 }
392 509
393 510 /**
511 + * Removes the tables.
394 512 * @throws MissingColumnsException
395 513 */
396 - public function removeTables(): void
514 + public function removeTables()
397 515 {
398 516 foreach ($this->getTables() as $table) {
399 517 $dropQuery = "DROP TABLE IF EXISTS `{$table->getName()}`";
400 518 $this->database->query($dropQuery);