aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2017-05-28 14:53:04 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2019-04-01 13:24:40 +0200
commitbfe02a0b481055bb4e799200c8daa9a0ad987c71 (patch)
treeb82431b5ca4b24de1ddab6f0407e1ae7cda54083 /src/Wallabag/CoreBundle
parent3620dae1e6b3fab5a4ba4001b4581ce7ed795996 (diff)
downloadwallabag-bfe02a0b481055bb4e799200c8daa9a0ad987c71.tar.gz
wallabag-bfe02a0b481055bb4e799200c8daa9a0ad987c71.tar.zst
wallabag-bfe02a0b481055bb4e799200c8daa9a0ad987c71.zip
Hash the urls to check if they exist
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r--src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php95
-rw-r--r--src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php1
-rw-r--r--src/Wallabag/CoreBundle/Entity/Entry.php30
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php2
4 files changed, 127 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..fe2644f2
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
@@ -0,0 +1,95 @@
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(
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
48 $output->writeln(sprintf('Generating hashed urls for the %d user account entries', count($users)));
49
50 foreach ($users as $user) {
51 $output->writeln(sprintf('Processing user %s', $user->getUsername()));
52 $this->generateHashedUrls($user);
53 }
54 $output->writeln(sprintf('Finished generated hashed urls'));
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
70 foreach ($entries as $entry) {
71 $entry->setHashedUrl(hash('sha512', $entry->getUrl()));
72 $em->persist($entry);
73 $em->flush();
74 }
75
76 $this->output->writeln(sprintf('Generated hashed urls for user %s', $user->getUserName()));
77 }
78
79 /**
80 * Fetches a user from its username.
81 *
82 * @param string $username
83 *
84 * @return \Wallabag\UserBundle\Entity\User
85 */
86 private function getUser($username)
87 {
88 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
89 }
90
91 private function getDoctrine()
92 {
93 return $this->getContainer()->get('doctrine');
94 }
95}
diff --git a/src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php b/src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php
index 024fcfdc..9c10500d 100644
--- a/src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php
+++ b/src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php
@@ -30,6 +30,7 @@ class EntryFixtures extends Fixture implements DependentFixtureInterface
30 'entry2' => [ 30 'entry2' => [
31 'user' => 'admin-user', 31 'user' => 'admin-user',
32 'url' => 'http://0.0.0.0/entry2', 32 'url' => 'http://0.0.0.0/entry2',
33 'hashed_url' => hash('md5', 'http://0.0.0.0/entry2'),
33 'reading_time' => 1, 34 'reading_time' => 1,
34 'domain' => 'domain.io', 35 'domain' => 'domain.io',
35 'mime' => 'text/html', 36 'mime' => 'text/html',
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php
index b3cfdc4a..17a1ed58 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="hashedurl", columns={"hashedurl"})
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="hashedurl", type="text", nullable=true)
83 */
84 private $hashedUrl;
85
86 /**
79 * @var bool 87 * @var bool
80 * 88 *
81 * @Exclude 89 * @Exclude
@@ -911,4 +919,24 @@ class Entry
911 { 919 {
912 return $this->originUrl; 920 return $this->originUrl;
913 } 921 }
922
923 /**
924 * @return string
925 */
926 public function getHashedUrl()
927 {
928 return $this->hashedUrl;
929 }
930
931 /**
932 * @param mixed $hashedUrl
933 *
934 * @return Entry
935 */
936 public function setHashedUrl($hashedUrl)
937 {
938 $this->hashedUrl = $hashedUrl;
939
940 return $this;
941 }
914} 942}
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index 31953f12..0534d27b 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -248,6 +248,8 @@ class ContentProxy
248 { 248 {
249 $this->updateOriginUrl($entry, $content['url']); 249 $this->updateOriginUrl($entry, $content['url']);
250 250
251 $entry->setHashedUrl(hash('md5', $entry->getUrl()));
252
251 $this->setEntryDomainName($entry); 253 $this->setEntryDomainName($entry);
252 254
253 if (!empty($content['title'])) { 255 if (!empty($content['title'])) {