3 namespace Wallabag\CoreBundle\Command
;
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
;
12 class GenerateUrlHashesCommand
extends ContainerAwareCommand
14 /** @var OutputInterface */
17 protected function configure()
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')
25 InputArgument
::OPTIONAL
,
26 'User to process entries'
30 protected function execute(InputInterface
$input, OutputInterface
$output)
32 $this->output
= $output;
34 $username = $input->getArgument('username');
38 $user = $this->getUser($username);
39 $this->generateHashedUrls($user);
40 } catch (NoResultException
$e) {
41 $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
46 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
48 $output->writeln(sprintf('Generating hashed urls for "%d" users', \
count($users)));
50 foreach ($users as $user) {
51 $output->writeln(sprintf('Processing user: %s', $user->getUsername()));
52 $this->generateHashedUrls($user);
54 $output->writeln('Finished generated hashed urls');
63 private function generateHashedUrls(User
$user)
65 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
66 $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
68 $entries = $repo->findByUser($user->getId());
71 foreach ($entries as $entry) {
72 $entry->setHashedUrl(hash('sha1', $entry->getUrl()));
75 if (0 === ($i %
20)) {
83 $this->output
->writeln(sprintf('Generated hashed urls for user: %s', $user->getUserName()));
87 * Fetches a user from its username.
89 * @param string $username
91 * @return \Wallabag\UserBundle\Entity\User
93 private function getUser($username)
95 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
98 private function getDoctrine()
100 return $this->getContainer()->get('doctrine');