diff options
Diffstat (limited to 'apps/theming/lib/Migration')
3 files changed, 158 insertions, 123 deletions
diff --git a/apps/theming/lib/Migration/InitBackgroundImagesMigration.php b/apps/theming/lib/Migration/InitBackgroundImagesMigration.php new file mode 100644 index 00000000000..dea1bb3aa83 --- /dev/null +++ b/apps/theming/lib/Migration/InitBackgroundImagesMigration.php @@ -0,0 +1,31 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\Theming\Migration; + +use OCA\Theming\Jobs\MigrateBackgroundImages; +use OCP\BackgroundJob\IJobList; +use OCP\Migration\IOutput; +use OCP\Migration\IRepairStep; + +class InitBackgroundImagesMigration implements IRepairStep { + + public function __construct( + private IJobList $jobList, + ) { + } + + public function getName() { + return 'Initialize migration of background images from dashboard to theming app'; + } + + public function run(IOutput $output) { + $this->jobList->add(MigrateBackgroundImages::class, ['stage' => MigrateBackgroundImages::STAGE_PREPARE]); + } +} diff --git a/apps/theming/lib/Migration/MigrateUserConfig.php b/apps/theming/lib/Migration/MigrateUserConfig.php deleted file mode 100644 index 0f8d982dfa7..00000000000 --- a/apps/theming/lib/Migration/MigrateUserConfig.php +++ /dev/null @@ -1,123 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright Copyright (c) 2019 Janis Köhr <janiskoehr@icloud.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Janis Köhr <janis.koehr@novatec-gmbh.de> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ -namespace OCA\Theming\Migration; - -use OCA\Theming\AppInfo\Application; -use OCA\Theming\Service\ThemesService; -use OCA\Theming\Themes\DarkHighContrastTheme; -use OCA\Theming\Themes\DarkTheme; -use OCA\Theming\Themes\DyslexiaFont; -use OCA\Theming\Themes\HighContrastTheme; -use OCP\IConfig; -use OCP\IUser; -use OCP\IUserManager; -use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; - -class MigrateUserConfig implements IRepairStep { - - protected IUserManager $userManager; - protected IConfig $config; - protected ThemesService $themesService; - protected DarkTheme $darkTheme; - protected DarkHighContrastTheme $darkHighContrastTheme; - protected HighContrastTheme $highContrastTheme; - protected DyslexiaFont $dyslexiaFont; - - /** - * MigrateUserConfig constructor. - */ - public function __construct(IConfig $config, - IUserManager $userManager, - ThemesService $themesService, - DarkTheme $darkTheme, - DarkHighContrastTheme $darkHighContrastTheme, - HighContrastTheme $highContrastTheme, - DyslexiaFont $dyslexiaFont) { - $this->config = $config; - $this->userManager = $userManager; - $this->themesService = $themesService; - - $this->darkTheme = $darkTheme; - $this->darkHighContrastTheme = $darkHighContrastTheme; - $this->highContrastTheme = $highContrastTheme; - $this->dyslexiaFont = $dyslexiaFont; - } - - /** - * Returns the step's name - * - * @return string - * @since 25.0.0 - */ - public function getName() { - return 'Migrate old user accessibility config'; - } - - /** - * Run repair step. - * Must throw exception on error. - * - * @param IOutput $output - * @throws \Exception in case of failure - * @since 25.0.0 - */ - public function run(IOutput $output) { - $output->startProgress(); - $this->userManager->callForSeenUsers(function (IUser $user) use ($output) { - $config = []; - - $font = $this->config->getUserValue($user->getUID(), 'accessibility', 'font', false); - $highcontrast = $this->config->getUserValue($user->getUID(), 'accessibility', 'highcontrast', false); - $theme = $this->config->getUserValue($user->getUID(), 'accessibility', 'theme', false); - - if ($highcontrast || $theme) { - if ($theme === 'dark' && $highcontrast === 'highcontrast') { - $config[] = $this->darkHighContrastTheme->getId(); - } else if ($theme === 'dark') { - $config[] = $this->darkTheme->getId(); - } else if ($highcontrast === 'highcontrast') { - $config[] = $this->highContrastTheme->getId(); - } - } - - if ($font === 'fontdyslexic') { - $config[] = $this->dyslexiaFont->getId(); - } - - if (!empty($config)) { - $this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_unique($config))); - } - - $output->advance(); - }); - - $this->config->deleteAppFromAllUsers('accessibility'); - - $output->finishProgress(); - } -} diff --git a/apps/theming/lib/Migration/Version2006Date20240905111627.php b/apps/theming/lib/Migration/Version2006Date20240905111627.php new file mode 100644 index 00000000000..8f4130cba46 --- /dev/null +++ b/apps/theming/lib/Migration/Version2006Date20240905111627.php @@ -0,0 +1,127 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\Theming\Migration; + +use Closure; +use OCA\Theming\AppInfo\Application; +use OCA\Theming\Jobs\RestoreBackgroundImageColor; +use OCP\BackgroundJob\IJobList; +use OCP\IAppConfig; +use OCP\IDBConnection; +use OCP\Migration\IMigrationStep; +use OCP\Migration\IOutput; + +// This can only be executed once because `background_color` is again used with Nextcloud 30, +// so this part only works when updating -> Nextcloud 29 -> 30 +class Version2006Date20240905111627 implements IMigrationStep { + + public function __construct( + private IJobList $jobList, + private IAppConfig $appConfig, + private IDBConnection $connection, + ) { + } + + public function name(): string { + return 'Restore custom primary color'; + } + + public function description(): string { + return 'Restore custom primary color after separating primary color from background color'; + } + + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + // nop + } + + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { + $this->restoreSystemColors($output); + + $userThemingEnabled = $this->appConfig->getValueBool('theming', 'disable-user-theming') === false; + if ($userThemingEnabled) { + $this->restoreUserColors($output); + } + + return null; + } + + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + $output->info('Initialize restoring of background colors for custom background images'); + // This is done in a background job as this can take a lot of time for large instances + $this->jobList->add(RestoreBackgroundImageColor::class, ['stage' => RestoreBackgroundImageColor::STAGE_PREPARE]); + } + + private function restoreSystemColors(IOutput $output): void { + $defaultColor = $this->appConfig->getValueString(Application::APP_ID, 'color', ''); + if ($defaultColor === '') { + $output->info('No custom system color configured - skipping'); + } else { + // Restore legacy value into new field + $this->appConfig->setValueString(Application::APP_ID, 'background_color', $defaultColor); + $this->appConfig->setValueString(Application::APP_ID, 'primary_color', $defaultColor); + // Delete legacy field + $this->appConfig->deleteKey(Application::APP_ID, 'color'); + // give some feedback + $output->info('Global primary color restored'); + } + } + + private function restoreUserColors(IOutput $output): void { + $output->info('Restoring user primary color'); + // For performance let the DB handle this + $qb = $this->connection->getQueryBuilder(); + // Rename the `background_color` config to `primary_color` as this was the behavior on Nextcloud 29 and older + // with Nextcloud 30 `background_color` is a new option to define the background color independent of the primary color. + $qb->update('preferences') + ->set('configkey', $qb->createNamedParameter('primary_color')) + ->where($qb->expr()->eq('appid', $qb->createNamedParameter(Application::APP_ID))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('background_color'))); + + try { + $qb->executeStatement(); + } catch (\Exception) { + $output->debug('Some users already configured the background color'); + $this->restoreUserColorsFallback($output); + } + + $output->info('Primary color of users restored'); + } + + /** + * Similar to restoreUserColors but also works if some users already setup a new value. + * This is only called if the first approach fails as this takes much longer on the DB. + */ + private function restoreUserColorsFallback(IOutput $output): void { + $qb = $this->connection->getQueryBuilder(); + $qb2 = $this->connection->getQueryBuilder(); + + $qb2->select('userid') + ->from('preferences') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter(Application::APP_ID))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('primary_color'))); + + // MySQL does not update on select of the same table, so this is a workaround: + if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL) { + $subquery = 'SELECT * from ( ' . $qb2->getSQL() . ' ) preferences_alias'; + } else { + $subquery = $qb2->getSQL(); + } + + $qb->update('preferences') + ->set('configkey', $qb->createNamedParameter('primary_color')) + ->where($qb->expr()->eq('appid', $qb->createNamedParameter(Application::APP_ID))) + ->andWhere( + $qb->expr()->eq('configkey', $qb->createNamedParameter('background_color')), + $qb->expr()->notIn('userid', $qb->createFunction($subquery)), + ); + + $qb->executeStatement(); + } +} |