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