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.php125
1 files changed, 83 insertions, 42 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 4071301d..eb5e3205 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -3,29 +3,15 @@
3namespace Wallabag\CoreBundle\Repository; 3namespace Wallabag\CoreBundle\Repository;
4 4
5use Doctrine\ORM\EntityRepository; 5use Doctrine\ORM\EntityRepository;
6use Doctrine\ORM\Query; 6use Doctrine\ORM\QueryBuilder;
7use Pagerfanta\Adapter\DoctrineORMAdapter; 7use Pagerfanta\Adapter\DoctrineORMAdapter;
8use Pagerfanta\Pagerfanta; 8use Pagerfanta\Pagerfanta;
9use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Entity\Tag; 10use Wallabag\CoreBundle\Entity\Tag;
10 11
11class EntryRepository extends EntityRepository 12class EntryRepository extends EntityRepository
12{ 13{
13 /** 14 /**
14 * Return a query builder to used by other getBuilderFor* method.
15 *
16 * @param int $userId
17 *
18 * @return QueryBuilder
19 */
20 private function getBuilderByUser($userId)
21 {
22 return $this->createQueryBuilder('e')
23 ->andWhere('e.user = :userId')->setParameter('userId', $userId)
24 ->orderBy('e.createdAt', 'desc')
25 ;
26 }
27
28 /**
29 * Retrieves all entries for a user. 15 * Retrieves all entries for a user.
30 * 16 *
31 * @param int $userId 17 * @param int $userId
@@ -89,7 +75,7 @@ class EntryRepository extends EntityRepository
89 * 75 *
90 * @param int $userId 76 * @param int $userId
91 * @param string $term 77 * @param string $term
92 * @param strint $currentRoute 78 * @param string $currentRoute
93 * 79 *
94 * @return QueryBuilder 80 * @return QueryBuilder
95 */ 81 */
@@ -108,7 +94,7 @@ class EntryRepository extends EntityRepository
108 94
109 // We lower() all parts here because PostgreSQL 'LIKE' verb is case-sensitive 95 // We lower() all parts here because PostgreSQL 'LIKE' verb is case-sensitive
110 $qb 96 $qb
111 ->andWhere('lower(e.content) LIKE lower(:term) OR lower(e.title) LIKE lower(:term) OR lower(e.url) LIKE lower(:term)')->setParameter('term', '%'.$term.'%') 97 ->andWhere('lower(e.content) LIKE lower(:term) OR lower(e.title) LIKE lower(:term) OR lower(e.url) LIKE lower(:term)')->setParameter('term', '%' . $term . '%')
112 ->leftJoin('e.tags', 't') 98 ->leftJoin('e.tags', 't')
113 ->groupBy('e.id'); 99 ->groupBy('e.id');
114 100
@@ -135,25 +121,30 @@ class EntryRepository extends EntityRepository
135 * @param int $userId 121 * @param int $userId
136 * @param bool $isArchived 122 * @param bool $isArchived
137 * @param bool $isStarred 123 * @param bool $isStarred
124 * @param bool $isPublic
138 * @param string $sort 125 * @param string $sort
139 * @param string $order 126 * @param string $order
140 * @param int $since 127 * @param int $since
141 * @param string $tags 128 * @param string $tags
142 * 129 *
143 * @return array 130 * @return Pagerfanta
144 */ 131 */
145 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '') 132 public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
146 { 133 {
147 $qb = $this->createQueryBuilder('e') 134 $qb = $this->createQueryBuilder('e')
148 ->leftJoin('e.tags', 't') 135 ->leftJoin('e.tags', 't')
149 ->where('e.user =:userId')->setParameter('userId', $userId); 136 ->where('e.user =:userId')->setParameter('userId', $userId);
150 137
151 if (null !== $isArchived) { 138 if (null !== $isArchived) {
152 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); 139 $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived);
153 } 140 }
154 141
155 if (null !== $isStarred) { 142 if (null !== $isStarred) {
156 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); 143 $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred);
144 }
145
146 if (null !== $isPublic) {
147 $qb->andWhere('e.uid IS ' . (true === $isPublic ? 'NOT' : '') . ' NULL');
157 } 148 }
158 149
159 if ($since > 0) { 150 if ($since > 0) {
@@ -182,7 +173,7 @@ class EntryRepository extends EntityRepository
182 * 173 *
183 * @param int $userId 174 * @param int $userId
184 * 175 *
185 * @return Entry 176 * @return array
186 */ 177 */
187 public function findOneWithTags($userId) 178 public function findOneWithTags($userId)
188 { 179 {
@@ -328,47 +319,97 @@ class EntryRepository extends EntityRepository
328 * 319 *
329 * @return int 320 * @return int
330 */ 321 */
331 public function countAllEntriesByUsername($userId) 322 public function countAllEntriesByUser($userId)
332 { 323 {
333 $qb = $this->createQueryBuilder('e') 324 $qb = $this->createQueryBuilder('e')
334 ->select('count(e)') 325 ->select('count(e)')
335 ->where('e.user=:userId')->setParameter('userId', $userId) 326 ->where('e.user=:userId')->setParameter('userId', $userId)
336 ; 327 ;
337 328
338 return $qb->getQuery()->getSingleScalarResult(); 329 return (int) $qb->getQuery()->getSingleScalarResult();
339 } 330 }
340 331
341 /** 332 /**
342 * Count all entries for a tag and a user. 333 * Remove all entries for a user id.
334 * Used when a user want to reset all informations.
343 * 335 *
344 * @param int $userId 336 * @param int $userId
345 * @param int $tagId 337 */
338 public function removeAllByUserId($userId)
339 {
340 $this->getEntityManager()
341 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
342 ->setParameter('userId', $userId)
343 ->execute();
344 }
345
346 public function removeArchivedByUserId($userId)
347 {
348 $this->getEntityManager()
349 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE')
350 ->setParameter('userId', $userId)
351 ->execute();
352 }
353
354 /**
355 * Get id and url from all entries
356 * Used for the clean-duplicates command.
357 */
358 public function findAllEntriesIdAndUrlByUserId($userId)
359 {
360 $qb = $this->createQueryBuilder('e')
361 ->select('e.id, e.url')
362 ->where('e.user = :userid')->setParameter(':userid', $userId);
363
364 return $qb->getQuery()->getArrayResult();
365 }
366
367 /**
368 * @param int $userId
346 * 369 *
347 * @return int 370 * @return array
348 */ 371 */
349 public function countAllEntriesByUserIdAndTagId($userId, $tagId) 372 public function findAllEntriesIdByUserId($userId = null)
350 { 373 {
351 $qb = $this->createQueryBuilder('e') 374 $qb = $this->createQueryBuilder('e')
352 ->select('count(e.id)') 375 ->select('e.id');
353 ->leftJoin('e.tags', 't')
354 ->where('e.user=:userId')->setParameter('userId', $userId)
355 ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
356 ;
357 376
358 return $qb->getQuery()->getSingleScalarResult(); 377 if (null !== $userId) {
378 $qb->where('e.user = :userid')->setParameter(':userid', $userId);
379 }
380
381 return $qb->getQuery()->getArrayResult();
359 } 382 }
360 383
361 /** 384 /**
362 * Remove all entries for a user id. 385 * Find all entries by url and owner.
363 * Used when a user want to reset all informations. 386 *
387 * @param $url
388 * @param $userId
389 *
390 * @return array
391 */
392 public function findAllByUrlAndUserId($url, $userId)
393 {
394 return $this->createQueryBuilder('e')
395 ->where('e.url = :url')->setParameter('url', urldecode($url))
396 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
397 ->getQuery()
398 ->getResult();
399 }
400
401 /**
402 * Return a query builder to used by other getBuilderFor* method.
364 * 403 *
365 * @param int $userId 404 * @param int $userId
405 *
406 * @return QueryBuilder
366 */ 407 */
367 public function removeAllByUserId($userId) 408 private function getBuilderByUser($userId)
368 { 409 {
369 $this->getEntityManager() 410 return $this->createQueryBuilder('e')
370 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') 411 ->andWhere('e.user = :userId')->setParameter('userId', $userId)
371 ->setParameter('userId', $userId) 412 ->orderBy('e.createdAt', 'desc')
372 ->execute(); 413 ;
373 } 414 }
374} 415}