PluginProbe
User Access Manager / 2.2.2
User Access Manager v2.2.2
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 +153 -34 2.3.142.2.2 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
@@ -49,9 +108,9 @@
49 108 $tables[] = $this->databaseObjectFactory->createTable(
50 109 $this->database->getUserGroupTable(),
51 110 $charsetCollate,
52 111 [
53 - $this->databaseObjectFactory->createColumn('ID', 'INT', false, null, true, true),
112 + $this->databaseObjectFactory->createColumn('ID', 'INT(11)', false, null, true, true),
54 113 $this->databaseObjectFactory->createColumn('groupname', 'TINYTEXT'),
55 114 $this->databaseObjectFactory->createColumn('groupdesc', 'TEXT'),
56 115 $this->databaseObjectFactory->createColumn('read_access', 'TINYTEXT'),
57 116 $this->databaseObjectFactory->createColumn('write_access', 'TINYTEXT'),
@@ -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) {
@@ -127,8 +194,9 @@
127 194 unset($existingColumns[$column->getName()]);
128 195
129 196 if ((string) $column !== (string) $existingColumn) {
130 197 $information[self::MODIFIED_COLUMNS][] = [$table, $column];
198 + continue;
131 199 }
132 200 }
133 201
134 202 foreach ($existingColumns as $existingColumn) {
@@ -136,8 +204,10 @@
136 204 }
137 205 }
138 206
139 207 /**
208 + * Returns corrupted database information.
209 + * @return array
140 210 * @throws MissingColumnsException
141 211 */
142 212 public function getCorruptedDatabaseInformation(): array
143 213 {
@@ -159,18 +229,36 @@
159 229
160 230 return $information;
161 231 }
162 232
233 + /**
234 + * Adds a new column.
235 + * @param Table $table
236 + * @param Column $column
237 + * @return bool
238 + */
163 239 private function addColumn(Table $table, Column $column): bool
164 240 {
165 241 return $this->database->query("ALTER TABLE `{$table->getName()}` ADD $column;") !== false;
166 242 }
167 243
244 + /**
245 + * Modify an existing column.
246 + * @param Table $table
247 + * @param Column $column
248 + * @return bool
249 + */
168 250 private function modifyColumn(Table $table, Column $column): bool
169 251 {
170 252 return $this->database->query("ALTER TABLE `{$table->getName()}` MODIFY $column;") !== false;
171 253 }
172 254
255 + /**
256 + * Drops an existing column.
257 + * @param Table $table
258 + * @param Column $column
259 + * @return bool
260 + */
173 261 private function dropColumn(Table $table, Column $column): bool
174 262 {
175 263 return $this->database->query("ALTER TABLE `{$table->getName()}` DROP `{$column->getName()}`;") !== false;
176 264 }
@@ -175,8 +263,11 @@
175 263 return $this->database->query("ALTER TABLE `{$table->getName()}` DROP `{$column->getName()}`;") !== false;
176 264 }
177 265
178 266 /**
267 + * Repairs a corrupt database.
268 + * @param array $information
269 + * @return bool
179 270 * @throws MissingColumnsException
180 271 */
181 272 public function repairDatabase(array $information = []): bool
182 273 {
@@ -201,8 +292,12 @@
201 292
202 293 return $success;
203 294 }
204 295
296 + /**
297 + * Returns all sites where the user access manager is active.
298 + * @return array
299 + */
205 300 private function getActivePluginSites(): array
206 301 {
207 302 $activeSites = [];
208 303
@@ -221,9 +316,10 @@
221 316 return $activeSites;
222 317 }
223 318
224 319 /**
225 - * @throws MissingColumnsException
320 + * Checks if a database update is necessary.
321 + * @return bool
226 322 */
227 323 public function isDatabaseUpdateNecessary(): bool
228 324 {
229 325 if ($this->wordpress->isSuperAdmin() === true) {
@@ -228,14 +324,14 @@
228 324 {
229 325 if ($this->wordpress->isSuperAdmin() === true) {
230 326 foreach ($this->getActivePluginSites() as $siteId) {
231 327 $table = $this->database->getBlogPrefix($siteId) . 'options';
232 - $select = "SELECT option_value FROM $table WHERE option_name = '%s' LIMIT 1";
328 + $select = "SELECT option_value FROM {$table} WHERE option_name = '%s' LIMIT 1";
233 329 $select = $this->database->prepare($select, 'uam_db_version');
234 330 $currentDbVersion = $this->database->getVariable($select);
235 331
236 332 if ($currentDbVersion !== null
237 - && version_compare((string) $currentDbVersion, UserAccessManager::DB_VERSION, '<') === true
333 + && version_compare($currentDbVersion, UserAccessManager::DB_VERSION, '<') === true
238 334 ) {
239 335 return true;
240 336 }
241 337 }
@@ -240,21 +336,19 @@
240 336 }
241 337 }
242 338 }
243 339
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 -
340 + $currentDbVersion = $this->wordpress->getOption('uam_db_version');
251 341 return version_compare($currentDbVersion, UserAccessManager::DB_VERSION, '<');
252 342 }
253 343
344 + /**
345 + * Creates a database backup.
346 + * @return bool
347 + */
254 348 public function backupDatabase(): bool
255 349 {
256 - $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version');
350 + $currentDbVersion = $this->wordpress->getOption('uam_db_version');
257 351
258 352 if (empty($currentDbVersion) === true
259 353 || version_compare($currentDbVersion, '1.2', '<') === true
260 354 ) {
@@ -269,11 +363,11 @@
269 363 $currentDbVersion = str_replace('.', '-', $currentDbVersion);
270 364 $success = true;
271 365
272 366 foreach ($tables as $table) {
273 - $createQuery = "CREATE TABLE `{$table}_$currentDbVersion` LIKE `$table`";
367 + $createQuery = "CREATE TABLE `{$table}_{$currentDbVersion}` LIKE `{$table}`";
274 368 $success = $success && ($this->database->query($createQuery) !== false);
275 - $insertQuery = "INSERT `{$table}_$currentDbVersion` SELECT * FROM `$table`";
369 + $insertQuery = "INSERT `{$table}_{$currentDbVersion}` SELECT * FROM `{$table}`";
276 370 $success = $success && ($this->database->query($insertQuery) !== false);
277 371 }
278 372
279 373 return $success;
@@ -278,12 +372,16 @@
278 372
279 373 return $success;
280 374 }
281 375
376 + /**
377 + * Returns the version for which a backup was created.
378 + * @return array
379 + */
282 380 public function getBackups(): array
283 381 {
284 382 $versions = [];
285 - $tables = $this->database->getColumn(
383 + $tables = (array) $this->database->getColumn(
286 384 "SHOW TABLES LIKE '{$this->database->getPrefix()}uam_%'"
287 385 );
288 386
289 387 foreach ($tables as $table) {
@@ -295,8 +393,13 @@
295 393
296 394 return $versions;
297 395 }
298 396
397 + /**
398 + * Returns the backup tables for the given version.
399 + * @param string $version
400 + * @return array
401 + */
299 402 private function getBackupTables(string $version): array
300 403 {
301 404 $backupTables = [];
302 405 $tables = [
@@ -307,9 +410,9 @@
307 410 $versionForDb = str_replace('.', '-', $version);
308 411
309 412 foreach ($tables as $table) {
310 413 $backupTable = (string) $this->database->getVariable(
311 - "SHOW TABLES LIKE '{$table}_$versionForDb'"
414 + "SHOW TABLES LIKE '{$table}_{$versionForDb}'"
312 415 );
313 416
314 417 if ($backupTable !== '') {
315 418 $backupTables[$table] = $backupTable;
@@ -318,8 +421,13 @@
318 421
319 422 return $backupTables;
320 423 }
321 424
425 + /**
426 + * Reverts the database to the given version.
427 + * @param string $version
428 + * @return bool
429 + */
322 430 public function revertDatabase(string $version): bool
323 431 {
324 432 $success = true;
325 433 $tables = $this->getBackupTables($version);
@@ -324,11 +432,11 @@
324 432 $success = true;
325 433 $tables = $this->getBackupTables($version);
326 434
327 435 foreach ($tables as $table => $backupTable) {
328 - $dropQuery = "DROP TABLE IF EXISTS `$table`";
436 + $dropQuery = "DROP TABLE IF EXISTS `{$table}`";
329 437 $success = $success && ($this->database->query($dropQuery) !== false);
330 - $renameQuery = "RENAME TABLE `$backupTable` TO `$table`";
438 + $renameQuery = "RENAME TABLE `{$backupTable}` TO `{$table}`";
331 439 $success = $success && ($this->database->query($renameQuery) !== false);
332 440 }
333 441
334 442 if ($success === true) {
@@ -337,15 +445,20 @@
337 445
338 446 return $success;
339 447 }
340 448
449 + /**
450 + * Deletes the given database backup.
451 + * @param string $version
452 + * @return bool
453 + */
341 454 public function deleteBackup(string $version): bool
342 455 {
343 456 $success = true;
344 457 $tables = $this->getBackupTables($version);
345 458
346 - foreach ($tables as $backupTable) {
347 - $dropQuery = "DROP TABLE IF EXISTS `$backupTable`";
459 + foreach ($tables as $table => $backupTable) {
460 + $dropQuery = "DROP TABLE IF EXISTS `{$backupTable}`";
348 461 $success = $success && ($this->database->query($dropQuery) !== false);
349 462 }
350 463
351 464 return $success;
@@ -351,8 +464,9 @@
351 464 return $success;
352 465 }
353 466
354 467 /**
468 + * Returns the ordered updates.
355 469 * @return UpdateInterface[]
356 470 */
357 471 private function getOrderedDatabaseUpdates(): array
358 472 {
@@ -366,11 +480,15 @@
366 480 uksort($updates, 'version_compare');
367 481 return $updates;
368 482 }
369 483
484 + /**
485 + * Updates the database.
486 + * @return bool
487 + */
370 488 public function updateDatabase(): bool
371 489 {
372 - $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version');
490 + $currentDbVersion = $this->wordpress->getOption('uam_db_version');
373 491
374 492 if (empty($currentDbVersion) === true) {
375 493 return false;
376 494 }
@@ -390,11 +508,12 @@
390 508 return $success;
391 509 }
392 510
393 511 /**
512 + * Removes the tables.
394 513 * @throws MissingColumnsException
395 514 */
396 - public function removeTables(): void
515 + public function removeTables()
397 516 {
398 517 foreach ($this->getTables() as $table) {
399 518 $dropQuery = "DROP TABLE IF EXISTS `{$table->getName()}`";
400 519 $this->database->query($dropQuery);