1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-08-06 17:41:01 +00:00

handle expiration time using a parameter

This commit is contained in:
Martin Chaine 2025-06-03 17:20:22 +02:00
parent 5c823c91a3
commit e08c10af5d
No known key found for this signature in database
GPG key ID: 2D04DFDC89D53FDE
5 changed files with 76 additions and 38 deletions

View file

@ -31,6 +31,7 @@ services:
$supportUrl: '@=service(''craue_config'').get(''wallabag_support_url'')' $supportUrl: '@=service(''craue_config'').get(''wallabag_support_url'')'
$fonts: '%wallabag.fonts%' $fonts: '%wallabag.fonts%'
$defaultIgnoreOriginInstanceRules: '%wallabag.default_ignore_origin_instance_rules%' $defaultIgnoreOriginInstanceRules: '%wallabag.default_ignore_origin_instance_rules%'
$entryDeletionExpirationDays: '%wallabag.entry_deletion_expiration_days%'
Wallabag\: Wallabag\:
resource: '../../src/*' resource: '../../src/*'
@ -254,6 +255,11 @@ services:
arguments: arguments:
$baseFolder: "%kernel.project_dir%/web/assets/images" $baseFolder: "%kernel.project_dir%/web/assets/images"
Wallabag\Helper\EntryDeletionExpirationConfig:
arguments:
$defaultExpirationDays: '%wallabag.entry_deletion_expiration_days%'
Wallabag\Command\InstallCommand: Wallabag\Command\InstallCommand:
arguments: arguments:
$databaseDriver: '%database_driver%' $databaseDriver: '%database_driver%'

View file

@ -32,6 +32,7 @@ parameters:
wallabag.action_mark_as_read: 1 wallabag.action_mark_as_read: 1
wallabag.list_mode: 0 wallabag.list_mode: 0
wallabag.display_thumbnails: 1 wallabag.display_thumbnails: 1
wallabag.entry_deletion_expiration_days: 90
wallabag.fetching_error_message_title: 'No title found' wallabag.fetching_error_message_title: 'No title found'
wallabag.fetching_error_message: | wallabag.fetching_error_message: |
wallabag can't retrieve contents for this article. Please <a href="https://doc.wallabag.org/en/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>. wallabag can't retrieve contents for this article. Please <a href="https://doc.wallabag.org/en/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.

View file

