diff options
3 files changed, 222 insertions, 0 deletions
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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Command; | ||
4 | |||
5 | use Doctrine\ORM\NoResultException; | ||
6 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
7 | use Symfony\Component\Console\Input\InputArgument; | ||
8 | use Symfony\Component\Console\Input\InputInterface; | ||
9 | use Symfony\Component\Console\Output\OutputInterface; | ||
10 | use Symfony\Component\Console\Style\SymfonyStyle; | ||
11 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | ||
12 | |||
13 | class ReloadEntryCommand extends ContainerAwareCommand | ||
14 | { | ||
15 | protected function configure() | ||
16 | { | ||
17 | $this | ||
18 | ->setName('wallabag:entry:reload') | ||
19 | ->setDescription('Reload entries') | ||
20 | ->setHelp('This command reload entries') | ||
21 | ->addArgument('username', InputArgument::OPTIONAL, 'Reload entries only for the given user') | ||
22 | ; | ||
23 | } | ||
24 | |||
25 | protected function execute(InputInterface $input, OutputInterface $output) | ||
26 | { | ||
27 | $io = new SymfonyStyle($input, $output); | ||
28 | |||
29 | $userId = null; | ||
30 | if ($username = $input->getArgument('username')) { | ||
31 | try { | ||
32 | $userId = $this->getContainer() | ||
33 | ->get('wallabag_user.user_repository') | ||
34 | ->findOneByUserName($username) | ||
35 | ->getId(); | ||
36 | } catch (NoResultException $e) { | ||
37 | $io->error(sprintf('User "%s" not found.', $username)); | ||
38 | |||
39 | return 1; | ||
40 | } | ||
41 | } | ||
42 | |||
43 | $entryRepository = $this->getContainer()->get('wallabag_core.entry_repository'); | ||
44 | $entryIds = $entryRepository->getAllEntriesId($userId); | ||
45 | |||
46 | $nbEntries = count($entryIds); | ||
47 | if (!$nbEntries) { | ||
48 | $io->success('No entry to reload.'); | ||
49 | |||
50 | return 0; | ||
51 | } | ||
52 | |||
53 | $io->note( | ||
54 | sprintf( | ||
55 | "You're going to reload %s entries. Depending on the number of entry to reload, this could be a very long process.", | ||
56 | $nbEntries | ||
57 | ) | ||
58 | ); | ||
59 | |||
60 | if (!$io->confirm('Are you sure you want to proceed?')) { | ||
61 | return 0; | ||
62 | } | ||
63 | |||
64 | $progressBar = $io->createProgressBar($nbEntries); | ||
65 | |||
66 | $contentProxy = $this->getContainer()->get('wallabag_core.content_proxy'); | ||
67 | $em = $this->getContainer()->get('doctrine')->getManager(); | ||
68 | $dispatcher = $this->getContainer()->get('event_dispatcher'); | ||
69 | |||
70 | $progressBar->start(); | ||
71 | foreach ($entryIds as $entryId) { | ||
72 | $entry = $entryRepository->find($entryId); | ||
73 | |||
74 | $contentProxy->updateEntry($entry, $entry->getUrl()); | ||
75 | $em->persist($entry); | ||
76 | $em->flush(); | ||
77 | |||
78 | $dispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
79 | $progressBar->advance(); | ||
80 | |||
81 | $em->detach($entry); | ||
82 | } | ||
83 | $progressBar->finish(); | ||
84 | |||
85 | $io->newLine(2); | ||
86 | $io->success('Done.'); | ||
87 | |||
88 | return 0; | ||
89 | } | ||
90 | } | ||
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 | |||
@@ -365,6 +365,23 @@ class EntryRepository extends EntityRepository | |||
365 | } | 365 | } |
366 | 366 | ||
367 | /** | 367 | /** |
368 | * @param int $userId | ||
369 | * | ||
370 | * @return array | ||
371 | */ | ||
372 | public function getAllEntriesId($userId = null) | ||
373 | { | ||
374 | $qb = $this->createQueryBuilder('e') | ||
375 | ->select('e.id'); | ||
376 | |||
377 | if (null !== $userId) { | ||
378 | $qb->where('e.user = :userid')->setParameter(':userid', $userId); | ||
379 | } | ||
380 | |||
381 | return $qb->getQuery()->getArrayResult(); | ||
382 | } | ||
383 | |||
384 | /** | ||
368 | * Find all entries by url and owner. | 385 | * Find all entries by url and owner. |
369 | * | 386 | * |
370 | * @param $url | 387 | * @param $url |
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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Command; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
6 | use Symfony\Component\Console\Tester\CommandTester; | ||
7 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | ||
8 | use Wallabag\CoreBundle\Command\ReloadEntryCommand; | ||
9 | use Wallabag\CoreBundle\Entity\Entry; | ||
10 | |||
11 | class ReloadEntryCommandTest extends WallabagCoreTestCase | ||
12 | { | ||
13 | public $url = 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html'; | ||
14 | |||
15 | /** | ||
16 | * @var entry | ||
17 | */ | ||
18 | public $adminEntry; | ||
19 | |||
20 | /** | ||
21 | * @var Entry | ||
22 | */ | ||
23 | public $bobEntry; | ||
24 | |||
25 | public function setUp() | ||
26 | { | ||
27 | parent::setUp(); | ||
28 | |||
29 | $userRepository = $this->getClient()->getContainer()->get('wallabag_user.user_repository'); | ||
30 | |||
31 | $user = $userRepository->findOneByUserName('admin'); | ||
32 | $this->adminEntry = new Entry($user); | ||
33 | $this->adminEntry->setUrl($this->url); | ||
34 | $this->adminEntry->setTitle('title foo'); | ||
35 | $this->adminEntry->setContent(''); | ||
36 | $this->getEntityManager()->persist($this->adminEntry); | ||
37 | |||
38 | $user = $userRepository->findOneByUserName('bob'); | ||
39 | $this->bobEntry = new Entry($user); | ||
40 | $this->bobEntry->setUrl($this->url); | ||
41 | $this->bobEntry->setTitle('title foo'); | ||
42 | $this->bobEntry->setContent(''); | ||
43 | $this->getEntityManager()->persist($this->bobEntry); | ||
44 | |||
45 | $this->getEntityManager()->flush(); | ||
46 | } | ||
47 | |||
48 | public function testRunReloadEntryCommand() | ||
49 | { | ||
50 | $application = new Application($this->getClient()->getKernel()); | ||
51 | $application->add(new ReloadEntryCommand()); | ||
52 | |||
53 | $command = $application->find('wallabag:entry:reload'); | ||
54 | $tester = new CommandTester($command); | ||
55 | $tester->execute([ | ||
56 | 'command' => $command->getName(), | ||
57 | ], [ | ||
58 | 'interactive' => false, | ||
59 | ]); | ||
60 | |||
61 | $reloadedEntries = $this->getClient() | ||
62 | ->getContainer() | ||
63 | ->get('wallabag_core.entry_repository') | ||
64 | ->findById([$this->adminEntry->getId(), $this->bobEntry->getId()]); | ||
65 | |||
66 | foreach ($reloadedEntries as $reloadedEntry) { | ||
67 | $this->assertNotEmpty($reloadedEntry->getContent()); | ||
68 | } | ||
69 | |||
70 | $this->assertContains('Done', $tester->getDisplay()); | ||
71 | } | ||
72 | |||
73 | public function testRunReloadEntryWithUsernameCommand() | ||
74 | { | ||
75 | $application = new Application($this->getClient()->getKernel()); | ||
76 | $application->add(new ReloadEntryCommand()); | ||
77 | |||
78 | $command = $application->find('wallabag:entry:reload'); | ||
79 | $tester = new CommandTester($command); | ||
80 | $tester->execute([ | ||
81 | 'command' => $command->getName(), | ||
82 | 'username' => 'admin', | ||
83 | ], [ | ||
84 | 'interactive' => false, | ||
85 | ]); | ||
86 | |||
87 | $entryRepository = $this->getClient()->getContainer()->get('wallabag_core.entry_repository'); | ||
88 | |||
89 | $reloadedAdminEntry = $entryRepository->find($this->adminEntry->getId()); | ||
90 | $this->assertNotEmpty($reloadedAdminEntry->getContent()); | ||
91 | |||
92 | $reloadedBobEntry = $entryRepository->find($this->bobEntry->getId()); | ||
93 | $this->assertEmpty($reloadedBobEntry->getContent()); | ||
94 | |||
95 | $this->assertContains('Done', $tester->getDisplay()); | ||
96 | } | ||
97 | |||
98 | public function testRunReloadEntryWithoutEntryCommand() | ||
99 | { | ||
100 | $application = new Application($this->getClient()->getKernel()); | ||
101 | $application->add(new ReloadEntryCommand()); | ||
102 | |||
103 | $command = $application->find('wallabag:entry:reload'); | ||
104 | $tester = new CommandTester($command); | ||
105 | $tester->execute([ | ||
106 | 'command' => $command->getName(), | ||
107 | 'username' => 'empty', | ||
108 | ], [ | ||
109 | 'interactive' => false, | ||
110 | ]); | ||
111 | |||
112 | $this->assertContains('No entry to reload', $tester->getDisplay()); | ||
113 | $this->assertNotContains('Done', $tester->getDisplay()); | ||
114 | } | ||
115 | } | ||