aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Command
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Command')
-rw-r--r--src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php98
-rw-r--r--src/Wallabag/CoreBundle/Command/InstallCommand.php5
-rw-r--r--src/Wallabag/CoreBundle/Command/ShowUserCommand.php3
3 files changed, 103 insertions, 3 deletions
diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
new file mode 100644
index 00000000..45bd8c5f
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
@@ -0,0 +1,98 @@
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;
10use Wallabag\UserBundle\Entity\User;
11
12class 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('username', InputArgument::OPTIONAL, 'User to process entries');
24 }
25
26 protected function execute(InputInterface $input, OutputInterface $output)
27 {
28 $this->output = $output;
29
30 $username = (string) $input->getArgument('username');
31
32 if ($username) {
33 try {
34 $user = $this->getUser($username);
35 $this->generateHashedUrls($user);
36 } catch (NoResultException $e) {
37 $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
38
39 return 1;
40 }
41 } else {
42 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
43
44 $output->writeln(sprintf('Generating hashed urls for "%d" users', \count($users)));
45
46 foreach ($users as $user) {
47 $output->writeln(sprintf('Processing user: %s', $user->getUsername()));
48 $this->generateHashedUrls($user);
49 }
50 $output->writeln('Finished generated hashed urls');
51 }
52
53 return 0;
54 }
55
56 /**
57 * @param User $user
58 */
59 private function generateHashedUrls(User $user)
60 {
61 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
62 $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
63
64 $entries = $repo->findByUser($user->getId());
65
66 $i = 1;
67 foreach ($entries as $entry) {
68 $entry->setHashedUrl(hash('sha1', $entry->getUrl()));
69 $em->persist($entry);
70
71 if (0 === ($i % 20)) {
72 $em->flush();
73 }
74 ++$i;
75 }
76
77 $em->flush();
78
79 $this->output->writeln(sprintf('Generated hashed urls for user: %s', $user->getUserName()));
80 }
81
82 /**
83 * Fetches a user from its username.
84 *
85 * @param string $username
86 *
87 * @return \Wallabag\UserBundle\Entity\User
88 */
89 private function getUser($username)
90 {
91 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
92 }
93
94 private function getDoctrine()
95 {
96 return $this->getContainer()->get('doctrine');
97 }
98}
diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php
index 3c76545c..c58ae2b5 100644
--- a/src/Wallabag/CoreBundle/Command/InstallCommand.php
+++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php
@@ -94,8 +94,9 @@ class InstallCommand extends ContainerAwareCommand
94 $status = '<info>OK!</info>'; 94 $status = '<info>OK!</info>';
95 $help = ''; 95 $help = '';
96 96
97 $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection();
98
97 try { 99 try {
98 $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection();
99 $conn->connect(); 100 $conn->connect();
100 } catch (\Exception $e) { 101 } catch (\Exception $e) {
101 if (false === strpos($e->getMessage(), 'Unknown database') 102 if (false === strpos($e->getMessage(), 'Unknown database')
@@ -253,7 +254,7 @@ class InstallCommand extends ContainerAwareCommand
253 $question->setHidden(true); 254 $question->setHidden(true);
254 $user->setPlainPassword($this->io->askQuestion($question)); 255 $user->setPlainPassword($this->io->askQuestion($question));
255 256
256 $user->setEmail($this->io->ask('Email', '')); 257 $user->setEmail($this->io->ask('Email', 'wallabag@wallabag.io'));
257 258
258 $user->setEnabled(true); 259 $user->setEnabled(true);
259 $user->addRole('ROLE_SUPER_ADMIN'); 260 $user->addRole('ROLE_SUPER_ADMIN');
diff --git a/src/Wallabag/CoreBundle/Command/ShowUserCommand.php b/src/Wallabag/CoreBundle/Command/ShowUserCommand.php
index a0184267..c95efbf3 100644
--- a/src/Wallabag/CoreBundle/Command/ShowUserCommand.php
+++ b/src/Wallabag/CoreBundle/Command/ShowUserCommand.php
@@ -57,7 +57,8 @@ class ShowUserCommand extends ContainerAwareCommand
57 sprintf('Display name: %s', $user->getName()), 57 sprintf('Display name: %s', $user->getName()),
58 sprintf('Creation date: %s', $user->getCreatedAt()->format('Y-m-d H:i:s')), 58 sprintf('Creation date: %s', $user->getCreatedAt()->format('Y-m-d H:i:s')),
59 sprintf('Last login: %s', null !== $user->getLastLogin() ? $user->getLastLogin()->format('Y-m-d H:i:s') : 'never'), 59 sprintf('Last login: %s', null !== $user->getLastLogin() ? $user->getLastLogin()->format('Y-m-d H:i:s') : 'never'),
60 sprintf('2FA activated: %s', $user->isTwoFactorAuthentication() ? 'yes' : 'no'), 60 sprintf('2FA (email) activated: %s', $user->isEmailTwoFactor() ? 'yes' : 'no'),
61 sprintf('2FA (OTP) activated: %s', $user->isGoogleAuthenticatorEnabled() ? 'yes' : 'no'),
61 ]); 62 ]);
62 } 63 }
63 64