Version2006Date20240905111627.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Theming\Migration;
  8. use Closure;
  9. use OCA\Theming\AppInfo\Application;
  10. use OCA\Theming\Jobs\RestoreBackgroundImageColor;
  11. use OCP\BackgroundJob\IJobList;
  12. use OCP\IAppConfig;
  13. use OCP\IDBConnection;
  14. use OCP\Migration\IMigrationStep;
  15. use OCP\Migration\IOutput;
  16. // This can only be executed once because `background_color` is again used with Nextcloud 30,
  17. // so this part only works when updating -> Nextcloud 29 -> 30
  18. class Version2006Date20240905111627 implements IMigrationStep {
  19. public function __construct(
  20. private IJobList $jobList,
  21. private IAppConfig $appConfig,
  22. private IDBConnection $connection,
  23. ) {
  24. }
  25. public function name(): string {
  26. return 'Restore custom primary color';
  27. }
  28. public function description(): string {
  29. return 'Restore custom primary color after separating primary color from background color';
  30. }
  31. public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  32. // nop
  33. }
  34. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
  35. $this->restoreSystemColors($output);
  36. $userThemingEnabled = $this->appConfig->getValueBool('theming', 'disable-user-theming') === false;
  37. if ($userThemingEnabled) {
  38. $this->restoreUserColors($output);
  39. }
  40. return null;
  41. }
  42. public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  43. $output->info('Initialize restoring of background colors for custom background images');
  44. // This is done in a background job as this can take a lot of time for large instances
  45. $this->jobList->add(RestoreBackgroundImageColor::class, ['stage' => RestoreBackgroundImageColor::STAGE_PREPARE]);
  46. }
  47. private function restoreSystemColors(IOutput $output): void {
  48. $defaultColor = $this->appConfig->getValueString(Application::APP_ID, 'color', '');
  49. if ($defaultColor === '') {
  50. $output->info('No custom system color configured - skipping');
  51. } else {
  52. // Restore legacy value into new field
  53. $this->appConfig->setValueString(Application::APP_ID, 'background_color', $defaultColor);
  54. $this->appConfig->setValueString(Application::APP_ID, 'primary_color', $defaultColor);
  55. // Delete legacy field
  56. $this->appConfig->deleteKey(Application::APP_ID, 'color');
  57. // give some feedback
  58. $output->info('Global primary color restored');
  59. }
  60. }
  61. private function restoreUserColors(IOutput $output): void {
  62. $output->info('Restoring user primary color');
  63. // For performance let the DB handle this
  64. $qb = $this->connection->getQueryBuilder();
  65. // Rename the `background_color` config to `primary_color` as this was the behavior on Nextcloud 29 and older
  66. // with Nextcloud 30 `background_color` is a new option to define the background color independent of the primary color.
  67. $qb->update('preferences')
  68. ->set('configkey', $qb->createNamedParameter('primary_color'))
  69. ->where($qb->expr()->eq('appid', $qb->createNamedParameter(Application::APP_ID)))
  70. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('background_color')));
  71. $qb->executeStatement();
  72. $output->info('Primary color of users restored');
  73. }
  74. }