]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/TagRepository.php
bd2d9f9736aa7d59b75caec06f55fb1cb5e4c9b4
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / TagRepository.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6 use Doctrine\ORM\QueryBuilder;
7 use Wallabag\CoreBundle\Entity\Tag;
8
9 class TagRepository extends EntityRepository
10 {
11 /**
12 * Count all tags per user.
13 *
14 * @param int $userId
15 * @param int $cacheLifeTime Duration of the cache for this query
16 *
17 * @return int
18 */
19 public function countAllTags($userId, $cacheLifeTime = null)
20 {
21 $query = $this->createQueryBuilder('t')
22 ->select('t.slug')
23 ->leftJoin('t.entries', 'e')
24 ->where('e.user = :userId')->setParameter('userId', $userId)
25 ->groupBy('t.slug')
26 ->getQuery();
27
28 if (null !== $cacheLifeTime) {
29 $query->useQueryCache(true);
30 $query->useResultCache(true);
31 $query->setResultCacheLifetime($cacheLifeTime);
32 }
33
34 return \count($query->getArrayResult());
35 }
36
37 /**
38 * Find all tags per user.
39 * Instead of just left joined on the Entry table, we select only id and group by id to avoid tag multiplication in results.
40 * Once we have all tags id, we can safely request them one by one.
41 * This'll still be fastest than the previous query.
42 *
43 * @param int $userId
44 *
45 * @return array
46 */
47 public function findAllTags($userId)
48 {
49 $ids = $this->getQueryBuilderByUser($userId)
50 ->select('t.id')
51 ->getQuery()
52 ->getArrayResult();
53
54 $tags = [];
55 foreach ($ids as $id) {
56 $tags[] = $this->find($id);
57 }
58
59 return $tags;
60 }
61
62 /**
63 * Find all tags (flat) per user with nb entries.
64 *
65 * @param int $userId
66 *
67 * @return array
68 */
69 public function findAllFlatTagsWithNbEntries($userId)
70 {
71 return $this->getQueryBuilderByUser($userId)
72 ->select('t.id, t.label, t.slug, count(e.id) as nbEntries')
73 ->distinct(true)
74 ->getQuery()
75 ->getArrayResult();
76 }
77
78 /**
79 * Used only in test case to get a tag for our entry.
80 *
81 * @return Tag
82 */
83 public function findOneByEntryAndTagLabel($entry, $label)
84 {
85 return $this->createQueryBuilder('t')
86 ->leftJoin('t.entries', 'e')
87 ->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
88 ->andWhere('t.label = :label')->setParameter('label', $label)
89 ->setMaxResults(1)
90 ->getQuery()
91 ->getSingleResult();
92 }
93
94 public function findForArchivedArticlesByUser($userId)
95 {
96 $ids = $this->getQueryBuilderByUser($userId)
97 ->select('t.id')
98 ->andWhere('e.isArchived = true')
99 ->getQuery()
100 ->getArrayResult();
101
102 $tags = [];
103 foreach ($ids as $id) {
104 $tags[] = $this->find($id);
105 }
106
107 return $tags;
108 }
109
110 /**
111 * Retrieve a sorted list of tags used by a user.
112 *
113 * @param int $userId
114 *
115 * @return QueryBuilder
116 */
117 private function getQueryBuilderByUser($userId)
118 {
119 return $this->createQueryBuilder('t')
120 ->leftJoin('t.entries', 'e')
121 ->where('e.user = :userId')->setParameter('userId', $userId)
122 ->groupBy('t.id')
123 ->orderBy('t.slug');
124 }
125 }