|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | /** |
4 | | - * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors |
5 | | - * SPDX-FileCopyrightText: 2016 ownCloud, Inc. |
| 4 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
6 | 5 | * SPDX-License-Identifier: AGPL-3.0-only |
7 | 6 | */ |
8 | 7 | namespace OC\Repair; |
9 | 8 |
|
| 9 | +use OC\Migration\NullOutput; |
10 | 10 | use OCP\DB\QueryBuilder\IQueryBuilder; |
11 | 11 | use OCP\IConfig; |
12 | 12 | use OCP\IDBConnection; |
13 | 13 | use OCP\Migration\IOutput; |
14 | 14 | use OCP\Migration\IRepairStep; |
15 | 15 |
|
16 | 16 | class RepairMimeTypes implements IRepairStep { |
17 | | - /** @var IConfig */ |
18 | | - protected $config; |
19 | | - /** @var IDBConnection */ |
20 | | - protected $connection; |
| 17 | + private bool $dryRun = false; |
| 18 | + private int $changeCount = 0; |
21 | 19 |
|
22 | 20 | /** @var int */ |
23 | 21 | protected $folderMimeTypeId; |
24 | 22 |
|
25 | | - public function __construct(IConfig $config, |
26 | | - IDBConnection $connection) { |
27 | | - $this->config = $config; |
28 | | - $this->connection = $connection; |
| 23 | + public function __construct( |
| 24 | + protected IConfig $config, |
| 25 | + protected IDBConnection $connection |
| 26 | + ) { |
29 | 27 | } |
30 | 28 |
|
31 | 29 | public function getName() { |
32 | 30 | return 'Repair mime types'; |
33 | 31 | } |
34 | 32 |
|
35 | 33 | private function updateMimetypes($updatedMimetypes) { |
| 34 | + if ($this->dryRun) { |
| 35 | + $this->changeCount += count($updatedMimetypes); |
| 36 | + return; |
| 37 | + } |
36 | 38 | $query = $this->connection->getQueryBuilder(); |
37 | 39 | $query->select('id') |
38 | 40 | ->from('mimetypes') |
@@ -236,77 +238,99 @@ private function introduceReStructuredTextFormatType() { |
236 | 238 | return $this->updateMimetypes($updatedMimetypes); |
237 | 239 | } |
238 | 240 |
|
| 241 | + public function migrationsAvailable(): bool { |
| 242 | + $this->dryRun = true; |
| 243 | + $this->run(new NullOutput()); |
| 244 | + $this->dryRun = false; |
| 245 | + return $this->changeCount > 0; |
| 246 | + } |
| 247 | + |
| 248 | + private function getMimeTypeVersion(): string { |
| 249 | + $serverVersion = $this->config->getSystemValue('version', '0.0.0'); |
| 250 | + // 29.0.0.10 is the last version with a mimetype migration before it was moved to a separate version number |
| 251 | + if (version_compare($serverVersion, '29.0.0.10', '>')) { |
| 252 | + return $this->config->getAppValue('files', 'mimetype_version', '29.0.0.10'); |
| 253 | + } else { |
| 254 | + return $serverVersion; |
| 255 | + } |
| 256 | + } |
| 257 | + |
239 | 258 | /** |
240 | 259 | * Fix mime types |
241 | 260 | */ |
242 | 261 | public function run(IOutput $out) { |
243 | | - $ocVersionFromBeforeUpdate = $this->config->getSystemValueString('version', '0.0.0'); |
| 262 | + $serverVersion = $this->config->getSystemValue('version', '0.0.0'); |
| 263 | + $mimeTypeVersion = $this->getMimeTypeVersion(); |
244 | 264 |
|
245 | 265 | // NOTE TO DEVELOPERS: when adding new mime types, please make sure to |
246 | 266 | // add a version comparison to avoid doing it every time |
247 | 267 |
|
248 | | - if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.14', '<') && $this->introduceImageTypes()) { |
| 268 | + if (version_compare($mimeTypeVersion, '12.0.0.14', '<') && $this->introduceImageTypes()) { |
249 | 269 | $out->info('Fixed image mime types'); |
250 | 270 | } |
251 | 271 |
|
252 | | - if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) { |
| 272 | + if (version_compare($mimeTypeVersion, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) { |
253 | 273 | $out->info('Fixed windows program mime types'); |
254 | 274 | } |
255 | 275 |
|
256 | | - if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.0', '<') && $this->introduceLocationTypes()) { |
| 276 | + if (version_compare($mimeTypeVersion, '13.0.0.0', '<') && $this->introduceLocationTypes()) { |
257 | 277 | $out->info('Fixed geospatial mime types'); |
258 | 278 | } |
259 | 279 |
|
260 | | - if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) { |
| 280 | + if (version_compare($mimeTypeVersion, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) { |
261 | 281 | $out->info('Fixed internet-shortcut mime types'); |
262 | 282 | } |
263 | 283 |
|
264 | | - if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.6', '<') && $this->introduceStreamingTypes()) { |
| 284 | + if (version_compare($mimeTypeVersion, '13.0.0.6', '<') && $this->introduceStreamingTypes()) { |
265 | 285 | $out->info('Fixed streaming mime types'); |
266 | 286 | } |
267 | 287 |
|
268 | | - if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.8', '<') && $this->introduceVisioTypes()) { |
| 288 | + if (version_compare($mimeTypeVersion, '14.0.0.8', '<') && $this->introduceVisioTypes()) { |
269 | 289 | $out->info('Fixed visio mime types'); |
270 | 290 | } |
271 | 291 |
|
272 | | - if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.10', '<') && $this->introduceComicbookTypes()) { |
| 292 | + if (version_compare($mimeTypeVersion, '14.0.0.10', '<') && $this->introduceComicbookTypes()) { |
273 | 293 | $out->info('Fixed comicbook mime types'); |
274 | 294 | } |
275 | 295 |
|
276 | | - if (version_compare($ocVersionFromBeforeUpdate, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) { |
| 296 | + if (version_compare($mimeTypeVersion, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) { |
277 | 297 | $out->info('Fixed OpenDocument template mime types'); |
278 | 298 | } |
279 | 299 |
|
280 | | - if (version_compare($ocVersionFromBeforeUpdate, '21.0.0.7', '<') && $this->introduceOrgModeType()) { |
| 300 | + if (version_compare($mimeTypeVersion, '21.0.0.7', '<') && $this->introduceOrgModeType()) { |
281 | 301 | $out->info('Fixed orgmode mime types'); |
282 | 302 | } |
283 | 303 |
|
284 | | - if (version_compare($ocVersionFromBeforeUpdate, '23.0.0.2', '<') && $this->introduceFlatOpenDocumentType()) { |
| 304 | + if (version_compare($mimeTypeVersion, '23.0.0.2', '<') && $this->introduceFlatOpenDocumentType()) { |
285 | 305 | $out->info('Fixed Flat OpenDocument mime types'); |
286 | 306 | } |
287 | 307 |
|
288 | | - if (version_compare($ocVersionFromBeforeUpdate, '25.0.0.2', '<') && $this->introduceOnlyofficeFormType()) { |
| 308 | + if (version_compare($mimeTypeVersion, '25.0.0.2', '<') && $this->introduceOnlyofficeFormType()) { |
289 | 309 | $out->info('Fixed ONLYOFFICE Forms OpenXML mime types'); |
290 | 310 | } |
291 | 311 |
|
292 | | - if (version_compare($ocVersionFromBeforeUpdate, '26.0.0.1', '<') && $this->introduceAsciidocType()) { |
| 312 | + if (version_compare($mimeTypeVersion, '26.0.0.1', '<') && $this->introduceAsciidocType()) { |
293 | 313 | $out->info('Fixed AsciiDoc mime types'); |
294 | 314 | } |
295 | 315 |
|
296 | | - if (version_compare($ocVersionFromBeforeUpdate, '28.0.0.5', '<') && $this->introduceEnhancedMetafileFormatType()) { |
| 316 | + if (version_compare($mimeTypeVersion, '28.0.0.5', '<') && $this->introduceEnhancedMetafileFormatType()) { |
297 | 317 | $out->info('Fixed Enhanced Metafile Format mime types'); |
298 | 318 | } |
299 | 319 |
|
300 | | - if (version_compare($ocVersionFromBeforeUpdate, '29.0.0.2', '<') && $this->introduceEmlAndMsgFormatType()) { |
| 320 | + if (version_compare($mimeTypeVersion, '29.0.0.2', '<') && $this->introduceEmlAndMsgFormatType()) { |
301 | 321 | $out->info('Fixed eml and msg mime type'); |
302 | 322 | } |
303 | 323 |
|
304 | | - if (version_compare($ocVersionFromBeforeUpdate, '29.0.0.6', '<') && $this->introduceAacAudioType()) { |
| 324 | + if (version_compare($mimeTypeVersion, '29.0.0.6', '<') && $this->introduceAacAudioType()) { |
305 | 325 | $out->info('Fixed aac mime type'); |
306 | 326 | } |
307 | 327 |
|
308 | | - if (version_compare($ocVersionFromBeforeUpdate, '29.0.0.10', '<') && $this->introduceReStructuredTextFormatType()) { |
| 328 | + if (version_compare($mimeTypeVersion, '29.0.0.10', '<') && $this->introduceReStructuredTextFormatType()) { |
309 | 329 | $out->info('Fixed ReStructured Text mime type'); |
310 | 330 | } |
| 331 | + |
| 332 | + if (!$this->dryRun) { |
| 333 | + $this->config->setAppValue('files', 'mimetype_version', $serverVersion); |
| 334 | + } |
311 | 335 | } |
312 | 336 | } |
0 commit comments