diff --git a/appinfo/info.xml b/appinfo/info.xml index 297db3ef12..9430c81a81 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -47,6 +47,7 @@ +t OCA\Deck\Command\ArchiveOldCards OCA\Deck\Command\UserExport OCA\Deck\Command\BoardImport OCA\Deck\Command\TransferOwnership diff --git a/lib/Command/ArchiveOldCards.php b/lib/Command/ArchiveOldCards.php new file mode 100644 index 0000000000..2c21b0173b --- /dev/null +++ b/lib/Command/ArchiveOldCards.php @@ -0,0 +1,91 @@ +db = $db; + $this->cardMapper = $cardMapper; + } + + protected function configure(): void { + $this->setName('deck:cards:archive-old') + ->setDescription('Archive Deck cards older than a specified number of days') + ->addOption( + 'days', + 'd', + InputOption::VALUE_REQUIRED, + 'Number of days of inactivity before archiving', + 60 + ) + ->addOption( + 'dry-run', + null, + InputOption::VALUE_NONE, + 'List cards that would be archived without modifying them' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $isDryRun = (bool)$input->getOption('dry-run'); + $days = (int)$input->getOption('days'); + + if ($days <= 0) { + $output->writeln('The --days parameter must be a positive integer.'); + return Command::INVALID; + } + + $cutoffTimestamp = (new DateTime("-{$days} days"))->getTimestamp(); + + $output->writeln("Searching for active cards unchanged for at least {$days} days (before " . date('Y-m-d H:i:s', $cutoffTimestamp) . ")..."); + + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from('deck_cards') + ->where($qb->expr()->eq('archived', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))) + ->andWhere($qb->expr()->lt('last_modified', $qb->createNamedParameter($cutoffTimestamp, IQueryBuilder::PARAM_INT))); + + $result = $qb->executeQuery(); + $count = 0; + + while ($row = $result->fetch()) { + $count++; + $cardTitle = $row['title'] ?? 'Untitled'; + $cardId = $row['id']; + + if ($isDryRun) { + $output->writeln("[Dry Run] Would archive card ID {$cardId}: {$cardTitle}"); + continue; + } + + /** @var Card $card */ + $card = $this->cardMapper->createEntity($row); + $card->setArchived(true); + $card->setLastModified(time()); + + $this->cardMapper->update($card); + $output->writeln("Archived card ID {$cardId}: {$cardTitle}"); + } + + $result->closeCursor(); + $output->writeln("Finished. Total cards processed: {$count}"); + + return Command::SUCCESS; + } +}