]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/EntryRepository.php
rename getAllEntriesId into findAllEntriesIdByUserId
[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;
52b84c11 6use Doctrine\ORM\QueryBuilder;
bcf53ab7
WD
7use Pagerfanta\Adapter\DoctrineORMAdapter;
8use Pagerfanta\Pagerfanta;
52b84c11 9use Wallabag\CoreBundle\Entity\Entry;
fc732227 10use Wallabag\CoreBundle\Entity\Tag;
9d50517c 11
be463487 12class EntryRepository extends EntityRepository
9d50517c 13{
2b7a4889
NL
14 /**
15 * Retrieves all entries for a user.
16 *
17 * @param int $userId
18 *
19 * @return QueryBuilder
20 */
21 public function getBuilderForAllByUser($userId)
22 {
23 return $this
24 ->getBuilderByUser($userId)
25 ;
26 }
27
0ab7404f
JB
28 /**
29 * Retrieves unread entries for a user.
30 *
31 * @param int $userId
32 *
33 * @return QueryBuilder
34 */
35 public function getBuilderForUnreadByUser($userId)
36 {
37 return $this
38 ->getBuilderByUser($userId)
39 ->andWhere('e.isArchived = false')
40 ;
9d50517c 41 }
bd9f0815 42
b84a8055 43 /**
4346a860 44 * Retrieves read entries for a user.
b84a8055 45 *
3b815d2d 46 * @param int $userId
3b815d2d 47 *
26864574 48 * @return QueryBuilder
b84a8055 49 */
0ab7404f 50 public function getBuilderForArchiveByUser($userId)
bd9f0815 51 {
0ab7404f
JB
52 return $this
53 ->getBuilderByUser($userId)
54 ->andWhere('e.isArchived = true')
55 ;
bd9f0815
NL
56 }
57
b84a8055 58 /**
4346a860 59 * Retrieves starred entries for a user.
b84a8055 60 *
3b815d2d 61 * @param int $userId
3b815d2d 62 *
26864574 63 * @return QueryBuilder
b84a8055 64 */
0ab7404f 65 public function getBuilderForStarredByUser($userId)
bd9f0815 66 {
0ab7404f
JB
67 return $this
68 ->getBuilderByUser($userId)
69 ->andWhere('e.isStarred = true')
70 ;
bd9f0815 71 }
a8c90c5c 72
ee122a75
NL
73 /**
74 * Retrieves entries filtered with a search term for a user.
75 *
76 * @param int $userId
77 * @param string $term
52b84c11 78 * @param string $currentRoute
ee122a75
NL
79 *
80 * @return QueryBuilder
81 */
49b042df 82 public function getBuilderForSearchByUser($userId, $term, $currentRoute)
ee122a75 83 {
49b042df
NL
84 $qb = $this
85 ->getBuilderByUser($userId);
86
87 if ('starred' === $currentRoute) {
88 $qb->andWhere('e.isStarred = true');
89 } elseif ('unread' === $currentRoute) {
90 $qb->andWhere('e.isArchived = false');
91 } elseif ('archive' === $currentRoute) {
92 $qb->andWhere('e.isArchived = true');
93 }
94
eac09b48 95 // We lower() all parts here because PostgreSQL 'LIKE' verb is case-sensitive
49b042df 96 $qb
f808b016 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 . '%')
ee122a75 98 ->leftJoin('e.tags', 't')
32f455c1 99 ->groupBy('e.id');
49b042df
NL
100
101 return $qb;
ee122a75
NL
102 }
103
b6520f0b
NL
104 /**
105 * Retrieves untagged entries for a user.
106 *
107 * @param int $userId
108 *
109 * @return QueryBuilder
110 */
111 public function getBuilderForUntaggedByUser($userId)
112 {
113 return $this
114 ->getBuilderByUser($userId)
9deac0c5 115 ->andWhere('size(e.tags) = 0');
b6520f0b
NL
116 }
117
3b815d2d 118 /**
4346a860 119 * Find Entries.
3b815d2d 120 *
2a94b1d1
NL
121 * @param int $userId
122 * @param bool $isArchived
123 * @param bool $isStarred
1112e547 124 * @param bool $isPublic
2a94b1d1
NL
125 * @param string $sort
126 * @param string $order
8cb869ea
TC
127 * @param int $since
128 * @param string $tags
3b815d2d 129 *
52b84c11 130 * @return Pagerfanta
3b815d2d 131 */
1112e547 132 public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
a8c90c5c
NL
133 {
134 $qb = $this->createQueryBuilder('e')
28803f10 135 ->leftJoin('e.tags', 't')
0f006880 136 ->where('e.user =:userId')->setParameter('userId', $userId);
6e334aba 137
3b815d2d 138 if (null !== $isArchived) {
1112e547 139 $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived);
6e334aba
NL
140 }
141
3b815d2d 142 if (null !== $isStarred) {
1112e547
JB
143 $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred);
144 }
145
146 if (null !== $isPublic) {
f808b016 147 $qb->andWhere('e.uid IS ' . (true === $isPublic ? 'NOT' : '') . ' NULL');
6e334aba
NL
148 }
149
c3f8b428 150 if ($since > 0) {
e5fb89e5 151 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
4f0558a0
TC
152 }
153
28803f10
TC
154 if ('' !== $tags) {
155 foreach (explode(',', $tags) as $tag) {
156 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
157 }
e5fb89e5
TC
158 }
159
bc782eaa 160 if ('created' === $sort) {
2385f891 161 $qb->orderBy('e.id', $order);
bc782eaa
NL
162 } elseif ('updated' === $sort) {
163 $qb->orderBy('e.updatedAt', $order);
164 }
165
5a5da369 166 $pagerAdapter = new DoctrineORMAdapter($qb, true, false);
bcf53ab7
WD
167
168 return new Pagerfanta($pagerAdapter);
a8c90c5c 169 }
46bbd8d3 170
a36737f4
NL
171 /**
172 * Fetch an entry with a tag. Only used for tests.
173 *
5c072d2b
NL
174 * @param int $userId
175 *
52b84c11 176 * @return array
a36737f4 177 */
092ca707 178 public function findOneWithTags($userId)
46bbd8d3
NL
179 {
180 $qb = $this->createQueryBuilder('e')
181 ->innerJoin('e.tags', 't')
0ca374e6
NL
182 ->innerJoin('e.user', 'u')
183 ->addSelect('t', 'u')
184 ->where('e.user=:userId')->setParameter('userId', $userId)
185 ;
092ca707 186
0ca374e6 187 return $qb->getQuery()->getResult();
46bbd8d3 188 }
d4ebe5c5
JB
189
190 /**
191 * Find distinct language for a given user.
192 * Used to build the filter language list.
193 *
194 * @param int $userId User id
195 *
196 * @return array
197 */
198 public function findDistinctLanguageByUser($userId)
199 {
200 $results = $this->createQueryBuilder('e')
201 ->select('e.language')
202 ->where('e.user = :userId')->setParameter('userId', $userId)
203 ->andWhere('e.language IS NOT NULL')
204 ->groupBy('e.language')
205 ->orderBy('e.language', ' ASC')
206 ->getQuery()
207 ->getResult();
208
4094ea47 209 $languages = [];
d4ebe5c5
JB
210 foreach ($results as $result) {
211 $languages[$result['language']] = $result['language'];
212 }
213
214 return $languages;
215 }
159986c4 216
159986c4 217 /**
cfb28c9d 218 * Used only in test case to get the right entry associated to the right user.
159986c4 219 *
cfb28c9d 220 * @param string $username
159986c4
JB
221 *
222 * @return Entry
223 */
224 public function findOneByUsernameAndNotArchived($username)
225 {
226 return $this->createQueryBuilder('e')
227 ->leftJoin('e.user', 'u')
228 ->where('u.username = :username')->setParameter('username', $username)
229 ->andWhere('e.isArchived = false')
230 ->setMaxResults(1)
231 ->getQuery()
232 ->getSingleResult();
233 }
fc732227
JB
234
235 /**
236 * Remove a tag from all user entries.
4059a061
JB
237 *
238 * 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
239 * 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:
240 *
241 * 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
242 *
243 * @param int $userId
244 * @param Tag $tag
245 */
246 public function removeTag($userId, Tag $tag)
247 {
4059a061
JB
248 $entries = $this->getBuilderByUser($userId)
249 ->innerJoin('e.tags', 't')
250 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
251 ->getQuery()
252 ->getResult();
253
254 foreach ($entries as $entry) {
255 $entry->removeTag($tag);
256 }
257
258 $this->getEntityManager()->flush();
4059a061
JB
259 }
260
4da01f49 261 /**
9bf83f1f 262 * Remove tags from all user entries.
4da01f49 263 *
9bf83f1f 264 * @param int $userId
4da01f49
TC
265 * @param Array<Tag> $tags
266 */
9bf83f1f
TC
267 public function removeTags($userId, $tags)
268 {
4da01f49
TC
269 foreach ($tags as $tag) {
270 $this->removeTag($userId, $tag);
271 }
272 }
273
4059a061
JB
274 /**
275 * Find all entries that are attached to a give tag id.
276 *
277 * @param int $userId
278 * @param int $tagId
279 *
280 * @return array
281 */
282 public function findAllByTagId($userId, $tagId)
283 {
284 return $this->getBuilderByUser($userId)
285 ->innerJoin('e.tags', 't')
286 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
287 ->getQuery()
288 ->getResult();
fc732227 289 }
303768df
NL
290
291 /**
292 * Find an entry by its url and its owner.
5a4bbcc9 293 * If it exists, return the entry otherwise return false.
303768df
NL
294 *
295 * @param $url
296 * @param $userId
297 *
4094ea47 298 * @return Entry|bool
303768df 299 */
78833672 300 public function findByUrlAndUserId($url, $userId)
303768df 301 {
5a4bbcc9 302 $res = $this->createQueryBuilder('e')
19ca0b2f 303 ->where('e.url = :url')->setParameter('url', urldecode($url))
303768df
NL
304 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
305 ->getQuery()
306 ->getResult();
5a4bbcc9 307
b4b592a0
JB
308 if (count($res)) {
309 return current($res);
5a4bbcc9
JB
310 }
311
312 return false;
303768df 313 }
5c072d2b
NL
314
315 /**
316 * Count all entries for a user.
317 *
318 * @param int $userId
319 *
e678c475 320 * @return int
5c072d2b 321 */
273b6f06 322 public function countAllEntriesByUser($userId)
5c072d2b
NL
323 {
324 $qb = $this->createQueryBuilder('e')
325 ->select('count(e)')
326 ->where('e.user=:userId')->setParameter('userId', $userId)
327 ;
328
38520658 329 return (int) $qb->getQuery()->getSingleScalarResult();
5c072d2b 330 }
28bb4890 331
191564b7
JB
332 /**
333 * Remove all entries for a user id.
8c61fd12 334 * Used when a user want to reset all informations.
191564b7 335 *
8c61fd12 336 * @param int $userId
191564b7
JB
337 */
338 public function removeAllByUserId($userId)
339 {
340 $this->getEntityManager()
b0de88f7
JB
341 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
342 ->setParameter('userId', $userId)
191564b7
JB
343 ->execute();
344 }
6da1aebc
TC
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 }
e2f3800c
TC
353
354 /**
355 * Get id and url from all entries
356 * Used for the clean-duplicates command.
357 */
358 public function getAllEntriesIdAndUrl($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 }
d09fe4d2 366
511f1ce1
NH
367 /**
368 * @param int $userId
369 *
370 * @return array
371 */
215409a8 372 public function findAllEntriesIdByUserId($userId = null)
511f1ce1
NH
373 {
374 $qb = $this->createQueryBuilder('e')
375 ->select('e.id');
376
377 if (null !== $userId) {
378 $qb->where('e.user = :userid')->setParameter(':userid', $userId);
379 }
380
381 return $qb->getQuery()->getArrayResult();
382 }
383
d09fe4d2
NL
384 /**
385 * Find all entries by url and owner.
386 *
387 * @param $url
388 * @param $userId
389 *
390 * @return array
391 */
392 public function findAllByUrlAndUserId($url, $userId)
393 {
89f108b4 394 return $this->createQueryBuilder('e')
d09fe4d2
NL
395 ->where('e.url = :url')->setParameter('url', urldecode($url))
396 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
397 ->getQuery()
398 ->getResult();
d09fe4d2 399 }
f808b016
JB
400
401 /**
402 * Return a query builder to used by other getBuilderFor* method.
403 *
404 * @param int $userId
405 *
406 * @return QueryBuilder
407 */
408 private function getBuilderByUser($userId)
409 {
410 return $this->createQueryBuilder('e')
411 ->andWhere('e.user = :userId')->setParameter('userId', $userId)
412 ->orderBy('e.createdAt', 'desc')
413 ;
414 }
9d50517c 415}