aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Repository/EntryRepository.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository/EntryRepository.php')
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php103
1 files changed, 93 insertions, 10 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index cebce714..3990932e 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -3,6 +3,7 @@
3namespace Wallabag\CoreBundle\Repository; 3namespace Wallabag\CoreBundle\Repository;
4 4
5use Doctrine\ORM\EntityRepository; 5use Doctrine\ORM\EntityRepository;
6use Doctrine\ORM\NoResultException;
6use Doctrine\ORM\QueryBuilder; 7use Doctrine\ORM\QueryBuilder;
7use Pagerfanta\Adapter\DoctrineORMAdapter; 8use Pagerfanta\Adapter\DoctrineORMAdapter;
8use Pagerfanta\Pagerfanta; 9use Pagerfanta\Pagerfanta;
@@ -50,7 +51,7 @@ class EntryRepository extends EntityRepository
50 public function getBuilderForArchiveByUser($userId) 51 public function getBuilderForArchiveByUser($userId)
51 { 52 {
52 return $this 53 return $this
53 ->getSortedQueryBuilderByUser($userId) 54 ->getSortedQueryBuilderByUser($userId, 'archivedAt', 'desc')
54 ->andWhere('e.isArchived = true') 55 ->andWhere('e.isArchived = true')
55 ; 56 ;
56 } 57 }
@@ -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 /**
@@ -139,15 +139,30 @@ class EntryRepository extends EntityRepository
139 * @param string $order 139 * @param string $order
140 * @param int $since 140 * @param int $since
141 * @param string $tags 141 * @param string $tags
142 * @param string $detail 'metadata' or 'full'. Include content field if 'full'
143 *
144 * @todo Breaking change: replace default detail=full by detail=metadata in a future version
142 * 145 *
143 * @return Pagerfanta 146 * @return Pagerfanta
144 */ 147 */
145 public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '') 148 public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '', $detail = 'full')
146 { 149 {
150 if (!\in_array(strtolower($detail), ['full', 'metadata'], true)) {
151 throw new \Exception('Detail "' . $detail . '" parameter is wrong, allowed: full or metadata');
152 }
153
147 $qb = $this->createQueryBuilder('e') 154 $qb = $this->createQueryBuilder('e')
148 ->leftJoin('e.tags', 't') 155 ->leftJoin('e.tags', 't')
149 ->where('e.user = :userId')->setParameter('userId', $userId); 156 ->where('e.user = :userId')->setParameter('userId', $userId);
150 157
158 if ('metadata' === $detail) {
159 $fieldNames = $this->getClassMetadata()->getFieldNames();
160 $fields = array_filter($fieldNames, function ($k) {
161 return 'content' !== $k;
162 });
163 $qb->select(sprintf('partial e.{%s}', implode(',', $fields)));
164 }
165
151 if (null !== $isArchived) { 166 if (null !== $isArchived) {
152 $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); 167 $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived);
153 } 168 }
@@ -193,6 +208,8 @@ class EntryRepository extends EntityRepository
193 $qb->orderBy('e.id', $order); 208 $qb->orderBy('e.id', $order);
194 } elseif ('updated' === $sort) { 209 } elseif ('updated' === $sort) {
195 $qb->orderBy('e.updatedAt', $order); 210 $qb->orderBy('e.updatedAt', $order);
211 } elseif ('archived' === $sort) {
212 $qb->orderBy('e.archivedAt', $order);
196 } 213 }
197 214
198 $pagerAdapter = new DoctrineORMAdapter($qb, true, false); 215 $pagerAdapter = new DoctrineORMAdapter($qb, true, false);
@@ -324,8 +341,8 @@ class EntryRepository extends EntityRepository
324 * Find an entry by its url and its owner. 341 * Find an entry by its url and its owner.
325 * If it exists, return the entry otherwise return false. 342 * If it exists, return the entry otherwise return false.
326 * 343 *
327 * @param $url 344 * @param string $url
328 * @param $userId 345 * @param int $userId
329 * 346 *
330 * @return Entry|bool 347 * @return Entry|bool
331 */ 348 */
@@ -345,6 +362,30 @@ class EntryRepository extends EntityRepository
345 } 362 }
346 363
347 /** 364 /**
365 * Find an entry by its hashed url and its owner.
366 * If it exists, return the entry otherwise return false.
367 *
368 * @param string $hashedUrl Url hashed using sha1
369 * @param int $userId
370 *
371 * @return Entry|bool
372 */
373 public function findByHashedUrlAndUserId($hashedUrl, $userId)
374 {
375 $res = $this->createQueryBuilder('e')
376 ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl)
377 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
378 ->getQuery()
379 ->getResult();
380
381 if (\count($res)) {
382 return current($res);
383 }
384
385 return false;
386 }
387
388 /**
348 * Count all entries for a user. 389 * Count all entries for a user.
349 * 390 *
350 * @param int $userId 391 * @param int $userId
@@ -416,8 +457,8 @@ class EntryRepository extends EntityRepository
416 /** 457 /**
417 * Find all entries by url and owner. 458 * Find all entries by url and owner.
418 * 459 *
419 * @param $url 460 * @param string $url
420 * @param $userId 461 * @param int $userId
421 * 462 *
422 * @return array 463 * @return array
423 */ 464 */
@@ -431,6 +472,49 @@ class EntryRepository extends EntityRepository
431 } 472 }
432 473
433 /** 474 /**
475 * Returns a random entry, filtering by status.
476 *
477 * @param int $userId
478 * @param string $type Can be unread, archive, starred, etc
479 *
480 * @throws NoResultException
481 *
482 * @return Entry
483 */
484 public function getRandomEntry($userId, $type = '')
485 {
486 $qb = $this->getQueryBuilderByUser($userId)
487 ->select('e.id');
488
489 switch ($type) {
490 case 'unread':
491 $qb->andWhere('e.isArchived = false');
492 break;
493 case 'archive':
494 $qb->andWhere('e.isArchived = true');
495 break;
496 case 'starred':
497 $qb->andWhere('e.isStarred = true');
498 break;
499 case 'untagged':
500 $qb->leftJoin('e.tags', 't');
501 $qb->andWhere('t.id is null');
502 break;
503 }
504
505 $ids = $qb->getQuery()->getArrayResult();
506
507 if (empty($ids)) {
508 throw new NoResultException();
509 }
510
511 // random select one in the list
512 $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id'];
513
514 return $this->find($randomId);
515 }
516
517 /**
434 * Return a query builder to be used by other getBuilderFor* method. 518 * Return a query builder to be used by other getBuilderFor* method.
435 * 519 *
436 * @param int $userId 520 * @param int $userId
@@ -468,7 +552,6 @@ class EntryRepository extends EntityRepository
468 */ 552 */
469 private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') 553 private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc')
470 { 554 {
471 return $qb 555 return $qb->orderBy(sprintf('e.%s', $sortBy), $direction);
472 ->orderBy(sprintf('e.%s', $sortBy), $direction);
473 } 556 }
474} 557}