]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Repository; |
9d50517c | 4 | |
9d50517c | 5 | use Doctrine\ORM\EntityRepository; |
bcf53ab7 WD |
6 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
7 | use Pagerfanta\Pagerfanta; | |
fc732227 | 8 | use Wallabag\CoreBundle\Entity\Tag; |
9d50517c | 9 | |
be463487 | 10 | class EntryRepository extends EntityRepository |
9d50517c | 11 | { |
b84a8055 | 12 | /** |
0ab7404f | 13 | * Return a query builder to used by other getBuilderFor* method. |
b84a8055 | 14 | * |
3b815d2d | 15 | * @param int $userId |
3b815d2d | 16 | * |
26864574 | 17 | * @return QueryBuilder |
b84a8055 | 18 | */ |
0ab7404f | 19 | private function getBuilderByUser($userId) |
9d50517c | 20 | { |
26864574 | 21 | return $this->createQueryBuilder('e') |
3b815d2d | 22 | ->leftJoin('e.user', 'u') |
0ab7404f JB |
23 | ->andWhere('u.id = :userId')->setParameter('userId', $userId) |
24 | ->orderBy('e.id', 'desc') | |
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 | |
b6520f0b NL |
87 | /** |
88 | * Retrieves untagged entries for a user. | |
89 | * | |
90 | * @param int $userId | |
91 | * | |
92 | * @return QueryBuilder | |
93 | */ | |
94 | public function getBuilderForUntaggedByUser($userId) | |
95 | { | |
96 | return $this | |
97 | ->getBuilderByUser($userId) | |
98 | ->leftJoin('e.tags', 't') | |
99 | ->groupBy('e.id') | |
100 | ->having('count(t.id) = 0'); | |
101 | } | |
102 | ||
3b815d2d | 103 | /** |
4346a860 | 104 | * Find Entries. |
3b815d2d | 105 | * |
2a94b1d1 NL |
106 | * @param int $userId |
107 | * @param bool $isArchived | |
108 | * @param bool $isStarred | |
2a94b1d1 NL |
109 | * @param string $sort |
110 | * @param string $order | |
8cb869ea TC |
111 | * @param int $since |
112 | * @param string $tags | |
3b815d2d | 113 | * |
017e2089 | 114 | * @return array |
3b815d2d | 115 | */ |
28803f10 | 116 | public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '') |
a8c90c5c NL |
117 | { |
118 | $qb = $this->createQueryBuilder('e') | |
28803f10 | 119 | ->leftJoin('e.tags', 't') |
0f006880 | 120 | ->where('e.user =:userId')->setParameter('userId', $userId); |
6e334aba | 121 | |
3b815d2d J |
122 | if (null !== $isArchived) { |
123 | $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); | |
6e334aba NL |
124 | } |
125 | ||
3b815d2d J |
126 | if (null !== $isStarred) { |
127 | $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); | |
6e334aba NL |
128 | } |
129 | ||
e5fb89e5 TC |
130 | if ($since >= 0) { |
131 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); | |
4f0558a0 TC |
132 | } |
133 | ||
28803f10 TC |
134 | if ('' !== $tags) { |
135 | foreach (explode(',', $tags) as $tag) { | |
136 | $qb->andWhere('t.label = :label')->setParameter('label', $tag); | |
137 | } | |
e5fb89e5 TC |
138 | } |
139 | ||
bc782eaa | 140 | if ('created' === $sort) { |
2385f891 | 141 | $qb->orderBy('e.id', $order); |
bc782eaa NL |
142 | } elseif ('updated' === $sort) { |
143 | $qb->orderBy('e.updatedAt', $order); | |
144 | } | |
145 | ||
bcf53ab7 WD |
146 | $pagerAdapter = new DoctrineORMAdapter($qb); |
147 | ||
148 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 149 | } |
46bbd8d3 | 150 | |
a36737f4 NL |
151 | /** |
152 | * Fetch an entry with a tag. Only used for tests. | |
153 | * | |
5c072d2b NL |
154 | * @param int $userId |
155 | * | |
a36737f4 NL |
156 | * @return Entry |
157 | */ | |
092ca707 | 158 | public function findOneWithTags($userId) |
46bbd8d3 NL |
159 | { |
160 | $qb = $this->createQueryBuilder('e') | |
161 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
162 | ->innerJoin('e.user', 'u') |
163 | ->addSelect('t', 'u') | |
164 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
165 | ; | |
092ca707 | 166 | |
0ca374e6 | 167 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 168 | } |
d4ebe5c5 JB |
169 | |
170 | /** | |
171 | * Find distinct language for a given user. | |
172 | * Used to build the filter language list. | |
173 | * | |
174 | * @param int $userId User id | |
175 | * | |
176 | * @return array | |
177 | */ | |
178 | public function findDistinctLanguageByUser($userId) | |
179 | { | |
180 | $results = $this->createQueryBuilder('e') | |
181 | ->select('e.language') | |
182 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
183 | ->andWhere('e.language IS NOT NULL') | |
184 | ->groupBy('e.language') | |
185 | ->orderBy('e.language', ' ASC') | |
186 | ->getQuery() | |
187 | ->getResult(); | |
188 | ||
4094ea47 | 189 | $languages = []; |
d4ebe5c5 JB |
190 | foreach ($results as $result) { |
191 | $languages[$result['language']] = $result['language']; | |
192 | } | |
193 | ||
194 | return $languages; | |
195 | } | |
159986c4 | 196 | |
159986c4 | 197 | /** |
cfb28c9d | 198 | * Used only in test case to get the right entry associated to the right user. |
159986c4 | 199 | * |
cfb28c9d | 200 | * @param string $username |
159986c4 JB |
201 | * |
202 | * @return Entry | |
203 | */ | |
204 | public function findOneByUsernameAndNotArchived($username) | |
205 | { | |
206 | return $this->createQueryBuilder('e') | |
207 | ->leftJoin('e.user', 'u') | |
208 | ->where('u.username = :username')->setParameter('username', $username) | |
209 | ->andWhere('e.isArchived = false') | |
210 | ->setMaxResults(1) | |
211 | ->getQuery() | |
212 | ->getSingleResult(); | |
213 | } | |
fc732227 JB |
214 | |
215 | /** | |
216 | * Remove a tag from all user entries. | |
4059a061 JB |
217 | * |
218 | * 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 |
219 | * 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: |
220 | * | |
221 | * 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 |
222 | * |
223 | * @param int $userId | |
224 | * @param Tag $tag | |
225 | */ | |
226 | public function removeTag($userId, Tag $tag) | |
227 | { | |
4059a061 JB |
228 | $entries = $this->getBuilderByUser($userId) |
229 | ->innerJoin('e.tags', 't') | |
230 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | |
231 | ->getQuery() | |
232 | ->getResult(); | |
233 | ||
234 | foreach ($entries as $entry) { | |
235 | $entry->removeTag($tag); | |
236 | } | |
237 | ||
238 | $this->getEntityManager()->flush(); | |
4059a061 JB |
239 | } |
240 | ||
4da01f49 | 241 | /** |
9bf83f1f | 242 | * Remove tags from all user entries. |
4da01f49 | 243 | * |
9bf83f1f | 244 | * @param int $userId |
4da01f49 TC |
245 | * @param Array<Tag> $tags |
246 | */ | |
9bf83f1f TC |
247 | public function removeTags($userId, $tags) |
248 | { | |
4da01f49 TC |
249 | foreach ($tags as $tag) { |
250 | $this->removeTag($userId, $tag); | |
251 | } | |
252 | } | |
253 | ||
4059a061 JB |
254 | /** |
255 | * Find all entries that are attached to a give tag id. | |
256 | * | |
257 | * @param int $userId | |
258 | * @param int $tagId | |
259 | * | |
260 | * @return array | |
261 | */ | |
262 | public function findAllByTagId($userId, $tagId) | |
263 | { | |
264 | return $this->getBuilderByUser($userId) | |
265 | ->innerJoin('e.tags', 't') | |
266 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | |
267 | ->getQuery() | |
268 | ->getResult(); | |
fc732227 | 269 | } |
303768df NL |
270 | |
271 | /** | |
272 | * Find an entry by its url and its owner. | |
5a4bbcc9 | 273 | * If it exists, return the entry otherwise return false. |
303768df NL |
274 | * |
275 | * @param $url | |
276 | * @param $userId | |
277 | * | |
4094ea47 | 278 | * @return Entry|bool |
303768df | 279 | */ |
78833672 | 280 | public function findByUrlAndUserId($url, $userId) |
303768df | 281 | { |
5a4bbcc9 | 282 | $res = $this->createQueryBuilder('e') |
303768df NL |
283 | ->where('e.url = :url')->setParameter('url', $url) |
284 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | |
285 | ->getQuery() | |
286 | ->getResult(); | |
5a4bbcc9 | 287 | |
b4b592a0 JB |
288 | if (count($res)) { |
289 | return current($res); | |
5a4bbcc9 JB |
290 | } |
291 | ||
292 | return false; | |
303768df | 293 | } |
5c072d2b NL |
294 | |
295 | /** | |
296 | * Count all entries for a user. | |
297 | * | |
298 | * @param int $userId | |
299 | * | |
e678c475 | 300 | * @return int |
5c072d2b NL |
301 | */ |
302 | public function countAllEntriesByUsername($userId) | |
303 | { | |
304 | $qb = $this->createQueryBuilder('e') | |
305 | ->select('count(e)') | |
306 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
307 | ; | |
308 | ||
309 | return $qb->getQuery()->getSingleScalarResult(); | |
310 | } | |
9d50517c | 311 | } |