3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Doctrine\ORM\Query
;
7 use Pagerfanta\Adapter\DoctrineORMAdapter
;
8 use Pagerfanta\Pagerfanta
;
9 use Wallabag\CoreBundle\Entity\Tag
;
11 class EntryRepository
extends EntityRepository
16 * Return a query builder to used by other getBuilderFor* method.
20 * @return QueryBuilder
22 private function getBuilderByUser($userId)
24 return $this->createQueryBuilder('e')
25 ->leftJoin('e.user', 'u')
26 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
27 ->orderBy('e.id', 'desc')
32 * Retrieves all entries for a user.
36 * @return QueryBuilder
38 public function getBuilderForAllByUser($userId)
41 ->getBuilderByUser($userId)
46 * Retrieves unread entries for a user.
50 * @return QueryBuilder
52 public function getBuilderForUnreadByUser($userId)
55 ->getBuilderByUser($userId)
56 ->andWhere('e.isArchived = false')
61 * Retrieves read entries for a user.
65 * @return QueryBuilder
67 public function getBuilderForArchiveByUser($userId)
70 ->getBuilderByUser($userId)
71 ->andWhere('e.isArchived = true')
76 * Retrieves starred entries for a user.
80 * @return QueryBuilder
82 public function getBuilderForStarredByUser($userId)
85 ->getBuilderByUser($userId)
86 ->andWhere('e.isStarred = true')
91 * Retrieves untagged entries for a user.
95 * @return QueryBuilder
97 public function getBuilderForUntaggedByUser($userId)
100 ->getBuilderByUser($userId)
101 ->leftJoin('e.tags', 't')
103 ->having('count(t.id) = 0');
110 * @param bool $isArchived
111 * @param bool $isStarred
112 * @param string $sort
113 * @param string $order
115 * @param string $tags
119 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
121 $qb = $this->createQueryBuilder('e')
122 ->leftJoin('e.tags', 't')
123 ->where('e.user =:userId')->setParameter('userId', $userId);
125 if (null !== $isArchived) {
126 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
129 if (null !== $isStarred) {
130 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
134 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \
DateTime(date('Y-m-d H:i:s', $since)));
138 foreach (explode(',', $tags) as $tag) {
139 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
143 if ('created' === $sort) {
144 $qb->orderBy('e.id', $order);
145 } elseif ('updated' === $sort) {
146 $qb->orderBy('e.updatedAt', $order);
149 $pagerAdapter = new DoctrineORMAdapter($qb);
151 return new Pagerfanta($pagerAdapter);
155 * Fetch an entry with a tag. Only used for tests.
161 public function findOneWithTags($userId)
163 $qb = $this->createQueryBuilder('e')
164 ->innerJoin('e.tags', 't')
165 ->innerJoin('e.user', 'u')
166 ->addSelect('t', 'u')
167 ->where('e.user=:userId')->setParameter('userId', $userId)
170 return $qb->getQuery()->getResult();
174 * Find distinct language for a given user.
175 * Used to build the filter language list.
177 * @param int $userId User id
181 public function findDistinctLanguageByUser($userId)
183 $results = $this->createQueryBuilder('e')
184 ->select('e.language')
185 ->where('e.user = :userId')->setParameter('userId', $userId)
186 ->andWhere('e.language IS NOT NULL')
187 ->groupBy('e.language')
188 ->orderBy('e.language', ' ASC')
193 foreach ($results as $result) {
194 $languages[$result['language']] = $result['language'];
201 * Used only in test case to get the right entry associated to the right user.
203 * @param string $username
207 public function findOneByUsernameAndNotArchived($username)
209 return $this->createQueryBuilder('e')
210 ->leftJoin('e.user', 'u')
211 ->where('u.username = :username')->setParameter('username', $username)
212 ->andWhere('e.isArchived = false')
219 * Remove a tag from all user entries.
221 * 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.
222 * 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:
224 * 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
229 public function removeTag($userId, Tag
$tag)
231 $entries = $this->getBuilderByUser($userId)
232 ->innerJoin('e.tags', 't')
233 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
237 foreach ($entries as $entry) {
238 $entry->removeTag($tag);
241 $this->getEntityManager()->flush();
245 * Remove tags from all user entries.
248 * @param Array<Tag> $tags
250 public function removeTags($userId, $tags)
252 foreach ($tags as $tag) {
253 $this->removeTag($userId, $tag);
258 * Find all entries that are attached to a give tag id.
265 public function findAllByTagId($userId, $tagId)
267 return $this->getBuilderByUser($userId)
268 ->innerJoin('e.tags', 't')
269 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
275 * Find an entry by its url and its owner.
276 * If it exists, return the entry otherwise return false.
283 public function findByUrlAndUserId($url, $userId)
285 $res = $this->createQueryBuilder('e')
286 ->where('e.url = :url')->setParameter('url', $url)
287 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
292 return current($res);
299 * Count all entries for a user.
305 public function countAllEntriesByUsername($userId)
307 $qb = $this->createQueryBuilder('e')
309 ->where('e.user=:userId')->setParameter('userId', $userId)
312 return $qb->getQuery()->getSingleScalarResult();
315 public function setLifeTime($lifeTime)
317 $this->lifeTime
= $lifeTime;
321 * Enable cache for a query.
323 * @param Query $query
327 public function enableCache(Query
$query)
329 $query->useQueryCache(true);
330 $query->useResultCache(true);
331 $query->setResultCacheLifetime($this->lifeTime
);