|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Migration; |
| 10 | + |
| 11 | +use Closure; |
| 12 | +use OCP\DB\ISchemaWrapper; |
| 13 | +use OCP\Migration\IOutput; |
| 14 | +use OCP\Migration\SimpleMigrationStep; |
| 15 | + |
| 16 | +/** |
| 17 | + * Normalize file names by removing extensions from the name field |
| 18 | + * - Removes file extensions from libresign_file.name based on metadata['extension'] |
| 19 | + * - After this migration, the removeExtensionFromName method is no longer needed |
| 20 | + */ |
| 21 | +class Version16001Date20251227000000 extends SimpleMigrationStep { |
| 22 | + public function __construct( |
| 23 | + private \OCP\IDBConnection $connection, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @param IOutput $output |
| 29 | + * @param Closure(): ISchemaWrapper $schemaClosure |
| 30 | + * @param array $options |
| 31 | + * @return null|ISchemaWrapper |
| 32 | + */ |
| 33 | + #[\Override] |
| 34 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 35 | + // No schema changes needed, only data migration |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Migrate file names by removing extensions |
| 41 | + */ |
| 42 | + #[\Override] |
| 43 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
| 44 | + $qb = $this->connection->getQueryBuilder(); |
| 45 | + |
| 46 | + $qb->select('id', 'name', 'metadata') |
| 47 | + ->from('libresign_file') |
| 48 | + ->where($qb->expr()->isNotNull('metadata')); |
| 49 | + |
| 50 | + $cursor = $qb->executeQuery(); |
| 51 | + $filesToUpdate = []; |
| 52 | + |
| 53 | + while ($row = $cursor->fetch()) { |
| 54 | + $metadata = json_decode($row['metadata'], true); |
| 55 | + |
| 56 | + // Only process files that have an extension in metadata |
| 57 | + if (!isset($metadata['extension']) || empty($metadata['extension'])) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + $name = $row['name']; |
| 62 | + $extension = $metadata['extension']; |
| 63 | + |
| 64 | + // Remove the extension from the name |
| 65 | + $extensionPattern = '/\.' . preg_quote($extension, '/') . '$/i'; |
| 66 | + $newName = preg_replace($extensionPattern, '', $name); |
| 67 | + |
| 68 | + // Only update if the name actually changed |
| 69 | + if ($newName !== $name) { |
| 70 | + $filesToUpdate[] = [ |
| 71 | + 'id' => (int)$row['id'], |
| 72 | + 'newName' => $newName, |
| 73 | + ]; |
| 74 | + } |
| 75 | + } |
| 76 | + $cursor->closeCursor(); |
| 77 | + |
| 78 | + // Update all files with normalized names |
| 79 | + if (!empty($filesToUpdate)) { |
| 80 | + $updateQb = $this->connection->getQueryBuilder(); |
| 81 | + |
| 82 | + foreach ($filesToUpdate as $file) { |
| 83 | + $updateQb->update('libresign_file') |
| 84 | + ->set('name', $updateQb->createNamedParameter($file['newName'])) |
| 85 | + ->where($updateQb->expr()->eq('id', $updateQb->createNamedParameter($file['id'], \OCP\DB\Types::INTEGER))) |
| 86 | + ->executeStatement(); |
| 87 | + } |
| 88 | + |
| 89 | + $output->info('Normalized ' . count($filesToUpdate) . ' file names by removing extensions from database'); |
| 90 | + } else { |
| 91 | + $output->info('No file names needed normalization'); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments