]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Repository; |
9d50517c | 4 | |
9d50517c | 5 | use Doctrine\ORM\EntityRepository; |
52b84c11 | 6 | use Doctrine\ORM\QueryBuilder; |
bcf53ab7 WD |
7 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
8 | use Pagerfanta\Pagerfanta; | |
52b84c11 | 9 | use Wallabag\CoreBundle\Entity\Entry; |
fc732227 | 10 | use Wallabag\CoreBundle\Entity\Tag; |
9d50517c | 11 | |
be463487 | 12 | class 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 | |
b7c5fda5 | 24 | ->getSortedQueryBuilderByUser($userId) |
2b7a4889 NL |
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 | |
b7c5fda5 | 38 | ->getSortedQueryBuilderByUser($userId) |
0ab7404f JB |
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 | 52 | return $this |
9007fe00 | 53 | ->getSortedQueryBuilderByUser($userId, 'archivedAt', 'desc') |
0ab7404f JB |
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 | 67 | return $this |
b7c5fda5 | 68 | ->getSortedQueryBuilderByUser($userId, 'starredAt', 'desc') |
0ab7404f JB |
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 | 84 | $qb = $this |
b7c5fda5 | 85 | ->getSortedQueryBuilderByUser($userId); |
49b042df NL |
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 | 104 | /** |
06366972 | 105 | * Retrieve a sorted list of untagged entries for a user. |
b6520f0b NL |
106 | * |
107 | * @param int $userId | |
108 | * | |
109 | * @return QueryBuilder | |
110 | */ | |
111 | public function getBuilderForUntaggedByUser($userId) | |
112 | { | |
113 | return $this | |
06366972 KD |
114 | ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); |
115 | } | |
116 | ||
117 | /** | |
118 | * Retrieve untagged entries for a user. | |
b8115ff4 | 119 | * |
06366972 | 120 | * @param int $userId |
b8115ff4 | 121 | * |
06366972 KD |
122 | * @return QueryBuilder |
123 | */ | |
124 | public function getRawBuilderForUntaggedByUser($userId) | |
125 | { | |
126 | return $this->getQueryBuilderByUser($userId) | |
127 | ->leftJoin('e.tags', 't') | |
128 | ->andWhere('t.id is null'); | |
b6520f0b NL |
129 | } |
130 | ||
3b815d2d | 131 | /** |
4346a860 | 132 | * Find Entries. |
3b815d2d | 133 | * |
2a94b1d1 NL |
134 | * @param int $userId |
135 | * @param bool $isArchived | |
136 | * @param bool $isStarred | |
1112e547 | 137 | * @param bool $isPublic |
2a94b1d1 NL |
138 | * @param string $sort |
139 | * @param string $order | |
8cb869ea TC |
140 | * @param int $since |
141 | * @param string $tags | |
3b815d2d | 142 | * |
52b84c11 | 143 | * @return Pagerfanta |
3b815d2d | 144 | */ |
1112e547 | 145 | public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '') |
a8c90c5c NL |
146 | { |
147 | $qb = $this->createQueryBuilder('e') | |
28803f10 | 148 | ->leftJoin('e.tags', 't') |
7c04b739 | 149 | ->where('e.user = :userId')->setParameter('userId', $userId); |
6e334aba | 150 | |
3b815d2d | 151 | if (null !== $isArchived) { |
1112e547 | 152 | $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); |
6e334aba NL |
153 | } |
154 | ||
3b815d2d | 155 | if (null !== $isStarred) { |
1112e547 JB |
156 | $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred); |
157 | } | |
158 | ||
159 | if (null !== $isPublic) { | |
f808b016 | 160 | $qb->andWhere('e.uid IS ' . (true === $isPublic ? 'NOT' : '') . ' NULL'); |
6e334aba NL |
161 | } |
162 | ||
c3f8b428 | 163 | if ($since > 0) { |
e5fb89e5 | 164 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); |
4f0558a0 TC |
165 | } |
166 | ||
2a1ceb67 | 167 | if (\is_string($tags) && '' !== $tags) { |
7c04b739 JB |
168 | foreach (explode(',', $tags) as $i => $tag) { |
169 | $entryAlias = 'e' . $i; | |
170 | $tagAlias = 't' . $i; | |
171 | ||
33264c2d | 172 | // Complexe queries to ensure multiple tags are associated to an entry |
7c04b739 JB |
173 | // https://stackoverflow.com/a/6638146/569101 |
174 | $qb->andWhere($qb->expr()->in( | |
175 | 'e.id', | |
176 | $this->createQueryBuilder($entryAlias) | |
177 | ->select($entryAlias . '.id') | |
178 | ->leftJoin($entryAlias . '.tags', $tagAlias) | |
179 | ->where($tagAlias . '.label = :label' . $i) | |
180 | ->getDQL() | |
181 | )); | |
182 | ||
183 | // bound parameter to the main query builder | |
184 | $qb->setParameter('label' . $i, $tag); | |
28803f10 | 185 | } |
e5fb89e5 TC |
186 | } |
187 | ||
bc782eaa | 188 | if ('created' === $sort) { |
2385f891 | 189 | $qb->orderBy('e.id', $order); |
bc782eaa NL |
190 | } elseif ('updated' === $sort) { |
191 | $qb->orderBy('e.updatedAt', $order); | |
7c0d6826 | 192 | } elseif ('archived' === $sort) { |
0e70e812 | 193 | $qb->orderBy('e.archivedAt', $order); |
bc782eaa NL |
194 | } |
195 | ||
5a5da369 | 196 | $pagerAdapter = new DoctrineORMAdapter($qb, true, false); |
bcf53ab7 WD |
197 | |
198 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 199 | } |
46bbd8d3 | 200 | |
a36737f4 NL |
201 | /** |
202 | * Fetch an entry with a tag. Only used for tests. | |
203 | * | |
5c072d2b NL |
204 | * @param int $userId |
205 | * | |
52b84c11 | 206 | * @return array |
a36737f4 | 207 | */ |
092ca707 | 208 | public function findOneWithTags($userId) |
46bbd8d3 NL |
209 | { |
210 | $qb = $this->createQueryBuilder('e') | |
211 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
212 | ->innerJoin('e.user', 'u') |
213 | ->addSelect('t', 'u') | |
7c04b739 | 214 | ->where('e.user = :userId')->setParameter('userId', $userId) |
0ca374e6 | 215 | ; |
092ca707 | 216 | |
0ca374e6 | 217 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 218 | } |
d4ebe5c5 JB |
219 | |
220 | /** | |
221 | * Find distinct language for a given user. | |
222 | * Used to build the filter language list. | |
223 | * | |
224 | * @param int $userId User id | |
225 | * | |
226 | * @return array | |
227 | */ | |
228 | public function findDistinctLanguageByUser($userId) | |
229 | { | |
230 | $results = $this->createQueryBuilder('e') | |
231 | ->select('e.language') | |
232 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
233 | ->andWhere('e.language IS NOT NULL') | |
234 | ->groupBy('e.language') | |
235 | ->orderBy('e.language', ' ASC') | |
236 | ->getQuery() | |
237 | ->getResult(); | |
238 | ||
4094ea47 | 239 | $languages = []; |
d4ebe5c5 JB |
240 | foreach ($results as $result) { |
241 | $languages[$result['language']] = $result['language']; | |
242 | } | |
243 | ||
244 | return $languages; | |
245 | } | |
159986c4 | 246 | |
159986c4 | 247 | /** |
cfb28c9d | 248 | * Used only in test case to get the right entry associated to the right user. |
159986c4 | 249 | * |
cfb28c9d | 250 | * @param string $username |
159986c4 JB |
251 | * |
252 | * @return Entry | |
253 | */ | |
254 | public function findOneByUsernameAndNotArchived($username) | |
255 | { | |
256 | return $this->createQueryBuilder('e') | |
257 | ->leftJoin('e.user', 'u') | |
258 | ->where('u.username = :username')->setParameter('username', $username) | |
259 | ->andWhere('e.isArchived = false') | |
260 | ->setMaxResults(1) | |
261 | ->getQuery() | |
262 | ->getSingleResult(); | |
263 | } | |
fc732227 JB |
264 | |
265 | /** | |
266 | * Remove a tag from all user entries. | |
4059a061 JB |
267 | * |
268 | * 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 |
269 | * 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: |
270 | * | |
271 | * 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 |
272 | * |
273 | * @param int $userId | |
274 | * @param Tag $tag | |
275 | */ | |
276 | public function removeTag($userId, Tag $tag) | |
277 | { | |
b7c5fda5 | 278 | $entries = $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
279 | ->innerJoin('e.tags', 't') |
280 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | |
281 | ->getQuery() | |
282 | ->getResult(); | |
283 | ||
284 | foreach ($entries as $entry) { | |
285 | $entry->removeTag($tag); | |
286 | } | |
287 | ||
288 | $this->getEntityManager()->flush(); | |
4059a061 JB |
289 | } |
290 | ||
4da01f49 | 291 | /** |
9bf83f1f | 292 | * Remove tags from all user entries. |
4da01f49 | 293 | * |
9bf83f1f | 294 | * @param int $userId |
4da01f49 TC |
295 | * @param Array<Tag> $tags |
296 | */ | |
9bf83f1f TC |
297 | public function removeTags($userId, $tags) |
298 | { | |
4da01f49 TC |
299 | foreach ($tags as $tag) { |
300 | $this->removeTag($userId, $tag); | |
301 | } | |
302 | } | |
303 | ||
4059a061 JB |
304 | /** |
305 | * Find all entries that are attached to a give tag id. | |
306 | * | |
307 | * @param int $userId | |
308 | * @param int $tagId | |
309 | * | |
310 | * @return array | |
311 | */ | |
312 | public function findAllByTagId($userId, $tagId) | |
313 | { | |
b7c5fda5 | 314 | return $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
315 | ->innerJoin('e.tags', 't') |
316 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | |
317 | ->getQuery() | |
318 | ->getResult(); | |
fc732227 | 319 | } |
303768df NL |
320 | |
321 | /** | |
322 | * Find an entry by its url and its owner. | |
5a4bbcc9 | 323 | * If it exists, return the entry otherwise return false. |
303768df NL |
324 | * |
325 | * @param $url | |
326 | * @param $userId | |
327 | * | |
4094ea47 | 328 | * @return Entry|bool |
303768df | 329 | */ |
78833672 | 330 | public function findByUrlAndUserId($url, $userId) |
303768df | 331 | { |
5a4bbcc9 | 332 | $res = $this->createQueryBuilder('e') |
19ca0b2f | 333 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
303768df NL |
334 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
335 | ->getQuery() | |
336 | ->getResult(); | |
5a4bbcc9 | 337 | |
2a1ceb67 | 338 | if (\count($res)) { |
b4b592a0 | 339 | return current($res); |
5a4bbcc9 JB |
340 | } |
341 | ||
342 | return false; | |
303768df | 343 | } |
5c072d2b NL |
344 | |
345 | /** | |
346 | * Count all entries for a user. | |
347 | * | |
348 | * @param int $userId | |
349 | * | |
e678c475 | 350 | * @return int |
5c072d2b | 351 | */ |
273b6f06 | 352 | public function countAllEntriesByUser($userId) |
5c072d2b NL |
353 | { |
354 | $qb = $this->createQueryBuilder('e') | |
355 | ->select('count(e)') | |
7c04b739 JB |
356 | ->where('e.user = :userId')->setParameter('userId', $userId) |
357 | ; | |
358 | ||
359 | return (int) $qb->getQuery()->getSingleScalarResult(); | |
360 | } | |
361 | ||
191564b7 JB |
362 | /** |
363 | * Remove all entries for a user id. | |
8c61fd12 | 364 | * Used when a user want to reset all informations. |
191564b7 | 365 | * |
8c61fd12 | 366 | * @param int $userId |
191564b7 JB |
367 | */ |
368 | public function removeAllByUserId($userId) | |
369 | { | |
370 | $this->getEntityManager() | |
b0de88f7 JB |
371 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') |
372 | ->setParameter('userId', $userId) | |
191564b7 JB |
373 | ->execute(); |
374 | } | |
6da1aebc TC |
375 | |
376 | public function removeArchivedByUserId($userId) | |
377 | { | |
378 | $this->getEntityManager() | |
379 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE') | |
380 | ->setParameter('userId', $userId) | |
381 | ->execute(); | |
382 | } | |
e2f3800c TC |
383 | |
384 | /** | |
385 | * Get id and url from all entries | |
386 | * Used for the clean-duplicates command. | |
387 | */ | |
dbf1188c | 388 | public function findAllEntriesIdAndUrlByUserId($userId) |
e2f3800c TC |
389 | { |
390 | $qb = $this->createQueryBuilder('e') | |
391 | ->select('e.id, e.url') | |
392 | ->where('e.user = :userid')->setParameter(':userid', $userId); | |
393 | ||
394 | return $qb->getQuery()->getArrayResult(); | |
395 | } | |
d09fe4d2 | 396 | |
511f1ce1 NH |
397 | /** |
398 | * @param int $userId | |
399 | * | |
400 | * @return array | |
401 | */ | |
215409a8 | 402 | public function findAllEntriesIdByUserId($userId = null) |
511f1ce1 NH |
403 | { |
404 | $qb = $this->createQueryBuilder('e') | |
405 | ->select('e.id'); | |
406 | ||
407 | if (null !== $userId) { | |
408 | $qb->where('e.user = :userid')->setParameter(':userid', $userId); | |
409 | } | |
410 | ||
411 | return $qb->getQuery()->getArrayResult(); | |
412 | } | |
413 | ||
d09fe4d2 NL |
414 | /** |
415 | * Find all entries by url and owner. | |
416 | * | |
417 | * @param $url | |
418 | * @param $userId | |
419 | * | |
420 | * @return array | |
421 | */ | |
422 | public function findAllByUrlAndUserId($url, $userId) | |
423 | { | |
89f108b4 | 424 | return $this->createQueryBuilder('e') |
d09fe4d2 NL |
425 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
426 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | |
427 | ->getQuery() | |
428 | ->getResult(); | |
d09fe4d2 | 429 | } |
f808b016 JB |
430 | |
431 | /** | |
b7c5fda5 KD |
432 | * Return a query builder to be used by other getBuilderFor* method. |
433 | * | |
b8115ff4 | 434 | * @param int $userId |
b7c5fda5 KD |
435 | * |
436 | * @return QueryBuilder | |
437 | */ | |
438 | private function getQueryBuilderByUser($userId) | |
439 | { | |
440 | return $this->createQueryBuilder('e') | |
441 | ->andWhere('e.user = :userId')->setParameter('userId', $userId); | |
442 | } | |
443 | ||
444 | /** | |
445 | * Return a sorted query builder to be used by other getBuilderFor* method. | |
f808b016 | 446 | * |
a991c46e F |
447 | * @param int $userId |
448 | * @param string $sortBy | |
449 | * @param string $direction | |
f808b016 JB |
450 | * |
451 | * @return QueryBuilder | |
452 | */ | |
b7c5fda5 | 453 | private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') |
f808b016 | 454 | { |
17476f4d | 455 | return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId), $sortBy, $direction); |
b7c5fda5 KD |
456 | } |
457 | ||
458 | /** | |
b8115ff4 KD |
459 | * Return the given QueryBuilder with an orderBy() call. |
460 | * | |
b7c5fda5 | 461 | * @param QueryBuilder $qb |
b8115ff4 KD |
462 | * @param string $sortBy |
463 | * @param string $direction | |
b7c5fda5 KD |
464 | * |
465 | * @return QueryBuilder | |
466 | */ | |
467 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') | |
468 | { | |
469 | return $qb | |
a991c46e | 470 | ->orderBy(sprintf('e.%s', $sortBy), $direction); |
f808b016 | 471 | } |
9d50517c | 472 | } |