Version2006Date20240905111627.php 3.2 KB

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