]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Merge pull request #2002 from wallabag/feature-display-itemsNumber
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Repository;
9d50517c 4
9d50517c 5use Doctrine\ORM\EntityRepository;
0b0233b1 6use Doctrine\ORM\Query;
bcf53ab7
WD
7use Pagerfanta\Adapter\DoctrineORMAdapter;
8use Pagerfanta\Pagerfanta;
fc732227 9use Wallabag\CoreBundle\Entity\Tag;
9d50517c 10
be463487 11class EntryRepository extends EntityRepository
9d50517c 12{
b3f4a11a
NL
13 private $lifeTime;
14
b84a8055 15 /**
0ab7404f 16 * Return a query builder to used by other getBuilderFor* method.
b84a8055 17 *
3b815d2d 18 * @param int $userId
3b815d2d 19 *
26864574 20 * @return QueryBuilder
b84a8055 21 */
0ab7404f 22 private function getBuilderByUser($userId)
9d50517c 23 {
26864574 24 return $this->createQueryBuilder('e')
3b815d2d 25 ->leftJoin('e.user', 'u')
0ab7404f
JB
26 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
27 ->orderBy('e.id', 'desc')
28 ;
29 }
30
2b7a4889
NL
31 /**
32 * Retrieves all entries for a user.
33 *
34 * @param int $userId
35 *
36 * @return QueryBuilder
37 */
38 public function getBuilderForAllByUser($userId)
39 {
40 return $this
41 ->getBuilderByUser($userId)
42 ;
43 }
44
0ab7404f
JB
45 /**
46 * Retrieves unread entries for a user.
47 *
48 * @param int $userId
49 *
50 * @return QueryBuilder
51 */
52 public function getBuilderForUnreadByUser($userId)
53 {
54 return $this
55 ->getBuilderByUser($userId)
56 ->andWhere('e.isArchived = false')
57 ;
9d50517c 58 }
bd9f0815 59
b84a8055 60 /**
4346a860 61 * Retrieves read entries for a user.
b84a8055 62 *
3b815d2d 63 * @param int $userId
3b815d2d 64 *
26864574 65 * @return QueryBuilder
b84a8055 66 */
0ab7404f 67 public function getBuilderForArchiveByUser($userId)
bd9f0815 68 {
0ab7404f
JB
69 return $this
70 ->getBuilderByUser($userId)
71 ->andWhere('e.isArchived = true')
72 ;
bd9f0815
NL
73 }
74
b84a8055 75 /**
4346a860 76 * Retrieves starred entries for a user.
b84a8055 77 *
3b815d2d 78 * @param int $userId
3b815d2d 79 *
26864574 80 * @return QueryBuilder
b84a8055 81 */
0ab7404f 82 public function getBuilderForStarredByUser($userId)
bd9f0815 83 {
0ab7404f
JB
84 return $this
85 ->getBuilderByUser($userId)
86 ->andWhere('e.isStarred = true')
87 ;
bd9f0815 88 }
a8c90c5c 89
b6520f0b
NL
90 /**
91 * Retrieves untagged entries for a user.
92 *
93 * @param int $userId
94 *
95 * @return QueryBuilder
96 */
97 public function getBuilderForUntaggedByUser($userId)
98 {
99 return $this
100 ->getBuilderByUser($userId)
101 ->leftJoin('e.tags', 't')
102 ->groupBy('e.id')
103 ->having('count(t.id) = 0');
104 }
105
3b815d2d 106 /**
4346a860 107 * Find Entries.
3b815d2d 108 *
2a94b1d1
NL
109 * @param int $userId
110 * @param bool $isArchived
111 * @param bool $isStarred
2a94b1d1
NL
112 * @param string $sort
113 * @param string $order
8cb869ea
TC
114 * @param int $since
115 * @param string $tags
3b815d2d 116 *
017e2089 117 * @return array
3b815d2d 118 */
28803f10 119 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
a8c90c5c
NL
120 {
121 $qb = $this->createQueryBuilder('e')
28803f10 122 ->leftJoin('e.tags', 't')
0f006880 123 ->where('e.user =:userId')->setParameter('userId', $userId);
6e334aba 124
3b815d2d
J
125 if (null !== $isArchived) {
126 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
6e334aba
NL
127 }
128
3b815d2d
J
129 if (null !== $isStarred) {
130 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
6e334aba
NL
131 }
132
e5fb89e5
TC
133 if ($since >= 0) {
134 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
4f0558a0
TC
135 }
136
28803f10
TC
137 if ('' !== $tags) {
138 foreach (explode(',', $tags) as $tag) {
139 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
140 }
e5fb89e5
TC
141 }
142
bc782eaa 143 if ('created' === $sort) {
2385f891 144 $qb->orderBy('e.id', $order);
bc782eaa
NL
145 } elseif ('updated' === $sort) {
146 $qb->orderBy('e.updatedAt', $order);
147 }
148
bcf53ab7
WD
149 $pagerAdapter = new DoctrineORMAdapter($qb);
150
151 return new Pagerfanta($pagerAdapter);
a8c90c5c 152 }
46bbd8d3 153
a36737f4
NL
154 /**
155 * Fetch an entry with a tag. Only used for tests.
156 *
5c072d2b
NL
157 * @param int $userId
158 *
a36737f4
NL
159 * @return Entry
160 */
092ca707 161 public function findOneWithTags($userId)
46bbd8d3
NL
162 {
163 $qb = $this->createQueryBuilder('e')
164 ->innerJoin('e.tags', 't')
0ca374e6
NL
165 ->innerJoin('e.user', 'u')
166 ->addSelect('t', 'u')
167 ->where('e.user=:userId')->setParameter('userId', $userId)
168 ;
092ca707 169
0ca374e6 170 return $qb->getQuery()->getResult();
46bbd8d3 171 }
d4ebe5c5
JB
172
173 /**
174 * Find distinct language for a given user.
175 * Used to build the filter language list.
176 *
177 * @param int $userId User id
178 *
179 * @return array
180 */
181 public function findDistinctLanguageByUser($userId)
182 {
183 $results = $this->createQueryBuilder('e')
184 ->select('e.language')
185 ->where('e.user = :userId')->setParameter('userId', $userId)
186 ->andWhere('e.language IS NOT NULL')
187 ->groupBy('e.language')
188 ->orderBy('e.language', ' ASC')
189 ->getQuery()
190 ->getResult();
191
4094ea47 192 $languages = [];
d4ebe5c5
JB
193 foreach ($results as $result) {
194 $languages[$result['language']] = $result['language'];
195 }
196
197 return $languages;
198 }
159986c4 199
159986c4 200 /**
cfb28c9d 201 * Used only in test case to get the right entry associated to the right user.
159986c4 202 *
cfb28c9d 203 * @param string $username
159986c4
JB
204 *
205 * @return Entry
206 */
207 public function findOneByUsernameAndNotArchived($username)
208 {
209 return $this->createQueryBuilder('e')
210 ->leftJoin('e.user', 'u')
211 ->where('u.username = :username')->setParameter('username', $username)
212 ->andWhere('e.isArchived = false')
213 ->setMaxResults(1)
214 ->getQuery()
215 ->getSingleResult();
216 }
fc732227
JB
217
218 /**
219 * Remove a tag from all user entries.
4059a061
JB
220 *
221 * We need to loop on each entry attached to the given tag to remove it, since Doctrine doesn't know EntryTag entity because it's a ManyToMany relation.
6be97501
JB
222 * It could be faster with one query but I don't know how to retrieve the table name `entry_tag` which can have a prefix:
223 *
224 * DELETE et FROM entry_tag et WHERE et.entry_id IN ( SELECT e.id FROM entry e WHERE e.user_id = :userId ) AND et.tag_id = :tagId
fc732227
JB
225 *
226 * @param int $userId
227 * @param Tag $tag
228 */
229 public function removeTag($userId, Tag $tag)
230 {
4059a061
JB
231 $entries = $this->getBuilderByUser($userId)
232 ->innerJoin('e.tags', 't')
233 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
234 ->getQuery()
235 ->getResult();
236
237 foreach ($entries as $entry) {
238 $entry->removeTag($tag);
239 }
240
241 $this->getEntityManager()->flush();
4059a061
JB
242 }
243
4da01f49 244 /**
9bf83f1f 245 * Remove tags from all user entries.
4da01f49 246 *
9bf83f1f 247 * @param int $userId
4da01f49
TC
248 * @param Array<Tag> $tags
249 */
9bf83f1f
TC
250 public function removeTags($userId, $tags)
251 {
4da01f49
TC
252 foreach ($tags as $tag) {
253 $this->removeTag($userId, $tag);
254 }
255 }
256
4059a061
JB
257 /**
258 * Find all entries that are attached to a give tag id.
259 *
260 * @param int $userId
261 * @param int $tagId
262 *
263 * @return array
264 */
265 public function findAllByTagId($userId, $tagId)
266 {
267 return $this->getBuilderByUser($userId)
268 ->innerJoin('e.tags', 't')
269 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
270 ->getQuery()
271 ->getResult();
fc732227 272 }
303768df
NL
273
274 /**
275 * Find an entry by its url and its owner.
5a4bbcc9 276 * If it exists, return the entry otherwise return false.
303768df
NL
277 *
278 * @param $url
279 * @param $userId
280 *
4094ea47 281 * @return Entry|bool
303768df 282 */
78833672 283 public function findByUrlAndUserId($url, $userId)
303768df 284 {
5a4bbcc9 285 $res = $this->createQueryBuilder('e')
303768df
NL
286 ->where('e.url = :url')->setParameter('url', $url)
287 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
288 ->getQuery()
289 ->getResult();
5a4bbcc9 290
b4b592a0
JB
291 if (count($res)) {
292 return current($res);
5a4bbcc9
JB
293 }
294
295 return false;
303768df 296 }
5c072d2b
NL
297
298 /**
299 * Count all entries for a user.
300 *
301 * @param int $userId
302 *
e678c475 303 * @return int
5c072d2b
NL
304 */
305 public function countAllEntriesByUsername($userId)
306 {
307 $qb = $this->createQueryBuilder('e')
308 ->select('count(e)')
309 ->where('e.user=:userId')->setParameter('userId', $userId)
310 ;
311
312 return $qb->getQuery()->getSingleScalarResult();
313 }
0b0233b1 314
b3f4a11a
NL
315 public function setLifeTime($lifeTime)
316 {
317 $this->lifeTime = $lifeTime;
318 }
319
0b0233b1 320 /**
b3f4a11a 321 * Enable cache for a query.
0b0233b1
NL
322 *
323 * @param Query $query
324 *
325 * @return Query
326 */
327 public function enableCache(Query $query)
328 {
329 $query->useQueryCache(true);
330 $query->useResultCache(true);
b3f4a11a 331 $query->setResultCacheLifetime($this->lifeTime);
0b0233b1
NL
332
333 return $query;
334 }
9d50517c 335}