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