]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Repository; |
9d50517c | 4 | |
9d50517c | 5 | use Doctrine\ORM\EntityRepository; |
0b0233b1 | 6 | use Doctrine\ORM\Query; |
bf6c0346 | 7 | use Doctrine\ORM\QueryBuilder; |
bcf53ab7 WD |
8 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
9 | use Pagerfanta\Pagerfanta; | |
fc732227 | 10 | use Wallabag\CoreBundle\Entity\Tag; |
9d50517c | 11 | |
be463487 | 12 | class EntryRepository extends EntityRepository |
9d50517c | 13 | { |
b84a8055 | 14 | /** |
0ab7404f | 15 | * Return a query builder to used by other getBuilderFor* method. |
b84a8055 | 16 | * |
3b815d2d | 17 | * @param int $userId |
3b815d2d | 18 | * |
26864574 | 19 | * @return QueryBuilder |
b84a8055 | 20 | */ |
0ab7404f | 21 | private function getBuilderByUser($userId) |
9d50517c | 22 | { |
26864574 | 23 | return $this->createQueryBuilder('e') |
45c159b7 | 24 | ->andWhere('e.user = :userId')->setParameter('userId', $userId) |
1e7b04d4 | 25 | ->orderBy('e.createdAt', 'desc') |
0ab7404f JB |
26 | ; |
27 | } | |
28 | ||
2b7a4889 NL |
29 | /** |
30 | * Retrieves all entries for a user. | |
31 | * | |
32 | * @param int $userId | |
33 | * | |
34 | * @return QueryBuilder | |
35 | */ | |
36 | public function getBuilderForAllByUser($userId) | |
37 | { | |
38 | return $this | |
39 | ->getBuilderByUser($userId) | |
40 | ; | |
41 | } | |
42 | ||
0ab7404f JB |
43 | /** |
44 | * Retrieves unread entries for a user. | |
45 | * | |
46 | * @param int $userId | |
47 | * | |
48 | * @return QueryBuilder | |
49 | */ | |
50 | public function getBuilderForUnreadByUser($userId) | |
51 | { | |
52 | return $this | |
53 | ->getBuilderByUser($userId) | |
54 | ->andWhere('e.isArchived = false') | |
55 | ; | |
9d50517c | 56 | } |
bd9f0815 | 57 | |
b84a8055 | 58 | /** |
4346a860 | 59 | * Retrieves read entries for a user. |
b84a8055 | 60 | * |
3b815d2d | 61 | * @param int $userId |
3b815d2d | 62 | * |
26864574 | 63 | * @return QueryBuilder |
b84a8055 | 64 | */ |
0ab7404f | 65 | public function getBuilderForArchiveByUser($userId) |
bd9f0815 | 66 | { |
0ab7404f JB |
67 | return $this |
68 | ->getBuilderByUser($userId) | |
69 | ->andWhere('e.isArchived = true') | |
70 | ; | |
bd9f0815 NL |
71 | } |
72 | ||
b84a8055 | 73 | /** |
4346a860 | 74 | * Retrieves starred entries for a user. |
b84a8055 | 75 | * |
3b815d2d | 76 | * @param int $userId |
3b815d2d | 77 | * |
26864574 | 78 | * @return QueryBuilder |
b84a8055 | 79 | */ |
0ab7404f | 80 | public function getBuilderForStarredByUser($userId) |
bd9f0815 | 81 | { |
0ab7404f JB |
82 | return $this |
83 | ->getBuilderByUser($userId) | |
84 | ->andWhere('e.isStarred = true') | |
85 | ; | |
bd9f0815 | 86 | } |
a8c90c5c | 87 | |
ee122a75 NL |
88 | /** |
89 | * Retrieves entries filtered with a search term for a user. | |
90 | * | |
91 | * @param int $userId | |
92 | * @param string $term | |
bf6c0346 | 93 | * @param string $currentRoute |
ee122a75 NL |
94 | * |
95 | * @return QueryBuilder | |
96 | */ | |
49b042df | 97 | public function getBuilderForSearchByUser($userId, $term, $currentRoute) |
ee122a75 | 98 | { |
49b042df NL |
99 | $qb = $this |
100 | ->getBuilderByUser($userId); | |
101 | ||
102 | if ('starred' === $currentRoute) { | |
103 | $qb->andWhere('e.isStarred = true'); | |
104 | } elseif ('unread' === $currentRoute) { | |
105 | $qb->andWhere('e.isArchived = false'); | |
106 | } elseif ('archive' === $currentRoute) { | |
107 | $qb->andWhere('e.isArchived = true'); | |
108 | } | |
109 | ||
eac09b48 | 110 | // We lower() all parts here because PostgreSQL 'LIKE' verb is case-sensitive |
49b042df | 111 | $qb |
eac09b48 | 112 | ->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 | 113 | ->leftJoin('e.tags', 't') |
32f455c1 | 114 | ->groupBy('e.id'); |
49b042df NL |
115 | |
116 | return $qb; | |
ee122a75 NL |
117 | } |
118 | ||
b6520f0b NL |
119 | /** |
120 | * Retrieves untagged entries for a user. | |
121 | * | |
122 | * @param int $userId | |
123 | * | |
124 | * @return QueryBuilder | |
125 | */ | |
126 | public function getBuilderForUntaggedByUser($userId) | |
127 | { | |
128 | return $this | |
129 | ->getBuilderByUser($userId) | |
9deac0c5 | 130 | ->andWhere('size(e.tags) = 0'); |
b6520f0b NL |
131 | } |
132 | ||
3b815d2d | 133 | /** |
4346a860 | 134 | * Find Entries. |
3b815d2d | 135 | * |
2a94b1d1 NL |
136 | * @param int $userId |
137 | * @param bool $isArchived | |
138 | * @param bool $isStarred | |
1112e547 | 139 | * @param bool $isPublic |
2a94b1d1 NL |
140 | * @param string $sort |
141 | * @param string $order | |
8cb869ea TC |
142 | * @param int $since |
143 | * @param string $tags | |
3b815d2d | 144 | * |
017e2089 | 145 | * @return array |
3b815d2d | 146 | */ |
1112e547 | 147 | public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '') |
a8c90c5c NL |
148 | { |
149 | $qb = $this->createQueryBuilder('e') | |
28803f10 | 150 | ->leftJoin('e.tags', 't') |
0f006880 | 151 | ->where('e.user =:userId')->setParameter('userId', $userId); |
6e334aba | 152 | |
3b815d2d | 153 | if (null !== $isArchived) { |
1112e547 | 154 | $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); |
6e334aba NL |
155 | } |
156 | ||
3b815d2d | 157 | if (null !== $isStarred) { |
1112e547 JB |
158 | $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred); |
159 | } | |
160 | ||
161 | if (null !== $isPublic) { | |
162 | $qb->andWhere('e.uid IS '.(true === $isPublic ? 'NOT' : '').' NULL'); | |
6e334aba NL |
163 | } |
164 | ||
c3f8b428 | 165 | if ($since > 0) { |
e5fb89e5 | 166 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); |
4f0558a0 TC |
167 | } |
168 | ||
28803f10 TC |
169 | if ('' !== $tags) { |
170 | foreach (explode(',', $tags) as $tag) { | |
171 | $qb->andWhere('t.label = :label')->setParameter('label', $tag); | |
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 | * | |
a36737f4 NL |
191 | * @return Entry |
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') | |
199 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
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)') | |
341 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
342 | ; | |
343 | ||
344 | return $qb->getQuery()->getSingleScalarResult(); | |
345 | } | |
28bb4890 JB |
346 | |
347 | /** | |
348 | * Count all entries for a tag and a user. | |
349 | * | |
350 | * @param int $userId | |
351 | * @param int $tagId | |
352 | * | |
353 | * @return int | |
354 | */ | |
355 | public function countAllEntriesByUserIdAndTagId($userId, $tagId) | |
356 | { | |
357 | $qb = $this->createQueryBuilder('e') | |
358 | ->select('count(e.id)') | |
359 | ->leftJoin('e.tags', 't') | |
360 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
361 | ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId) | |
362 | ; | |
363 | ||
364 | return $qb->getQuery()->getSingleScalarResult(); | |
365 | } | |
191564b7 JB |
366 | |
367 | /** | |
368 | * Remove all entries for a user id. | |
8c61fd12 | 369 | * Used when a user want to reset all informations. |
191564b7 | 370 | * |
8c61fd12 | 371 | * @param int $userId |
191564b7 JB |
372 | */ |
373 | public function removeAllByUserId($userId) | |
374 | { | |
375 | $this->getEntityManager() | |
b0de88f7 JB |
376 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') |
377 | ->setParameter('userId', $userId) | |
191564b7 JB |
378 | ->execute(); |
379 | } | |
6da1aebc TC |
380 | |
381 | public function removeArchivedByUserId($userId) | |
382 | { | |
383 | $this->getEntityManager() | |
384 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE') | |
385 | ->setParameter('userId', $userId) | |
386 | ->execute(); | |
387 | } | |
e2f3800c TC |
388 | |
389 | /** | |
390 | * Get id and url from all entries | |
391 | * Used for the clean-duplicates command. | |
392 | */ | |
393 | public function getAllEntriesIdAndUrl($userId) | |
394 | { | |
395 | $qb = $this->createQueryBuilder('e') | |
396 | ->select('e.id, e.url') | |
397 | ->where('e.user = :userid')->setParameter(':userid', $userId); | |
398 | ||
399 | return $qb->getQuery()->getArrayResult(); | |
400 | } | |
d09fe4d2 NL |
401 | |
402 | /** | |
403 | * Find all entries by url and owner. | |
404 | * | |
405 | * @param $url | |
406 | * @param $userId | |
407 | * | |
408 | * @return array | |
409 | */ | |
410 | public function findAllByUrlAndUserId($url, $userId) | |
411 | { | |
89f108b4 | 412 | return $this->createQueryBuilder('e') |
d09fe4d2 NL |
413 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
414 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | |
415 | ->getQuery() | |
416 | ->getResult(); | |
d09fe4d2 | 417 | } |
bf6c0346 TC |
418 | |
419 | /** | |
420 | * @param $userId | |
421 | * @return QueryBuilder | |
422 | */ | |
423 | public function getBuilderForRecommendationsByUser($userId) | |
424 | { | |
425 | return $this->getBuilderByUser($userId) | |
426 | ->andWhere('e.recommended = true') | |
427 | ; | |
428 | } | |
9d50517c | 429 | } |