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