]>
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 |
b7c5fda5 | 53 | ->getSortedQueryBuilderByUser($userId) |
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); | |
192 | } | |
193 | ||
5a5da369 | 194 | $pagerAdapter = new DoctrineORMAdapter($qb, true, false); |
bcf53ab7 WD |
195 | |
196 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 197 | } |
46bbd8d3 | 198 | |
a36737f4 NL |
199 | /** |
200 | * Fetch an entry with a tag. Only used for tests. | |
201 | * | |
5c072d2b NL |
202 | * @param int $userId |
203 | * | |
52b84c11 | 204 | * @return array |
a36737f4 | 205 | */ |
092ca707 | 206 | public function findOneWithTags($userId) |
46bbd8d3 NL |
207 | { |
208 | $qb = $this->createQueryBuilder('e') | |
209 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
210 | ->innerJoin('e.user', 'u') |
211 | ->addSelect('t', 'u') | |
7c04b739 | 212 | ->where('e.user = :userId')->setParameter('userId', $userId) |
0ca374e6 | 213 | ; |
092ca707 | 214 | |
0ca374e6 | 215 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 216 | } |
d4ebe5c5 JB |
217 | |
218 | /** | |
219 | * Find distinct language for a given user. | |
220 | * Used to build the filter language list. | |
221 | * | |
222 | * @param int $userId User id | |
223 | * | |
224 | * @return array | |
225 | */ | |
226 | public function findDistinctLanguageByUser($userId) | |
227 | { | |
228 | $results = $this->createQueryBuilder('e') | |
229 | ->select('e.language') | |
230 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
231 | ->andWhere('e.language IS NOT NULL') | |
232 | ->groupBy('e.language') | |
233 | ->orderBy('e.language', ' ASC') | |
234 | ->getQuery() | |
235 | ->getResult(); | |
236 | ||
4094ea47 | 237 | $languages = []; |
d4ebe5c5 JB |
238 | foreach ($results as $result) { |
239 | $languages[$result['language']] = $result['language']; | |
240 | } | |
241 | ||
242 | return $languages; | |
243 | } | |
159986c4 | 244 | |
159986c4 | 245 | /** |
cfb28c9d | 246 | * Used only in test case to get the right entry associated to the right user. |
159986c4 | 247 | * |
cfb28c9d | 248 | * @param string $username |
159986c4 JB |
249 | * |
250 | * @return Entry | |
251 | */ | |
252 | public function findOneByUsernameAndNotArchived($username) | |
253 | { | |
254 | return $this->createQueryBuilder('e') | |
255 | ->leftJoin('e.user', 'u') | |
256 | ->where('u.username = :username')->setParameter('username', $username) | |
257 | ->andWhere('e.isArchived = false') | |
258 | ->setMaxResults(1) | |
259 | ->getQuery() | |
260 | ->getSingleResult(); | |
261 | } | |
fc732227 JB |
262 | |
263 | /** | |
264 | * Remove a tag from all user entries. | |
4059a061 JB |
265 | * |
266 | * 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 |
267 | * 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: |
268 | * | |
269 | * 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 |
270 | * |
271 | * @param int $userId | |
272 | * @param Tag $tag | |
273 | */ | |
274 | public function removeTag($userId, Tag $tag) | |
275 | { | |
b7c5fda5 | 276 | $entries = $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
277 | ->innerJoin('e.tags', 't') |
278 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | |
279 | ->getQuery() | |
280 | ->getResult(); | |
281 | ||
282 | foreach ($entries as $entry) { | |
283 | $entry->removeTag($tag); | |
284 | } | |
285 | ||
286 | $this->getEntityManager()->flush(); | |
4059a061 JB |
287 | } |
288 | ||
4da01f49 | 289 | /** |
9bf83f1f | 290 | * Remove tags from all user entries. |
4da01f49 | 291 | * |
9bf83f1f | 292 | * @param int $userId |
4da01f49 TC |
293 | * @param Array<Tag> $tags |
294 | */ | |
9bf83f1f TC |
295 | public function removeTags($userId, $tags) |
296 | { | |
4da01f49 TC |
297 | foreach ($tags as $tag) { |
298 | $this->removeTag($userId, $tag); | |
299 | } | |
300 | } | |
301 | ||
4059a061 JB |
302 | /** |
303 | * Find all entries that are attached to a give tag id. | |
304 | * | |
305 | * @param int $userId | |
306 | * @param int $tagId | |
307 | * | |
308 | * @return array | |
309 | */ | |
310 | public function findAllByTagId($userId, $tagId) | |
311 | { | |
b7c5fda5 | 312 | return $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
313 | ->innerJoin('e.tags', 't') |
314 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | |
315 | ->getQuery() | |
316 | ->getResult(); | |
fc732227 | 317 | } |
303768df NL |
318 | |
319 | /** | |
320 | * Find an entry by its url and its owner. | |
5a4bbcc9 | 321 | * If it exists, return the entry otherwise return false. |
303768df NL |
322 | * |
323 | * @param $url | |
324 | * @param $userId | |
325 | * | |
4094ea47 | 326 | * @return Entry|bool |
303768df | 327 | */ |
78833672 | 328 | public function findByUrlAndUserId($url, $userId) |
303768df | 329 | { |
5a4bbcc9 | 330 | $res = $this->createQueryBuilder('e') |
19ca0b2f | 331 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
303768df NL |
332 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
333 | ->getQuery() | |
334 | ->getResult(); | |
5a4bbcc9 | 335 | |
2a1ceb67 | 336 | if (\count($res)) { |
b4b592a0 | 337 | return current($res); |
5a4bbcc9 JB |
338 | } |
339 | ||
340 | return false; | |
303768df | 341 | } |
5c072d2b NL |
342 | |
343 | /** | |
344 | * Count all entries for a user. | |
345 | * | |
346 | * @param int $userId | |
347 | * | |
e678c475 | 348 | * @return int |
5c072d2b | 349 | */ |
273b6f06 | 350 | public function countAllEntriesByUser($userId) |
5c072d2b NL |
351 | { |
352 | $qb = $this->createQueryBuilder('e') | |
353 | ->select('count(e)') | |
7c04b739 JB |
354 | ->where('e.user = :userId')->setParameter('userId', $userId) |
355 | ; | |
356 | ||
357 | return (int) $qb->getQuery()->getSingleScalarResult(); | |
358 | } | |
359 | ||
191564b7 JB |
360 | /** |
361 | * Remove all entries for a user id. | |
8c61fd12 | 362 | * Used when a user want to reset all informations. |
191564b7 | 363 | * |
8c61fd12 | 364 | * @param int $userId |
191564b7 JB |
365 | */ |
366 | public function removeAllByUserId($userId) | |
367 | { | |
368 | $this->getEntityManager() | |
b0de88f7 JB |
369 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') |
370 | ->setParameter('userId', $userId) | |
191564b7 JB |
371 | ->execute(); |
372 | } | |
6da1aebc TC |
373 | |
374 | public function removeArchivedByUserId($userId) | |
375 | { | |
376 | $this->getEntityManager() | |
377 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE') | |
378 | ->setParameter('userId', $userId) | |
379 | ->execute(); | |
380 | } | |
e2f3800c TC |
381 | |
382 | /** | |
383 | * Get id and url from all entries | |
384 | * Used for the clean-duplicates command. | |
385 | */ | |
dbf1188c | 386 | public function findAllEntriesIdAndUrlByUserId($userId) |
e2f3800c TC |
387 | { |
388 | $qb = $this->createQueryBuilder('e') | |
389 | ->select('e.id, e.url') | |
390 | ->where('e.user = :userid')->setParameter(':userid', $userId); | |
391 | ||
392 | return $qb->getQuery()->getArrayResult(); | |
393 | } | |
d09fe4d2 | 394 | |
511f1ce1 NH |
395 | /** |
396 | * @param int $userId | |
397 | * | |
398 | * @return array | |
399 | */ | |
215409a8 | 400 | public function findAllEntriesIdByUserId($userId = null) |
511f1ce1 NH |
401 | { |
402 | $qb = $this->createQueryBuilder('e') | |
403 | ->select('e.id'); | |
404 | ||
405 | if (null !== $userId) { | |
406 | $qb->where('e.user = :userid')->setParameter(':userid', $userId); | |
407 | } | |
408 | ||
409 | return $qb->getQuery()->getArrayResult(); | |
410 | } | |
411 | ||
d09fe4d2 NL |
412 | /** |
413 | * Find all entries by url and owner. | |
414 | * | |
415 | * @param $url | |
416 | * @param $userId | |
417 | * | |
418 | * @return array | |
419 | */ | |
420 | public function findAllByUrlAndUserId($url, $userId) | |
421 | { | |
89f108b4 | 422 | return $this->createQueryBuilder('e') |
d09fe4d2 NL |
423 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
424 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | |
425 | ->getQuery() | |
426 | ->getResult(); | |
d09fe4d2 | 427 | } |
f808b016 JB |
428 | |
429 | /** | |
b7c5fda5 KD |
430 | * Return a query builder to be used by other getBuilderFor* method. |
431 | * | |
b8115ff4 | 432 | * @param int $userId |
b7c5fda5 KD |
433 | * |
434 | * @return QueryBuilder | |
435 | */ | |
436 | private function getQueryBuilderByUser($userId) | |
437 | { | |
438 | return $this->createQueryBuilder('e') | |
439 | ->andWhere('e.user = :userId')->setParameter('userId', $userId); | |
440 | } | |
441 | ||
442 | /** | |
443 | * Return a sorted query builder to be used by other getBuilderFor* method. | |
f808b016 | 444 | * |
a991c46e F |
445 | * @param int $userId |
446 | * @param string $sortBy | |
447 | * @param string $direction | |
f808b016 JB |
448 | * |
449 | * @return QueryBuilder | |
450 | */ | |
b7c5fda5 | 451 | private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') |
f808b016 | 452 | { |
17476f4d | 453 | return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId), $sortBy, $direction); |
b7c5fda5 KD |
454 | } |
455 | ||
456 | /** | |
b8115ff4 KD |
457 | * Return the given QueryBuilder with an orderBy() call. |
458 | * | |
b7c5fda5 | 459 | * @param QueryBuilder $qb |
b8115ff4 KD |
460 | * @param string $sortBy |
461 | * @param string $direction | |
b7c5fda5 KD |
462 | * |
463 | * @return QueryBuilder | |
464 | */ | |
465 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') | |
466 | { | |
467 | return $qb | |
a991c46e | 468 | ->orderBy(sprintf('e.%s', $sortBy), $direction); |
f808b016 | 469 | } |
9d50517c | 470 | } |