PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/Setup/Update/DatabaseUpdate1.php +104 -42 2.3.19 → 2.1.11 View file →
@@ -1,18 +1,47 @@
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 45 $alterQuery = "ALTER TABLE {$userGroupTable}
17 46 ADD read_access TINYTEXT NOT NULL DEFAULT '',
18 47 ADD write_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'";
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,46 +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 FROM {$legacyTables[$objectType]}";
103 + return "SELECT {$dbIdName} AS id, group_id AS groupId FROM {$database} {$addition}";
60 104 }
61 105
62 - private function updateToUserGroupToObjectTableUpdate(): bool
106 + /**
107 + * Updates the user group to object table to version 1.0.
108 + */
109 + private function updateToUserGroupToObjectTableUpdate()
63 110 {
64 111 $prefix = $this->database->getPrefix();
112 +
65 113 $charsetCollate = $this->database->getCharset();
66 - $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 - ];
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';
73 119
74 - $alterQuery = "ALTER TABLE '$userGroupToObject'
75 - CHANGE 'object_id' 'object_id' VARCHAR(64) $charsetCollate";
120 + $alterQuery = "ALTER TABLE '{$userGroupToObject}'
121 + CHANGE 'object_id' 'object_id' VARCHAR(64) {$charsetCollate}";
76 122 $success = $this->database->query($alterQuery) !== false;
77 123
78 124 if ($success === false) {
79 125 return false;
@@ -78,16 +124,24 @@
78 124 if ($success === false) {
79 125 return false;
80 126 }
81 127
82 - foreach ($this->objectHandler->getObjectTypes() as $objectType) {
83 - $query = $this->getObjectSelectQuery($objectType, $legacyTables);
128 + $objectTypes = $this->objectHandler->getObjectTypes();
84 129
130 + foreach ($objectTypes as $objectType) {
131 + $query = $this->getObjectSelectQuery(
132 + $objectType,
133 + $userGroupToPost,
134 + $userGroupToCategory,
135 + $userGroupToUser,
136 + $userGroupToRole
137 + );
138 +
85 139 if ($query === null) {
86 140 continue;
87 141 }
88 142
89 - $dbObjects = (array) $this->database->getResults($query);
143 + $dbObjects = (array)$this->database->getResults($query);
90 144
91 145 foreach ($dbObjects as $dbObject) {
92 146 $insert = $this->database->insert(
93 147 $userGroupToObject,
@@ -105,18 +159,26 @@
105 159 $success = $success && $insert !== false;
106 160 }
107 161 }
108 162
109 - $dropQuery = 'DROP TABLE ' . implode(', ', $legacyTables);
163 + $dropQuery = "DROP TABLE {$userGroupToPost},
164 + {$userGroupToUser},
165 + {$userGroupToCategory},
166 + {$userGroupToRole}";
110 167
111 168 return $success && $this->database->query($dropQuery) !== false;
112 169 }
113 170
114 - public function update(): bool
171 + /**
172 + * Executes the update.
173 + *
174 + * @return bool
175 + */
176 + public function update()
115 177 {
116 178 $success = true;
117 179 $userGroupTable = $this->database->getUserGroupTable();
118 - $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '$userGroupTable'");
180 + $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '{$userGroupTable}'");
119 181
120 182 if ($dbUserGroup === $userGroupTable) {
121 183 $success = $this->updateToUserGroupTableUpdate($userGroupTable);
122 184 }