|
33 | 33 | */ |
34 | 34 | namespace OC\Repair; |
35 | 35 |
|
| 36 | +use OC\Migration\NullOutput; |
36 | 37 | use OCP\DB\QueryBuilder\IQueryBuilder; |
37 | 38 | use OCP\IConfig; |
38 | 39 | use OCP\IDBConnection; |
39 | 40 | use OCP\Migration\IOutput; |
40 | 41 | use OCP\Migration\IRepairStep; |
41 | 42 |
|
42 | 43 | class RepairMimeTypes implements IRepairStep { |
43 | | - /** @var IConfig */ |
44 | | - protected $config; |
45 | | - /** @var IDBConnection */ |
46 | | - protected $connection; |
| 44 | + private bool $dryRun = false; |
| 45 | + private int $changeCount = 0; |
47 | 46 |
|
48 | 47 | /** @var int */ |
49 | 48 | protected $folderMimeTypeId; |
50 | 49 |
|
51 | | - public function __construct(IConfig $config, |
52 | | - IDBConnection $connection) { |
53 | | - $this->config = $config; |
54 | | - $this->connection = $connection; |
| 50 | + public function __construct( |
| 51 | + protected IConfig $config, |
| 52 | + protected IDBConnection $connection |
| 53 | + ) { |
55 | 54 | } |
56 | 55 |
|
57 | 56 | public function getName() { |
58 | 57 | return 'Repair mime types'; |
59 | 58 | } |
60 | 59 |
|
61 | 60 | private function updateMimetypes($updatedMimetypes) { |
| 61 | + if ($this->dryRun) { |
| 62 | + $this->changeCount += count($updatedMimetypes); |
| 63 | + return; |
| 64 | + } |
62 | 65 | $query = $this->connection->getQueryBuilder(); |
63 | 66 | $query->select('id') |
64 | 67 | ->from('mimetypes') |
@@ -262,77 +265,97 @@ private function introduceReStructuredTextFormatType() { |
262 | 265 | return $this->updateMimetypes($updatedMimetypes); |
263 | 266 | } |
264 | 267 |
|
| 268 | + public function migrationsAvailable(): bool { |
| 269 | + $this->dryRun = true; |
| 270 | + $this->run(new NullOutput()); |
| 271 | + $this->dryRun = false; |
| 272 | + return $this->changeCount > 0; |
| 273 | + } |
| 274 | + |
| 275 | + private function getMimeTypeVersion(): string { |
| 276 | + $mimeVersion = $this->config->getAppValue('files', 'mimetype_version', ''); |
| 277 | + if ($mimeVersion) { |
| 278 | + return $mimeVersion; |
| 279 | + } |
| 280 | + return $this->config->getSystemValueString('version', '0.0.0'); |
| 281 | + } |
| 282 | + |
265 | 283 | /** |
266 | 284 | * Fix mime types |
267 | 285 | */ |
268 | 286 | public function run(IOutput $out) { |
269 | | - $ocVersionFromBeforeUpdate = $this->config->getSystemValueString('version', '0.0.0'); |
| 287 | + $serverVersion = $this->config->getSystemValueString('version', '0.0.0'); |
| 288 | + $mimeTypeVersion = $this->getMimeTypeVersion(); |
270 | 289 |
|
271 | 290 | // NOTE TO DEVELOPERS: when adding new mime types, please make sure to |
272 | 291 | // add a version comparison to avoid doing it every time |
273 | 292 |
|
274 | | - if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.14', '<') && $this->introduceImageTypes()) { |
| 293 | + if (version_compare($mimeTypeVersion, '12.0.0.14', '<') && $this->introduceImageTypes()) { |
275 | 294 | $out->info('Fixed image mime types'); |
276 | 295 | } |
277 | 296 |
|
278 | | - if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) { |
| 297 | + if (version_compare($mimeTypeVersion, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) { |
279 | 298 | $out->info('Fixed windows program mime types'); |
280 | 299 | } |
281 | 300 |
|
282 | | - if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.0', '<') && $this->introduceLocationTypes()) { |
| 301 | + if (version_compare($mimeTypeVersion, '13.0.0.0', '<') && $this->introduceLocationTypes()) { |
283 | 302 | $out->info('Fixed geospatial mime types'); |
284 | 303 | } |
285 | 304 |
|
286 | | - if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) { |
| 305 | + if (version_compare($mimeTypeVersion, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) { |
287 | 306 | $out->info('Fixed internet-shortcut mime types'); |
288 | 307 | } |
289 | 308 |
|
290 | | - if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.6', '<') && $this->introduceStreamingTypes()) { |
| 309 | + if (version_compare($mimeTypeVersion, '13.0.0.6', '<') && $this->introduceStreamingTypes()) { |
291 | 310 | $out->info('Fixed streaming mime types'); |
292 | 311 | } |
293 | 312 |
|
294 | | - if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.8', '<') && $this->introduceVisioTypes()) { |
| 313 | + if (version_compare($mimeTypeVersion, '14.0.0.8', '<') && $this->introduceVisioTypes()) { |
295 | 314 | $out->info('Fixed visio mime types'); |
296 | 315 | } |
297 | 316 |
|
298 | | - if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.10', '<') && $this->introduceComicbookTypes()) { |
| 317 | + if (version_compare($mimeTypeVersion, '14.0.0.10', '<') && $this->introduceComicbookTypes()) { |
299 | 318 | $out->info('Fixed comicbook mime types'); |
300 | 319 | } |
301 | 320 |
|
302 | | - if (version_compare($ocVersionFromBeforeUpdate, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) { |
| 321 | + if (version_compare($mimeTypeVersion, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) { |
303 | 322 | $out->info('Fixed OpenDocument template mime types'); |
304 | 323 | } |
305 | 324 |
|
306 | | - if (version_compare($ocVersionFromBeforeUpdate, '21.0.0.7', '<') && $this->introduceOrgModeType()) { |
| 325 | + if (version_compare($mimeTypeVersion, '21.0.0.7', '<') && $this->introduceOrgModeType()) { |
307 | 326 | $out->info('Fixed orgmode mime types'); |
308 | 327 | } |
309 | 328 |
|
310 | | - if (version_compare($ocVersionFromBeforeUpdate, '23.0.0.2', '<') && $this->introduceFlatOpenDocumentType()) { |
| 329 | + if (version_compare($mimeTypeVersion, '23.0.0.2', '<') && $this->introduceFlatOpenDocumentType()) { |
311 | 330 | $out->info('Fixed Flat OpenDocument mime types'); |
312 | 331 | } |
313 | 332 |
|
314 | | - if (version_compare($ocVersionFromBeforeUpdate, '25.0.0.2', '<') && $this->introduceOnlyofficeFormType()) { |
| 333 | + if (version_compare($mimeTypeVersion, '25.0.0.2', '<') && $this->introduceOnlyofficeFormType()) { |
315 | 334 | $out->info('Fixed ONLYOFFICE Forms OpenXML mime types'); |
316 | 335 | } |
317 | 336 |
|
318 | | - if (version_compare($ocVersionFromBeforeUpdate, '26.0.0.1', '<') && $this->introduceAsciidocType()) { |
| 337 | + if (version_compare($mimeTypeVersion, '26.0.0.1', '<') && $this->introduceAsciidocType()) { |
319 | 338 | $out->info('Fixed AsciiDoc mime types'); |
320 | 339 | } |
321 | 340 |
|
322 | | - if (version_compare($ocVersionFromBeforeUpdate, '28.0.0.5', '<') && $this->introduceEnhancedMetafileFormatType()) { |
| 341 | + if (version_compare($mimeTypeVersion, '28.0.0.5', '<') && $this->introduceEnhancedMetafileFormatType()) { |
323 | 342 | $out->info('Fixed Enhanced Metafile Format mime types'); |
324 | 343 | } |
325 | 344 |
|
326 | | - if (version_compare($ocVersionFromBeforeUpdate, '29.0.0.2', '<') && $this->introduceEmlAndMsgFormatType()) { |
| 345 | + if (version_compare($mimeTypeVersion, '29.0.0.2', '<') && $this->introduceEmlAndMsgFormatType()) { |
327 | 346 | $out->info('Fixed eml and msg mime type'); |
328 | 347 | } |
329 | 348 |
|
330 | | - if (version_compare($ocVersionFromBeforeUpdate, '29.0.0.6', '<') && $this->introduceAacAudioType()) { |
| 349 | + if (version_compare($mimeTypeVersion, '29.0.0.6', '<') && $this->introduceAacAudioType()) { |
331 | 350 | $out->info('Fixed aac mime type'); |
332 | 351 | } |
333 | 352 |
|
334 | | - if (version_compare($ocVersionFromBeforeUpdate, '29.0.0.10', '<') && $this->introduceReStructuredTextFormatType()) { |
| 353 | + if (version_compare($mimeTypeVersion, '29.0.0.10', '<') && $this->introduceReStructuredTextFormatType()) { |
335 | 354 | $out->info('Fixed ReStructured Text mime type'); |
336 | 355 | } |
| 356 | + |
| 357 | + if (!$this->dryRun) { |
| 358 | + $this->config->setAppValue('files', 'mimetype_version', $serverVersion); |
| 359 | + } |
337 | 360 | } |
338 | 361 | } |
0 commit comments