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