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 ->getBuilderByUser($userId)
29 * Retrieves unread entries for a user.
33 * @return QueryBuilder
35 public function getBuilderForUnreadByUser($userId)
38 ->getBuilderByUser($userId)
39 ->andWhere('e.isArchived = false')
44 * Retrieves read entries for a user.
48 * @return QueryBuilder
50 public function getBuilderForArchiveByUser($userId)
53 ->getBuilderByUser($userId)
54 ->andWhere('e.isArchived = true')
59 * Retrieves starred entries for a user.
63 * @return QueryBuilder
65 public function getBuilderForStarredByUser($userId)
68 ->getBuilderByUser($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 ->getBuilderByUser($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 * Retrieves untagged entries for a user.
109 * @return QueryBuilder
111 public function getBuilderForUntaggedByUser($userId)
114 ->getBuilderByUser($userId)
115 ->andWhere('size(e.tags) = 0');
122 * @param bool $isArchived
123 * @param bool $isStarred
124 * @param bool $isPublic
125 * @param string $sort
126 * @param string $order
128 * @param string $tags
132 public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
134 $qb = $this->createQueryBuilder('e')
135 ->leftJoin('e.tags', 't')
136 ->where('e.user = :userId')->setParameter('userId', $userId);
138 if (null !== $isArchived) {
139 $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived);
142 if (null !== $isStarred) {
143 $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred);
146 if (null !== $isPublic) {
147 $qb->andWhere('e.uid IS ' . (true === $isPublic ? 'NOT' : '') . ' NULL');
151 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \
DateTime(date('Y-m-d H:i:s', $since)));
154 if (is_string($tags) && '' !== $tags) {
155 foreach (explode(',', $tags) as $i => $tag) {
156 $entryAlias = 'e' . $i;
157 $tagAlias = 't' . $i;
159 // Complexe queries to ensure multiple tags are associated to an entry
160 // https://stackoverflow.com/a/6638146/569101
161 $qb->andWhere($qb->expr()->in(
163 $this->createQueryBuilder($entryAlias)
164 ->select($entryAlias . '.id')
165 ->leftJoin($entryAlias . '.tags', $tagAlias)
166 ->where($tagAlias . '.label = :label' . $i)
170 // bound parameter to the main query builder
171 $qb->setParameter('label' . $i, $tag);
175 if ('created' === $sort) {
176 $qb->orderBy('e.id', $order);
177 } elseif ('updated' === $sort) {
178 $qb->orderBy('e.updatedAt', $order);
181 $pagerAdapter = new DoctrineORMAdapter($qb, true, false);
183 return new Pagerfanta($pagerAdapter);
187 * Fetch an entry with a tag. Only used for tests.
193 public function findOneWithTags($userId)
195 $qb = $this->createQueryBuilder('e')
196 ->innerJoin('e.tags', 't')
197 ->innerJoin('e.user', 'u')
198 ->addSelect('t', 'u')
199 ->where('e.user = :userId')->setParameter('userId', $userId)
202 return $qb->getQuery()->getResult();
206 * Find distinct language for a given user.
207 * Used to build the filter language list.
209 * @param int $userId User id
213 public function findDistinctLanguageByUser($userId)
215 $results = $this->createQueryBuilder('e')
216 ->select('e.language')
217 ->where('e.user = :userId')->setParameter('userId', $userId)
218 ->andWhere('e.language IS NOT NULL')
219 ->groupBy('e.language')
220 ->orderBy('e.language', ' ASC')
225 foreach ($results as $result) {
226 $languages[$result['language']] = $result['language'];
233 * Used only in test case to get the right entry associated to the right user.
235 * @param string $username
239 public function findOneByUsernameAndNotArchived($username)
241 return $this->createQueryBuilder('e')
242 ->leftJoin('e.user', 'u')
243 ->where('u.username = :username')->setParameter('username', $username)
244 ->andWhere('e.isArchived = false')
251 * Remove a tag from all user entries.
253 * 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.
254 * 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:
256 * 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
261 public function removeTag($userId, Tag
$tag)
263 $entries = $this->getBuilderByUser($userId)
264 ->innerJoin('e.tags', 't')
265 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
269 foreach ($entries as $entry) {
270 $entry->removeTag($tag);
273 $this->getEntityManager()->flush();
277 * Remove tags from all user entries.
280 * @param Array<Tag> $tags
282 public function removeTags($userId, $tags)
284 foreach ($tags as $tag) {
285 $this->removeTag($userId, $tag);
290 * Find all entries that are attached to a give tag id.
297 public function findAllByTagId($userId, $tagId)
299 return $this->getBuilderByUser($userId)
300 ->innerJoin('e.tags', 't')
301 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
307 * Find an entry by its url and its owner.
308 * If it exists, return the entry otherwise return false.
315 public function findByUrlAndUserId($url, $userId)
317 $res = $this->createQueryBuilder('e')
318 ->where('e.url = :url')->setParameter('url', urldecode($url))
319 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
324 return current($res);
331 * Count all entries for a user.
337 public function countAllEntriesByUser($userId)
339 $qb = $this->createQueryBuilder('e')
341 ->where('e.user = :userId')->setParameter('userId', $userId)
344 return (int) $qb->getQuery()->getSingleScalarResult();
348 * Remove all entries for a user id.
349 * Used when a user want to reset all informations.
353 public function removeAllByUserId($userId)
355 $this->getEntityManager()
356 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
357 ->setParameter('userId', $userId)
361 public function removeArchivedByUserId($userId)
363 $this->getEntityManager()
364 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE')
365 ->setParameter('userId', $userId)
370 * Get id and url from all entries
371 * Used for the clean-duplicates command.
373 public function findAllEntriesIdAndUrlByUserId($userId)
375 $qb = $this->createQueryBuilder('e')
376 ->select('e.id, e.url')
377 ->where('e.user = :userid')->setParameter(':userid', $userId);
379 return $qb->getQuery()->getArrayResult();
387 public function findAllEntriesIdByUserId($userId = null)
389 $qb = $this->createQueryBuilder('e')
392 if (null !== $userId) {
393 $qb->where('e.user = :userid')->setParameter(':userid', $userId);
396 return $qb->getQuery()->getArrayResult();
400 * Find all entries by url and owner.
407 public function findAllByUrlAndUserId($url, $userId)
409 return $this->createQueryBuilder('e')
410 ->where('e.url = :url')->setParameter('url', urldecode($url))
411 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
417 * Return a query builder to used by other getBuilderFor* method.
420 * @param string $sortBy
421 * @param string $direction
423 * @return QueryBuilder
425 private function getBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc')
427 return $this->createQueryBuilder('e')
428 ->andWhere('e.user = :userId')->setParameter('userId', $userId)
429 ->orderBy(sprintf('e.%s', $sortBy), $direction);