From d5744bf0dfdbee4dbbe380d8a076d07b89fc76e6 Mon Sep 17 00:00:00 2001 From: Olivier Mehani Date: Fri, 3 May 2019 22:23:04 +1000 Subject: Delegate findByUrlAndUserId to findByHashedUrlAndUserId Signed-off-by: Olivier Mehani --- src/Wallabag/CoreBundle/Repository/EntryRepository.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 3990932e..960b682d 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -348,17 +348,9 @@ class EntryRepository extends EntityRepository */ public function findByUrlAndUserId($url, $userId) { - $res = $this->createQueryBuilder('e') - ->where('e.url = :url')->setParameter('url', urldecode($url)) - ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) - ->getQuery() - ->getResult(); - - if (\count($res)) { - return current($res); - } - - return false; + return $this->findByHashedUrlAndUserId( + hash('sha1', $url), // XXX: the hash logic would better be in a separate util to avoid duplication with GenerateUrlHashesCommand::generateHashedUrls + $userId); } /** -- cgit v1.2.3 From 4a5516376bf4c8b0cdc1e81d24ce1cca68425785 Mon Sep 17 00:00:00 2001 From: Olivier Mehani Date: Fri, 10 May 2019 22:07:55 +1000 Subject: Add Wallabag\CoreBundle\Helper\UrlHasher Signed-off-by: Olivier Mehani --- .../Command/GenerateUrlHashesCommand.php | 5 +++- src/Wallabag/CoreBundle/Entity/Entry.php | 3 ++- src/Wallabag/CoreBundle/Helper/UrlHasher.php | 22 ++++++++++++++++ .../CoreBundle/Repository/EntryRepository.php | 29 +++++++++++++++++++++- 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Helper/UrlHasher.php (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php index 45bd8c5f..775b0413 100644 --- a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php +++ b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php @@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Wallabag\CoreBundle\Helper\UrlHasher; use Wallabag\UserBundle\Entity\User; class GenerateUrlHashesCommand extends ContainerAwareCommand @@ -65,7 +66,9 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand $i = 1; foreach ($entries as $entry) { - $entry->setHashedUrl(hash('sha1', $entry->getUrl())); + $entry->setHashedUrl( + UrlHasher::hashUrl($entry->getUrl()) + ); $em->persist($entry); if (0 === ($i % 20)) { diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index c3fb87d2..1b4367fd 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -13,6 +13,7 @@ use JMS\Serializer\Annotation\XmlRoot; use Symfony\Component\Validator\Constraints as Assert; use Wallabag\AnnotationBundle\Entity\Annotation; use Wallabag\CoreBundle\Helper\EntityTimestampsTrait; +use Wallabag\CoreBundle\Helper\UrlHasher; use Wallabag\UserBundle\Entity\User; /** @@ -324,7 +325,7 @@ class Entry public function setUrl($url) { $this->url = $url; - $this->hashedUrl = hash('sha1', $url); + $this->hashedUrl = UrlHasher::hashUrl($url); return $this; } diff --git a/src/Wallabag/CoreBundle/Helper/UrlHasher.php b/src/Wallabag/CoreBundle/Helper/UrlHasher.php new file mode 100644 index 00000000..e44f219a --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/UrlHasher.php @@ -0,0 +1,22 @@ +findByHashedUrlAndUserId( - hash('sha1', $url), // XXX: the hash logic would better be in a separate util to avoid duplication with GenerateUrlHashesCommand::generateHashedUrls + UrlHasher::hashUrl($url), $userId); } @@ -506,6 +507,32 @@ class EntryRepository extends EntityRepository return $this->find($randomId); } + /** + * Inject a UrlHasher. + * + * @param UrlHasher $hasher + */ + public function setUrlHasher(UrlHasher $hasher) + { + $this->urlHasher = $hasher; + } + + /** + * Get the UrlHasher, or create a default one if not injected. + * + * XXX: the default uses the default hash algorithm + * + * @return UrlHasher + */ + protected function getUrlHasher() + { + if (!isset($this->urlHasher)) { + $this->setUrlHasher(new UrlHasher()); + } + + return $this->urlHasher; + } + /** * Return a query builder to be used by other getBuilderFor* method. * -- cgit v1.2.3 From 0132ccd2a2e73a831fa198940c369bcdd5249e8b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 24 May 2019 15:15:12 +0200 Subject: Change the way to define algorithm for hashing url --- .../CoreBundle/Command/GenerateUrlHashesCommand.php | 6 ++---- src/Wallabag/CoreBundle/Helper/UrlHasher.php | 13 +++++++------ src/Wallabag/CoreBundle/Repository/EntryRepository.php | 3 ++- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php index 775b0413..8f2bff11 100644 --- a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php +++ b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php @@ -66,9 +66,7 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand $i = 1; foreach ($entries as $entry) { - $entry->setHashedUrl( - UrlHasher::hashUrl($entry->getUrl()) - ); + $entry->setHashedUrl(UrlHasher::hashUrl($entry->getUrl())); $em->persist($entry); if (0 === ($i % 20)) { @@ -87,7 +85,7 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand * * @param string $username * - * @return \Wallabag\UserBundle\Entity\User + * @return User */ private function getUser($username) { diff --git a/src/Wallabag/CoreBundle/Helper/UrlHasher.php b/src/Wallabag/CoreBundle/Helper/UrlHasher.php index e44f219a..d123eaba 100644 --- a/src/Wallabag/CoreBundle/Helper/UrlHasher.php +++ b/src/Wallabag/CoreBundle/Helper/UrlHasher.php @@ -7,16 +7,17 @@ namespace Wallabag\CoreBundle\Helper; */ class UrlHasher { - /** @var string */ - const ALGORITHM = 'sha1'; - /** + * Hash the given url using the given algorithm. + * Hashed url are faster to be retrieved in the database than the real url. + * * @param string $url + * @param string $algorithm * - * @return string hashed $url + * @return string */ - public static function hashUrl(string $url) + public static function hashUrl(string $url, $algorithm = 'sha1') { - return hash(static::ALGORITHM, $url); + return hash($algorithm, urldecode($url)); } } diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 37fc1000..7c4a05ed 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -351,7 +351,8 @@ class EntryRepository extends EntityRepository { return $this->findByHashedUrlAndUserId( UrlHasher::hashUrl($url), - $userId); + $userId + ); } /** -- cgit v1.2.3 From 629a3797bcef33943df8ef5631328e05d12634ed Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 24 May 2019 15:43:30 +0200 Subject: Remove useless methods Also fix a phpdoc block --- .../CoreBundle/Repository/EntryRepository.php | 26 ---------------------- 1 file changed, 26 deletions(-) (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 7c4a05ed..880e7c65 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -508,32 +508,6 @@ class EntryRepository extends EntityRepository return $this->find($randomId); } - /** - * Inject a UrlHasher. - * - * @param UrlHasher $hasher - */ - public function setUrlHasher(UrlHasher $hasher) - { - $this->urlHasher = $hasher; - } - - /** - * Get the UrlHasher, or create a default one if not injected. - * - * XXX: the default uses the default hash algorithm - * - * @return UrlHasher - */ - protected function getUrlHasher() - { - if (!isset($this->urlHasher)) { - $this->setUrlHasher(new UrlHasher()); - } - - return $this->urlHasher; - } - /** * Return a query builder to be used by other getBuilderFor* method. * -- cgit v1.2.3