3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Doctrine\ORM\QueryBuilder
;
7 use Pagerfanta\Adapter\DoctrineORMAdapter
;
8 use Pagerfanta\Pagerfanta
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Entity\Tag
;
12 class EntryRepository
extends EntityRepository
15 * Retrieves all entries for a user.
19 * @return QueryBuilder
21 public function getBuilderForAllByUser($userId)
24 ->getSortedQueryBuilderByUser($userId)
29 * Retrieves unread entries for a user.
33 * @return QueryBuilder
35 public function getBuilderForUnreadByUser($userId)
38 ->getSortedQueryBuilderByUser($userId)
39 ->andWhere('e.isArchived = false')
44 * Retrieves read entries for a user.
48 * @return QueryBuilder
50 public function getBuilderForArchiveByUser($userId)
53 ->getSortedQueryBuilderByUser($userId, 'archivedAt', 'desc')
54 ->andWhere('e.isArchived = true')
59 * Retrieves starred entries for a user.
63 * @return QueryBuilder
65 public function getBuilderForStarredByUser($userId)
68 ->getSortedQueryBuilderByUser($userId, 'starredAt', 'desc')
69 ->andWhere('e.isStarred = true')
74 * Retrieves entries filtered with a search term for a user.
78 * @param string $currentRoute
80 * @return QueryBuilder
82 public function getBuilderForSearchByUser($userId, $term, $currentRoute)
85 ->getSortedQueryBuilderByUser($userId);
87 if ('starred' === $currentRoute) {
88 $qb->andWhere('e.isStarred = true');
89 } elseif ('unread' === $currentRoute) {
90 $qb->andWhere('e.isArchived = false');
91 } elseif ('archive' === $currentRoute) {
92 $qb->andWhere('e.isArchived = true');
95 // We lower() all parts here because PostgreSQL 'LIKE' verb is case-sensitive
97 ->andWhere('lower(e.content) LIKE lower(:term) OR lower(e.title) LIKE lower(:term) OR lower(e.url) LIKE lower(:term)')->setParameter('term', '%' . $term . '%')
98 ->leftJoin('e.tags', 't')
105 * Retrieve a sorted list of untagged entries for a user.
109 * @return QueryBuilder
111 public function getBuilderForUntaggedByUser($userId)
114 ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
118 * Retrieve untagged entries for a user.
122 * @return QueryBuilder
124 public function getRawBuilderForUntaggedByUser($userId)
126 return $this->getQueryBuilderByUser($userId)
127 ->leftJoin('e.tags', 't')
128 ->andWhere('t.id is null');
135 * @param bool $isArchived
136 * @param bool $isStarred
137 * @param bool $isPublic
138 * @param string $sort
139 * @param string $order
141 * @param string $tags
145 public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
147 $qb = $this->createQueryBuilder('e')
148 ->leftJoin('e.tags', 't')
149 ->where('e.user = :userId')->setParameter('userId', $userId);
151 if (null !== $isArchived) {
152 $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived);
155 if (null !== $isStarred) {
156 $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred);
159 if (null !== $isPublic) {
160 $qb->andWhere('e.uid IS ' . (true === $isPublic ? 'NOT' : '') . ' NULL');
164 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \
DateTime(date('Y-m-d H:i:s', $since)));
167 if (\
is_string($tags) && '' !== $tags) {
168 foreach (explode(',', $tags) as $i => $tag) {
169 $entryAlias = 'e' . $i;
170 $tagAlias = 't' . $i;
172 // Complexe queries to ensure multiple tags are associated to an entry
173 // https://stackoverflow.com/a/6638146/569101
174 $qb->andWhere($qb->expr()->in(
176 $this->createQueryBuilder($entryAlias)
177 ->select($entryAlias . '.id')
178 ->leftJoin($entryAlias . '.tags', $tagAlias)
179 ->where($tagAlias . '.label = :label' . $i)
183 // bound parameter to the main query builder
184 $qb->setParameter('label' . $i, $tag);
188 if ('created' === $sort) {
189 $qb->orderBy('e.id', $order);
190 } elseif ('updated' === $sort) {
191 $qb->orderBy('e.updatedAt', $order);
192 } elseif ('archived' === $sort) {
193 $qb->orderBy('e.archivedAt', $order);
196 $pagerAdapter = new DoctrineORMAdapter($qb, true, false);
198 return new Pagerfanta($pagerAdapter);
202 * Fetch an entry with a tag. Only used for tests.
208 public function findOneWithTags($userId)
210 $qb = $this->createQueryBuilder('e')
211 ->innerJoin('e.tags', 't')
212 ->innerJoin('e.user', 'u')
213 ->addSelect('t', 'u')
214 ->where('e.user = :userId')->setParameter('userId', $userId)
217 return $qb->getQuery()->getResult();
221 * Find distinct language for a given user.
222 * Used to build the filter language list.
224 * @param int $userId User id
228 public function findDistinctLanguageByUser($userId)
230 $results = $this->createQueryBuilder('e')
231 ->select('e.language')
232 ->where('e.user = :userId')->setParameter('userId', $userId)
233 ->andWhere('e.language IS NOT NULL')
234 ->groupBy('e.language')
235 ->orderBy('e.language', ' ASC')
240 foreach ($results as $result) {
241 $languages[$result['language']] = $result['language'];
248 * Used only in test case to get the right entry associated to the right user.
250 * @param string $username
254 public function findOneByUsernameAndNotArchived($username)
256 return $this->createQueryBuilder('e')
257 ->leftJoin('e.user', 'u')
258 ->where('u.username = :username')->setParameter('username', $username)
259 ->andWhere('e.isArchived = false')
266 * Remove a tag from all user entries.
268 * 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.
269 * 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:
271 * 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
276 public function removeTag($userId, Tag
$tag)
278 $entries = $this->getSortedQueryBuilderByUser($userId)
279 ->innerJoin('e.tags', 't')
280 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
284 foreach ($entries as $entry) {
285 $entry->removeTag($tag);
288 $this->getEntityManager()->flush();
292 * Remove tags from all user entries.
295 * @param Array<Tag> $tags
297 public function removeTags($userId, $tags)
299 foreach ($tags as $tag) {
300 $this->removeTag($userId, $tag);
305 * Find all entries that are attached to a give tag id.
312 public function findAllByTagId($userId, $tagId)
314 return $this->getSortedQueryBuilderByUser($userId)
315 ->innerJoin('e.tags', 't')
316 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
322 * Find an entry by its url and its owner.
323 * If it exists, return the entry otherwise return false.
330 public function findByUrlAndUserId($url, $userId)
332 $res = $this->createQueryBuilder('e')
333 ->where('e.url = :url')->setParameter('url', urldecode($url))
334 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
339 return current($res);
346 * Count all entries for a user.
352 public function countAllEntriesByUser($userId)
354 $qb = $this->createQueryBuilder('e')
356 ->where('e.user = :userId')->setParameter('userId', $userId)
359 return (int) $qb->getQuery()->getSingleScalarResult();
363 * Remove all entries for a user id.
364 * Used when a user want to reset all informations.
368 public function removeAllByUserId($userId)
370 $this->getEntityManager()
371 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
372 ->setParameter('userId', $userId)
376 public function removeArchivedByUserId($userId)
378 $this->getEntityManager()
379 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE')
380 ->setParameter('userId', $userId)
385 * Get id and url from all entries
386 * Used for the clean-duplicates command.
388 public function findAllEntriesIdAndUrlByUserId($userId)
390 $qb = $this->createQueryBuilder('e')
391 ->select('e.id, e.url')
392 ->where('e.user = :userid')->setParameter(':userid', $userId);
394 return $qb->getQuery()->getArrayResult();
402 public function findAllEntriesIdByUserId($userId = null)
404 $qb = $this->createQueryBuilder('e')
407 if (null !== $userId) {
408 $qb->where('e.user = :userid')->setParameter(':userid', $userId);
411 return $qb->getQuery()->getArrayResult();
415 * Find all entries by url and owner.
422 public function findAllByUrlAndUserId($url, $userId)
424 return $this->createQueryBuilder('e')
425 ->where('e.url = :url')->setParameter('url', urldecode($url))
426 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
432 * Return a query builder to be used by other getBuilderFor* method.
436 * @return QueryBuilder
438 private function getQueryBuilderByUser($userId)
440 return $this->createQueryBuilder('e')
441 ->andWhere('e.user = :userId')->setParameter('userId', $userId);
445 * Return a sorted query builder to be used by other getBuilderFor* method.
448 * @param string $sortBy
449 * @param string $direction
451 * @return QueryBuilder
453 private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc')
455 return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId), $sortBy, $direction);
459 * Return the given QueryBuilder with an orderBy() call.
461 * @param QueryBuilder $qb
462 * @param string $sortBy
463 * @param string $direction
465 * @return QueryBuilder
467 private function sortQueryBuilder(QueryBuilder
$qb, $sortBy = 'createdAt', $direction = 'desc')
470 ->orderBy(sprintf('e.%s', $sortBy), $direction);