@@ -40,188 +40,188 @@ |
||
| 40 | 40 | use Psr\Log\LoggerInterface; |
| 41 | 41 | |
| 42 | 42 | class MigrateBackgroundImages extends QueuedJob { |
| 43 | - public const TIME_SENSITIVE = 0; |
|
| 44 | - |
|
| 45 | - public const STAGE_PREPARE = 'prepare'; |
|
| 46 | - public const STAGE_EXECUTE = 'execute'; |
|
| 47 | - // will be saved in appdata/theming/global/ |
|
| 48 | - protected const STATE_FILE_NAME = '25_dashboard_to_theming_migration_users.json'; |
|
| 49 | - |
|
| 50 | - private IAppDataFactory $appDataFactory; |
|
| 51 | - private IJobList $jobList; |
|
| 52 | - private IDBConnection $dbc; |
|
| 53 | - private IAppData $appData; |
|
| 54 | - private LoggerInterface $logger; |
|
| 55 | - |
|
| 56 | - public function __construct( |
|
| 57 | - ITimeFactory $time, |
|
| 58 | - IAppDataFactory $appDataFactory, |
|
| 59 | - IJobList $jobList, |
|
| 60 | - IDBConnection $dbc, |
|
| 61 | - IAppData $appData, |
|
| 62 | - LoggerInterface $logger |
|
| 63 | - ) { |
|
| 64 | - parent::__construct($time); |
|
| 65 | - $this->appDataFactory = $appDataFactory; |
|
| 66 | - $this->jobList = $jobList; |
|
| 67 | - $this->dbc = $dbc; |
|
| 68 | - $this->appData = $appData; |
|
| 69 | - $this->logger = $logger; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - protected function run(mixed $argument): void { |
|
| 73 | - if (!is_array($argument) || !isset($argument['stage'])) { |
|
| 74 | - throw new \Exception('Job '.self::class.' called with wrong argument'); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - switch ($argument['stage']) { |
|
| 78 | - case self::STAGE_PREPARE: |
|
| 79 | - $this->runPreparation(); |
|
| 80 | - break; |
|
| 81 | - case self::STAGE_EXECUTE: |
|
| 82 | - $this->runMigration(); |
|
| 83 | - break; |
|
| 84 | - default: |
|
| 85 | - break; |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - protected function runPreparation(): void { |
|
| 90 | - try { |
|
| 91 | - $selector = $this->dbc->getQueryBuilder(); |
|
| 92 | - $result = $selector->select('userid') |
|
| 93 | - ->from('preferences') |
|
| 94 | - ->where($selector->expr()->eq('appid', $selector->createNamedParameter('theming'))) |
|
| 95 | - ->andWhere($selector->expr()->eq('configkey', $selector->createNamedParameter('background'))) |
|
| 96 | - ->andWhere($selector->expr()->eq('configvalue', $selector->createNamedParameter('custom', IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)) |
|
| 97 | - ->executeQuery(); |
|
| 98 | - |
|
| 99 | - $userIds = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
| 100 | - $this->storeUserIdsToProcess($userIds); |
|
| 101 | - } catch (\Throwable $t) { |
|
| 102 | - $this->jobList->add(self::class, ['stage' => self::STAGE_PREPARE]); |
|
| 103 | - throw $t; |
|
| 104 | - } |
|
| 105 | - $this->jobList->add(self::class, ['stage' => self::STAGE_EXECUTE]); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @throws NotPermittedException |
|
| 110 | - * @throws NotFoundException |
|
| 111 | - */ |
|
| 112 | - protected function runMigration(): void { |
|
| 113 | - $allUserIds = $this->readUserIdsToProcess(); |
|
| 114 | - $notSoFastMode = count($allUserIds) > 5000; |
|
| 115 | - $dashboardData = $this->appDataFactory->get('dashboard'); |
|
| 116 | - |
|
| 117 | - $userIds = $notSoFastMode ? array_slice($allUserIds, 0, 5000) : $allUserIds; |
|
| 118 | - foreach ($userIds as $userId) { |
|
| 119 | - try { |
|
| 120 | - // migration |
|
| 121 | - $file = $dashboardData->getFolder($userId)->getFile('background.jpg'); |
|
| 122 | - $targetDir = $this->getUserFolder($userId); |
|
| 123 | - |
|
| 124 | - if (!$targetDir->fileExists('background.jpg')) { |
|
| 125 | - $targetDir->newFile('background.jpg', $file->getContent()); |
|
| 126 | - } |
|
| 127 | - $file->delete(); |
|
| 128 | - } catch (NotFoundException|NotPermittedException $e) { |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if ($notSoFastMode) { |
|
| 133 | - $remainingUserIds = array_slice($allUserIds, 5000); |
|
| 134 | - $this->storeUserIdsToProcess($remainingUserIds); |
|
| 135 | - $this->jobList->add(self::class, ['stage' => self::STAGE_EXECUTE]); |
|
| 136 | - } else { |
|
| 137 | - $this->deleteStateFile(); |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @throws NotPermittedException |
|
| 143 | - * @throws NotFoundException |
|
| 144 | - */ |
|
| 145 | - protected function readUserIdsToProcess(): array { |
|
| 146 | - $globalFolder = $this->appData->getFolder('global'); |
|
| 147 | - if ($globalFolder->fileExists(self::STATE_FILE_NAME)) { |
|
| 148 | - $file = $globalFolder->getFile(self::STATE_FILE_NAME); |
|
| 149 | - try { |
|
| 150 | - $userIds = \json_decode($file->getContent(), true); |
|
| 151 | - } catch (NotFoundException $e) { |
|
| 152 | - $userIds = []; |
|
| 153 | - } |
|
| 154 | - if ($userIds === null) { |
|
| 155 | - $userIds = []; |
|
| 156 | - } |
|
| 157 | - } else { |
|
| 158 | - $userIds = []; |
|
| 159 | - } |
|
| 160 | - return $userIds; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @throws NotFoundException |
|
| 165 | - */ |
|
| 166 | - protected function storeUserIdsToProcess(array $userIds): void { |
|
| 167 | - $storableUserIds = \json_encode($userIds); |
|
| 168 | - $globalFolder = $this->appData->getFolder('global'); |
|
| 169 | - try { |
|
| 170 | - if ($globalFolder->fileExists(self::STATE_FILE_NAME)) { |
|
| 171 | - $file = $globalFolder->getFile(self::STATE_FILE_NAME); |
|
| 172 | - } else { |
|
| 173 | - $file = $globalFolder->newFile(self::STATE_FILE_NAME); |
|
| 174 | - } |
|
| 175 | - $file->putContent($storableUserIds); |
|
| 176 | - } catch (NotFoundException $e) { |
|
| 177 | - } catch (NotPermittedException $e) { |
|
| 178 | - $this->logger->warning('Lacking permissions to create {file}', |
|
| 179 | - [ |
|
| 180 | - 'app' => 'theming', |
|
| 181 | - 'file' => self::STATE_FILE_NAME, |
|
| 182 | - 'exception' => $e, |
|
| 183 | - ] |
|
| 184 | - ); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * @throws NotFoundException |
|
| 190 | - */ |
|
| 191 | - protected function deleteStateFile(): void { |
|
| 192 | - $globalFolder = $this->appData->getFolder('global'); |
|
| 193 | - if ($globalFolder->fileExists(self::STATE_FILE_NAME)) { |
|
| 194 | - $file = $globalFolder->getFile(self::STATE_FILE_NAME); |
|
| 195 | - try { |
|
| 196 | - $file->delete(); |
|
| 197 | - } catch (NotPermittedException $e) { |
|
| 198 | - $this->logger->info('Could not delete {file} due to permissions. It is safe to delete manually inside data -> appdata -> theming -> global.', |
|
| 199 | - [ |
|
| 200 | - 'app' => 'theming', |
|
| 201 | - 'file' => $file->getName(), |
|
| 202 | - 'exception' => $e, |
|
| 203 | - ] |
|
| 204 | - ); |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get the root location for users theming data |
|
| 211 | - */ |
|
| 212 | - protected function getUserFolder(string $userId): ISimpleFolder { |
|
| 213 | - $themingData = $this->appDataFactory->get(Application::APP_ID); |
|
| 214 | - |
|
| 215 | - try { |
|
| 216 | - $rootFolder = $themingData->getFolder('users'); |
|
| 217 | - } catch (NotFoundException $e) { |
|
| 218 | - $rootFolder = $themingData->newFolder('users'); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - try { |
|
| 222 | - return $rootFolder->getFolder($userId); |
|
| 223 | - } catch (NotFoundException $e) { |
|
| 224 | - return $rootFolder->newFolder($userId); |
|
| 225 | - } |
|
| 226 | - } |
|
| 43 | + public const TIME_SENSITIVE = 0; |
|
| 44 | + |
|
| 45 | + public const STAGE_PREPARE = 'prepare'; |
|
| 46 | + public const STAGE_EXECUTE = 'execute'; |
|
| 47 | + // will be saved in appdata/theming/global/ |
|
| 48 | + protected const STATE_FILE_NAME = '25_dashboard_to_theming_migration_users.json'; |
|
| 49 | + |
|
| 50 | + private IAppDataFactory $appDataFactory; |
|
| 51 | + private IJobList $jobList; |
|
| 52 | + private IDBConnection $dbc; |
|
| 53 | + private IAppData $appData; |
|
| 54 | + private LoggerInterface $logger; |
|
| 55 | + |
|
| 56 | + public function __construct( |
|
| 57 | + ITimeFactory $time, |
|
| 58 | + IAppDataFactory $appDataFactory, |
|
| 59 | + IJobList $jobList, |
|
| 60 | + IDBConnection $dbc, |
|
| 61 | + IAppData $appData, |
|
| 62 | + LoggerInterface $logger |
|
| 63 | + ) { |
|
| 64 | + parent::__construct($time); |
|
| 65 | + $this->appDataFactory = $appDataFactory; |
|
| 66 | + $this->jobList = $jobList; |
|
| 67 | + $this->dbc = $dbc; |
|
| 68 | + $this->appData = $appData; |
|
| 69 | + $this->logger = $logger; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + protected function run(mixed $argument): void { |
|
| 73 | + if (!is_array($argument) || !isset($argument['stage'])) { |
|
| 74 | + throw new \Exception('Job '.self::class.' called with wrong argument'); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + switch ($argument['stage']) { |
|
| 78 | + case self::STAGE_PREPARE: |
|
| 79 | + $this->runPreparation(); |
|
| 80 | + break; |
|
| 81 | + case self::STAGE_EXECUTE: |
|
| 82 | + $this->runMigration(); |
|
| 83 | + break; |
|
| 84 | + default: |
|
| 85 | + break; |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + protected function runPreparation(): void { |
|
| 90 | + try { |
|
| 91 | + $selector = $this->dbc->getQueryBuilder(); |
|
| 92 | + $result = $selector->select('userid') |
|
| 93 | + ->from('preferences') |
|
| 94 | + ->where($selector->expr()->eq('appid', $selector->createNamedParameter('theming'))) |
|
| 95 | + ->andWhere($selector->expr()->eq('configkey', $selector->createNamedParameter('background'))) |
|
| 96 | + ->andWhere($selector->expr()->eq('configvalue', $selector->createNamedParameter('custom', IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)) |
|
| 97 | + ->executeQuery(); |
|
| 98 | + |
|
| 99 | + $userIds = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
| 100 | + $this->storeUserIdsToProcess($userIds); |
|
| 101 | + } catch (\Throwable $t) { |
|
| 102 | + $this->jobList->add(self::class, ['stage' => self::STAGE_PREPARE]); |
|
| 103 | + throw $t; |
|
| 104 | + } |
|
| 105 | + $this->jobList->add(self::class, ['stage' => self::STAGE_EXECUTE]); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @throws NotPermittedException |
|
| 110 | + * @throws NotFoundException |
|
| 111 | + */ |
|
| 112 | + protected function runMigration(): void { |
|
| 113 | + $allUserIds = $this->readUserIdsToProcess(); |
|
| 114 | + $notSoFastMode = count($allUserIds) > 5000; |
|
| 115 | + $dashboardData = $this->appDataFactory->get('dashboard'); |
|
| 116 | + |
|
| 117 | + $userIds = $notSoFastMode ? array_slice($allUserIds, 0, 5000) : $allUserIds; |
|
| 118 | + foreach ($userIds as $userId) { |
|
| 119 | + try { |
|
| 120 | + // migration |
|
| 121 | + $file = $dashboardData->getFolder($userId)->getFile('background.jpg'); |
|
| 122 | + $targetDir = $this->getUserFolder($userId); |
|
| 123 | + |
|
| 124 | + if (!$targetDir->fileExists('background.jpg')) { |
|
| 125 | + $targetDir->newFile('background.jpg', $file->getContent()); |
|
| 126 | + } |
|
| 127 | + $file->delete(); |
|
| 128 | + } catch (NotFoundException|NotPermittedException $e) { |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if ($notSoFastMode) { |
|
| 133 | + $remainingUserIds = array_slice($allUserIds, 5000); |
|
| 134 | + $this->storeUserIdsToProcess($remainingUserIds); |
|
| 135 | + $this->jobList->add(self::class, ['stage' => self::STAGE_EXECUTE]); |
|
| 136 | + } else { |
|
| 137 | + $this->deleteStateFile(); |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @throws NotPermittedException |
|
| 143 | + * @throws NotFoundException |
|
| 144 | + */ |
|
| 145 | + protected function readUserIdsToProcess(): array { |
|
| 146 | + $globalFolder = $this->appData->getFolder('global'); |
|
| 147 | + if ($globalFolder->fileExists(self::STATE_FILE_NAME)) { |
|
| 148 | + $file = $globalFolder->getFile(self::STATE_FILE_NAME); |
|
| 149 | + try { |
|
| 150 | + $userIds = \json_decode($file->getContent(), true); |
|
| 151 | + } catch (NotFoundException $e) { |
|
| 152 | + $userIds = []; |
|
| 153 | + } |
|
| 154 | + if ($userIds === null) { |
|
| 155 | + $userIds = []; |
|
| 156 | + } |
|
| 157 | + } else { |
|
| 158 | + $userIds = []; |
|
| 159 | + } |
|
| 160 | + return $userIds; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @throws NotFoundException |
|
| 165 | + */ |
|
| 166 | + protected function storeUserIdsToProcess(array $userIds): void { |
|
| 167 | + $storableUserIds = \json_encode($userIds); |
|
| 168 | + $globalFolder = $this->appData->getFolder('global'); |
|
| 169 | + try { |
|
| 170 | + if ($globalFolder->fileExists(self::STATE_FILE_NAME)) { |
|
| 171 | + $file = $globalFolder->getFile(self::STATE_FILE_NAME); |
|
| 172 | + } else { |
|
| 173 | + $file = $globalFolder->newFile(self::STATE_FILE_NAME); |
|
| 174 | + } |
|
| 175 | + $file->putContent($storableUserIds); |
|
| 176 | + } catch (NotFoundException $e) { |
|
| 177 | + } catch (NotPermittedException $e) { |
|
| 178 | + $this->logger->warning('Lacking permissions to create {file}', |
|
| 179 | + [ |
|
| 180 | + 'app' => 'theming', |
|
| 181 | + 'file' => self::STATE_FILE_NAME, |
|
| 182 | + 'exception' => $e, |
|
| 183 | + ] |
|
| 184 | + ); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * @throws NotFoundException |
|
| 190 | + */ |
|
| 191 | + protected function deleteStateFile(): void { |
|
| 192 | + $globalFolder = $this->appData->getFolder('global'); |
|
| 193 | + if ($globalFolder->fileExists(self::STATE_FILE_NAME)) { |
|
| 194 | + $file = $globalFolder->getFile(self::STATE_FILE_NAME); |
|
| 195 | + try { |
|
| 196 | + $file->delete(); |
|
| 197 | + } catch (NotPermittedException $e) { |
|
| 198 | + $this->logger->info('Could not delete {file} due to permissions. It is safe to delete manually inside data -> appdata -> theming -> global.', |
|
| 199 | + [ |
|
| 200 | + 'app' => 'theming', |
|
| 201 | + 'file' => $file->getName(), |
|
| 202 | + 'exception' => $e, |
|
| 203 | + ] |
|
| 204 | + ); |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get the root location for users theming data |
|
| 211 | + */ |
|
| 212 | + protected function getUserFolder(string $userId): ISimpleFolder { |
|
| 213 | + $themingData = $this->appDataFactory->get(Application::APP_ID); |
|
| 214 | + |
|
| 215 | + try { |
|
| 216 | + $rootFolder = $themingData->getFolder('users'); |
|
| 217 | + } catch (NotFoundException $e) { |
|
| 218 | + $rootFolder = $themingData->newFolder('users'); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + try { |
|
| 222 | + return $rootFolder->getFolder($userId); |
|
| 223 | + } catch (NotFoundException $e) { |
|
| 224 | + return $rootFolder->newFolder($userId); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | 227 | } |