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