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