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\CoreBundle\Helper\UrlHasher
;
11 use Wallabag\UserBundle\Entity\User
;
13 class GenerateUrlHashesCommand
extends ContainerAwareCommand
15 /** @var OutputInterface */
18 protected function configure()
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')
24 ->addArgument('username', InputArgument
::OPTIONAL
, 'User to process entries');
27 protected function execute(InputInterface
$input, OutputInterface
$output)
29 $this->output
= $output;
31 $username = (string) $input->getArgument('username');
35 $user = $this->getUser($username);
36 $this->generateHashedUrls($user);
37 } catch (NoResultException
$e) {
38 $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
43 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
45 $output->writeln(sprintf('Generating hashed urls for "%d" users', \
count($users)));
47 foreach ($users as $user) {
48 $output->writeln(sprintf('Processing user: %s', $user->getUsername()));
49 $this->generateHashedUrls($user);
51 $output->writeln('Finished generated hashed urls');
57 private function generateHashedUrls(User
$user)
59 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
60 $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
62 $entries = $repo->findByUser($user->getId());
65 foreach ($entries as $entry) {
66 $entry->setHashedUrl(UrlHasher
::hashUrl($entry->getUrl()));
69 if (0 === ($i %
20)) {
77 $this->output
->writeln(sprintf('Generated hashed urls for user: %s', $user->getUserName()));
81 * Fetches a user from its username.
83 * @param string $username
87 private function getUser($username)
89 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
92 private function getDoctrine()
94 return $this->getContainer()->get('doctrine');