From 625acf335298186b4ff983f9321900d1238e854b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sat, 24 Oct 2015 15:11:06 +0200 Subject: Add a command to automatically tag all entries for a user --- src/Wallabag/CoreBundle/Command/TagAllCommand.php | 66 +++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/Wallabag/CoreBundle/Command/TagAllCommand.php (limited to 'src/Wallabag/CoreBundle/Command/TagAllCommand.php') diff --git a/src/Wallabag/CoreBundle/Command/TagAllCommand.php b/src/Wallabag/CoreBundle/Command/TagAllCommand.php new file mode 100644 index 00000000..2cf3f808 --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/TagAllCommand.php @@ -0,0 +1,66 @@ +setName('wallabag:tag:all') + ->setDescription('Tag all entries using the tagging rules.') + ->addArgument( + 'username', + InputArgument::REQUIRED, + 'User to tag entries for.' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + try { + $user = $this->getUser($input->getArgument('username')); + } catch (NoResultException $e) { + $output->writeln(sprintf('User %s not found.', $input->getArgument('username'))); + + return 1; + } + $tagger = $this->getContainer()->get('wallabag_core.rule_based_tagger'); + + $output->write(sprintf('Tagging entries for user « %s »... ', $user->getUserName())); + + $entries = $tagger->tagAllForUser($user); + + $em = $this->getDoctrine()->getManager(); + foreach ($entries as $entry) { + $em->persist($entry); + } + $em->flush(); + + $output->writeln('Done.'); + } + + /** + * Fetches a user from its username. + * + * @param string $username + * + * @return \Wallabag\UserBundle\Entity\User + */ + private function getUser($username) + { + return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username); + } + + private function getDoctrine() + { + return $this->getContainer()->get('doctrine'); + } +} -- cgit v1.2.3