| @@ -1,34 +1,63 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * DatabaseUpdate1.php | |
| 4 | + * | |
| 5 | + * The DatabaseUpdate1 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\Update; |
| 6 | 19 | |
| 20 | +use UserAccessManager\Setup\Database\DatabaseUpdate; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * Class DatabaseUpdate1 | |
| 24 | + * | |
| 25 | + * @package UserAccessManager\Setup\Update | |
| 26 | + */ | |
| 7 | 27 | class DatabaseUpdate1 extends DatabaseUpdate |
| 8 | 28 | { |
| 29 | + /** | |
| 30 | + * Returns the version. | |
| 31 | + * @return string | |
| 32 | + */ | |
| 9 | 33 | public function getVersion(): string |
| 10 | 34 | { |
| 11 | 35 | return '1.0'; |
| 12 | 36 | } |
| 13 | 37 | |
| 38 | + /** | |
| 39 | + * Updates the user group table to version 1.0. | |
| 40 | + * @param string $userGroupTable | |
| 41 | + * @return bool | |
| 42 | + */ | |
| 14 | 43 | private function updateToUserGroupTableUpdate(string $userGroupTable): bool |
| 15 | 44 | { |
| 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 ''"; | |
| 45 | + $alterQuery = "ALTER TABLE {$userGroupTable} | |
| 46 | + ADD read_access TINYTEXT NOT NULL DEFAULT '', | |
| 47 | + ADD write_access TINYTEXT NOT NULL DEFAULT '', | |
| 48 | + ADD ip_range MEDIUMTEXT NULL DEFAULT ''"; | |
| 20 | 49 | |
| 21 | 50 | $this->database->query($alterQuery); |
| 22 | 51 | |
| 23 | - $updateQuery = "UPDATE `$userGroupTable` SET `read_access` = 'group', `write_access` = 'group'"; | |
| 52 | + $updateQuery = "UPDATE {$userGroupTable} SET read_access = 'group', write_access = 'group'"; | |
| 24 | 53 | $success = $this->database->query($updateQuery) !== false; |
| 25 | 54 | |
| 26 | - $selectQuery = "SHOW COLUMNS FROM `$userGroupTable` LIKE 'ip_range'"; | |
| 55 | + $selectQuery = "SHOW columns FROM {$userGroupTable} LIKE 'ip_range'"; | |
| 27 | 56 | $dbIpRange = (string) $this->database->getVariable($selectQuery); |
| 28 | 57 | |
| 29 | 58 | if ($dbIpRange !== 'ip_range') { |
| 30 | - $alterQuery = "ALTER TABLE `$userGroupTable` ADD `ip_range` MEDIUMTEXT NULL DEFAULT ''"; | |
| 59 | + $alterQuery = "ALTER TABLE {$userGroupTable} ADD ip_range MEDIUMTEXT NULL DEFAULT ''"; | |
| 31 | 60 | $success = $this->database->query($alterQuery) !== false; |
| 32 | 61 | } |
| 33 | 62 | |
| 34 | 63 | return $success; |
| @@ -34,59 +63,61 @@ | ||
| 34 | 63 | return $success; |
| 35 | 64 | } |
| 36 | 65 | |
| 37 | 66 | /** |
| 38 | - * @param string[] $legacyTables Legacy per-object-type tables, keyed by object type. | |
| 67 | + * Returns the select query for the object type. | |
| 68 | + * @param string $objectType | |
| 69 | + * @param string $userGroupToPost | |
| 70 | + * @param string $userGroupToCategory | |
| 71 | + * @param string $userGroupToUser | |
| 72 | + * @param string $userGroupToRole | |
| 73 | + * @return null|string | |
| 39 | 74 | */ |
| 40 | - private function getObjectSelectQuery(string $objectType, array $legacyTables): ?string | |
| 41 | - { | |
| 75 | + private function getObjectSelectQuery( | |
| 76 | + string $objectType, | |
| 77 | + string $userGroupToPost, | |
| 78 | + string $userGroupToCategory, | |
| 79 | + string $userGroupToUser, | |
| 80 | + string $userGroupToRole | |
| 81 | + ): ?string { | |
| 82 | + $addition = ''; | |
| 83 | + | |
| 42 | 84 | 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) { | |
| 85 | + $dbIdName = 'post_id'; | |
| 86 | + $database = $userGroupToPost . ', ' . $this->database->getPostsTable(); | |
| 87 | + $addition = " WHERE post_id = ID AND post_type = '{$objectType}'"; | |
| 88 | + } elseif ($objectType === 'category') { | |
| 89 | + $dbIdName = 'category_id'; | |
| 90 | + $database = $userGroupToCategory; | |
| 91 | + } elseif ($objectType === 'user') { | |
| 92 | + $dbIdName = 'user_id'; | |
| 93 | + $database = $userGroupToUser; | |
| 94 | + } elseif ($objectType === 'role') { | |
| 95 | + $dbIdName = 'role_name'; | |
| 96 | + $database = $userGroupToRole; | |
| 97 | + } else { | |
| 56 | 98 | return null; |
| 57 | 99 | } |
| 58 | 100 | |
| 59 | - return "SELECT `{$idColumns[$objectType]}` AS `id`, `group_id` AS `groupId` | |
| 60 | - FROM `{$legacyTables[$objectType]}`"; | |
| 101 | + return "SELECT {$dbIdName} AS id, group_id AS groupId FROM {$database} {$addition}"; | |
| 61 | 102 | } |
| 62 | 103 | |
| 63 | 104 | /** |
| 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[] | |
| 105 | + * Updates the user group to object table to version 1.0. | |
| 69 | 106 | */ |
| 70 | - private function getMigratableObjectTypes(): array | |
| 71 | - { | |
| 72 | - return array_merge($this->objectHandler->getObjectTypes(), ['user', 'role']); | |
| 73 | - } | |
| 74 | - | |
| 75 | 107 | private function updateToUserGroupToObjectTableUpdate(): bool |
| 76 | 108 | { |
| 77 | 109 | $prefix = $this->database->getPrefix(); |
| 78 | - $charsetCollate = $this->database->getColumnCharset(); | |
| 110 | + | |
| 111 | + $charsetCollate = $this->database->getCharset(); | |
| 79 | 112 | $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 | - ]; | |
| 113 | + $userGroupToPost = $prefix . 'uam_accessgroup_to_post'; | |
| 114 | + $userGroupToUser = $prefix . 'uam_accessgroup_to_user'; | |
| 115 | + $userGroupToCategory = $prefix . 'uam_accessgroup_to_category'; | |
| 116 | + $userGroupToRole = $prefix . 'uam_accessgroup_to_role'; | |
| 86 | 117 | |
| 87 | - $alterQuery = "ALTER TABLE `$userGroupToObject` | |
| 88 | - CHANGE `object_id` `object_id` VARCHAR(64) $charsetCollate"; | |
| 118 | + $alterQuery = "ALTER TABLE '{$userGroupToObject}' | |
| 119 | + CHANGE 'object_id' 'object_id' VARCHAR(64) {$charsetCollate}"; | |
| 89 | 120 | $success = $this->database->query($alterQuery) !== false; |
| 90 | 121 | |
| 91 | 122 | if ($success === false) { |
| 92 | 123 | return false; |
| @@ -91,11 +122,19 @@ | ||
| 91 | 122 | if ($success === false) { |
| 92 | 123 | return false; |
| 93 | 124 | } |
| 94 | 125 | |
| 95 | - foreach ($this->getMigratableObjectTypes() as $objectType) { | |
| 96 | - $query = $this->getObjectSelectQuery($objectType, $legacyTables); | |
| 126 | + $objectTypes = $this->objectHandler->getObjectTypes(); | |
| 97 | 127 | |
| 128 | + foreach ($objectTypes as $objectType) { | |
| 129 | + $query = $this->getObjectSelectQuery( | |
| 130 | + $objectType, | |
| 131 | + $userGroupToPost, | |
| 132 | + $userGroupToCategory, | |
| 133 | + $userGroupToUser, | |
| 134 | + $userGroupToRole | |
| 135 | + ); | |
| 136 | + | |
| 98 | 137 | if ($query === null) { |
| 99 | 138 | continue; |
| 100 | 139 | } |
| 101 | 140 | |
| @@ -108,12 +147,11 @@ | ||
| 108 | 147 | 'group_id' => $dbObject->groupId, |
| 109 | 148 | 'object_id' => $dbObject->id, |
| 110 | 149 | 'object_type' => $objectType |
| 111 | 150 | ], |
| 112 | - // All three are strings, roles carry their name as the object id. | |
| 113 | 151 | [ |
| 114 | - '%s', | |
| 115 | - '%s', | |
| 152 | + '%d', | |
| 153 | + '%d', | |
| 116 | 154 | '%s' |
| 117 | 155 | ] |
| 118 | 156 | ); |
| 119 | 157 | $success = $success && $insert !== false; |
| @@ -119,18 +157,25 @@ | ||
| 119 | 157 | $success = $success && $insert !== false; |
| 120 | 158 | } |
| 121 | 159 | } |
| 122 | 160 | |
| 123 | - $dropQuery = 'DROP TABLE IF EXISTS `' . implode('`, `', $legacyTables) . '`'; | |
| 161 | + $dropQuery = "DROP TABLE {$userGroupToPost}, | |
| 162 | + {$userGroupToUser}, | |
| 163 | + {$userGroupToCategory}, | |
| 164 | + {$userGroupToRole}"; | |
| 124 | 165 | |
| 125 | 166 | return $success && $this->database->query($dropQuery) !== false; |
| 126 | 167 | } |
| 127 | 168 | |
| 169 | + /** | |
| 170 | + * Executes the update. | |
| 171 | + * @return bool | |
| 172 | + */ | |
| 128 | 173 | public function update(): bool |
| 129 | 174 | { |
| 130 | 175 | $success = true; |
| 131 | 176 | $userGroupTable = $this->database->getUserGroupTable(); |
| 132 | - $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '$userGroupTable'"); | |
| 177 | + $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '{$userGroupTable}'"); | |
| 133 | 178 | |
| 134 | 179 | if ($dbUserGroup === $userGroupTable) { |
| 135 | 180 | $success = $this->updateToUserGroupTableUpdate($userGroupTable); |
| 136 | 181 | } |