]>
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; |
bcf53ab7 WD |
7 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
8 | use Pagerfanta\Pagerfanta; | |
fc732227 | 9 | use Wallabag\CoreBundle\Entity\Tag; |
9d50517c | 10 | |
be463487 | 11 | class 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 | |
2a94b1d1 NL |
138 | * @param string $sort |
139 | * @param string $order | |
8cb869ea TC |
140 | * @param int $since |
141 | * @param string $tags | |
3b815d2d | 142 | * |
017e2089 | 143 | * @return array |
3b815d2d | 144 | */ |
28803f10 | 145 | public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '') |
a8c90c5c NL |
146 | { |
147 | $qb = $this->createQueryBuilder('e') | |
28803f10 | 148 | ->leftJoin('e.tags', 't') |
0f006880 | 149 | ->where('e.user =:userId')->setParameter('userId', $userId); |
6e334aba | 150 | |
3b815d2d J |
151 | if (null !== $isArchived) { |
152 | $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); | |
6e334aba NL |
153 | } |
154 | ||
3b815d2d J |
155 | if (null !== $isStarred) { |
156 | $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); | |
6e334aba NL |
157 | } |
158 | ||
c3f8b428 | 159 | if ($since > 0) { |
e5fb89e5 | 160 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); |
4f0558a0 TC |
161 | } |
162 | ||
28803f10 TC |
163 | if ('' !== $tags) { |
164 | foreach (explode(',', $tags) as $tag) { | |
165 | $qb->andWhere('t.label = :label')->setParameter('label', $tag); | |
166 | } | |
e5fb89e5 TC |
167 | } |
168 | ||
bc782eaa | 169 | if ('created' === $sort) { |
2385f891 | 170 | $qb->orderBy('e.id', $order); |
bc782eaa NL |
171 | } elseif ('updated' === $sort) { |
172 | $qb->orderBy('e.updatedAt', $order); | |
173 | } | |
174 | ||
5a5da369 | 175 | $pagerAdapter = new DoctrineORMAdapter($qb, true, false); |
bcf53ab7 WD |
176 | |
177 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 178 | } |
46bbd8d3 | 179 | |
a36737f4 NL |
180 | /** |
181 | * Fetch an entry with a tag. Only used for tests. | |
182 | * | |
5c072d2b NL |
183 | * @param int $userId |
184 | * | |
a36737f4 NL |
185 | * @return Entry |
186 | */ | |
092ca707 | 187 | public function findOneWithTags($userId) |
46bbd8d3 NL |
188 | { |
189 | $qb = $this->createQueryBuilder('e') | |
190 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
191 | ->innerJoin('e.user', 'u') |
192 | ->addSelect('t', 'u') | |
193 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
194 | ; | |
092ca707 | 195 | |
0ca374e6 | 196 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 197 | } |
d4ebe5c5 JB |
198 | |
199 | /** | |
200 | * Find distinct language for a given user. | |
201 | * Used to build the filter language list. | |
202 | * | |
203 | * @param int $userId User id | |
204 | * | |
205 | * @return array | |
206 | */ | |
207 | public function findDistinctLanguageByUser($userId) | |
208 | { | |
209 | $results = $this->createQueryBuilder('e') | |
210 | ->select('e.language') | |
211 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
212 | ->andWhere('e.language IS NOT NULL') | |
213 | ->groupBy('e.language') | |
214 | ->orderBy('e.language', ' ASC') | |
215 | ->getQuery() | |
216 | ->getResult(); | |
217 | ||
4094ea47 | 218 | $languages = []; |
d4ebe5c5 JB |
219 | foreach ($results as $result) { |
220 | $languages[$result['language']] = $result['language']; | |
221 | } | |
222 | ||
223 | return $languages; | |
224 | } | |
159986c4 | 225 | |
159986c4 | 226 | /** |
cfb28c9d | 227 | * Used only in test case to get the right entry associated to the right user. |
159986c4 | 228 | * |
cfb28c9d | 229 | * @param string $username |
159986c4 JB |
230 | * |
231 | * @return Entry | |
232 | */ | |
233 | public function findOneByUsernameAndNotArchived($username) | |
234 | { | |
235 | return $this->createQueryBuilder('e') | |
236 | ->leftJoin('e.user', 'u') | |
237 | ->where('u.username = :username')->setParameter('username', $username) | |
238 | ->andWhere('e.isArchived = false') | |
239 | ->setMaxResults(1) | |
240 | ->getQuery() | |
241 | ->getSingleResult(); | |
242 | } | |
fc732227 JB |
243 | |
244 | /** | |
245 | * Remove a tag from all user entries. | |
4059a061 JB |
246 | * |
247 | * 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 |
248 | * 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: |
249 | * | |
250 | * 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 |
251 | * |
252 | * @param int $userId | |
253 | * @param Tag $tag | |
254 | */ | |
255 | public function removeTag($userId, Tag $tag) | |
256 | { | |
4059a061 JB |
257 | $entries = $this->getBuilderByUser($userId) |
258 | ->innerJoin('e.tags', 't') | |
259 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | |
260 | ->getQuery() | |
261 | ->getResult(); | |
262 | ||
263 | foreach ($entries as $entry) { | |
264 | $entry->removeTag($tag); | |
265 | } | |
266 | ||
267 | $this->getEntityManager()->flush(); | |
4059a061 JB |
268 | } |
269 | ||
4da01f49 | 270 | /** |
9bf83f1f | 271 | * Remove tags from all user entries. |
4da01f49 | 272 | * |
9bf83f1f | 273 | * @param int $userId |
4da01f49 TC |
274 | * @param Array<Tag> $tags |
275 | */ | |
9bf83f1f TC |
276 | public function removeTags($userId, $tags) |
277 | { | |
4da01f49 TC |
278 | foreach ($tags as $tag) { |
279 | $this->removeTag($userId, $tag); | |
280 | } | |
281 | } | |
282 | ||
4059a061 JB |
283 | /** |
284 | * Find all entries that are attached to a give tag id. | |
285 | * | |
286 | * @param int $userId | |
287 | * @param int $tagId | |
288 | * | |
289 | * @return array | |
290 | */ | |
291 | public function findAllByTagId($userId, $tagId) | |
292 | { | |
293 | return $this->getBuilderByUser($userId) | |
294 | ->innerJoin('e.tags', 't') | |
295 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | |
296 | ->getQuery() | |
297 | ->getResult(); | |
fc732227 | 298 | } |
303768df NL |
299 | |
300 | /** | |
301 | * Find an entry by its url and its owner. | |
5a4bbcc9 | 302 | * If it exists, return the entry otherwise return false. |
303768df NL |
303 | * |
304 | * @param $url | |
305 | * @param $userId | |
306 | * | |
4094ea47 | 307 | * @return Entry|bool |
303768df | 308 | */ |
78833672 | 309 | public function findByUrlAndUserId($url, $userId) |
303768df | 310 | { |
5a4bbcc9 | 311 | $res = $this->createQueryBuilder('e') |
19ca0b2f | 312 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
303768df NL |
313 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
314 | ->getQuery() | |
315 | ->getResult(); | |
5a4bbcc9 | 316 | |
b4b592a0 JB |
317 | if (count($res)) { |
318 | return current($res); | |
5a4bbcc9 JB |
319 | } |
320 | ||
321 | return false; | |
303768df | 322 | } |
5c072d2b NL |
323 | |
324 | /** | |
325 | * Count all entries for a user. | |
326 | * | |
327 | * @param int $userId | |
328 | * | |
e678c475 | 329 | * @return int |
5c072d2b NL |
330 | */ |
331 | public function countAllEntriesByUsername($userId) | |
332 | { | |
333 | $qb = $this->createQueryBuilder('e') | |
334 | ->select('count(e)') | |
335 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
336 | ; | |
337 | ||
338 | return $qb->getQuery()->getSingleScalarResult(); | |
339 | } | |
28bb4890 JB |
340 | |
341 | /** | |
342 | * Count all entries for a tag and a user. | |
343 | * | |
344 | * @param int $userId | |
345 | * @param int $tagId | |
346 | * | |
347 | * @return int | |
348 | */ | |
349 | public function countAllEntriesByUserIdAndTagId($userId, $tagId) | |
350 | { | |
351 | $qb = $this->createQueryBuilder('e') | |
352 | ->select('count(e.id)') | |
353 | ->leftJoin('e.tags', 't') | |
354 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
355 | ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId) | |
356 | ; | |
357 | ||
358 | return $qb->getQuery()->getSingleScalarResult(); | |
359 | } | |
191564b7 JB |
360 | |
361 | /** | |
362 | * Remove all entries for a user id. | |
8c61fd12 | 363 | * Used when a user want to reset all informations. |
191564b7 | 364 | * |
8c61fd12 | 365 | * @param int $userId |
191564b7 JB |
366 | */ |
367 | public function removeAllByUserId($userId) | |
368 | { | |
369 | $this->getEntityManager() | |
b0de88f7 JB |
370 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') |
371 | ->setParameter('userId', $userId) | |
191564b7 JB |
372 | ->execute(); |
373 | } | |
9d50517c | 374 | } |