| @@ -159,23 +159,13 @@ | ||
| 159 | 159 | |
| 160 | 160 | return $information; |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - private function addColumn(Table $table, Column $column): bool | |
| 163 | + private function alterTable(Table $table, string $alteration): bool | |
| 164 | 164 | { |
| 165 | - return $this->database->query("ALTER TABLE `{$table->getName()}` ADD $column;") !== false; | |
| 165 | + return $this->database->query("ALTER TABLE `{$table->getName()}` $alteration;") !== 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 | - | |
| 178 | 168 | /** |
| 179 | 169 | * @throws MissingColumnsException |
| 180 | 170 | */ |
| 181 | 171 | public function repairDatabase(array $information = []): bool |
| @@ -186,20 +176,20 @@ | ||
| 186 | 176 | foreach ($information[self::MISSING_TABLES] as $table) { |
| 187 | 177 | $this->addTable($table); |
| 188 | 178 | } |
| 189 | 179 | |
| 190 | - foreach ($information[self::MISSING_COLUMNS] as $columnInformation) { | |
| 191 | - $success = $success && $this->addColumn($columnInformation[0], $columnInformation[1]); | |
| 192 | - } | |
| 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 | + ]; | |
| 193 | 185 | |
| 194 | - foreach ($information[self::MODIFIED_COLUMNS] as $columnInformation) { | |
| 195 | - $success = $success && $this->modifyColumn($columnInformation[0], $columnInformation[1]); | |
| 186 | + foreach ($alterationsByInformationKey as $informationKey => $buildAlteration) { | |
| 187 | + foreach ($information[$informationKey] as [$table, $column]) { | |
| 188 | + $success = $success && $this->alterTable($table, $buildAlteration($column)); | |
| 189 | + } | |
| 196 | 190 | } |
| 197 | 191 | |
| 198 | - foreach ($information[self::EXTRA_COLUMNS] as $columnInformation) { | |
| 199 | - $success = $success && $this->dropColumn($columnInformation[0], $columnInformation[1]); | |
| 200 | - } | |
| 201 | - | |
| 202 | 192 | return $success; |
| 203 | 193 | } |
| 204 | 194 | |
| 205 | 195 | private function getActivePluginSites(): array |
| @@ -220,26 +210,33 @@ | ||
| 220 | 210 | |
| 221 | 211 | return $activeSites; |
| 222 | 212 | } |
| 223 | 213 | |
| 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 | + | |
| 224 | 232 | /** |
| 225 | 233 | * @throws MissingColumnsException |
| 226 | 234 | */ |
| 227 | 235 | public function isDatabaseUpdateNecessary(): bool |
| 228 | 236 | { |
| 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 | - } | |
| 237 | + if ($this->wordpress->isSuperAdmin() === true && $this->hasSiteWithOutdatedDatabase() === true) { | |
| 238 | + return true; | |
| 242 | 239 | } |
| 243 | 240 | |
| 244 | 241 | $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version'); |
| 245 | 242 | |
| @@ -250,8 +247,19 @@ | ||
| 250 | 247 | |
| 251 | 248 | return version_compare($currentDbVersion, UserAccessManager::DB_VERSION, '<'); |
| 252 | 249 | } |
| 253 | 250 | |
| 251 | + /** | |
| 252 | + * @return string[] | |
| 253 | + */ | |
| 254 | + private function getTableNames(): array | |
| 255 | + { | |
| 256 | + return [ | |
| 257 | + $this->database->getUserGroupTable(), | |
| 258 | + $this->database->getUserGroupToObjectTable() | |
| 259 | + ]; | |
| 260 | + } | |
| 261 | + | |
| 254 | 262 | public function backupDatabase(): bool |
| 255 | 263 | { |
| 256 | 264 | $currentDbVersion = (string) $this->wordpress->getOption('uam_db_version'); |
| 257 | 265 | |
| @@ -260,17 +268,12 @@ | ||
| 260 | 268 | ) { |
| 261 | 269 | return false; |
| 262 | 270 | } |
| 263 | 271 | |
| 264 | - $tables = [ | |
| 265 | - $this->database->getUserGroupTable(), | |
| 266 | - $this->database->getUserGroupToObjectTable() | |
| 267 | - ]; | |
| 268 | - | |
| 269 | 272 | $currentDbVersion = str_replace('.', '-', $currentDbVersion); |
| 270 | 273 | $success = true; |
| 271 | 274 | |
| 272 | - foreach ($tables as $table) { | |
| 275 | + foreach ($this->getTableNames() as $table) { | |
| 273 | 276 | $createQuery = "CREATE TABLE `{$table}_$currentDbVersion` LIKE `$table`"; |
| 274 | 277 | $success = $success && ($this->database->query($createQuery) !== false); |
| 275 | 278 | $insertQuery = "INSERT `{$table}_$currentDbVersion` SELECT * FROM `$table`"; |
| 276 | 279 | $success = $success && ($this->database->query($insertQuery) !== false); |
| @@ -298,16 +301,11 @@ | ||
| 298 | 301 | |
| 299 | 302 | private function getBackupTables(string $version): array |
| 300 | 303 | { |
| 301 | 304 | $backupTables = []; |
| 302 | - $tables = [ | |
| 303 | - $this->database->getUserGroupTable(), | |
| 304 | - $this->database->getUserGroupToObjectTable() | |
| 305 | - ]; | |
| 306 | - | |
| 307 | 305 | $versionForDb = str_replace('.', '-', $version); |
| 308 | 306 | |
| 309 | - foreach ($tables as $table) { | |
| 307 | + foreach ($this->getTableNames() as $table) { | |
| 310 | 308 | $backupTable = (string) $this->database->getVariable( |
| 311 | 309 | "SHOW TABLES LIKE '{$table}_$versionForDb'" |
| 312 | 310 | ); |
| 313 | 311 | |