From 09ef25c3c3882db94b3941f3ba33ebc78b5dbe4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 22 Dec 2017 15:44:00 +0100 Subject: Added random feature --- .../CoreBundle/Repository/EntryRepository.php | 46 ++++++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) (limited to 'src/Wallabag/CoreBundle/Repository') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 8b6cf443..a26de0a8 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -110,8 +110,7 @@ class EntryRepository extends EntityRepository */ public function getBuilderForUntaggedByUser($userId) { - return $this - ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); + return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); } /** @@ -432,6 +431,46 @@ class EntryRepository extends EntityRepository ->getResult(); } + /** + * Returns a random entry, filtering by status. + * + * @param $userId + * @param string $status can be unread, archive or starred + * + * @throws \Doctrine\ORM\NoResultException + * @throws \Doctrine\ORM\NonUniqueResultException + * + * @return Entry + */ + public function getRandomEntry($userId, $status = '') + { + $max = $this->getEntityManager() + ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') + ->setParameter('userId', $userId) + ->getSingleScalarResult(); + + $qb = $this->createQueryBuilder('e') + ->where('e.user = :user_id')->setParameter('user_id', $userId); + + if ('unread' === $status) { + $qb->andWhere('e.isArchived = false'); + } + + if ('archive' === $status) { + $qb->andWhere('e.isArchived = true'); + } + + if ('starred' === $status) { + $qb->andWhere('e.isStarred = true'); + } + + return $qb->andWhere('e.id >= :rand') + ->setParameter('rand', rand(0, $max)) + ->setMaxResults(1) + ->getQuery() + ->getSingleResult(); + } + /** * Return a query builder to be used by other getBuilderFor* method. * @@ -470,7 +509,6 @@ class EntryRepository extends EntityRepository */ private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') { - return $qb - ->orderBy(sprintf('e.%s', $sortBy), $direction); + return $qb->orderBy(sprintf('e.%s', $sortBy), $direction); } } -- cgit v1.2.3 From f85d220c19872dd7e9199fd150060555584a2886 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 12 Oct 2018 15:01:19 +0200 Subject: Fix tests --- src/Wallabag/CoreBundle/Repository/EntryRepository.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/Wallabag/CoreBundle/Repository') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index a26de0a8..6107e19e 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -464,6 +464,11 @@ class EntryRepository extends EntityRepository $qb->andWhere('e.isStarred = true'); } + if ('untagged' === $status) { + $qb->leftJoin('e.tags', 't'); + $qb->andWhere('t.id is null'); + } + return $qb->andWhere('e.id >= :rand') ->setParameter('rand', rand(0, $max)) ->setMaxResults(1) -- cgit v1.2.3 From 062fad434ad883ffc6f0c3798f2a490688199f3c Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 12 Oct 2018 21:41:28 +0200 Subject: Better random function --- .../CoreBundle/Repository/EntryRepository.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'src/Wallabag/CoreBundle/Repository') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 6107e19e..05563079 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -440,17 +440,14 @@ class EntryRepository extends EntityRepository * @throws \Doctrine\ORM\NoResultException * @throws \Doctrine\ORM\NonUniqueResultException * + * @see https://github.com/doctrine/doctrine2/issues/5479#issuecomment-403862934 + * * @return Entry */ public function getRandomEntry($userId, $status = '') { - $max = $this->getEntityManager() - ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') - ->setParameter('userId', $userId) - ->getSingleScalarResult(); - - $qb = $this->createQueryBuilder('e') - ->where('e.user = :user_id')->setParameter('user_id', $userId); + $qb = $this->getQueryBuilderByUser($userId) + ->select('MIN(e.id)', 'MAX(e.id)'); if ('unread' === $status) { $qb->andWhere('e.isArchived = false'); @@ -469,8 +466,15 @@ class EntryRepository extends EntityRepository $qb->andWhere('t.id is null'); } - return $qb->andWhere('e.id >= :rand') - ->setParameter('rand', rand(0, $max)) + $idLimits = $qb + ->getQuery() + ->getOneOrNullResult(); + $randomPossibleIds = rand($idLimits[1], $idLimits[2]); + + return $qb + ->select('e') + ->andWhere('e.id >= :random_id') + ->setParameter('random_id', $randomPossibleIds) ->setMaxResults(1) ->getQuery() ->getSingleResult(); -- cgit v1.2.3 From 9a57653aec85b0f5220436d5cb76545e66c24a11 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 12 Oct 2018 22:13:33 +0200 Subject: Redirect to the current view instead of homepage --- .../CoreBundle/Repository/EntryRepository.php | 33 +++++++++++----------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'src/Wallabag/CoreBundle/Repository') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 05563079..879e6671 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -435,7 +435,7 @@ class EntryRepository extends EntityRepository * Returns a random entry, filtering by status. * * @param $userId - * @param string $status can be unread, archive or starred + * @param string $type can be unread, archive, starred, etc * * @throws \Doctrine\ORM\NoResultException * @throws \Doctrine\ORM\NonUniqueResultException @@ -444,26 +444,25 @@ class EntryRepository extends EntityRepository * * @return Entry */ - public function getRandomEntry($userId, $status = '') + public function getRandomEntry($userId, $type = '') { $qb = $this->getQueryBuilderByUser($userId) ->select('MIN(e.id)', 'MAX(e.id)'); - if ('unread' === $status) { - $qb->andWhere('e.isArchived = false'); - } - - if ('archive' === $status) { - $qb->andWhere('e.isArchived = true'); - } - - if ('starred' === $status) { - $qb->andWhere('e.isStarred = true'); - } - - if ('untagged' === $status) { - $qb->leftJoin('e.tags', 't'); - $qb->andWhere('t.id is null'); + switch ($type) { + case 'unread': + $qb->andWhere('e.isArchived = false'); + break; + case 'archive': + $qb->andWhere('e.isArchived = true'); + break; + case 'starred': + $qb->andWhere('e.isStarred = true'); + break; + case 'untagged': + $qb->leftJoin('e.tags', 't'); + $qb->andWhere('t.id is null'); + break; } $idLimits = $qb -- cgit v1.2.3 From 50f35f0db2be9586205e793f315608eec59c9666 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 19 Jan 2019 22:08:29 +0100 Subject: Move icon into the top menu bar Change the way to select a random entry: - select all ids from the given user (with filters) - choose randomly one in php - find that entry --- .../CoreBundle/Repository/EntryRepository.php | 33 ++++++++-------------- 1 file changed, 12 insertions(+), 21 deletions(-) (limited to 'src/Wallabag/CoreBundle/Repository') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 879e6671..fabb1963 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -325,8 +325,8 @@ class EntryRepository extends EntityRepository * Find an entry by its url and its owner. * If it exists, return the entry otherwise return false. * - * @param $url - * @param $userId + * @param string $url + * @param int $userId * * @return Entry|bool */ @@ -417,8 +417,8 @@ class EntryRepository extends EntityRepository /** * Find all entries by url and owner. * - * @param $url - * @param $userId + * @param string $url + * @param int $userId * * @return array */ @@ -434,20 +434,17 @@ class EntryRepository extends EntityRepository /** * Returns a random entry, filtering by status. * - * @param $userId - * @param string $type can be unread, archive, starred, etc + * @param int $userId + * @param string $type Can be unread, archive, starred, etc * * @throws \Doctrine\ORM\NoResultException - * @throws \Doctrine\ORM\NonUniqueResultException - * - * @see https://github.com/doctrine/doctrine2/issues/5479#issuecomment-403862934 * * @return Entry */ public function getRandomEntry($userId, $type = '') { $qb = $this->getQueryBuilderByUser($userId) - ->select('MIN(e.id)', 'MAX(e.id)'); + ->select('e.id'); switch ($type) { case 'unread': @@ -465,18 +462,12 @@ class EntryRepository extends EntityRepository break; } - $idLimits = $qb - ->getQuery() - ->getOneOrNullResult(); - $randomPossibleIds = rand($idLimits[1], $idLimits[2]); + $ids = $qb->getQuery()->getArrayResult(); - return $qb - ->select('e') - ->andWhere('e.id >= :random_id') - ->setParameter('random_id', $randomPossibleIds) - ->setMaxResults(1) - ->getQuery() - ->getSingleResult(); + // random select one in the list + $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id']; + + return $this->find($randomId); } /** -- cgit v1.2.3 From 091bafeb4c4da9af8ea1201d5371887062f03f2e Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 19 Jan 2019 22:30:50 +0100 Subject: Handle no random result found --- src/Wallabag/CoreBundle/Repository/EntryRepository.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/Wallabag/CoreBundle/Repository') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index fabb1963..45366623 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -3,6 +3,7 @@ namespace Wallabag\CoreBundle\Repository; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\NoResultException; use Doctrine\ORM\QueryBuilder; use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Pagerfanta; @@ -437,7 +438,7 @@ class EntryRepository extends EntityRepository * @param int $userId * @param string $type Can be unread, archive, starred, etc * - * @throws \Doctrine\ORM\NoResultException + * @throws NoResultException * * @return Entry */ @@ -464,6 +465,10 @@ class EntryRepository extends EntityRepository $ids = $qb->getQuery()->getArrayResult(); + if (empty($ids)) { + throw new NoResultException(); + } + // random select one in the list $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id']; -- cgit v1.2.3