]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Command/TagAllCommand.php
Merge pull request #2409 from wallabag/Quent-in-patch-1
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / TagAllCommand.php
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
11 class TagAllCommand extends ContainerAwareCommand
12 {
13 protected function configure()
14 {
15 $this
16 ->setName('wallabag:tag:all')
17 ->setDescription('Tag all entries using the tagging rules.')
18 ->addArgument(
19 'username',
20 InputArgument::REQUIRED,
21 'User to tag entries for.'
22 )
23 ;
24 }
25
26 protected function execute(InputInterface $input, OutputInterface $output)
27 {
28 try {
29 $user = $this->getUser($input->getArgument('username'));
30 } catch (NoResultException $e) {
31 $output->writeln(sprintf('<error>User "%s" not found.</error>', $input->getArgument('username')));
32
33 return 1;
34 }
35 $tagger = $this->getContainer()->get('wallabag_core.rule_based_tagger');
36
37 $output->write(sprintf('Tagging entries for user « <info>%s</info> »... ', $user->getUserName()));
38
39 $entries = $tagger->tagAllForUser($user);
40
41 $em = $this->getDoctrine()->getManager();
42 foreach ($entries as $entry) {
43 $em->persist($entry);
44 }
45 $em->flush();
46
47 $output->writeln('<info>Done.</info>');
48 }
49
50 /**
51 * Fetches a user from its username.
52 *
53 * @param string $username
54 *
55 * @return \Wallabag\UserBundle\Entity\User
56 */
57 private function getUser($username)
58 {
59 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
60 }
61
62 private function getDoctrine()
63 {
64 return $this->getContainer()->get('doctrine');
65 }
66 }