diff options
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r-- | src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php | 98 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Entity/Entry.php | 31 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 24 |
3 files changed, 152 insertions, 1 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 | |||
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('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/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index b3cfdc4a..c3fb87d2 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php | |||
@@ -25,7 +25,8 @@ use Wallabag\UserBundle\Entity\User; | |||
25 | * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"}, | 25 | * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"}, |
26 | * indexes={ | 26 | * indexes={ |
27 | * @ORM\Index(name="created_at", columns={"created_at"}), | 27 | * @ORM\Index(name="created_at", columns={"created_at"}), |
28 | * @ORM\Index(name="uid", columns={"uid"}) | 28 | * @ORM\Index(name="uid", columns={"uid"}), |
29 | * @ORM\Index(name="hashed_url_user_id", columns={"user_id", "hashed_url"}, options={"lengths"={null, 40}}) | ||
29 | * } | 30 | * } |
30 | * ) | 31 | * ) |
31 | * @ORM\HasLifecycleCallbacks() | 32 | * @ORM\HasLifecycleCallbacks() |
@@ -76,6 +77,13 @@ class Entry | |||
76 | private $url; | 77 | private $url; |
77 | 78 | ||
78 | /** | 79 | /** |
80 | * @var string | ||
81 | * | ||
82 | * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true) | ||
83 | */ | ||
84 | private $hashedUrl; | ||
85 | |||
86 | /** | ||
79 | * @var bool | 87 | * @var bool |
80 | * | 88 | * |
81 | * @Exclude | 89 | * @Exclude |
@@ -316,6 +324,7 @@ class Entry | |||
316 | public function setUrl($url) | 324 | public function setUrl($url) |
317 | { | 325 | { |
318 | $this->url = $url; | 326 | $this->url = $url; |
327 | $this->hashedUrl = hash('sha1', $url); | ||
319 | 328 | ||
320 | return $this; | 329 | return $this; |
321 | } | 330 | } |
@@ -911,4 +920,24 @@ class Entry | |||
911 | { | 920 | { |
912 | return $this->originUrl; | 921 | return $this->originUrl; |
913 | } | 922 | } |
923 | |||
924 | /** | ||
925 | * @return string | ||
926 | */ | ||
927 | public function getHashedUrl() | ||
928 | { | ||
929 | return $this->hashedUrl; | ||
930 | } | ||
931 | |||
932 | /** | ||
933 | * @param mixed $hashedUrl | ||
934 | * | ||
935 | * @return Entry | ||
936 | */ | ||
937 | public function setHashedUrl($hashedUrl) | ||
938 | { | ||
939 | $this->hashedUrl = $hashedUrl; | ||
940 | |||
941 | return $this; | ||
942 | } | ||
914 | } | 943 | } |
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 45366623..f5089729 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -347,6 +347,30 @@ class EntryRepository extends EntityRepository | |||
347 | } | 347 | } |
348 | 348 | ||
349 | /** | 349 | /** |
350 | * Find an entry by its hashed url and its owner. | ||
351 | * If it exists, return the entry otherwise return false. | ||
352 | * | ||
353 | * @param string $hashedUrl Url hashed using sha1 | ||
354 | * @param int $userId | ||
355 | * | ||
356 | * @return Entry|bool | ||
357 | */ | ||
358 | public function findByHashedUrlAndUserId($hashedUrl, $userId) | ||
359 | { | ||
360 | $res = $this->createQueryBuilder('e') | ||
361 | ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) | ||
362 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | ||
363 | ->getQuery() | ||
364 | ->getResult(); | ||
365 | |||
366 | if (\count($res)) { | ||
367 | return current($res); | ||
368 | } | ||
369 | |||
370 | return false; | ||
371 | } | ||
372 | |||
373 | /** | ||
350 | * Count all entries for a user. | 374 | * Count all entries for a user. |
351 | * | 375 | * |
352 | * @param int $userId | 376 | * @param int $userId |