| @@ -3,10 +3,8 @@ | ||
| 3 | 3 | declare(strict_types=1); |
| 4 | 4 | |
| 5 | 5 | namespace UserAccessManager\Setup\Update; |
| 6 | 6 | |
| 7 | -use UserAccessManager\Setup\Database\DatabaseUpdate; | |
| 8 | - | |
| 9 | 7 | class DatabaseUpdate1 extends DatabaseUpdate |
| 10 | 8 | { |
| 11 | 9 | public function getVersion(): string |
| 12 | 10 | { |
| @@ -14,23 +12,23 @@ | ||
| 14 | 12 | } |
| 15 | 13 | |
| 16 | 14 | private function updateToUserGroupTableUpdate(string $userGroupTable): bool |
| 17 | 15 | { |
| 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 ''"; | |
| 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 ''"; | |
| 22 | 20 | |
| 23 | 21 | $this->database->query($alterQuery); |
| 24 | 22 | |
| 25 | - $updateQuery = "UPDATE $userGroupTable SET read_access = 'group', write_access = 'group'"; | |
| 23 | + $updateQuery = "UPDATE `$userGroupTable` SET `read_access` = 'group', `write_access` = 'group'"; | |
| 26 | 24 | $success = $this->database->query($updateQuery) !== false; |
| 27 | 25 | |
| 28 | - $selectQuery = "SHOW columns FROM $userGroupTable LIKE 'ip_range'"; | |
| 26 | + $selectQuery = "SHOW COLUMNS FROM `$userGroupTable` LIKE 'ip_range'"; | |
| 29 | 27 | $dbIpRange = (string) $this->database->getVariable($selectQuery); |
| 30 | 28 | |
| 31 | 29 | if ($dbIpRange !== 'ip_range') { |
| 32 | - $alterQuery = "ALTER TABLE $userGroupTable ADD ip_range MEDIUMTEXT NULL DEFAULT ''"; | |
| 30 | + $alterQuery = "ALTER TABLE `$userGroupTable` ADD `ip_range` MEDIUMTEXT NULL DEFAULT ''"; | |
| 33 | 31 | $success = $this->database->query($alterQuery) !== false; |
| 34 | 32 | } |
| 35 | 33 | |
| 36 | 34 | return $success; |
| @@ -35,50 +33,60 @@ | ||
| 35 | 33 | |
| 36 | 34 | return $success; |
| 37 | 35 | } |
| 38 | 36 | |
| 39 | - private function getObjectSelectQuery( | |
| 40 | - string $objectType, | |
| 41 | - string $userGroupToPost, | |
| 42 | - string $userGroupToCategory, | |
| 43 | - string $userGroupToUser, | |
| 44 | - string $userGroupToRole | |
| 45 | - ): ?string { | |
| 46 | - $addition = ''; | |
| 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 | + { | |
| 42 | + if ($this->objectHandler->isPostType($objectType) === true) { | |
| 43 | + $source = '`' . $legacyTables['post'] . '`, `' . $this->database->getPostsTable() . '`'; | |
| 47 | 44 | |
| 48 | - if ($this->objectHandler->isPostType($objectType) === true) { | |
| 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 { | |
| 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) { | |
| 62 | 56 | return null; |
| 63 | 57 | } |
| 64 | 58 | |
| 65 | - return "SELECT $dbIdName AS id, group_id AS groupId FROM $database $addition"; | |
| 59 | + return "SELECT `{$idColumns[$objectType]}` AS `id`, `group_id` AS `groupId` | |
| 60 | + FROM `{$legacyTables[$objectType]}`"; | |
| 66 | 61 | } |
| 67 | 62 | |
| 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 | + | |
| 68 | 75 | private function updateToUserGroupToObjectTableUpdate(): bool |
| 69 | 76 | { |
| 70 | 77 | $prefix = $this->database->getPrefix(); |
| 71 | - | |
| 72 | - $charsetCollate = $this->database->getCharset(); | |
| 78 | + $charsetCollate = $this->database->getColumnCharset(); | |
| 73 | 79 | $userGroupToObject = $prefix . 'uam_accessgroup_to_object'; |
| 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'; | |
| 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 | + ]; | |
| 78 | 86 | |
| 79 | - $alterQuery = "ALTER TABLE '$userGroupToObject' | |
| 80 | - CHANGE 'object_id' 'object_id' VARCHAR(64) $charsetCollate"; | |
| 87 | + $alterQuery = "ALTER TABLE `$userGroupToObject` | |
| 88 | + CHANGE `object_id` `object_id` VARCHAR(64) $charsetCollate"; | |
| 81 | 89 | $success = $this->database->query($alterQuery) !== false; |
| 82 | 90 | |
| 83 | 91 | if ($success === false) { |
| 84 | 92 | return false; |
| @@ -83,19 +91,11 @@ | ||
| 83 | 91 | if ($success === false) { |
| 84 | 92 | return false; |
| 85 | 93 | } |
| 86 | 94 | |
| 87 | - $objectTypes = $this->objectHandler->getObjectTypes(); | |
| 95 | + foreach ($this->getMigratableObjectTypes() as $objectType) { | |
| 96 | + $query = $this->getObjectSelectQuery($objectType, $legacyTables); | |
| 88 | 97 | |
| 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,11 +108,12 @@ | ||
| 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. | |
| 112 | 113 | [ |
| 113 | - '%d', | |
| 114 | - '%d', | |
| 114 | + '%s', | |
| 115 | + '%s', | |
| 115 | 116 | '%s' |
| 116 | 117 | ] |
| 117 | 118 | ); |
| 118 | 119 | $success = $success && $insert !== false; |
| @@ -118,12 +119,9 @@ | ||
| 118 | 119 | $success = $success && $insert !== false; |
| 119 | 120 | } |
| 120 | 121 | } |
| 121 | 122 | |
| 122 | - $dropQuery = "DROP TABLE $userGroupToPost, | |
| 123 | - $userGroupToUser, | |
| 124 | - $userGroupToCategory, | |
| 125 | - $userGroupToRole"; | |
| 123 | + $dropQuery = 'DROP TABLE IF EXISTS `' . implode('`, `', $legacyTables) . '`'; | |
| 126 | 124 | |
| 127 | 125 | return $success && $this->database->query($dropQuery) !== false; |
| 128 | 126 | } |
| 129 | 127 | |