|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Theming\Jobs; |
| 11 | + |
| 12 | +use OCA\Theming\AppInfo\Application; |
| 13 | +use OCA\Theming\Service\BackgroundService; |
| 14 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 15 | +use OCP\BackgroundJob\IJobList; |
| 16 | +use OCP\BackgroundJob\QueuedJob; |
| 17 | +use OCP\Files\IAppData; |
| 18 | +use OCP\Files\NotFoundException; |
| 19 | +use OCP\Files\NotPermittedException; |
| 20 | +use OCP\IConfig; |
| 21 | +use OCP\IDBConnection; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | + |
| 24 | +class RestoreBackgroundImageColor extends QueuedJob { |
| 25 | + |
| 26 | + public const STAGE_PREPARE = 'prepare'; |
| 27 | + public const STAGE_EXECUTE = 'execute'; |
| 28 | + // will be saved in appdata/theming/global/ |
| 29 | + protected const STATE_FILE_NAME = '30_background_image_color_restoration.json'; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + ITimeFactory $time, |
| 33 | + private IConfig $config, |
| 34 | + private IAppData $appData, |
| 35 | + private IJobList $jobList, |
| 36 | + private IDBConnection $dbc, |
| 37 | + private LoggerInterface $logger, |
| 38 | + private BackgroundService $service, |
| 39 | + ) { |
| 40 | + parent::__construct($time); |
| 41 | + } |
| 42 | + |
| 43 | + protected function run(mixed $argument): void { |
| 44 | + if (!is_array($argument) || !isset($argument['stage'])) { |
| 45 | + throw new \Exception('Job '.self::class.' called with wrong argument'); |
| 46 | + } |
| 47 | + |
| 48 | + switch ($argument['stage']) { |
| 49 | + case self::STAGE_PREPARE: |
| 50 | + $this->runPreparation(); |
| 51 | + break; |
| 52 | + case self::STAGE_EXECUTE: |
| 53 | + $this->runMigration(); |
| 54 | + break; |
| 55 | + default: |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + protected function runPreparation(): void { |
| 61 | + try { |
| 62 | + $qb = $this->dbc->getQueryBuilder(); |
| 63 | + $qb2 = $this->dbc->getQueryBuilder(); |
| 64 | + |
| 65 | + $innerSQL = $qb2->select('userid') |
| 66 | + ->from('preferences') |
| 67 | + ->where($qb2->expr()->eq('configkey', $qb->createNamedParameter('background_color'))); |
| 68 | + |
| 69 | + // Get those users, that have a background_image set - not the default, but no background_color. |
| 70 | + $result = $qb->selectDistinct('a.userid') |
| 71 | + ->from('preferences', 'a') |
| 72 | + ->leftJoin('a', $qb->createFunction('('.$innerSQL->getSQL().')'), 'b', 'a.userid = b.userid') |
| 73 | + ->where($qb2->expr()->eq('a.configkey', $qb->createNamedParameter('background_image'))) |
| 74 | + ->andWhere($qb2->expr()->neq('a.configvalue', $qb->createNamedParameter(BackgroundService::BACKGROUND_DEFAULT))) |
| 75 | + ->andWhere($qb2->expr()->isNull('b.userid')) |
| 76 | + ->executeQuery(); |
| 77 | + |
| 78 | + $userIds = $result->fetchAll(\PDO::FETCH_COLUMN); |
| 79 | + $this->logger->info('Prepare to restore background information for {users} users', ['users' => count($userIds)]); |
| 80 | + $this->storeUserIdsToProcess($userIds); |
| 81 | + } catch (\Throwable $t) { |
| 82 | + $this->jobList->add(self::class, ['stage' => self::STAGE_PREPARE]); |
| 83 | + throw $t; |
| 84 | + } |
| 85 | + $this->jobList->add(self::class, ['stage' => self::STAGE_EXECUTE]); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @throws NotPermittedException |
| 90 | + * @throws NotFoundException |
| 91 | + */ |
| 92 | + protected function runMigration(): void { |
| 93 | + $allUserIds = $this->readUserIdsToProcess(); |
| 94 | + $notSoFastMode = count($allUserIds) > 1000; |
| 95 | + |
| 96 | + $userIds = array_slice($allUserIds, 0, 1000); |
| 97 | + foreach ($userIds as $userId) { |
| 98 | + $backgroundColor = $this->config->getUserValue($userId, Application::APP_ID, 'background_color'); |
| 99 | + if ($backgroundColor !== '') { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + $background = $this->config->getUserValue($userId, Application::APP_ID, 'background_image'); |
| 104 | + switch($background) { |
| 105 | + case BackgroundService::BACKGROUND_DEFAULT: |
| 106 | + $this->service->setDefaultBackground($userId); |
| 107 | + break; |
| 108 | + case BackgroundService::BACKGROUND_COLOR: |
| 109 | + break; |
| 110 | + case BackgroundService::BACKGROUND_CUSTOM: |
| 111 | + $this->service->recalculateMeanColor($userId); |
| 112 | + break; |
| 113 | + default: |
| 114 | + // shipped backgrounds |
| 115 | + // do not alter primary color |
| 116 | + $primary = $this->config->getUserValue($userId, Application::APP_ID, 'primary_color'); |
| 117 | + if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$background])) { |
| 118 | + $this->service->setShippedBackground($background, $userId); |
| 119 | + } else { |
| 120 | + $this->service->setDefaultBackground($userId); |
| 121 | + } |
| 122 | + // Restore primary |
| 123 | + if ($primary !== '') { |
| 124 | + $this->config->setUserValue($userId, Application::APP_ID, 'primary_color', $primary); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if ($notSoFastMode) { |
| 130 | + $remainingUserIds = array_slice($allUserIds, 1000); |
| 131 | + $this->storeUserIdsToProcess($remainingUserIds); |
| 132 | + $this->jobList->add(self::class, ['stage' => self::STAGE_EXECUTE]); |
| 133 | + } else { |
| 134 | + $this->deleteStateFile(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @throws NotPermittedException |
| 140 | + * @throws NotFoundException |
| 141 | + */ |
| 142 | + protected function readUserIdsToProcess(): array { |
| 143 | + $globalFolder = $this->appData->getFolder('global'); |
| 144 | + if ($globalFolder->fileExists(self::STATE_FILE_NAME)) { |
| 145 | + $file = $globalFolder->getFile(self::STATE_FILE_NAME); |
| 146 | + try { |
| 147 | + $userIds = \json_decode($file->getContent(), true); |
| 148 | + } catch (NotFoundException $e) { |
| 149 | + $userIds = []; |
| 150 | + } |
| 151 | + if ($userIds === null) { |
| 152 | + $userIds = []; |
| 153 | + } |
| 154 | + } else { |
| 155 | + $userIds = []; |
| 156 | + } |
| 157 | + return $userIds; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @throws NotFoundException |
| 162 | + */ |
| 163 | + protected function storeUserIdsToProcess(array $userIds): void { |
| 164 | + $storableUserIds = \json_encode($userIds); |
| 165 | + $globalFolder = $this->appData->getFolder('global'); |
| 166 | + try { |
| 167 | + if ($globalFolder->fileExists(self::STATE_FILE_NAME)) { |
| 168 | + $file = $globalFolder->getFile(self::STATE_FILE_NAME); |
| 169 | + } else { |
| 170 | + $file = $globalFolder->newFile(self::STATE_FILE_NAME); |
| 171 | + } |
| 172 | + $file->putContent($storableUserIds); |
| 173 | + } catch (NotFoundException $e) { |
| 174 | + } catch (NotPermittedException $e) { |
| 175 | + $this->logger->warning('Lacking permissions to create {file}', |
| 176 | + [ |
| 177 | + 'app' => 'theming', |
| 178 | + 'file' => self::STATE_FILE_NAME, |
| 179 | + 'exception' => $e, |
| 180 | + ] |
| 181 | + ); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * @throws NotFoundException |
| 187 | + */ |
| 188 | + protected function deleteStateFile(): void { |
| 189 | + $globalFolder = $this->appData->getFolder('global'); |
| 190 | + if ($globalFolder->fileExists(self::STATE_FILE_NAME)) { |
| 191 | + $file = $globalFolder->getFile(self::STATE_FILE_NAME); |
| 192 | + try { |
| 193 | + $file->delete(); |
| 194 | + } catch (NotPermittedException $e) { |
| 195 | + $this->logger->info('Could not delete {file} due to permissions. It is safe to delete manually inside data -> appdata -> theming -> global.', |
| 196 | + [ |
| 197 | + 'app' => 'theming', |
| 198 | + 'file' => $file->getName(), |
| 199 | + 'exception' => $e, |
| 200 | + ] |
| 201 | + ); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments