PluginProbe
User Access Manager / 2.3.13
User Access Manager v2.3.13
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/Database/DatabaseHandler.php +47 -45 trunk2.3.13 View file →
@@ -159,13 +159,23 @@
159 159
160 160 return $information;
161 161 }
162 162
163 - private function alterTable(Table $table, string $alteration): bool
163 + private function addColumn(Table $table, Column $column): bool
164 164 {
165 - return $this->database->query("ALTER TABLE `{$table->getName()}` $alteration;") !== false;
165 + return $this->database->query("ALTER TABLE `{$table->getName()}` ADD $column;") !== false;
166 166 }
167 167
168 + private function modifyColumn(Table $table, Column $column): bool
169 + {
170 + return $this->database->query("ALTER TABLE `{$table->getName()}` MODIFY $column;") !== false;
171 + }
172 +
173 + private function dropColumn(Table $table, Column $column): bool
174 + {
175 + return $this->database->query("ALTER TABLE `{$table->getName()}` DROP `{$column->getName()}`;") !== false;
176 + }
177 +
168 178 /**
169 179 * @throws MissingColumnsException
170 180 */
171 181 public function repairDatabase(array $information = []): bool
@@ -176,20 +186,20 @@
176 186 foreach ($information[self::MISSING_TABLES] as $table) {
177 187 $this->addTable($table);
178 188 }
179 189
180 - $alterationsByInformationKey = [
181 - self::MISSING_COLUMNS => static fn(Column $column) => "ADD $column",
182 - self::MODIFIED_COLUMNS => static fn(Column $column) => "MODIFY $column",
183 - self::EXTRA_COLUMNS => static fn(Column $column) => "DROP `{$column->getName()}`"
184 - ];
190 + foreach ($information[self::MISSING_COLUMNS] as $columnInformation) {
191 + $success = $success && $this->addColumn($columnInformation[0], $columnInformation[1]);
192 + }
185 193
186 - foreach ($alterationsByInformationKey as $informationKey => $buildAlteration) {
187 - foreach ($information[$informationKey] as [$table, $column]) {
188 - $success = $success && $this->alterTable($table, $buildAlteration($column));
189 - }
194 + foreach ($information[self::MODIFIED_COLUMNS] as $columnInformation) {
195 + $success = $success && $this->modifyColumn($columnInformation[0], $columnInformation[1]);
190 196 }
191 197
198 + foreach ($information[self::EXTRA_COLUMNS] as $columnInformation) {
199 + $success = $success && $this->dropColumn($columnInformation[0], $columnInformation[1]);
200 + }
201 +
192 202 return $success;
193 203 }
194 204
195 205 private function getActivePluginSites(): array
@@ -210,33 +220,26 @@
210 220
211 221 return $activeSites;
212 222 }
213 223
214 - private function hasSiteWithOutdatedDatabase(): bool
215 - {
216 - foreach ($this->getActivePluginSites() as $siteId) {
217 - $table = $this->database->getBlogPrefix($siteId) . 'options';
218 - $select = "SELECT `option_value` FROM `$table` WHERE `option_name` = '%s' LIMIT 1";
219 - $select = $this->database->prepare($select, 'uam_db_version');
220 - $currentDbVersion = $this->database->getVariable($select);
221 -
222 - if ($currentDbVersion !== null
223 - && version_compare((string) $currentDbVersion, UserAccessManager::DB_VERSION, '<') === true
224 - ) {
225 - return true;
226 - }
227 - }
228 -
229 - return false;
230 - }
231 -
232 224 /**
233 225 * @throws MissingColumnsException
234 226 */
235 227 public function isDatabaseUpdateNecessary(): bool
236 228 {
237 - if ($this->wordpress->isSuperAdmin() === true && $this->hasSiteWithOutdatedDatabase() === true) {
238 - return true;
229 + if ($this->wordpress->isSuperAdmin() === true) {
230 + foreach ($this->getActivePluginSites() as $siteId) {
231 + $table = $this->database->getBlogPrefix($siteId) . 'options';
232 + $select = "SELECT option_value FROM $table WHERE option_name = '%s' LIMIT 1";
233 + $select = $this->database->prepare($select, 'uam_db_version');
234 + $currentDbVersion = $this->database->getVariable($select);
235 +
236 + if ($currentDbVersion !== null
237 + && version_compare((string) $currentDbVersion, UserAccessManager::DB_VERSION, '<') === true
238 + ) {
239 + return true;
240 + }
241 + }
239 242 }
240 243
241 244 $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version');
242 245
@@ -247,19 +250,8 @@
247 250
248 251 return version_compare($currentDbVersion, UserAccessManager::DB_VERSION, '<');
249 252 }
250 253
251 - /**
252 - * @return string[]
253 - */
254 - private function getTableNames(): array
255 - {
256 - return [
257 - $this->database->getUserGroupTable(),
258 - $this->database->getUserGroupToObjectTable()
259 - ];
260 - }
261 -
262 254 public function backupDatabase(): bool
263 255 {
264 256 $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version');
265 257
@@ -268,12 +260,17 @@
268 260 ) {
269 261 return false;
270 262 }
271 263
264 + $tables = [
265 + $this->database->getUserGroupTable(),
266 + $this->database->getUserGroupToObjectTable()
267 + ];
268 +
272 269 $currentDbVersion = str_replace('.', '-', $currentDbVersion);
273 270 $success = true;
274 271
275 - foreach ($this->getTableNames() as $table) {
272 + foreach ($tables as $table) {
276 273 $createQuery = "CREATE TABLE `{$table}_$currentDbVersion` LIKE `$table`";
277 274 $success = $success && ($this->database->query($createQuery) !== false);
278 275 $insertQuery = "INSERT `{$table}_$currentDbVersion` SELECT * FROM `$table`";
279 276 $success = $success && ($this->database->query($insertQuery) !== false);
@@ -289,9 +286,9 @@
289 286 "SHOW TABLES LIKE '{$this->database->getPrefix()}uam_%'"
290 287 );
291 288
292 289 foreach ($tables as $table) {
293 - if (preg_match('/.*_([0-9\-]+)/', $table, $matches) === 1) {
290 + if (preg_match('/.*_([0-9\-]+)/i', $table, $matches) === 1) {
294 291 $version = str_replace('-', '.', $matches[1]);
295 292 $versions[$version] = $version;
296 293 }
297 294 }
@@ -301,11 +298,16 @@
301 298
302 299 private function getBackupTables(string $version): array
303 300 {
304 301 $backupTables = [];
302 + $tables = [
303 + $this->database->getUserGroupTable(),
304 + $this->database->getUserGroupToObjectTable()
305 + ];
306 +
305 307 $versionForDb = str_replace('.', '-', $version);
306 308
307 - foreach ($this->getTableNames() as $table) {
309 + foreach ($tables as $table) {
308 310 $backupTable = (string) $this->database->getVariable(
309 311 "SHOW TABLES LIKE '{$table}_$versionForDb'"
310 312 );
311 313