From 511f1ce1e87e0f30a455ca6ed73e008bfd557f83 Mon Sep 17 00:00:00 2001 From: Nicolas Hart Date: Mon, 21 Aug 2017 10:36:56 +0200 Subject: Add reload entry command --- .../CoreBundle/Command/ReloadEntryCommand.php | 90 ++++++++++++++++ .../CoreBundle/Repository/EntryRepository.php | 17 +++ .../CoreBundle/Command/ReloadEntryCommandTest.php | 115 +++++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php create mode 100644 tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php diff --git a/src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php b/src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php new file mode 100644 index 00000000..83887224 --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php @@ -0,0 +1,90 @@ +setName('wallabag:entry:reload') + ->setDescription('Reload entries') + ->setHelp('This command reload entries') + ->addArgument('username', InputArgument::OPTIONAL, 'Reload entries only for the given user') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $userId = null; + if ($username = $input->getArgument('username')) { + try { + $userId = $this->getContainer() + ->get('wallabag_user.user_repository') + ->findOneByUserName($username) + ->getId(); + } catch (NoResultException $e) { + $io->error(sprintf('User "%s" not found.', $username)); + + return 1; + } + } + + $entryRepository = $this->getContainer()->get('wallabag_core.entry_repository'); + $entryIds = $entryRepository->getAllEntriesId($userId); + + $nbEntries = count($entryIds); + if (!$nbEntries) { + $io->success('No entry to reload.'); + + return 0; + } + + $io->note( + sprintf( + "You're going to reload %s entries. Depending on the number of entry to reload, this could be a very long process.", + $nbEntries + ) + ); + + if (!$io->confirm('Are you sure you want to proceed?')) { + return 0; + } + + $progressBar = $io->createProgressBar($nbEntries); + + $contentProxy = $this->getContainer()->get('wallabag_core.content_proxy'); + $em = $this->getContainer()->get('doctrine')->getManager(); + $dispatcher = $this->getContainer()->get('event_dispatcher'); + + $progressBar->start(); + foreach ($entryIds as $entryId) { + $entry = $entryRepository->find($entryId); + + $contentProxy->updateEntry($entry, $entry->getUrl()); + $em->persist($entry); + $em->flush(); + + $dispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + $progressBar->advance(); + + $em->detach($entry); + } + $progressBar->finish(); + + $io->newLine(2); + $io->success('Done.'); + + return 0; + } +} diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index d70d6ca6..9a30cd79 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -364,6 +364,23 @@ class EntryRepository extends EntityRepository return $qb->getQuery()->getArrayResult(); } + /** + * @param int $userId + * + * @return array + */ + public function getAllEntriesId($userId = null) + { + $qb = $this->createQueryBuilder('e') + ->select('e.id'); + + if (null !== $userId) { + $qb->where('e.user = :userid')->setParameter(':userid', $userId); + } + + return $qb->getQuery()->getArrayResult(); + } + /** * Find all entries by url and owner. * diff --git a/tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php b/tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php new file mode 100644 index 00000000..63c068b4 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php @@ -0,0 +1,115 @@ +getClient()->getContainer()->get('wallabag_user.user_repository'); + + $user = $userRepository->findOneByUserName('admin'); + $this->adminEntry = new Entry($user); + $this->adminEntry->setUrl($this->url); + $this->adminEntry->setTitle('title foo'); + $this->adminEntry->setContent(''); + $this->getEntityManager()->persist($this->adminEntry); + + $user = $userRepository->findOneByUserName('bob'); + $this->bobEntry = new Entry($user); + $this->bobEntry->setUrl($this->url); + $this->bobEntry->setTitle('title foo'); + $this->bobEntry->setContent(''); + $this->getEntityManager()->persist($this->bobEntry); + + $this->getEntityManager()->flush(); + } + + public function testRunReloadEntryCommand() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new ReloadEntryCommand()); + + $command = $application->find('wallabag:entry:reload'); + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + ], [ + 'interactive' => false, + ]); + + $reloadedEntries = $this->getClient() + ->getContainer() + ->get('wallabag_core.entry_repository') + ->findById([$this->adminEntry->getId(), $this->bobEntry->getId()]); + + foreach ($reloadedEntries as $reloadedEntry) { + $this->assertNotEmpty($reloadedEntry->getContent()); + } + + $this->assertContains('Done', $tester->getDisplay()); + } + + public function testRunReloadEntryWithUsernameCommand() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new ReloadEntryCommand()); + + $command = $application->find('wallabag:entry:reload'); + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'username' => 'admin', + ], [ + 'interactive' => false, + ]); + + $entryRepository = $this->getClient()->getContainer()->get('wallabag_core.entry_repository'); + + $reloadedAdminEntry = $entryRepository->find($this->adminEntry->getId()); + $this->assertNotEmpty($reloadedAdminEntry->getContent()); + + $reloadedBobEntry = $entryRepository->find($this->bobEntry->getId()); + $this->assertEmpty($reloadedBobEntry->getContent()); + + $this->assertContains('Done', $tester->getDisplay()); + } + + public function testRunReloadEntryWithoutEntryCommand() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new ReloadEntryCommand()); + + $command = $application->find('wallabag:entry:reload'); + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'username' => 'empty', + ], [ + 'interactive' => false, + ]); + + $this->assertContains('No entry to reload', $tester->getDisplay()); + $this->assertNotContains('Done', $tester->getDisplay()); + } +} -- cgit v1.2.3 From 215409a8b2ea3888fc197c8cd7beddda0a9f1403 Mon Sep 17 00:00:00 2001 From: Nicolas Hart Date: Mon, 21 Aug 2017 18:19:42 +0200 Subject: rename getAllEntriesId into findAllEntriesIdByUserId --- src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php | 2 +- src/Wallabag/CoreBundle/Repository/EntryRepository.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php b/src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php index 83887224..91998841 100644 --- a/src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php +++ b/src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php @@ -41,7 +41,7 @@ class ReloadEntryCommand extends ContainerAwareCommand } $entryRepository = $this->getContainer()->get('wallabag_core.entry_repository'); - $entryIds = $entryRepository->getAllEntriesId($userId); + $entryIds = $entryRepository->findAllEntriesIdByUserId($userId); $nbEntries = count($entryIds); if (!$nbEntries) { diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 9a30cd79..febc86d3 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -369,7 +369,7 @@ class EntryRepository extends EntityRepository * * @return array */ - public function getAllEntriesId($userId = null) + public function findAllEntriesIdByUserId($userId = null) { $qb = $this->createQueryBuilder('e') ->select('e.id'); -- cgit v1.2.3 From dbf1188c5b78b7190cbfce2db00e08d5e69029ff Mon Sep 17 00:00:00 2001 From: Nicolas Hart Date: Tue, 22 Aug 2017 10:42:54 +0200 Subject: rename getAllEntriesIdAndUrl into findAllEntriesIdAndUrlByUserId --- src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php | 2 +- src/Wallabag/CoreBundle/Repository/EntryRepository.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php b/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php index 1caaa391..b58909db 100644 --- a/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php +++ b/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php @@ -71,7 +71,7 @@ class CleanDuplicatesCommand extends ContainerAwareCommand $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $repo = $this->getContainer()->get('wallabag_core.entry_repository'); - $entries = $repo->getAllEntriesIdAndUrl($user->getId()); + $entries = $repo->findAllEntriesIdAndUrlByUserId($user->getId()); $duplicatesCount = 0; $urls = []; diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index febc86d3..eb5e3205 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -355,7 +355,7 @@ class EntryRepository extends EntityRepository * Get id and url from all entries * Used for the clean-duplicates command. */ - public function getAllEntriesIdAndUrl($userId) + public function findAllEntriesIdAndUrlByUserId($userId) { $qb = $this->createQueryBuilder('e') ->select('e.id, e.url') -- cgit v1.2.3