aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Command
diff options
context:
space:
mode:
authorKévin Gomez <contact@kevingomez.fr>2015-10-24 15:11:06 +0200
committerKévin Gomez <contact@kevingomez.fr>2015-11-11 16:27:19 +0100
commit625acf335298186b4ff983f9321900d1238e854b (patch)
treebc3149d9cee320429475ced6a1cb8b53c71ad7eb /src/Wallabag/CoreBundle/Command
parentcad8cda7af06234a63b86253da1d813e7b0fd0f2 (diff)
downloadwallabag-625acf335298186b4ff983f9321900d1238e854b.tar.gz
wallabag-625acf335298186b4ff983f9321900d1238e854b.tar.zst
wallabag-625acf335298186b4ff983f9321900d1238e854b.zip
Add a command to automatically tag all entries for a user
Diffstat (limited to 'src/Wallabag/CoreBundle/Command')
-rw-r--r--src/Wallabag/CoreBundle/Command/TagAllCommand.php66
1 files changed, 66 insertions, 0 deletions
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Doctrine\ORM\NoResultException;
6use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
10
11class 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}