]>
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 | |
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 | 67 | return $this |
a991c46e | 68 | ->getBuilderByUser($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 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') |
7c04b739 | 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 | ||
fafdf171 | 154 | if (is_string($tags) && '' !== $tags) { |
7c04b739 JB |
155 | foreach (explode(',', $tags) as $i => $tag) { |
156 | $entryAlias = 'e' . $i; | |
157 | $tagAlias = 't' . $i; | |
158 | ||
33264c2d | 159 | // Complexe queries to ensure multiple tags are associated to an entry |
7c04b739 JB |
160 | // https://stackoverflow.com/a/6638146/569101 |
161 | $qb->andWhere($qb->expr()->in( | |
162 | 'e.id', | |
163 | $this->createQueryBuilder($entryAlias) | |
164 | ->select($entryAlias . '.id') | |
165 | ->leftJoin($entryAlias . '.tags', $tagAlias) | |
166 | ->where($tagAlias . '.label = :label' . $i) | |
167 | ->getDQL() | |
168 | )); | |
169 | ||
170 | // bound parameter to the main query builder | |
171 | $qb->setParameter('label' . $i, $tag); | |
28803f10 | 172 | } |
e5fb89e5 TC |
173 | } |
174 | ||
bc782eaa | 175 | if ('created' === $sort) { |
2385f891 | 176 | $qb->orderBy('e.id', $order); |
bc782eaa NL |
177 | } elseif ('updated' === $sort) { |
178 | $qb->orderBy('e.updatedAt', $order); | |
179 | } | |
180 | ||
5a5da369 | 181 | $pagerAdapter = new DoctrineORMAdapter($qb, true, false); |
bcf53ab7 WD |
182 | |
183 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 184 | } |
46bbd8d3 | 185 | |
a36737f4 NL |
186 | /** |
187 | * Fetch an entry with a tag. Only used for tests. | |
188 | * | |
5c072d2b NL |
189 | * @param int $userId |
190 | * | |
52b84c11 | 191 | * @return array |
a36737f4 | 192 | */ |
092ca707 | 193 | public function findOneWithTags($userId) |
46bbd8d3 NL |
194 | { |
195 | $qb = $this->createQueryBuilder('e') | |
196 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
197 | ->innerJoin('e.user', 'u') |
198 | ->addSelect('t', 'u') | |
7c04b739 | 199 | ->where('e.user = :userId')->setParameter('userId', $userId) |
0ca374e6 | 200 | ; |
092ca707 | 201 | |
0ca374e6 | 202 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 203 | } |
d4ebe5c5 JB |
204 | |
205 | /** | |
206 | * Find distinct language for a given user. | |
207 | * Used to build the filter language list. | |
208 | * | |
209 | * @param int $userId User id | |
210 | * | |
211 | * @return array | |
212 | */ | |
213 | public function findDistinctLanguageByUser($userId) | |
214 | { | |
215 | $results = $this->createQueryBuilder('e') | |
216 | ->select('e.language') | |
217 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
218 | ->andWhere('e.language IS NOT NULL') | |
219 | ->groupBy('e.language') | |
220 | ->orderBy('e.language', ' ASC') | |
221 | ->getQuery() | |
222 | ->getResult(); | |
223 | ||
4094ea47 | 224 | $languages = []; |
d4ebe5c5 JB |
225 | foreach ($results as $result) { |
226 | $languages[$result['language']] = $result['language']; | |
227 | } | |
228 | ||
229 | return $languages; | |
230 | } | |
159986c4 | 231 | |
159986c4 | 232 | /** |
cfb28c9d | 233 | * Used only in test case to get the right entry associated to the right user. |
159986c4 | 234 | * |
cfb28c9d | 235 | * @param string $username |
159986c4 JB |
236 | * |
237 | * @return Entry | |
238 | */ | |
239 | public function findOneByUsernameAndNotArchived($username) | |
240 | { | |
241 | return $this->createQueryBuilder('e') | |
242 | ->leftJoin('e.user', 'u') | |
243 | ->where('u.username = :username')->setParameter('username', $username) | |
244 | ->andWhere('e.isArchived = false') | |
245 | ->setMaxResults(1) | |
246 | ->getQuery() | |
247 | ->getSingleResult(); | |
248 | } | |
fc732227 JB |
249 | |
250 | /** | |
251 | * Remove a tag from all user entries. | |
4059a061 JB |
252 | * |
253 | * 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 |
254 | * 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: |
255 | * | |
256 | * 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 |
257 | * |
258 | * @param int $userId | |
259 | * @param Tag $tag | |
260 | */ | |
261 | public function removeTag($userId, Tag $tag) | |
262 | { | |
4059a061 JB |
263 | $entries = $this->getBuilderByUser($userId) |
264 | ->innerJoin('e.tags', 't') | |
265 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | |
266 | ->getQuery() | |
267 | ->getResult(); | |
268 | ||
269 | foreach ($entries as $entry) { | |
270 | $entry->removeTag($tag); | |
271 | } | |
272 | ||
273 | $this->getEntityManager()->flush(); | |
4059a061 JB |
274 | } |
275 | ||
4da01f49 | 276 | /** |
9bf83f1f | 277 | * Remove tags from all user entries. |
4da01f49 | 278 | * |
9bf83f1f | 279 | * @param int $userId |
4da01f49 TC |
280 | * @param Array<Tag> $tags |
281 | */ | |
9bf83f1f TC |
282 | public function removeTags($userId, $tags) |
283 | { | |
4da01f49 TC |
284 | foreach ($tags as $tag) { |
285 | $this->removeTag($userId, $tag); | |
286 | } | |
287 | } | |
288 | ||
4059a061 JB |
289 | /** |
290 | * Find all entries that are attached to a give tag id. | |
291 | * | |
292 | * @param int $userId | |
293 | * @param int $tagId | |
294 | * | |
295 | * @return array | |
296 | */ | |
297 | public function findAllByTagId($userId, $tagId) | |
298 | { | |
299 | return $this->getBuilderByUser($userId) | |
300 | ->innerJoin('e.tags', 't') | |
301 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | |
302 | ->getQuery() | |
303 | ->getResult(); | |
fc732227 | 304 | } |
303768df NL |
305 | |
306 | /** | |
307 | * Find an entry by its url and its owner. | |
5a4bbcc9 | 308 | * If it exists, return the entry otherwise return false. |
303768df NL |
309 | * |
310 | * @param $url | |
311 | * @param $userId | |
312 | * | |
4094ea47 | 313 | * @return Entry|bool |
303768df | 314 | */ |
78833672 | 315 | public function findByUrlAndUserId($url, $userId) |
303768df | 316 | { |
5a4bbcc9 | 317 | $res = $this->createQueryBuilder('e') |
19ca0b2f | 318 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
303768df NL |
319 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
320 | ->getQuery() | |
321 | ->getResult(); | |
5a4bbcc9 | 322 | |
b4b592a0 JB |
323 | if (count($res)) { |
324 | return current($res); | |
5a4bbcc9 JB |
325 | } |
326 | ||
327 | return false; | |
303768df | 328 | } |
5c072d2b NL |
329 | |
330 | /** | |
331 | * Count all entries for a user. | |
332 | * | |
333 | * @param int $userId | |
334 | * | |
e678c475 | 335 | * @return int |
5c072d2b | 336 | */ |
273b6f06 | 337 | public function countAllEntriesByUser($userId) |
5c072d2b NL |
338 | { |
339 | $qb = $this->createQueryBuilder('e') | |
340 | ->select('count(e)') | |
7c04b739 JB |
341 | ->where('e.user = :userId')->setParameter('userId', $userId) |
342 | ; | |
343 | ||
344 | return (int) $qb->getQuery()->getSingleScalarResult(); | |
345 | } | |
346 | ||
191564b7 JB |
347 | /** |
348 | * Remove all entries for a user id. | |
8c61fd12 | 349 | * Used when a user want to reset all informations. |
191564b7 | 350 | * |
8c61fd12 | 351 | * @param int $userId |
191564b7 JB |
352 | */ |
353 | public function removeAllByUserId($userId) | |
354 | { | |
355 | $this->getEntityManager() | |
b0de88f7 JB |
356 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') |
357 | ->setParameter('userId', $userId) | |
191564b7 JB |
358 | ->execute(); |
359 | } | |
6da1aebc TC |
360 | |
361 | public function removeArchivedByUserId($userId) | |
362 | { | |
363 | $this->getEntityManager() | |
364 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE') | |
365 | ->setParameter('userId', $userId) | |
366 | ->execute(); | |
367 | } | |
e2f3800c TC |
368 | |
369 | /** | |
370 | * Get id and url from all entries | |
371 | * Used for the clean-duplicates command. | |
372 | */ | |
dbf1188c | 373 | public function findAllEntriesIdAndUrlByUserId($userId) |
e2f3800c TC |
374 | { |
375 | $qb = $this->createQueryBuilder('e') | |
376 | ->select('e.id, e.url') | |
377 | ->where('e.user = :userid')->setParameter(':userid', $userId); | |
378 | ||
379 | return $qb->getQuery()->getArrayResult(); | |
380 | } | |
d09fe4d2 | 381 | |
511f1ce1 NH |
382 | /** |
383 | * @param int $userId | |
384 | * | |
385 | * @return array | |
386 | */ | |
215409a8 | 387 | public function findAllEntriesIdByUserId($userId = null) |
511f1ce1 NH |
388 | { |
389 | $qb = $this->createQueryBuilder('e') | |
390 | ->select('e.id'); | |
391 | ||
392 | if (null !== $userId) { | |
393 | $qb->where('e.user = :userid')->setParameter(':userid', $userId); | |
394 | } | |
395 | ||
396 | return $qb->getQuery()->getArrayResult(); | |
397 | } | |
398 | ||
d09fe4d2 NL |
399 | /** |
400 | * Find all entries by url and owner. | |
401 | * | |
402 | * @param $url | |
403 | * @param $userId | |
404 | * | |
405 | * @return array | |
406 | */ | |
407 | public function findAllByUrlAndUserId($url, $userId) | |
408 | { | |
89f108b4 | 409 | return $this->createQueryBuilder('e') |
d09fe4d2 NL |
410 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
411 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | |
412 | ->getQuery() | |
413 | ->getResult(); | |
d09fe4d2 | 414 | } |
f808b016 JB |
415 | |
416 | /** | |
417 | * Return a query builder to used by other getBuilderFor* method. | |
418 | * | |
a991c46e F |
419 | * @param int $userId |
420 | * @param string $sortBy | |
421 | * @param string $direction | |
f808b016 JB |
422 | * |
423 | * @return QueryBuilder | |
424 | */ | |
a991c46e | 425 | private function getBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') |
f808b016 JB |
426 | { |
427 | return $this->createQueryBuilder('e') | |
428 | ->andWhere('e.user = :userId')->setParameter('userId', $userId) | |
a991c46e | 429 | ->orderBy(sprintf('e.%s', $sortBy), $direction); |
f808b016 | 430 | } |
9d50517c | 431 | } |