aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2019-04-01 11:50:33 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2019-04-01 13:24:40 +0200
commit9c2b2aae70b06411336e6eb6ac43b3ebd30dc38c (patch)
treeb8b37a0f8b3edd0c4db1678815d906a7b6126f31 /src/Wallabag
parentbfe02a0b481055bb4e799200c8daa9a0ad987c71 (diff)
downloadwallabag-9c2b2aae70b06411336e6eb6ac43b3ebd30dc38c.tar.gz
wallabag-9c2b2aae70b06411336e6eb6ac43b3ebd30dc38c.tar.zst
wallabag-9c2b2aae70b06411336e6eb6ac43b3ebd30dc38c.zip
Keep url in exists endpoint
- Add migration - Use md5 instead of sha512 (we don't need security here, just a hash) - Update tests
Diffstat (limited to 'src/Wallabag')
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php55
-rw-r--r--src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php19
-rw-r--r--src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php2
-rw-r--r--src/Wallabag/CoreBundle/Entity/Entry.php4
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php24
5 files changed, 70 insertions, 34 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 26746f7d..0ecf1a0e 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -27,10 +27,10 @@ class EntryRestController extends WallabagRestController
27 * @ApiDoc( 27 * @ApiDoc(
28 * parameters={ 28 * parameters={
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"="DEPRECATED, use hashed_url instead"},
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"="DEPRECATED, use hashed_urls instead"},
32 * {"name"="hashedurl", "dataType"="string", "required"=true, "format"="An url", "description"="Md5 url to check if it exists"}, 32 * {"name"="hashed_url", "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"} 33 * {"name"="hashed_urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Md5 urls (as an array) to check if it exists"}
34 * } 34 * }
35 * ) 35 * )
36 * 36 *
@@ -39,22 +39,18 @@ class EntryRestController extends WallabagRestController
39 public function getEntriesExistsAction(Request $request) 39 public function getEntriesExistsAction(Request $request)
40 { 40 {
41 $this->validateAuthentication(); 41 $this->validateAuthentication();
42 $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
42 43
43 $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); 44 $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id');
44 $urls = $request->query->get('urls', []);
45 45
46 $hashedUrls = $request->query->get('hashedurls', []); 46 $urls = $request->query->get('urls', []);
47 $hashedUrls = $request->query->get('hashed_urls', []);
47 48
48 // handle multiple urls first 49 // handle multiple urls first
49 if (!empty($hashedUrls)) { 50 if (!empty($hashedUrls)) {
50 $results = []; 51 $results = [];
51 foreach ($hashedUrls as $hashedUrl) { 52 foreach ($hashedUrls as $hashedUrl) {
52 $res = $this->getDoctrine() 53 $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId());
53 ->getRepository('WallabagCoreBundle:Entry')
54 ->findOneBy([
55 'hashedUrl' => $hashedUrl,
56 'user' => $this->getUser()->getId(),
57 ]);
58 54
59 // $results[$url] = $this->returnExistInformation($res, $returnId); 55 // $results[$url] = $this->returnExistInformation($res, $returnId);
60 $results[$hashedUrl] = $this->returnExistInformation($res, $returnId); 56 $results[$hashedUrl] = $this->returnExistInformation($res, $returnId);
@@ -63,24 +59,33 @@ class EntryRestController extends WallabagRestController
63 return $this->sendResponse($results); 59 return $this->sendResponse($results);
64 } 60 }
65 61
62 // @deprecated, to be remove in 3.0
63 if (!empty($urls)) {
64 $results = [];
65 foreach ($urls as $url) {
66 $res = $repo->findByUrlAndUserId($url, $this->getUser()->getId());
67
68 $results[$url] = $this->returnExistInformation($res, $returnId);
69 }
70
71 return $this->sendResponse($results);
72 }
73
66 // let's see if it is a simple url? 74 // let's see if it is a simple url?
67 $hashedUrl = $request->query->get('hashedurl', ''); 75 $url = $request->query->get('url', '');
76 $hashedUrl = $request->query->get('hashed_url', '');
68 77
69 // if (empty($url)) { 78 if (empty($url) && empty($hashedUrl)) {
70 // throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); 79 throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId());
71 // } 80 }
72 81
73 if (empty($hashedUrl)) { 82 $method = 'findByUrlAndUserId';
74 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId()); 83 if (!empty($hashedUrl)) {
84 $method = 'findByHashedUrlAndUserId';
85 $url = $hashedUrl;
75 } 86 }
76 87
77 $res = $this->getDoctrine() 88 $res = $repo->$method($url, $this->getUser()->getId());
78 ->getRepository('WallabagCoreBundle:Entry')
79 // ->findByUrlAndUserId($url, $this->getUser()->getId());
80 ->findOneBy([
81 'hashedUrl' => $hashedUrl,
82 'user' => $this->getUser()->getId(),
83 ]);
84 89
85 return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]); 90 return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]);
86 } 91 }
diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
index fe2644f2..fb598390 100644
--- a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
+++ b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
@@ -45,13 +45,13 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
45 } else { 45 } else {
46 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll(); 46 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
47 47
48 $output->writeln(sprintf('Generating hashed urls for the %d user account entries', count($users))); 48 $output->writeln(sprintf('Generating hashed urls for "%d" users', \count($users)));
49 49
50 foreach ($users as $user) { 50 foreach ($users as $user) {
51 $output->writeln(sprintf('Processing user %s', $user->getUsername())); 51 $output->writeln(sprintf('Processing user: %s', $user->getUsername()));
52 $this->generateHashedUrls($user); 52 $this->generateHashedUrls($user);
53 } 53 }
54 $output->writeln(sprintf('Finished generated hashed urls')); 54 $output->writeln('Finished generated hashed urls');
55 } 55 }
56 56
57 return 0; 57 return 0;
@@ -67,13 +67,20 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
67 67
68 $entries = $repo->findByUser($user->getId()); 68 $entries = $repo->findByUser($user->getId());
69 69
70 $i = 1;
70 foreach ($entries as $entry) { 71 foreach ($entries as $entry) {
71 $entry->setHashedUrl(hash('sha512', $entry->getUrl())); 72 $entry->setHashedUrl(hash('md5', $entry->getUrl()));
72 $em->persist($entry); 73 $em->persist($entry);
73 $em->flush(); 74
75 if (0 === ($i % 20)) {
76 $em->flush();
77 }
78 ++$i;
74 } 79 }
75 80
76 $this->output->writeln(sprintf('Generated hashed urls for user %s', $user->getUserName())); 81 $em->flush();
82
83 $this->output->writeln(sprintf('Generated hashed urls for user: %s', $user->getUserName()));
77 } 84 }
78 85
79 /** 86 /**
diff --git a/src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php b/src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php
index 9c10500d..1b18cad6 100644
--- a/src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php
+++ b/src/Wallabag/CoreBundle/DataFixtures/EntryFixtures.php
@@ -30,7 +30,6 @@ 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'),
34 'reading_time' => 1, 33 'reading_time' => 1,
35 'domain' => 'domain.io', 34 'domain' => 'domain.io',
36 'mime' => 'text/html', 35 'mime' => 'text/html',
@@ -90,6 +89,7 @@ class EntryFixtures extends Fixture implements DependentFixtureInterface
90 foreach ($entries as $reference => $item) { 89 foreach ($entries as $reference => $item) {
91 $entry = new Entry($this->getReference($item['user'])); 90 $entry = new Entry($this->getReference($item['user']));
92 $entry->setUrl($item['url']); 91 $entry->setUrl($item['url']);
92 $entry->setHashedUrl(hash('md5', $item['url']));
93 $entry->setReadingTime($item['reading_time']); 93 $entry->setReadingTime($item['reading_time']);
94 $entry->setDomainName($item['domain']); 94 $entry->setDomainName($item['domain']);
95 $entry->setMimetype($item['mime']); 95 $entry->setMimetype($item['mime']);
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php
index 17a1ed58..a04f101f 100644
--- a/src/Wallabag/CoreBundle/Entity/Entry.php
+++ b/src/Wallabag/CoreBundle/Entity/Entry.php
@@ -26,7 +26,7 @@ use Wallabag\UserBundle\Entity\User;
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 * @ORM\Index(name="hashed_url", columns={"hashed_url"})
30 * } 30 * }
31 * ) 31 * )
32 * @ORM\HasLifecycleCallbacks() 32 * @ORM\HasLifecycleCallbacks()
@@ -79,7 +79,7 @@ class Entry
79 /** 79 /**
80 * @var string 80 * @var string
81 * 81 *
82 * @ORM\Column(name="hashedurl", type="text", nullable=true) 82 * @ORM\Column(name="hashed_url", type="string", length=32, nullable=true)
83 */ 83 */
84 private $hashedUrl; 84 private $hashedUrl;
85 85
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 45366623..0c175abb 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 $hashedUrl
354 * @param $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', urldecode($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