| @@ -1,17 +1,46 @@ | ||
| 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 | 45 | $alterQuery = "ALTER TABLE {$userGroupTable} |
| 17 | 46 | ADD read_access TINYTEXT NOT NULL DEFAULT '', |
| @@ -19,16 +48,16 @@ | ||
| 19 | 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,46 +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 FROM {$legacyTables[$objectType]}"; | |
| 101 | + return "SELECT {$dbIdName} AS id, group_id AS groupId FROM {$database} {$addition}"; | |
| 60 | 102 | } |
| 61 | 103 | |
| 104 | + /** | |
| 105 | + * Updates the user group to object table to version 1.0. | |
| 106 | + */ | |
| 62 | 107 | private function updateToUserGroupToObjectTableUpdate(): bool |
| 63 | 108 | { |
| 64 | 109 | $prefix = $this->database->getPrefix(); |
| 110 | + | |
| 65 | 111 | $charsetCollate = $this->database->getCharset(); |
| 66 | 112 | $userGroupToObject = $prefix . 'uam_accessgroup_to_object'; |
| 67 | - $legacyTables = [ | |
| 68 | - 'post' => $prefix . 'uam_accessgroup_to_post', | |
| 69 | - 'user' => $prefix . 'uam_accessgroup_to_user', | |
| 70 | - 'category' => $prefix . 'uam_accessgroup_to_category', | |
| 71 | - 'role' => $prefix . 'uam_accessgroup_to_role' | |
| 72 | - ]; | |
| 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'; | |
| 73 | 117 | |
| 74 | - $alterQuery = "ALTER TABLE '$userGroupToObject' | |
| 75 | - CHANGE 'object_id' 'object_id' VARCHAR(64) $charsetCollate"; | |
| 118 | + $alterQuery = "ALTER TABLE '{$userGroupToObject}' | |
| 119 | + CHANGE 'object_id' 'object_id' VARCHAR(64) {$charsetCollate}"; | |
| 76 | 120 | $success = $this->database->query($alterQuery) !== false; |
| 77 | 121 | |
| 78 | 122 | if ($success === false) { |
| 79 | 123 | return false; |
| @@ -78,11 +122,19 @@ | ||
| 78 | 122 | if ($success === false) { |
| 79 | 123 | return false; |
| 80 | 124 | } |
| 81 | 125 | |
| 82 | - foreach ($this->objectHandler->getObjectTypes() as $objectType) { | |
| 83 | - $query = $this->getObjectSelectQuery($objectType, $legacyTables); | |
| 126 | + $objectTypes = $this->objectHandler->getObjectTypes(); | |
| 84 | 127 | |
| 128 | + foreach ($objectTypes as $objectType) { | |
| 129 | + $query = $this->getObjectSelectQuery( | |
| 130 | + $objectType, | |
| 131 | + $userGroupToPost, | |
| 132 | + $userGroupToCategory, | |
| 133 | + $userGroupToUser, | |
| 134 | + $userGroupToRole | |
| 135 | + ); | |
| 136 | + | |
| 85 | 137 | if ($query === null) { |
| 86 | 138 | continue; |
| 87 | 139 | } |
| 88 | 140 | |
| @@ -105,18 +157,25 @@ | ||
| 105 | 157 | $success = $success && $insert !== false; |
| 106 | 158 | } |
| 107 | 159 | } |
| 108 | 160 | |
| 109 | - $dropQuery = 'DROP TABLE ' . implode(', ', $legacyTables); | |
| 161 | + $dropQuery = "DROP TABLE {$userGroupToPost}, | |
| 162 | + {$userGroupToUser}, | |
| 163 | + {$userGroupToCategory}, | |
| 164 | + {$userGroupToRole}"; | |
| 110 | 165 | |
| 111 | 166 | return $success && $this->database->query($dropQuery) !== false; |
| 112 | 167 | } |
| 113 | 168 | |
| 169 | + /** | |
| 170 | + * Executes the update. | |
| 171 | + * @return bool | |
| 172 | + */ | |
| 114 | 173 | public function update(): bool |
| 115 | 174 | { |
| 116 | 175 | $success = true; |
| 117 | 176 | $userGroupTable = $this->database->getUserGroupTable(); |
| 118 | - $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '$userGroupTable'"); | |
| 177 | + $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '{$userGroupTable}'"); | |
| 119 | 178 | |
| 120 | 179 | if ($dbUserGroup === $userGroupTable) { |
| 121 | 180 | $success = $this->updateToUserGroupTableUpdate($userGroupTable); |
| 122 | 181 | } |