aboutsummaryrefslogtreecommitdiffhomepage
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
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>
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php36
-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
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php55
-rw-r--r--tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php101
7 files changed, 306 insertions, 14 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 5c850091..26746f7d 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -29,6 +29,8 @@ class EntryRestController extends WallabagRestController
29 * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"}, 29 * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"},
30 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, 30 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
31 * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"} 31 * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"}
32 * {"name"="hashedurl", "dataType"="string", "required"=true, "format"="An url", "description"="Md5 url to check if it exists"},
33 * {"name"="hashedurls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Md5 urls (as an array) to check if it exists"}
32 * } 34 * }
33 * ) 35 * )
34 * 36 *
@@ -41,34 +43,46 @@ class EntryRestController extends WallabagRestController
41 $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); 43 $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id');
42 $urls = $request->query->get('urls', []); 44 $urls = $request->query->get('urls', []);
43 45
46 $hashedUrls = $request->query->get('hashedurls', []);
47
44 // handle multiple urls first 48 // handle multiple urls first
45 if (!empty($urls)) { 49 if (!empty($hashedUrls)) {
46 $results = []; 50 $results = [];
47 foreach ($urls as $url) { 51 foreach ($hashedUrls as $hashedUrl) {
48 $res = $this->getDoctrine() 52 $res = $this->getDoctrine()
49 ->getRepository('WallabagCoreBundle:Entry') 53 ->getRepository('WallabagCoreBundle:Entry')
50 ->findByUrlAndUserId($url, $this->getUser()->getId()); 54 ->findOneBy([
55 'hashedUrl' => $hashedUrl,
56 'user' => $this->getUser()->getId(),
57 ]);
51 58
52 $results[$url] = $this->returnExistInformation($res, $returnId); 59 // $results[$url] = $this->returnExistInformation($res, $returnId);
60 $results[$hashedUrl] = $this->returnExistInformation($res, $returnId);
53 } 61 }
54 62
55 return $this->sendResponse($results); 63 return $this->sendResponse($results);
56 } 64 }
57 65
58 // let's see if it is a simple url? 66 // let's see if it is a simple url?
59 $url = $request->query->get('url', ''); 67 $hashedUrl = $request->query->get('hashedurl', '');
68
69 // if (empty($url)) {
70 // throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId());
71 // }
60 72
61 if (empty($url)) { 73 if (empty($hashedUrl)) {
62 throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); 74 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
63 } 75 }
64 76
65 $res = $this->getDoctrine() 77 $res = $this->getDoctrine()
66 ->getRepository('WallabagCoreBundle:Entry') 78 ->getRepository('WallabagCoreBundle:Entry')
67 ->findByUrlAndUserId($url, $this->getUser()->getId()); 79 // ->findByUrlAndUserId($url, $this->getUser()->getId());
68 80 ->findOneBy([
69 $exists = $this->returnExistInformation($res, $returnId); 81 'hashedUrl' => $hashedUrl,
82 'user' => $this->getUser()->getId(),
83 ]);
70 84
71 return $this->sendResponse(['exists' => $exists]); 85 return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]);
72 } 86 }
73 87
74 /** 88 /**
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'])) {
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index 2151f587..8d96d7b8 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -987,6 +987,8 @@ class EntryRestControllerTest extends WallabagApiTestCase
987 { 987 {
988 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); 988 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
989 989
990 $this->client->request('GET', '/api/entries/exists?hashedurl=' . hash('md5', 'http://0.0.0.0/entry2'));
991
990 $this->assertSame(200, $this->client->getResponse()->getStatusCode()); 992 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
991 993
992 $content = json_decode($this->client->getResponse()->getContent(), true); 994 $content = json_decode($this->client->getResponse()->getContent(), true);
@@ -994,10 +996,22 @@ class EntryRestControllerTest extends WallabagApiTestCase
994 $this->assertTrue($content['exists']); 996 $this->assertTrue($content['exists']);
995 } 997 }
996 998
999 public function testGetEntriesExistsWithHash()
1000 {
1001 $this->client->request('GET', '/api/entries/exists?hashedurl=' . hash('md5', 'http://0.0.0.0/entry2'));
1002
1003 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1004
1005 $content = json_decode($this->client->getResponse()->getContent(), true);
1006
1007 $this->assertSame(2, $content['exists']);
1008 }
1009
997 public function testGetEntriesExistsWithManyUrls() 1010 public function testGetEntriesExistsWithManyUrls()
998 { 1011 {
999 $url1 = 'http://0.0.0.0/entry2'; 1012 $url1 = 'http://0.0.0.0/entry2';
1000 $url2 = 'http://0.0.0.0/entry10'; 1013 $url2 = 'http://0.0.0.0/entry10';
1014
1001 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1'); 1015 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
1002 1016
1003 $this->assertSame(200, $this->client->getResponse()->getStatusCode()); 1017 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
@@ -1027,9 +1041,46 @@ class EntryRestControllerTest extends WallabagApiTestCase
1027 $this->assertFalse($content[$url2]); 1041 $this->assertFalse($content[$url2]);
1028 } 1042 }
1029 1043
1044 public function testGetEntriesExistsWithManyUrlsHashed()
1045 {
1046 $url1 = 'http://0.0.0.0/entry2';
1047 $url2 = 'http://0.0.0.0/entry10';
1048 $this->client->request('GET', '/api/entries/exists?hashedurls[]='.hash('md5',$url1).'&hashedurls[]='.hash('md5',$url2) . '&return_id=1');
1049
1050 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1051
1052 $content = json_decode($this->client->getResponse()->getContent(), true);
1053
1054 $this->assertArrayHasKey($url1, $content);
1055 $this->assertArrayHasKey($url2, $content);
1056 $this->assertSame(2, $content[$url1]);
1057 $this->assertNull($content[$url2]);
1058
1059 $this->assertArrayHasKey(hash('md5', $url1), $content);
1060 $this->assertArrayHasKey(hash('md5', $url2), $content);
1061 $this->assertEquals(2, $content[hash('md5', $url1)]);
1062 $this->assertEquals(false, $content[hash('md5', $url2)]);
1063 }
1064
1065 public function testGetEntriesExistsWithManyUrlsHashedReturnBool()
1066 {
1067 $url1 = 'http://0.0.0.0/entry2';
1068 $url2 = 'http://0.0.0.0/entry10';
1069 $this->client->request('GET', '/api/entries/exists?hashedurls[]='.hash('md5',$url1).'&hashedurls[]='.hash('md5',$url2));
1070
1071 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1072
1073 $content = json_decode($this->client->getResponse()->getContent(), true);
1074
1075 $this->assertArrayHasKey($url1, $content);
1076 $this->assertArrayHasKey($url2, $content);
1077 $this->assertTrue($content[$url1]);
1078 $this->assertFalse($content[$url2]);
1079 }
1080
1030 public function testGetEntriesExistsWhichDoesNotExists() 1081 public function testGetEntriesExistsWhichDoesNotExists()
1031 { 1082 {
1032 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); 1083 $this->client->request('GET', '/api/entries/exists?hashedurl='.hash('md5','http://google.com/entry2'));
1033 1084
1034 $this->assertSame(200, $this->client->getResponse()->getStatusCode()); 1085 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1035 1086
@@ -1040,7 +1091,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
1040 1091
1041 public function testGetEntriesExistsWithNoUrl() 1092 public function testGetEntriesExistsWithNoUrl()
1042 { 1093 {
1043 $this->client->request('GET', '/api/entries/exists?url='); 1094 $this->client->request('GET', '/api/entries/exists?hashedurl=');
1044 1095
1045 $this->assertSame(403, $this->client->getResponse()->getStatusCode()); 1096 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
1046 } 1097 }
diff --git a/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php b/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php
new file mode 100644
index 00000000..8ca772cb
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php
@@ -0,0 +1,101 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Console\Application;
6use Symfony\Component\Console\Tester\CommandTester;
7use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
8use Wallabag\CoreBundle\Command\GenerateUrlHashesCommand;
9use Wallabag\CoreBundle\Entity\Entry;
10
11class GenerateUrlHashesCommandTest extends WallabagCoreTestCase
12{
13 public function testRunGenerateUrlHashesCommand()
14 {
15 $application = new Application($this->getClient()->getKernel());
16 $application->add(new GenerateUrlHashesCommand());
17
18 $command = $application->find('wallabag:generate-hashed-urls');
19
20 $tester = new CommandTester($command);
21 $tester->execute([
22 'command' => $command->getName(),
23 ]);
24
25 $this->assertContains('Generating hashed urls for the 3 user account entries', $tester->getDisplay());
26 $this->assertContains('Finished generated hashed urls', $tester->getDisplay());
27 }
28
29 public function testRunGenerateUrlHashesCommandWithBadUsername()
30 {
31 $application = new Application($this->getClient()->getKernel());
32 $application->add(new GenerateUrlHashesCommand());
33
34 $command = $application->find('wallabag:generate-hashed-urls');
35
36 $tester = new CommandTester($command);
37 $tester->execute([
38 'command' => $command->getName(),
39 'username' => 'unknown',
40 ]);
41
42 $this->assertContains('User "unknown" not found', $tester->getDisplay());
43 }
44
45 public function testRunGenerateUrlHashesCommandForUser()
46 {
47 $application = new Application($this->getClient()->getKernel());
48 $application->add(new GenerateUrlHashesCommand());
49
50 $command = $application->find('wallabag:generate-hashed-urls');
51
52 $tester = new CommandTester($command);
53 $tester->execute([
54 'command' => $command->getName(),
55 'username' => 'admin',
56 ]);
57
58 $this->assertContains('Generated hashed urls for user admin', $tester->getDisplay());
59 }
60
61 public function testGenerateUrls()
62 {
63 $url = 'http://www.lemonde.fr/sport/visuel/2017/05/05/rondelle-prison-blanchissage-comprendre-le-hockey-sur-glace_5122587_3242.html';
64 $client = $this->getClient();
65 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
66
67 $this->logInAs('admin');
68
69 $user = $em->getRepository('WallabagUserBundle:User')->findOneById($this->getLoggedInUserId());
70
71 $entry1 = new Entry($user);
72 $entry1->setUrl($url);
73
74 $em->persist($entry1);
75
76 $em->flush();
77
78 $this->assertNull($entry1->getHashedUrl());
79
80 $application = new Application($this->getClient()->getKernel());
81 $application->add(new GenerateUrlHashesCommand());
82
83 $command = $application->find('wallabag:generate-hashed-urls');
84
85 $tester = new CommandTester($command);
86 $tester->execute([
87 'command' => $command->getName(),
88 'username' => 'admin',
89 ]);
90
91 $this->assertContains('Generated hashed urls for user admin', $tester->getDisplay());
92
93 $entry = $em->getRepository('WallabagCoreBundle:Entry')->findOneByUrl($url);
94
95 $this->assertEquals($entry->getHashedUrl(), hash('sha512', $url));
96
97 $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url');
98 $query->setParameter('url', $url);
99 $query->execute();
100 }
101}