| @@ -12,23 +12,23 @@ | ||
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | private function updateToUserGroupTableUpdate(string $userGroupTable): bool |
| 15 | 15 | { |
| 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 ''"; | |
| 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 ''"; | |
| 20 | 20 | |
| 21 | 21 | $this->database->query($alterQuery); |
| 22 | 22 | |
| 23 | - $updateQuery = "UPDATE $userGroupTable SET read_access = 'group', write_access = 'group'"; | |
| 23 | + $updateQuery = "UPDATE `$userGroupTable` SET `read_access` = 'group', `write_access` = 'group'"; | |
| 24 | 24 | $success = $this->database->query($updateQuery) !== false; |
| 25 | 25 | |
| 26 | - $selectQuery = "SHOW columns FROM $userGroupTable LIKE 'ip_range'"; | |
| 26 | + $selectQuery = "SHOW COLUMNS FROM `$userGroupTable` LIKE 'ip_range'"; | |
| 27 | 27 | $dbIpRange = (string) $this->database->getVariable($selectQuery); |
| 28 | 28 | |
| 29 | 29 | if ($dbIpRange !== 'ip_range') { |
| 30 | - $alterQuery = "ALTER TABLE $userGroupTable ADD ip_range MEDIUMTEXT NULL DEFAULT ''"; | |
| 30 | + $alterQuery = "ALTER TABLE `$userGroupTable` ADD `ip_range` MEDIUMTEXT NULL DEFAULT ''"; | |
| 31 | 31 | $success = $this->database->query($alterQuery) !== false; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | return $success; |
| @@ -39,12 +39,12 @@ | ||
| 39 | 39 | */ |
| 40 | 40 | private function getObjectSelectQuery(string $objectType, array $legacyTables): ?string |
| 41 | 41 | { |
| 42 | 42 | if ($this->objectHandler->isPostType($objectType) === true) { |
| 43 | - $source = $legacyTables['post'] . ', ' . $this->database->getPostsTable(); | |
| 43 | + $source = '`' . $legacyTables['post'] . '`, `' . $this->database->getPostsTable() . '`'; | |
| 44 | 44 | |
| 45 | - return "SELECT post_id AS id, group_id AS groupId FROM $source" | |
| 46 | - . " WHERE post_id = ID AND post_type = '$objectType'"; | |
| 45 | + return "SELECT `post_id` AS `id`, `group_id` AS `groupId` FROM $source" | |
| 46 | + . " WHERE `post_id` = `ID` AND `post_type` = '$objectType'"; | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | $idColumns = [ |
| 50 | 50 | 'category' => 'category_id', |
| @@ -55,15 +55,28 @@ | ||
| 55 | 55 | if (isset($idColumns[$objectType]) === false) { |
| 56 | 56 | return null; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - return "SELECT {$idColumns[$objectType]} AS id, group_id AS groupId FROM {$legacyTables[$objectType]}"; | |
| 59 | + return "SELECT `{$idColumns[$objectType]}` AS `id`, `group_id` AS `groupId` | |
| 60 | + FROM `{$legacyTables[$objectType]}`"; | |
| 60 | 61 | } |
| 61 | 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 | + | |
| 62 | 75 | private function updateToUserGroupToObjectTableUpdate(): bool |
| 63 | 76 | { |
| 64 | 77 | $prefix = $this->database->getPrefix(); |
| 65 | - $charsetCollate = $this->database->getCharset(); | |
| 78 | + $charsetCollate = $this->database->getColumnCharset(); | |
| 66 | 79 | $userGroupToObject = $prefix . 'uam_accessgroup_to_object'; |
| 67 | 80 | $legacyTables = [ |
| 68 | 81 | 'post' => $prefix . 'uam_accessgroup_to_post', |
| 69 | 82 | 'user' => $prefix . 'uam_accessgroup_to_user', |
| @@ -70,10 +83,10 @@ | ||
| 70 | 83 | 'category' => $prefix . 'uam_accessgroup_to_category', |
| 71 | 84 | 'role' => $prefix . 'uam_accessgroup_to_role' |
| 72 | 85 | ]; |
| 73 | 86 | |
| 74 | - $alterQuery = "ALTER TABLE '$userGroupToObject' | |
| 75 | - CHANGE 'object_id' 'object_id' VARCHAR(64) $charsetCollate"; | |
| 87 | + $alterQuery = "ALTER TABLE `$userGroupToObject` | |
| 88 | + CHANGE `object_id` `object_id` VARCHAR(64) $charsetCollate"; | |
| 76 | 89 | $success = $this->database->query($alterQuery) !== false; |
| 77 | 90 | |
| 78 | 91 | if ($success === false) { |
| 79 | 92 | return false; |
| @@ -78,9 +91,9 @@ | ||
| 78 | 91 | if ($success === false) { |
| 79 | 92 | return false; |
| 80 | 93 | } |
| 81 | 94 | |
| 82 | - foreach ($this->objectHandler->getObjectTypes() as $objectType) { | |
| 95 | + foreach ($this->getMigratableObjectTypes() as $objectType) { | |
| 83 | 96 | $query = $this->getObjectSelectQuery($objectType, $legacyTables); |
| 84 | 97 | |
| 85 | 98 | if ($query === null) { |
| 86 | 99 | continue; |
| @@ -95,11 +108,12 @@ | ||
| 95 | 108 | 'group_id' => $dbObject->groupId, |
| 96 | 109 | 'object_id' => $dbObject->id, |
| 97 | 110 | 'object_type' => $objectType |
| 98 | 111 | ], |
| 112 | + // All three are strings, roles carry their name as the object id. | |
| 99 | 113 | [ |
| 100 | - '%d', | |
| 101 | - '%d', | |
| 114 | + '%s', | |
| 115 | + '%s', | |
| 102 | 116 | '%s' |
| 103 | 117 | ] |
| 104 | 118 | ); |
| 105 | 119 | $success = $success && $insert !== false; |
| @@ -105,9 +119,9 @@ | ||
| 105 | 119 | $success = $success && $insert !== false; |
| 106 | 120 | } |
| 107 | 121 | } |
| 108 | 122 | |
| 109 | - $dropQuery = 'DROP TABLE ' . implode(', ', $legacyTables); | |
| 123 | + $dropQuery = 'DROP TABLE IF EXISTS `' . implode('`, `', $legacyTables) . '`'; | |
| 110 | 124 | |
| 111 | 125 | return $success && $this->database->query($dropQuery) !== false; |
| 112 | 126 | } |
| 113 | 127 | |