diff options
author | Kevin Decherf <kevin@kdecherf.com> | 2019-01-22 18:30:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-22 18:30:02 +0100 |
commit | 3527c300215e6e6010efb8bb840b8b6f5c63a1cc (patch) | |
tree | d3c23a18c966831ecd5f956d6365a2c7126c98c8 /src/Wallabag/CoreBundle/Repository/EntryRepository.php | |
parent | c73025ad8b684de1ac21ba7583f1af6f1d185159 (diff) | |
parent | fe5850d6e27f8cf20e7efc5c56672974b4779d9f (diff) | |
download | wallabag-3527c300215e6e6010efb8bb840b8b6f5c63a1cc.tar.gz wallabag-3527c300215e6e6010efb8bb840b8b6f5c63a1cc.tar.zst wallabag-3527c300215e6e6010efb8bb840b8b6f5c63a1cc.zip |
Merge pull request #3526 from wallabag/add-random-article
Add random feature
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository/EntryRepository.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 58 |
1 files changed, 50 insertions, 8 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 8b6cf443..45366623 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace Wallabag\CoreBundle\Repository; | 3 | namespace Wallabag\CoreBundle\Repository; |
4 | 4 | ||
5 | use Doctrine\ORM\EntityRepository; | 5 | use Doctrine\ORM\EntityRepository; |
6 | use Doctrine\ORM\NoResultException; | ||
6 | use Doctrine\ORM\QueryBuilder; | 7 | use Doctrine\ORM\QueryBuilder; |
7 | use Pagerfanta\Adapter\DoctrineORMAdapter; | 8 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
8 | use Pagerfanta\Pagerfanta; | 9 | use Pagerfanta\Pagerfanta; |
@@ -110,8 +111,7 @@ class EntryRepository extends EntityRepository | |||
110 | */ | 111 | */ |
111 | public function getBuilderForUntaggedByUser($userId) | 112 | public function getBuilderForUntaggedByUser($userId) |
112 | { | 113 | { |
113 | return $this | 114 | return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); |
114 | ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); | ||
115 | } | 115 | } |
116 | 116 | ||
117 | /** | 117 | /** |
@@ -326,8 +326,8 @@ class EntryRepository extends EntityRepository | |||
326 | * Find an entry by its url and its owner. | 326 | * Find an entry by its url and its owner. |
327 | * If it exists, return the entry otherwise return false. | 327 | * If it exists, return the entry otherwise return false. |
328 | * | 328 | * |
329 | * @param $url | 329 | * @param string $url |
330 | * @param $userId | 330 | * @param int $userId |
331 | * | 331 | * |
332 | * @return Entry|bool | 332 | * @return Entry|bool |
333 | */ | 333 | */ |
@@ -418,8 +418,8 @@ class EntryRepository extends EntityRepository | |||
418 | /** | 418 | /** |
419 | * Find all entries by url and owner. | 419 | * Find all entries by url and owner. |
420 | * | 420 | * |
421 | * @param $url | 421 | * @param string $url |
422 | * @param $userId | 422 | * @param int $userId |
423 | * | 423 | * |
424 | * @return array | 424 | * @return array |
425 | */ | 425 | */ |
@@ -433,6 +433,49 @@ class EntryRepository extends EntityRepository | |||
433 | } | 433 | } |
434 | 434 | ||
435 | /** | 435 | /** |
436 | * Returns a random entry, filtering by status. | ||
437 | * | ||
438 | * @param int $userId | ||
439 | * @param string $type Can be unread, archive, starred, etc | ||
440 | * | ||
441 | * @throws NoResultException | ||
442 | * | ||
443 | * @return Entry | ||
444 | */ | ||
445 | public function getRandomEntry($userId, $type = '') | ||
446 | { | ||
447 | $qb = $this->getQueryBuilderByUser($userId) | ||
448 | ->select('e.id'); | ||
449 | |||
450 | switch ($type) { | ||
451 | case 'unread': | ||
452 | $qb->andWhere('e.isArchived = false'); | ||
453 | break; | ||
454 | case 'archive': | ||
455 | $qb->andWhere('e.isArchived = true'); | ||
456 | break; | ||
457 | case 'starred': | ||
458 | $qb->andWhere('e.isStarred = true'); | ||
459 | break; | ||
460 | case 'untagged': | ||
461 | $qb->leftJoin('e.tags', 't'); | ||
462 | $qb->andWhere('t.id is null'); | ||
463 | break; | ||
464 | } | ||
465 | |||
466 | $ids = $qb->getQuery()->getArrayResult(); | ||
467 | |||
468 | if (empty($ids)) { | ||
469 | throw new NoResultException(); | ||
470 | } | ||
471 | |||
472 | // random select one in the list | ||
473 | $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id']; | ||
474 | |||
475 | return $this->find($randomId); | ||
476 | } | ||
477 | |||
478 | /** | ||
436 | * Return a query builder to be used by other getBuilderFor* method. | 479 | * Return a query builder to be used by other getBuilderFor* method. |
437 | * | 480 | * |
438 | * @param int $userId | 481 | * @param int $userId |
@@ -470,7 +513,6 @@ class EntryRepository extends EntityRepository | |||
470 | */ | 513 | */ |
471 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') | 514 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') |
472 | { | 515 | { |
473 | return $qb | 516 | return $qb->orderBy(sprintf('e.%s', $sortBy), $direction); |
474 | ->orderBy(sprintf('e.%s', $sortBy), $direction); | ||
475 | } | 517 | } |
476 | } | 518 | } |