]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Merge pull request #3245 from wallabag/fix-bc
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Repository;
9d50517c 4
9d50517c 5use Doctrine\ORM\EntityRepository;
0b0233b1 6use Doctrine\ORM\Query;
bcf53ab7
WD
7use Pagerfanta\Adapter\DoctrineORMAdapter;
8use Pagerfanta\Pagerfanta;
fc732227 9use Wallabag\CoreBundle\Entity\Tag;
9d50517c 10
be463487 11class EntryRepository extends EntityRepository
9d50517c 12{
b84a8055 13 /**
0ab7404f 14 * Return a query builder to used by other getBuilderFor* method.
b84a8055 15 *
3b815d2d 16 * @param int $userId
3b815d2d 17 *
26864574 18 * @return QueryBuilder
b84a8055 19 */
0ab7404f 20 private function getBuilderByUser($userId)
9d50517c 21 {
26864574 22 return $this->createQueryBuilder('e')
45c159b7 23 ->andWhere('e.user = :userId')->setParameter('userId', $userId)
1e7b04d4 24 ->orderBy('e.createdAt', 'desc')
0ab7404f
JB
25 ;
26 }
27
2b7a4889
NL
28 /**
29 * Retrieves all entries for a user.
30 *
31 * @param int $userId
32 *
33 * @return QueryBuilder
34 */
35 public function getBuilderForAllByUser($userId)
36 {
37 return $this
38 ->getBuilderByUser($userId)
39 ;
40 }
41
0ab7404f
JB
42 /**
43 * Retrieves unread entries for a user.
44 *
45 * @param int $userId
46 *
47 * @return QueryBuilder
48 */
49 public function getBuilderForUnreadByUser($userId)
50 {
51 return $this
52 ->getBuilderByUser($userId)
53 ->andWhere('e.isArchived = false')
54 ;
9d50517c 55 }
bd9f0815 56
b84a8055 57 /**
4346a860 58 * Retrieves read entries for a user.
b84a8055 59 *
3b815d2d 60 * @param int $userId
3b815d2d 61 *
26864574 62 * @return QueryBuilder
b84a8055 63 */
0ab7404f 64 public function getBuilderForArchiveByUser($userId)
bd9f0815 65 {
0ab7404f
JB
66 return $this
67 ->getBuilderByUser($userId)
68 ->andWhere('e.isArchived = true')
69 ;
bd9f0815
NL
70 }
71
b84a8055 72 /**
4346a860 73 * Retrieves starred entries for a user.
b84a8055 74 *
3b815d2d 75 * @param int $userId
3b815d2d 76 *
26864574 77 * @return QueryBuilder
b84a8055 78 */
0ab7404f 79 public function getBuilderForStarredByUser($userId)
bd9f0815 80 {
0ab7404f
JB
81 return $this
82 ->getBuilderByUser($userId)
83 ->andWhere('e.isStarred = true')
84 ;
bd9f0815 85 }
a8c90c5c 86
ee122a75
NL
87 /**
88 * Retrieves entries filtered with a search term for a user.
89 *
90 * @param int $userId
91 * @param string $term
49b042df 92 * @param strint $currentRoute
ee122a75
NL
93 *
94 * @return QueryBuilder
95 */
49b042df 96 public function getBuilderForSearchByUser($userId, $term, $currentRoute)
ee122a75 97 {
49b042df
NL
98 $qb = $this
99 ->getBuilderByUser($userId);
100
101 if ('starred' === $currentRoute) {
102 $qb->andWhere('e.isStarred = true');
103 } elseif ('unread' === $currentRoute) {
104 $qb->andWhere('e.isArchived = false');
105 } elseif ('archive' === $currentRoute) {
106 $qb->andWhere('e.isArchived = true');
107 }
108
eac09b48 109 // We lower() all parts here because PostgreSQL 'LIKE' verb is case-sensitive
49b042df 110 $qb
eac09b48 111 ->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 112 ->leftJoin('e.tags', 't')
32f455c1 113 ->groupBy('e.id');
49b042df
NL
114
115 return $qb;
ee122a75
NL
116 }
117
b6520f0b
NL
118 /**
119 * Retrieves untagged entries for a user.
120 *
121 * @param int $userId
122 *
123 * @return QueryBuilder
124 */
125 public function getBuilderForUntaggedByUser($userId)
126 {
127 return $this
128 ->getBuilderByUser($userId)
9deac0c5 129 ->andWhere('size(e.tags) = 0');
b6520f0b
NL
130 }
131
3b815d2d 132 /**
4346a860 133 * Find Entries.
3b815d2d 134 *
2a94b1d1
NL
135 * @param int $userId
136 * @param bool $isArchived
137 * @param bool $isStarred
1112e547 138 * @param bool $isPublic
2a94b1d1
NL
139 * @param string $sort
140 * @param string $order
8cb869ea
TC
141 * @param int $since
142 * @param string $tags
3b815d2d 143 *
017e2089 144 * @return array
3b815d2d 145 */
1112e547 146 public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
a8c90c5c
NL
147 {
148 $qb = $this->createQueryBuilder('e')
28803f10 149 ->leftJoin('e.tags', 't')
0f006880 150 ->where('e.user =:userId')->setParameter('userId', $userId);
6e334aba 151
3b815d2d 152 if (null !== $isArchived) {
1112e547 153 $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived);
6e334aba
NL
154 }
155
3b815d2d 156 if (null !== $isStarred) {
1112e547
JB
157 $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred);
158 }
159
160 if (null !== $isPublic) {
161 $qb->andWhere('e.uid IS '.(true === $isPublic ? 'NOT' : '').' NULL');
6e334aba
NL
162 }
163
c3f8b428 164 if ($since > 0) {
e5fb89e5 165 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
4f0558a0
TC
166 }
167
28803f10
TC
168 if ('' !== $tags) {
169 foreach (explode(',', $tags) as $tag) {
170 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
171 }
e5fb89e5
TC
172 }
173
bc782eaa 174 if ('created' === $sort) {
2385f891 175 $qb->orderBy('e.id', $order);
bc782eaa
NL
176 } elseif ('updated' === $sort) {
177 $qb->orderBy('e.updatedAt', $order);
178 }
179
5a5da369 180 $pagerAdapter = new DoctrineORMAdapter($qb, true, false);
bcf53ab7
WD
181
182 return new Pagerfanta($pagerAdapter);
a8c90c5c 183 }
46bbd8d3 184
a36737f4
NL
185 /**
186 * Fetch an entry with a tag. Only used for tests.
187 *
5c072d2b
NL
188 * @param int $userId
189 *
a36737f4
NL
190 * @return Entry
191 */
092ca707 192 public function findOneWithTags($userId)
46bbd8d3
NL
193 {
194 $qb = $this->createQueryBuilder('e')
195 ->innerJoin('e.tags', 't')
0ca374e6
NL
196 ->innerJoin('e.user', 'u')
197 ->addSelect('t', 'u')
198 ->where('e.user=:userId')->setParameter('userId', $userId)
199 ;
092ca707 200
0ca374e6 201 return $qb->getQuery()->getResult();
46bbd8d3 202 }
d4ebe5c5
JB
203
204 /**
205 * Find distinct language for a given user.
206 * Used to build the filter language list.
207 *
208 * @param int $userId User id
209 *
210 * @return array
211 */
212 public function findDistinctLanguageByUser($userId)
213 {
214 $results = $this->createQueryBuilder('e')
215 ->select('e.language')
216 ->where('e.user = :userId')->setParameter('userId', $userId)
217 ->andWhere('e.language IS NOT NULL')
218 ->groupBy('e.language')
219 ->orderBy('e.language', ' ASC')
220 ->getQuery()
221 ->getResult();
222
4094ea47 223 $languages = [];
d4ebe5c5
JB
224 foreach ($results as $result) {
225 $languages[$result['language']] = $result['language'];
226 }
227
228 return $languages;
229 }
159986c4 230
159986c4 231 /**
cfb28c9d 232 * Used only in test case to get the right entry associated to the right user.
159986c4 233 *
cfb28c9d 234 * @param string $username
159986c4
JB
235 *
236 * @return Entry
237 */
238 public function findOneByUsernameAndNotArchived($username)
239 {
240 return $this->createQueryBuilder('e')
241 ->leftJoin('e.user', 'u')
242 ->where('u.username = :username')->setParameter('username', $username)
243 ->andWhere('e.isArchived = false')
244 ->setMaxResults(1)
245 ->getQuery()
246 ->getSingleResult();
247 }
fc732227
JB
248
249 /**
250 * Remove a tag from all user entries.
4059a061
JB
251 *
252 * 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
253 * 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:
254 *
255 * 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
256 *
257 * @param int $userId
258 * @param Tag $tag
259 */
260 public function removeTag($userId, Tag $tag)
261 {
4059a061
JB
262 $entries = $this->getBuilderByUser($userId)
263 ->innerJoin('e.tags', 't')
264 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
265 ->getQuery()
266 ->getResult();
267
268 foreach ($entries as $entry) {
269 $entry->removeTag($tag);
270 }
271
272 $this->getEntityManager()->flush();
4059a061
JB
273 }
274
4da01f49 275 /**
9bf83f1f 276 * Remove tags from all user entries.
4da01f49 277 *
9bf83f1f 278 * @param int $userId
4da01f49
TC
279 * @param Array<Tag> $tags
280 */
9bf83f1f
TC
281 public function removeTags($userId, $tags)
282 {
4da01f49
TC
283 foreach ($tags as $tag) {
284 $this->removeTag($userId, $tag);
285 }
286 }
287
4059a061
JB
288 /**
289 * Find all entries that are attached to a give tag id.
290 *
291 * @param int $userId
292 * @param int $tagId
293 *
294 * @return array
295 */
296 public function findAllByTagId($userId, $tagId)
297 {
298 return $this->getBuilderByUser($userId)
299 ->innerJoin('e.tags', 't')
300 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
301 ->getQuery()
302 ->getResult();
fc732227 303 }
303768df
NL
304
305 /**
306 * Find an entry by its url and its owner.
5a4bbcc9 307 * If it exists, return the entry otherwise return false.
303768df
NL
308 *
309 * @param $url
310 * @param $userId
311 *
4094ea47 312 * @return Entry|bool
303768df 313 */
78833672 314 public function findByUrlAndUserId($url, $userId)
303768df 315 {
5a4bbcc9 316 $res = $this->createQueryBuilder('e')
19ca0b2f 317 ->where('e.url = :url')->setParameter('url', urldecode($url))
303768df
NL
318 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
319 ->getQuery()
320 ->getResult();
5a4bbcc9 321
b4b592a0
JB
322 if (count($res)) {
323 return current($res);
5a4bbcc9
JB
324 }
325
326 return false;
303768df 327 }
5c072d2b
NL
328
329 /**
330 * Count all entries for a user.
331 *
332 * @param int $userId
333 *
e678c475 334 * @return int
5c072d2b 335 */
273b6f06 336 public function countAllEntriesByUser($userId)
5c072d2b
NL
337 {
338 $qb = $this->createQueryBuilder('e')
339 ->select('count(e)')
340 ->where('e.user=:userId')->setParameter('userId', $userId)
341 ;
342
343 return $qb->getQuery()->getSingleScalarResult();
344 }
28bb4890
JB
345
346 /**
347 * Count all entries for a tag and a user.
348 *
349 * @param int $userId
350 * @param int $tagId
351 *
352 * @return int
353 */
354 public function countAllEntriesByUserIdAndTagId($userId, $tagId)
355 {
356 $qb = $this->createQueryBuilder('e')
357 ->select('count(e.id)')
358 ->leftJoin('e.tags', 't')
359 ->where('e.user=:userId')->setParameter('userId', $userId)
360 ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
361 ;
362
363 return $qb->getQuery()->getSingleScalarResult();
364 }
191564b7
JB
365
366 /**
367 * Remove all entries for a user id.
8c61fd12 368 * Used when a user want to reset all informations.
191564b7 369 *
8c61fd12 370 * @param int $userId
191564b7
JB
371 */
372 public function removeAllByUserId($userId)
373 {
374 $this->getEntityManager()
b0de88f7
JB
375 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
376 ->setParameter('userId', $userId)
191564b7
JB
377 ->execute();
378 }
6da1aebc
TC
379
380 public function removeArchivedByUserId($userId)
381 {
382 $this->getEntityManager()
383 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE')
384 ->setParameter('userId', $userId)
385 ->execute();
386 }
e2f3800c
TC
387
388 /**
389 * Get id and url from all entries
390 * Used for the clean-duplicates command.
391 */
392 public function getAllEntriesIdAndUrl($userId)
393 {
394 $qb = $this->createQueryBuilder('e')
395 ->select('e.id, e.url')
396 ->where('e.user = :userid')->setParameter(':userid', $userId);
397
398 return $qb->getQuery()->getArrayResult();
399 }
d09fe4d2
NL
400
401 /**
402 * Find all entries by url and owner.
403 *
404 * @param $url
405 * @param $userId
406 *
407 * @return array
408 */
409 public function findAllByUrlAndUserId($url, $userId)
410 {
89f108b4 411 return $this->createQueryBuilder('e')
d09fe4d2
NL
412 ->where('e.url = :url')->setParameter('url', urldecode($url))
413 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
414 ->getQuery()
415 ->getResult();
d09fe4d2 416 }
9d50517c 417}