]>
Commit | Line | Data |
---|---|---|
511f1ce1 NH |
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'); | |
215409a8 | 44 | $entryIds = $entryRepository->findAllEntriesIdByUserId($userId); |
511f1ce1 | 45 | |
2a1ceb67 | 46 | $nbEntries = \count($entryIds); |
511f1ce1 NH |
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 | } |