|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\ContactsInteraction\Migration; |
| 11 | + |
| 12 | +use OC\Migration\BackgroundRepair; |
| 13 | +use OCA\DAV\AppInfo\Application; |
| 14 | +use OCP\BackgroundJob\IJobList; |
| 15 | +use OCP\IDBConnection; |
| 16 | +use OCP\Migration\IOutput; |
| 17 | +use OCP\Migration\IRepairStep; |
| 18 | +use Sabre\VObject\ParseException; |
| 19 | +use Sabre\VObject\Reader; |
| 20 | + |
| 21 | +class FixVcardCategory implements IRepairStep { |
| 22 | + |
| 23 | + private const CARDS_PER_BATCH = 5000; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private readonly IDBConnection $connection, |
| 27 | + private readonly IJobList $jobList, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function getName(): string { |
| 32 | + return 'Fix category of recent contacts vcards'; |
| 33 | + } |
| 34 | + |
| 35 | + public function run(IOutput $output): void { |
| 36 | + $query = $this->connection->getQueryBuilder(); |
| 37 | + |
| 38 | + $cardsWithTranslatedCategory = $query->select(['id', 'card']) |
| 39 | + ->from('recent_contact') |
| 40 | + ->where($query->expr()->notLike( |
| 41 | + 'card', |
| 42 | + $query->createNamedParameter('%CATEGORIES:Recently contacted%') |
| 43 | + )) |
| 44 | + ->setMaxResults(self::CARDS_PER_BATCH) |
| 45 | + ->executeQuery(); |
| 46 | + $rowCount = $cardsWithTranslatedCategory->rowCount(); |
| 47 | + |
| 48 | + $output->startProgress($rowCount); |
| 49 | + |
| 50 | + $this->connection->beginTransaction(); |
| 51 | + |
| 52 | + $updateQuery = $query->update('recent_contact') |
| 53 | + ->set('card', $query->createParameter('card')) |
| 54 | + ->where($query->expr()->eq('id', $query->createParameter('id'))); |
| 55 | + |
| 56 | + while ($card = $cardsWithTranslatedCategory->fetch()) { |
| 57 | + $output->advance(1); |
| 58 | + |
| 59 | + try { |
| 60 | + $vcard = Reader::read($card['card']); |
| 61 | + } catch (ParseException $e) { |
| 62 | + $output->info('Could not parse vcard with id ' . $card['id']); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + $vcard->remove('CATEGORIES'); |
| 67 | + $vcard->add('CATEGORIES', 'Recently contacted'); |
| 68 | + |
| 69 | + $updateQuery->setParameter('id', $card['id']); |
| 70 | + $updateQuery->setParameter('card', $vcard->serialize()); |
| 71 | + $updateQuery->executeStatement(); |
| 72 | + } |
| 73 | + |
| 74 | + $this->connection->commit(); |
| 75 | + |
| 76 | + $cardsWithTranslatedCategory->closeCursor(); |
| 77 | + |
| 78 | + $output->finishProgress(); |
| 79 | + |
| 80 | + if ($rowCount === self::CARDS_PER_BATCH) { |
| 81 | + $this->jobList->add(BackgroundRepair::class, [ |
| 82 | + 'app' => Application::APP_ID, |
| 83 | + 'step' => FixVcardCategory::class |
| 84 | + ]); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments