]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Repository; |
9d50517c | 4 | |
9d50517c | 5 | use Doctrine\ORM\EntityRepository; |
091bafeb | 6 | use Doctrine\ORM\NoResultException; |
52b84c11 | 7 | use Doctrine\ORM\QueryBuilder; |
bcf53ab7 WD |
8 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
9 | use Pagerfanta\Pagerfanta; | |
52b84c11 | 10 | use Wallabag\CoreBundle\Entity\Entry; |
fc732227 | 11 | use Wallabag\CoreBundle\Entity\Tag; |
9d50517c | 12 | |
be463487 | 13 | class EntryRepository extends EntityRepository |
9d50517c | 14 | { |
2b7a4889 NL |
15 | /** |
16 | * Retrieves all entries for a user. | |
17 | * | |
18 | * @param int $userId | |
19 | * | |
20 | * @return QueryBuilder | |
21 | */ | |
22 | public function getBuilderForAllByUser($userId) | |
23 | { | |
24 | return $this | |
b7c5fda5 | 25 | ->getSortedQueryBuilderByUser($userId) |
2b7a4889 NL |
26 | ; |
27 | } | |
28 | ||
0ab7404f JB |
29 | /** |
30 | * Retrieves unread entries for a user. | |
31 | * | |
32 | * @param int $userId | |
33 | * | |
34 | * @return QueryBuilder | |
35 | */ | |
36 | public function getBuilderForUnreadByUser($userId) | |
37 | { | |
38 | return $this | |
b7c5fda5 | 39 | ->getSortedQueryBuilderByUser($userId) |
0ab7404f JB |
40 | ->andWhere('e.isArchived = false') |
41 | ; | |
9d50517c | 42 | } |
bd9f0815 | 43 | |
b84a8055 | 44 | /** |
4346a860 | 45 | * Retrieves read entries for a user. |
b84a8055 | 46 | * |
3b815d2d | 47 | * @param int $userId |
3b815d2d | 48 | * |
26864574 | 49 | * @return QueryBuilder |
b84a8055 | 50 | */ |
0ab7404f | 51 | public function getBuilderForArchiveByUser($userId) |
bd9f0815 | 52 | { |
0ab7404f | 53 | return $this |
9007fe00 | 54 | ->getSortedQueryBuilderByUser($userId, 'archivedAt', 'desc') |
0ab7404f JB |
55 | ->andWhere('e.isArchived = true') |
56 | ; | |
bd9f0815 NL |
57 | } |
58 | ||
b84a8055 | 59 | /** |
4346a860 | 60 | * Retrieves starred entries for a user. |
b84a8055 | 61 | * |
3b815d2d | 62 | * @param int $userId |
3b815d2d | 63 | * |
26864574 | 64 | * @return QueryBuilder |
b84a8055 | 65 | */ |
0ab7404f | 66 | public function getBuilderForStarredByUser($userId) |
bd9f0815 | 67 | { |
0ab7404f | 68 | return $this |
b7c5fda5 | 69 | ->getSortedQueryBuilderByUser($userId, 'starredAt', 'desc') |
0ab7404f JB |
70 | ->andWhere('e.isStarred = true') |
71 | ; | |
bd9f0815 | 72 | } |
a8c90c5c | 73 | |
ee122a75 NL |
74 | /** |
75 | * Retrieves entries filtered with a search term for a user. | |
76 | * | |
77 | * @param int $userId | |
78 | * @param string $term | |
52b84c11 | 79 | * @param string $currentRoute |
ee122a75 NL |
80 | * |
81 | * @return QueryBuilder | |
82 | */ | |
49b042df | 83 | public function getBuilderForSearchByUser($userId, $term, $currentRoute) |
ee122a75 | 84 | { |
49b042df | 85 | $qb = $this |
b7c5fda5 | 86 | ->getSortedQueryBuilderByUser($userId); |
49b042df NL |
87 | |
88 | if ('starred' === $currentRoute) { | |
89 | $qb->andWhere('e.isStarred = true'); | |
90 | } elseif ('unread' === $currentRoute) { | |
91 | $qb->andWhere('e.isArchived = false'); | |
92 | } elseif ('archive' === $currentRoute) { | |
93 | $qb->andWhere('e.isArchived = true'); | |
94 | } | |
95 | ||
eac09b48 | 96 | // We lower() all parts here because PostgreSQL 'LIKE' verb is case-sensitive |
49b042df | 97 | $qb |
f808b016 | 98 | ->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 | 99 | ->leftJoin('e.tags', 't') |
32f455c1 | 100 | ->groupBy('e.id'); |
49b042df NL |
101 | |
102 | return $qb; | |
ee122a75 NL |
103 | } |
104 | ||
b6520f0b | 105 | /** |
06366972 | 106 | * Retrieve a sorted list of untagged entries for a user. |
b6520f0b NL |
107 | * |
108 | * @param int $userId | |
109 | * | |
110 | * @return QueryBuilder | |
111 | */ | |
112 | public function getBuilderForUntaggedByUser($userId) | |
113 | { | |
09ef25c3 | 114 | return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); |
06366972 KD |
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 | */ |
78e3fafa | 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 | ||
78e3fafa JB |
188 | if (!\in_array(strtolower($order), ['asc', 'desc'], true)) { |
189 | throw new \Exception('Order "' . $order . '" parameter is wrong, allowed: asc or desc'); | |
190 | } | |
191 | ||
bc782eaa | 192 | if ('created' === $sort) { |
2385f891 | 193 | $qb->orderBy('e.id', $order); |
bc782eaa NL |
194 | } elseif ('updated' === $sort) { |
195 | $qb->orderBy('e.updatedAt', $order); | |
7c0d6826 | 196 | } elseif ('archived' === $sort) { |
0e70e812 | 197 | $qb->orderBy('e.archivedAt', $order); |
bc782eaa NL |
198 | } |
199 | ||
5a5da369 | 200 | $pagerAdapter = new DoctrineORMAdapter($qb, true, false); |
bcf53ab7 WD |
201 | |
202 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 203 | } |
46bbd8d3 | 204 | |
a36737f4 NL |
205 | /** |
206 | * Fetch an entry with a tag. Only used for tests. | |
207 | * | |
5c072d2b NL |
208 | * @param int $userId |
209 | * | |
52b84c11 | 210 | * @return array |
a36737f4 | 211 | */ |
092ca707 | 212 | public function findOneWithTags($userId) |
46bbd8d3 NL |
213 | { |
214 | $qb = $this->createQueryBuilder('e') | |
215 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
216 | ->innerJoin('e.user', 'u') |
217 | ->addSelect('t', 'u') | |
7c04b739 | 218 | ->where('e.user = :userId')->setParameter('userId', $userId) |
0ca374e6 | 219 | ; |
092ca707 | 220 | |
0ca374e6 | 221 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 222 | } |
d4ebe5c5 JB |
223 | |
224 | /** | |
225 | * Find distinct language for a given user. | |
226 | * Used to build the filter language list. | |
227 | * | |
228 | * @param int $userId User id | |
229 | * | |
230 | * @return array | |
231 | */ | |
232 | public function findDistinctLanguageByUser($userId) | |
233 | { | |
234 | $results = $this->createQueryBuilder('e') | |
235 | ->select('e.language') | |
236 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
237 | ->andWhere('e.language IS NOT NULL') | |
238 | ->groupBy('e.language') | |
239 | ->orderBy('e.language', ' ASC') | |
240 | ->getQuery() | |
241 | ->getResult(); | |
242 | ||
4094ea47 | 243 | $languages = []; |
d4ebe5c5 JB |
244 | foreach ($results as $result) { |
245 | $languages[$result['language']] = $result['language']; | |
246 | } | |
247 | ||
248 | return $languages; | |
249 | } | |
159986c4 | 250 | |
159986c4 | 251 | /** |
cfb28c9d | 252 | * Used only in test case to get the right entry associated to the right user. |
159986c4 | 253 | * |
cfb28c9d | 254 | * @param string $username |
159986c4 JB |
255 | * |
256 | * @return Entry | |
257 | */ | |
258 | public function findOneByUsernameAndNotArchived($username) | |
259 | { | |
260 | return $this->createQueryBuilder('e') | |
261 | ->leftJoin('e.user', 'u') | |
262 | ->where('u.username = :username')->setParameter('username', $username) | |
263 | ->andWhere('e.isArchived = false') | |
264 | ->setMaxResults(1) | |
265 | ->getQuery() | |
266 | ->getSingleResult(); | |
267 | } | |
fc732227 JB |
268 | |
269 | /** | |
270 | * Remove a tag from all user entries. | |
4059a061 JB |
271 | * |
272 | * 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 |
273 | * 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: |
274 | * | |
275 | * 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 |
276 | * |
277 | * @param int $userId | |
278 | * @param Tag $tag | |
279 | */ | |
280 | public function removeTag($userId, Tag $tag) | |
281 | { | |
b7c5fda5 | 282 | $entries = $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
283 | ->innerJoin('e.tags', 't') |
284 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | |
285 | ->getQuery() | |
286 | ->getResult(); | |
287 | ||
288 | foreach ($entries as $entry) { | |
289 | $entry->removeTag($tag); | |
290 | } | |
291 | ||
292 | $this->getEntityManager()->flush(); | |
4059a061 JB |
293 | } |
294 | ||
4da01f49 | 295 | /** |
9bf83f1f | 296 | * Remove tags from all user entries. |
4da01f49 | 297 | * |
9bf83f1f | 298 | * @param int $userId |
4da01f49 TC |
299 | * @param Array<Tag> $tags |
300 | */ | |
9bf83f1f TC |
301 | public function removeTags($userId, $tags) |
302 | { | |
4da01f49 TC |
303 | foreach ($tags as $tag) { |
304 | $this->removeTag($userId, $tag); | |
305 | } | |
306 | } | |
307 | ||
4059a061 JB |
308 | /** |
309 | * Find all entries that are attached to a give tag id. | |
310 | * | |
311 | * @param int $userId | |
312 | * @param int $tagId | |
313 | * | |
314 | * @return array | |
315 | */ | |
316 | public function findAllByTagId($userId, $tagId) | |
317 | { | |
b7c5fda5 | 318 | return $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
319 | ->innerJoin('e.tags', 't') |
320 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | |
321 | ->getQuery() | |
322 | ->getResult(); | |
fc732227 | 323 | } |
303768df NL |
324 | |
325 | /** | |
326 | * Find an entry by its url and its owner. | |
5a4bbcc9 | 327 | * If it exists, return the entry otherwise return false. |
303768df | 328 | * |
50f35f0d JB |
329 | * @param string $url |
330 | * @param int $userId | |
303768df | 331 | * |
4094ea47 | 332 | * @return Entry|bool |
303768df | 333 | */ |
78833672 | 334 | public function findByUrlAndUserId($url, $userId) |
303768df | 335 | { |
5a4bbcc9 | 336 | $res = $this->createQueryBuilder('e') |
19ca0b2f | 337 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
303768df NL |
338 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
339 | ->getQuery() | |
340 | ->getResult(); | |
5a4bbcc9 | 341 | |
2a1ceb67 | 342 | if (\count($res)) { |
b4b592a0 | 343 | return current($res); |
5a4bbcc9 JB |
344 | } |
345 | ||
346 | return false; | |
303768df | 347 | } |
5c072d2b | 348 | |
9c2b2aae JB |
349 | /** |
350 | * Find an entry by its hashed url and its owner. | |
351 | * If it exists, return the entry otherwise return false. | |
352 | * | |
c579ce23 JB |
353 | * @param string $hashedUrl Url hashed using sha1 |
354 | * @param int $userId | |
9c2b2aae JB |
355 | * |
356 | * @return Entry|bool | |
357 | */ | |
358 | public function findByHashedUrlAndUserId($hashedUrl, $userId) | |
359 | { | |
360 | $res = $this->createQueryBuilder('e') | |
c579ce23 | 361 | ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) |
9c2b2aae JB |
362 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
363 | ->getQuery() | |
364 | ->getResult(); | |
365 | ||
366 | if (\count($res)) { | |
367 | return current($res); | |
368 | } | |
369 | ||
370 | return false; | |
371 | } | |
372 | ||
5c072d2b NL |
373 | /** |
374 | * Count all entries for a user. | |
375 | * | |
376 | * @param int $userId | |
377 | * | |
e678c475 | 378 | * @return int |
5c072d2b | 379 | */ |
273b6f06 | 380 | public function countAllEntriesByUser($userId) |
5c072d2b NL |
381 | { |
382 | $qb = $this->createQueryBuilder('e') | |
383 | ->select('count(e)') | |
7c04b739 JB |
384 | ->where('e.user = :userId')->setParameter('userId', $userId) |
385 | ; | |
386 | ||
387 | return (int) $qb->getQuery()->getSingleScalarResult(); | |
388 | } | |
389 | ||
191564b7 JB |
390 | /** |
391 | * Remove all entries for a user id. | |
8c61fd12 | 392 | * Used when a user want to reset all informations. |
191564b7 | 393 | * |
8c61fd12 | 394 | * @param int $userId |
191564b7 JB |
395 | */ |
396 | public function removeAllByUserId($userId) | |
397 | { | |
398 | $this->getEntityManager() | |
b0de88f7 JB |
399 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') |
400 | ->setParameter('userId', $userId) | |
191564b7 JB |
401 | ->execute(); |
402 | } | |
6da1aebc TC |
403 | |
404 | public function removeArchivedByUserId($userId) | |
405 | { | |
406 | $this->getEntityManager() | |
407 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE') | |
408 | ->setParameter('userId', $userId) | |
409 | ->execute(); | |
410 | } | |
e2f3800c TC |
411 | |
412 | /** | |
413 | * Get id and url from all entries | |
414 | * Used for the clean-duplicates command. | |
415 | */ | |
dbf1188c | 416 | public function findAllEntriesIdAndUrlByUserId($userId) |
e2f3800c TC |
417 | { |
418 | $qb = $this->createQueryBuilder('e') | |
419 | ->select('e.id, e.url') | |
420 | ->where('e.user = :userid')->setParameter(':userid', $userId); | |
421 | ||
422 | return $qb->getQuery()->getArrayResult(); | |
423 | } | |
d09fe4d2 | 424 | |
511f1ce1 NH |
425 | /** |
426 | * @param int $userId | |
427 | * | |
428 | * @return array | |
429 | */ | |
215409a8 | 430 | public function findAllEntriesIdByUserId($userId = null) |
511f1ce1 NH |
431 | { |
432 | $qb = $this->createQueryBuilder('e') | |
433 | ->select('e.id'); | |
434 | ||
435 | if (null !== $userId) { | |
436 | $qb->where('e.user = :userid')->setParameter(':userid', $userId); | |
437 | } | |
438 | ||
439 | return $qb->getQuery()->getArrayResult(); | |
440 | } | |
441 | ||
d09fe4d2 NL |
442 | /** |
443 | * Find all entries by url and owner. | |
444 | * | |
50f35f0d JB |
445 | * @param string $url |
446 | * @param int $userId | |
d09fe4d2 NL |
447 | * |
448 | * @return array | |
449 | */ | |
450 | public function findAllByUrlAndUserId($url, $userId) | |
451 | { | |
89f108b4 | 452 | return $this->createQueryBuilder('e') |
d09fe4d2 NL |
453 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
454 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | |
455 | ->getQuery() | |
456 | ->getResult(); | |
d09fe4d2 | 457 | } |
f808b016 | 458 | |
09ef25c3 NL |
459 | /** |
460 | * Returns a random entry, filtering by status. | |
461 | * | |
50f35f0d JB |
462 | * @param int $userId |
463 | * @param string $type Can be unread, archive, starred, etc | |
09ef25c3 | 464 | * |
091bafeb | 465 | * @throws NoResultException |
062fad43 | 466 | * |
09ef25c3 NL |
467 | * @return Entry |
468 | */ | |
9a57653a | 469 | public function getRandomEntry($userId, $type = '') |
09ef25c3 | 470 | { |
062fad43 | 471 | $qb = $this->getQueryBuilderByUser($userId) |
50f35f0d | 472 | ->select('e.id'); |
09ef25c3 | 473 | |
9a57653a JB |
474 | switch ($type) { |
475 | case 'unread': | |
476 | $qb->andWhere('e.isArchived = false'); | |
477 | break; | |
478 | case 'archive': | |
479 | $qb->andWhere('e.isArchived = true'); | |
480 | break; | |
481 | case 'starred': | |
482 | $qb->andWhere('e.isStarred = true'); | |
483 | break; | |
484 | case 'untagged': | |
485 | $qb->leftJoin('e.tags', 't'); | |
486 | $qb->andWhere('t.id is null'); | |
487 | break; | |
f85d220c JB |
488 | } |
489 | ||
50f35f0d | 490 | $ids = $qb->getQuery()->getArrayResult(); |
062fad43 | 491 | |
091bafeb JB |
492 | if (empty($ids)) { |
493 | throw new NoResultException(); | |
494 | } | |
495 | ||
50f35f0d JB |
496 | // random select one in the list |
497 | $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id']; | |
498 | ||
499 | return $this->find($randomId); | |
09ef25c3 NL |
500 | } |
501 | ||
f808b016 | 502 | /** |
b7c5fda5 KD |
503 | * Return a query builder to be used by other getBuilderFor* method. |
504 | * | |
b8115ff4 | 505 | * @param int $userId |
b7c5fda5 KD |
506 | * |
507 | * @return QueryBuilder | |
508 | */ | |
509 | private function getQueryBuilderByUser($userId) | |
510 | { | |
511 | return $this->createQueryBuilder('e') | |
512 | ->andWhere('e.user = :userId')->setParameter('userId', $userId); | |
513 | } | |
514 | ||
515 | /** | |
516 | * Return a sorted query builder to be used by other getBuilderFor* method. | |
f808b016 | 517 | * |
a991c46e F |
518 | * @param int $userId |
519 | * @param string $sortBy | |
520 | * @param string $direction | |
f808b016 JB |
521 | * |
522 | * @return QueryBuilder | |
523 | */ | |
b7c5fda5 | 524 | private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') |
f808b016 | 525 | { |
17476f4d | 526 | return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId), $sortBy, $direction); |
b7c5fda5 KD |
527 | } |
528 | ||
529 | /** | |
b8115ff4 KD |
530 | * Return the given QueryBuilder with an orderBy() call. |
531 | * | |
b7c5fda5 | 532 | * @param QueryBuilder $qb |
b8115ff4 KD |
533 | * @param string $sortBy |
534 | * @param string $direction | |
b7c5fda5 KD |
535 | * |
536 | * @return QueryBuilder | |
537 | */ | |
538 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') | |
539 | { | |
09ef25c3 | 540 | return $qb->orderBy(sprintf('e.%s', $sortBy), $direction); |
f808b016 | 541 | } |
9d50517c | 542 | } |