| @@ -3,8 +3,10 @@ | ||
| 3 | 3 | declare(strict_types=1); |
| 4 | 4 | |
| 5 | 5 | namespace UserAccessManager\Setup\Update; |
| 6 | 6 | |
| 7 | +use UserAccessManager\Setup\Database\DatabaseUpdate; | |
| 8 | + | |
| 7 | 9 | class DatabaseUpdate1 extends DatabaseUpdate |
| 8 | 10 | { |
| 9 | 11 | public function getVersion(): string |
| 10 | 12 | { |
| @@ -12,23 +14,23 @@ | ||
| 12 | 14 | } |
| 13 | 15 | |
| 14 | 16 | private function updateToUserGroupTableUpdate(string $userGroupTable): bool |
| 15 | 17 | { |
| 16 | - $alterQuery = "ALTER TABLE `{$userGroupTable}` | |
| 17 | - ADD `read_access` TINYTEXT NOT NULL DEFAULT '', | |
| 18 | - ADD `write_access` TINYTEXT NOT NULL DEFAULT '', | |
| 19 | - ADD `ip_range` MEDIUMTEXT NULL DEFAULT ''"; | |
| 18 | + $alterQuery = "ALTER TABLE {$userGroupTable} | |
| 19 | + ADD read_access TINYTEXT NOT NULL DEFAULT '', | |
| 20 | + ADD write_access TINYTEXT NOT NULL DEFAULT '', | |
| 21 | + ADD ip_range MEDIUMTEXT NULL DEFAULT ''"; | |
| 20 | 22 | |
| 21 | 23 | $this->database->query($alterQuery); |
| 22 | 24 | |
| 23 | - $updateQuery = "UPDATE `$userGroupTable` SET `read_access` = 'group', `write_access` = 'group'"; | |
| 25 | + $updateQuery = "UPDATE $userGroupTable SET read_access = 'group', write_access = 'group'"; | |
| 24 | 26 | $success = $this->database->query($updateQuery) !== false; |
| 25 | 27 | |
| 26 | - $selectQuery = "SHOW COLUMNS FROM `$userGroupTable` LIKE 'ip_range'"; | |
| 28 | + $selectQuery = "SHOW columns FROM $userGroupTable LIKE 'ip_range'"; | |
| 27 | 29 | $dbIpRange = (string) $this->database->getVariable($selectQuery); |
| 28 | 30 | |
| 29 | 31 | if ($dbIpRange !== 'ip_range') { |
| 30 | - $alterQuery = "ALTER TABLE `$userGroupTable` ADD `ip_range` MEDIUMTEXT NULL DEFAULT ''"; | |
| 32 | + $alterQuery = "ALTER TABLE $userGroupTable ADD ip_range MEDIUMTEXT NULL DEFAULT ''"; | |
| 31 | 33 | $success = $this->database->query($alterQuery) !== false; |
| 32 | 34 | } |
| 33 | 35 | |
| 34 | 36 | return $success; |
| @@ -33,60 +35,50 @@ | ||
| 33 | 35 | |
| 34 | 36 | return $success; |
| 35 | 37 | } |
| 36 | 38 | |
| 37 | - /** | |
| 38 | - * @param string[] $legacyTables Legacy per-object-type tables, keyed by object type. | |
| 39 | - */ | |
| 40 | - private function getObjectSelectQuery(string $objectType, array $legacyTables): ?string | |
| 41 | - { | |
| 39 | + private function getObjectSelectQuery( | |
| 40 | + string $objectType, | |
| 41 | + string $userGroupToPost, | |
| 42 | + string $userGroupToCategory, | |
| 43 | + string $userGroupToUser, | |
| 44 | + string $userGroupToRole | |
| 45 | + ): ?string { | |
| 46 | + $addition = ''; | |
| 47 | + | |
| 42 | 48 | if ($this->objectHandler->isPostType($objectType) === true) { |
| 43 | - $source = '`' . $legacyTables['post'] . '`, `' . $this->database->getPostsTable() . '`'; | |
| 44 | - | |
| 45 | - return "SELECT `post_id` AS `id`, `group_id` AS `groupId` FROM $source" | |
| 46 | - . " WHERE `post_id` = `ID` AND `post_type` = '$objectType'"; | |
| 47 | - } | |
| 48 | - | |
| 49 | - $idColumns = [ | |
| 50 | - 'category' => 'category_id', | |
| 51 | - 'user' => 'user_id', | |
| 52 | - 'role' => 'role_name' | |
| 53 | - ]; | |
| 54 | - | |
| 55 | - if (isset($idColumns[$objectType]) === false) { | |
| 49 | + $dbIdName = 'post_id'; | |
| 50 | + $database = $userGroupToPost . ', ' . $this->database->getPostsTable(); | |
| 51 | + $addition = " WHERE post_id = ID AND post_type = '$objectType'"; | |
| 52 | + } elseif ($objectType === 'category') { | |
| 53 | + $dbIdName = 'category_id'; | |
| 54 | + $database = $userGroupToCategory; | |
| 55 | + } elseif ($objectType === 'user') { | |
| 56 | + $dbIdName = 'user_id'; | |
| 57 | + $database = $userGroupToUser; | |
| 58 | + } elseif ($objectType === 'role') { | |
| 59 | + $dbIdName = 'role_name'; | |
| 60 | + $database = $userGroupToRole; | |
| 61 | + } else { | |
| 56 | 62 | return null; |
| 57 | 63 | } |
| 58 | 64 | |
| 59 | - return "SELECT `{$idColumns[$objectType]}` AS `id`, `group_id` AS `groupId` | |
| 60 | - FROM `{$legacyTables[$objectType]}`"; | |
| 65 | + return "SELECT $dbIdName AS id, group_id AS groupId FROM $database $addition"; | |
| 61 | 66 | } |
| 62 | 67 | |
| 63 | - /** | |
| 64 | - * The object types whose assignments the legacy tables can hold. getObjectTypes() only | |
| 65 | - * knows post types and taxonomies, so the user and role types have to be added for | |
| 66 | - * their own legacy tables, whose rows would otherwise be dropped unmigrated. | |
| 67 | - * | |
| 68 | - * @return string[] | |
| 69 | - */ | |
| 70 | - private function getMigratableObjectTypes(): array | |
| 71 | - { | |
| 72 | - return array_merge($this->objectHandler->getObjectTypes(), ['user', 'role']); | |
| 73 | - } | |
| 74 | - | |
| 75 | 68 | private function updateToUserGroupToObjectTableUpdate(): bool |
| 76 | 69 | { |
| 77 | 70 | $prefix = $this->database->getPrefix(); |
| 78 | - $charsetCollate = $this->database->getColumnCharset(); | |
| 71 | + | |
| 72 | + $charsetCollate = $this->database->getCharset(); | |
| 79 | 73 | $userGroupToObject = $prefix . 'uam_accessgroup_to_object'; |
| 80 | - $legacyTables = [ | |
| 81 | - 'post' => $prefix . 'uam_accessgroup_to_post', | |
| 82 | - 'user' => $prefix . 'uam_accessgroup_to_user', | |
| 83 | - 'category' => $prefix . 'uam_accessgroup_to_category', | |
| 84 | - 'role' => $prefix . 'uam_accessgroup_to_role' | |
| 85 | - ]; | |
| 74 | + $userGroupToPost = $prefix . 'uam_accessgroup_to_post'; | |
| 75 | + $userGroupToUser = $prefix . 'uam_accessgroup_to_user'; | |
| 76 | + $userGroupToCategory = $prefix . 'uam_accessgroup_to_category'; | |
| 77 | + $userGroupToRole = $prefix . 'uam_accessgroup_to_role'; | |
| 86 | 78 | |
| 87 | - $alterQuery = "ALTER TABLE `$userGroupToObject` | |
| 88 | - CHANGE `object_id` `object_id` VARCHAR(64) $charsetCollate"; | |
| 79 | + $alterQuery = "ALTER TABLE '$userGroupToObject' | |
| 80 | + CHANGE 'object_id' 'object_id' VARCHAR(64) $charsetCollate"; | |
| 89 | 81 | $success = $this->database->query($alterQuery) !== false; |
| 90 | 82 | |
| 91 | 83 | if ($success === false) { |
| 92 | 84 | return false; |
| @@ -91,11 +83,19 @@ | ||
| 91 | 83 | if ($success === false) { |
| 92 | 84 | return false; |
| 93 | 85 | } |
| 94 | 86 | |
| 95 | - foreach ($this->getMigratableObjectTypes() as $objectType) { | |
| 96 | - $query = $this->getObjectSelectQuery($objectType, $legacyTables); | |
| 87 | + $objectTypes = $this->objectHandler->getObjectTypes(); | |
| 97 | 88 | |
| 89 | + foreach ($objectTypes as $objectType) { | |
| 90 | + $query = $this->getObjectSelectQuery( | |
| 91 | + $objectType, | |
| 92 | + $userGroupToPost, | |
| 93 | + $userGroupToCategory, | |
| 94 | + $userGroupToUser, | |
| 95 | + $userGroupToRole | |
| 96 | + ); | |
| 97 | + | |
| 98 | 98 | if ($query === null) { |
| 99 | 99 | continue; |
| 100 | 100 | } |
| 101 | 101 | |
| @@ -108,12 +108,11 @@ | ||
| 108 | 108 | 'group_id' => $dbObject->groupId, |
| 109 | 109 | 'object_id' => $dbObject->id, |
| 110 | 110 | 'object_type' => $objectType |
| 111 | 111 | ], |
| 112 | - // All three are strings, roles carry their name as the object id. | |
| 113 | 112 | [ |
| 114 | - '%s', | |
| 115 | - '%s', | |
| 113 | + '%d', | |
| 114 | + '%d', | |
| 116 | 115 | '%s' |
| 117 | 116 | ] |
| 118 | 117 | ); |
| 119 | 118 | $success = $success && $insert !== false; |
| @@ -119,9 +118,12 @@ | ||
| 119 | 118 | $success = $success && $insert !== false; |
| 120 | 119 | } |
| 121 | 120 | } |
| 122 | 121 | |
| 123 | - $dropQuery = 'DROP TABLE IF EXISTS `' . implode('`, `', $legacyTables) . '`'; | |
| 122 | + $dropQuery = "DROP TABLE $userGroupToPost, | |
| 123 | + $userGroupToUser, | |
| 124 | + $userGroupToCategory, | |
| 125 | + $userGroupToRole"; | |
| 124 | 126 | |
| 125 | 127 | return $success && $this->database->query($dropQuery) !== false; |
| 126 | 128 | } |
| 127 | 129 | |