aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Repository
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository')
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php125
-rw-r--r--src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php47
-rw-r--r--src/Wallabag/CoreBundle/Repository/TagRepository.php42
3 files changed, 172 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}
diff --git a/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php b/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php
new file mode 100644
index 00000000..36906761
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php
@@ -0,0 +1,47 @@
1<?php
2
3namespace Wallabag\CoreBundle\Repository;
4
5use Wallabag\CoreBundle\Helper\CryptoProxy;
6
7/**
8 * SiteCredentialRepository.
9 */
10class SiteCredentialRepository extends \Doctrine\ORM\EntityRepository
11{
12 private $cryptoProxy;
13
14 public function setCrypto(CryptoProxy $cryptoProxy)
15 {
16 $this->cryptoProxy = $cryptoProxy;
17 }
18
19 /**
20 * Retrieve one username/password for the given host and userId.
21 *
22 * @param string $host
23 * @param int $userId
24 *
25 * @return null|array
26 */
27 public function findOneByHostAndUser($host, $userId)
28 {
29 $res = $this->createQueryBuilder('s')
30 ->select('s.username', 's.password')
31 ->where('s.host = :hostname')->setParameter('hostname', $host)
32 ->andWhere('s.user = :userId')->setParameter('userId', $userId)
33 ->setMaxResults(1)
34 ->getQuery()
35 ->getOneOrNullResult();
36
37 if (null === $res) {
38 return;
39 }
40
41 // decrypt user & password before returning them
42 $res['username'] = $this->cryptoProxy->decrypt($res['username']);
43 $res['password'] = $this->cryptoProxy->decrypt($res['password']);
44
45 return $res;
46 }
47}
diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php
index 2182df25..5c45211f 100644
--- a/src/Wallabag/CoreBundle/Repository/TagRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php
@@ -3,6 +3,7 @@
3namespace Wallabag\CoreBundle\Repository; 3namespace Wallabag\CoreBundle\Repository;
4 4
5use Doctrine\ORM\EntityRepository; 5use Doctrine\ORM\EntityRepository;
6use Wallabag\CoreBundle\Entity\Tag;
6 7
7class TagRepository extends EntityRepository 8class TagRepository extends EntityRepository
8{ 9{
@@ -62,6 +63,27 @@ class TagRepository extends EntityRepository
62 } 63 }
63 64
64 /** 65 /**
66 * Find all tags (flat) per user with nb entries.
67 *
68 * @param int $userId
69 *
70 * @return array
71 */
72 public function findAllFlatTagsWithNbEntries($userId)
73 {
74 return $this->createQueryBuilder('t')
75 ->select('t.id, t.label, t.slug, count(e.id) as nbEntries')
76 ->distinct(true)
77 ->leftJoin('t.entries', 'e')
78 ->where('e.user = :userId')
79 ->groupBy('t.id')
80 ->orderBy('t.slug')
81 ->setParameter('userId', $userId)
82 ->getQuery()
83 ->getArrayResult();
84 }
85
86 /**
65 * Used only in test case to get a tag for our entry. 87 * Used only in test case to get a tag for our entry.
66 * 88 *
67 * @return Tag 89 * @return Tag
@@ -76,4 +98,24 @@ class TagRepository extends EntityRepository
76 ->getQuery() 98 ->getQuery()
77 ->getSingleResult(); 99 ->getSingleResult();
78 } 100 }
101
102 public function findForArchivedArticlesByUser($userId)
103 {
104 $ids = $this->createQueryBuilder('t')
105 ->select('t.id')
106 ->leftJoin('t.entries', 'e')
107 ->where('e.user = :userId')->setParameter('userId', $userId)
108 ->andWhere('e.isArchived = true')
109 ->groupBy('t.id')
110 ->orderBy('t.slug')
111 ->getQuery()
112 ->getArrayResult();
113
114 $tags = [];
115 foreach ($ids as $id) {
116 $tags[] = $this->find($id);
117 }
118
119 return $tags;
120 }
79} 121}