]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
Update deps
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / GenerateUrlHashesCommand.php
CommitLineData
bfe02a0b
TC
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;
4a551637 10use Wallabag\CoreBundle\Helper\UrlHasher;
bfe02a0b
TC
11use Wallabag\UserBundle\Entity\User;
12
13class GenerateUrlHashesCommand extends ContainerAwareCommand
14{
15 /** @var OutputInterface */
16 protected $output;
17
18 protected function configure()
19 {
20 $this
21 ->setName('wallabag:generate-hashed-urls')
22 ->setDescription('Generates hashed urls for each entry')
23 ->setHelp('This command helps you to generates hashes of the url of each entry, to check through API if an URL is already saved')
c579ce23 24 ->addArgument('username', InputArgument::OPTIONAL, 'User to process entries');
bfe02a0b
TC
25 }
26
27 protected function execute(InputInterface $input, OutputInterface $output)
28 {
29 $this->output = $output;
30
c579ce23 31 $username = (string) $input->getArgument('username');
bfe02a0b
TC
32
33 if ($username) {
34 try {
35 $user = $this->getUser($username);
36 $this->generateHashedUrls($user);
37 } catch (NoResultException $e) {
38 $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
39
40 return 1;
41 }
42 } else {
43 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
44
9c2b2aae 45 $output->writeln(sprintf('Generating hashed urls for "%d" users', \count($users)));
bfe02a0b
TC
46
47 foreach ($users as $user) {
9c2b2aae 48 $output->writeln(sprintf('Processing user: %s', $user->getUsername()));
bfe02a0b
TC
49 $this->generateHashedUrls($user);
50 }
9c2b2aae 51 $output->writeln('Finished generated hashed urls');
bfe02a0b
TC
52 }
53
54 return 0;
55 }
56
bfe02a0b
TC
57 private function generateHashedUrls(User $user)
58 {
59 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
60 $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
61
62 $entries = $repo->findByUser($user->getId());
63
9c2b2aae 64 $i = 1;
bfe02a0b 65 foreach ($entries as $entry) {
0132ccd2 66 $entry->setHashedUrl(UrlHasher::hashUrl($entry->getUrl()));
bfe02a0b 67 $em->persist($entry);
9c2b2aae
JB
68
69 if (0 === ($i % 20)) {
70 $em->flush();
71 }
72 ++$i;
bfe02a0b
TC
73 }
74
9c2b2aae
JB
75 $em->flush();
76
77 $this->output->writeln(sprintf('Generated hashed urls for user: %s', $user->getUserName()));
bfe02a0b
TC
78 }
79
80 /**
81 * Fetches a user from its username.
82 *
83 * @param string $username
84 *
0132ccd2 85 * @return User
bfe02a0b
TC
86 */
87 private function getUser($username)
88 {
89 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
90 }
91
92 private function getDoctrine()
93 {
94 return $this->getContainer()->get('doctrine');
95 }
96}