@ -8,6 +8,7 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use Wallabag\Helper\EntryDeletionExpirationConfig;
use Wallabag\Repository\EntryDeletionRepository; use Wallabag\Repository\EntryDeletionRepository;
class PurgeEntryDeletionsCommand extends Command class PurgeEntryDeletionsCommand extends Command
@ -18,6 +19,7 @@ class PurgeEntryDeletionsCommand extends Command
public function __construct( public function __construct(
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly EntryDeletionRepository $entryDeletionRepository, private readonly EntryDeletionRepository $entryDeletionRepository,
private readonly EntryDeletionExpirationConfig $expirationConfig,
) { ) {
parent::__construct(); parent::__construct();
} }
@ -25,13 +27,6 @@ class PurgeEntryDeletionsCommand extends Command
protected function configure() protected function configure()
{ {
$this $this
->addOption(
'older-than',
null,
InputOption::VALUE_REQUIRED,
'Purge records older than this date (format: YYYY-MM-DD)',
'-30 days'
)
->addOption( ->addOption(
'dry-run', 'dry-run',
null, null,
@ -44,18 +39,10 @@ class PurgeEntryDeletionsCommand extends Command
{ {
$io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
$olderThan = $input->getOption('older-than');
$dryRun = (bool) $input->getOption('dry-run'); $dryRun = (bool) $input->getOption('dry-run');
try { $cutoff = $this->expirationConfig->getCutoffDate();
$date = new \DateTime($olderThan); $count = $this->entryDeletionRepository->countAllBefore($cutoff);
} catch (\Exception $e) {
$io->error(sprintf('Invalid date format: %s.\nYou can use any format supported by PHP (e.g. YYYY-MM-DD).', $olderThan));
return 1;
}
$count = $this->entryDeletionRepository->countAllBefore($date);
if ($dryRun) { if ($dryRun) {
$io->text('Dry run mode <info>enabled</info> (no records will be deleted)'); $io->text('Dry run mode <info>enabled</info> (no records will be deleted)');
@ -78,14 +65,14 @@ class PurgeEntryDeletionsCommand extends Command
$confirmMessage = sprintf( $confirmMessage = sprintf(
'Are you sure you want to delete records older than %s? (count: %d)', 'Are you sure you want to delete records older than %s? (count: %d)',
$date->format('Y-m-d'), $cutoff->format('Y-m-d'),
$count, $count,
); );
if (!$io->confirm($confirmMessage)) { if (!$io->confirm($confirmMessage)) {
return 0; return 0;
} }
$this->entryDeletionRepository->deleteAllBefore($date); $this->entryDeletionRepository->deleteAllBefore($cutoff);
$io->success(sprintf('Successfully deleted %d records.', $count)); $io->success(sprintf('Successfully deleted %d records.', $count));

View file

@ -0,0 +1,33 @@
<?php
namespace Wallabag\Helper;
class EntryDeletionExpirationConfig
{
private int $expirationDays;
public function __construct(int $defaultExpirationDays)
{
$this->expirationDays = $defaultExpirationDays;
}
public function getCutoffDate(): \DateTime
{
return new \DateTime("-{$this->expirationDays} days");
}
public function getExpirationDays(): int
{
return $this->expirationDays;
}
/**
* Override the expiration days parameter.
* This is mostly useful for testing purposes and should not be used in other contexts.
*/
public function setExpirationDays(int $days): self
{
$this->expirationDays = $days;
return $this;
}
}

View file

@ -2,10 +2,13 @@
namespace Tests\Wallabag\Command; namespace Tests\Wallabag\Command;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\WallabagTestCase;
use Wallabag\Entity\EntryDeletion; use Wallabag\Entity\EntryDeletion;
use Wallabag\Helper\EntryDeletionExpirationConfig;
use Wallabag\Repository\EntryDeletionRepository;
/** /**
* Test the purge entry deletions command. * Test the purge entry deletions command.
@ -15,59 +18,67 @@ use Wallabag\Entity\EntryDeletion;
* - Admin user: 1 deletion from 1 day ago (entry_id: 1001) * - Admin user: 1 deletion from 1 day ago (entry_id: 1001)
* - Bob user: 1 deletion from 3 days ago (entry_id: 1003) * - Bob user: 1 deletion from 3 days ago (entry_id: 1003)
*/ */
class PurgeEntryDeletionsCommandTest extends WallabagTestCase class PurgeEntryDeletionsCommandTest extends KernelTestCase
{ {
private EntryDeletionExpirationConfig $expirationConfig;
private EntryDeletionRepository $entryDeletionRepository;
protected function setUp(): void
{
self::bootKernel();
$this->expirationConfig = self::getContainer()->get(EntryDeletionExpirationConfig::class);
$this->expirationConfig->setExpirationDays(2);
$em = self::getContainer()->get(EntityManagerInterface::class);
$this->entryDeletionRepository = $em->getRepository(EntryDeletion::class);
}
public function testRunPurgeEntryDeletionsCommandWithDryRun() public function testRunPurgeEntryDeletionsCommandWithDryRun()
{ {
$application = new Application($this->getTestClient()->getKernel()); $application = new Application(self::$kernel);
$command = $application->find('wallabag:purge-entry-deletions'); $command = $application->find('wallabag:purge-entry-deletions');
$dateStr = '-2 days';
$tester = new CommandTester($command); $tester = new CommandTester($command);
$tester->execute([ $tester->execute([
'--older-than' => $dateStr,
'--dry-run' => true, '--dry-run' => true,
]); ]);
$this->assertStringContainsString('Dry run mode enabled', $tester->getDisplay()); $this->assertStringContainsString('Dry run mode enabled', $tester->getDisplay());
$this->assertSame(0, $tester->getStatusCode()); $this->assertSame(0, $tester->getStatusCode());
$em = $this->getEntityManager(); $count = $this->entryDeletionRepository->countAllBefore($this->expirationConfig->getCutoffDate());
$count = $em->getRepository(EntryDeletion::class)->countAllBefore(new \DateTime($dateStr));
$this->assertSame(2, $count); $this->assertSame(2, $count);
} }
public function testRunPurgeEntryDeletionsCommand() public function testRunPurgeEntryDeletionsCommand()
{ {
$application = new Application($this->getTestClient()->getKernel()); $application = new Application(self::$kernel);
$command = $application->find('wallabag:purge-entry-deletions'); $command = $application->find('wallabag:purge-entry-deletions');
$dateStr = '-2 days';
$tester = new CommandTester($command); $tester = new CommandTester($command);
$tester->setInputs(['yes']); // confirm deletion $tester->setInputs(['yes']); // confirm deletion
$tester->execute(['--older-than' => $dateStr]); $tester->execute([]);
$this->assertStringContainsString('Successfully deleted 2 records', $tester->getDisplay()); $this->assertStringContainsString('Successfully deleted 2 records', $tester->getDisplay());
$this->assertSame(0, $tester->getStatusCode()); $this->assertSame(0, $tester->getStatusCode());
$em = $this->getEntityManager(); $count = $this->entryDeletionRepository->countAllBefore($this->expirationConfig->getCutoffDate());
$count = $em->getRepository(EntryDeletion::class)->countAllBefore(new \DateTime($dateStr));
$this->assertSame(0, $count); $this->assertSame(0, $count);
$count = $em->getRepository(EntryDeletion::class)->countAllBefore(new \DateTime('now')); $countAll = $this->entryDeletionRepository->countAllBefore(new \DateTime('now'));
$this->assertSame(1, $count); $this->assertSame(1, $countAll);
} }
public function testRunPurgeEntryDeletionsCommandWithNoRecords() public function testRunPurgeEntryDeletionsCommandWithNoRecords()
{ {
$application = new Application($this->getTestClient()->getKernel()); $this->expirationConfig->setExpirationDays(10);
$application = new Application(self::$kernel);
$command = $application->find('wallabag:purge-entry-deletions'); $command = $application->find('wallabag:purge-entry-deletions');
$tester = new CommandTester($command); $tester = new CommandTester($command);
$tester->execute([ $tester->execute([]);
'--older-than' => '-1 year',
]);
$this->assertStringContainsString('No entry deletion records found', $tester->getDisplay()); $this->assertStringContainsString('No entry deletion records found', $tester->getDisplay());
$this->assertSame(0, $tester->getStatusCode()); $this->assertSame(0, $tester->getStatusCode());