]>
Commit | Line | Data |
---|---|---|
b3dc0749 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Repository; | |
4 | ||
5 | use Doctrine\ORM\EntityRepository; | |
6708bf23 | 6 | use Doctrine\ORM\QueryBuilder; |
52b84c11 | 7 | use Wallabag\CoreBundle\Entity\Tag; |
b3dc0749 | 8 | |
6d37a7e6 | 9 | class TagRepository extends EntityRepository |
b3dc0749 | 10 | { |
7244d6cb | 11 | /** |
28987583 | 12 | * Count all tags per user. |
7244d6cb | 13 | * |
fc732227 | 14 | * @param int $userId |
faa86e06 | 15 | * @param int $cacheLifeTime Duration of the cache for this query |
7244d6cb | 16 | * |
28987583 | 17 | * @return int |
7244d6cb | 18 | */ |
28987583 | 19 | public function countAllTags($userId, $cacheLifeTime = null) |
faa86e06 JB |
20 | { |
21 | $query = $this->createQueryBuilder('t') | |
28987583 | 22 | ->select('t.slug') |
faa86e06 JB |
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 | ||
2a1ceb67 | 34 | return \count($query->getArrayResult()); |
faa86e06 JB |
35 | } |
36 | ||
37 | /** | |
28bb4890 | 38 | * Find all tags per user. |
b0de88f7 JB |
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. | |
faa86e06 JB |
42 | * |
43 | * @param int $userId | |
44 | * | |
45 | * @return array | |
46 | */ | |
28bb4890 | 47 | public function findAllTags($userId) |
7244d6cb | 48 | { |
6708bf23 | 49 | $ids = $this->getQueryBuilderByUser($userId) |
b0de88f7 | 50 | ->select('t.id') |
faa86e06 | 51 | ->getQuery() |
28bb4890 | 52 | ->getArrayResult(); |
567421af | 53 | |
b0de88f7 JB |
54 | $tags = []; |
55 | foreach ($ids as $id) { | |
56 | $tags[] = $this->find($id); | |
57 | } | |
58 | ||
59 | return $tags; | |
206bade5 JB |
60 | } |
61 | ||
935e9fff NH |
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 | { | |
6708bf23 | 71 | return $this->getQueryBuilderByUser($userId) |
935e9fff NH |
72 | ->select('t.id, t.label, t.slug, count(e.id) as nbEntries') |
73 | ->distinct(true) | |
935e9fff NH |
74 | ->getQuery() |
75 | ->getArrayResult(); | |
76 | } | |
77 | ||
2a0e0a47 KD |
78 | public function findByLabelsAndUser($labels, $userId) |
79 | { | |
80 | $qb = $this->getQueryBuilderByUser($userId) | |
81 | ->select('t.id'); | |
82 | ||
83 | $ids = $qb->andWhere($qb->expr()->in('t.label', $labels)) | |
84 | ->getQuery() | |
85 | ->getArrayResult(); | |
86 | ||
87 | $tags = []; | |
88 | foreach ($ids as $id) { | |
89 | $tags[] = $this->find($id); | |
90 | } | |
91 | ||
92 | return $tags; | |
93 | } | |
94 | ||
567421af TC |
95 | /** |
96 | * Used only in test case to get a tag for our entry. | |
97 | * | |
98 | * @return Tag | |
99 | */ | |
e686a76d | 100 | public function findOneByEntryAndTagLabel($entry, $label) |
567421af TC |
101 | { |
102 | return $this->createQueryBuilder('t') | |
103 | ->leftJoin('t.entries', 'e') | |
104 | ->where('e.id = :entryId')->setParameter('entryId', $entry->getId()) | |
105 | ->andWhere('t.label = :label')->setParameter('label', $label) | |
106 | ->setMaxResults(1) | |
107 | ->getQuery() | |
108 | ->getSingleResult(); | |
109 | } | |
6da1aebc | 110 | |
13a592a1 | 111 | public function findForArchivedArticlesByUser($userId) |
6da1aebc | 112 | { |
6708bf23 | 113 | $ids = $this->getQueryBuilderByUser($userId) |
6da1aebc | 114 | ->select('t.id') |
6da1aebc | 115 | ->andWhere('e.isArchived = true') |
6da1aebc TC |
116 | ->getQuery() |
117 | ->getArrayResult(); | |
118 | ||
119 | $tags = []; | |
120 | foreach ($ids as $id) { | |
121 | $tags[] = $this->find($id); | |
122 | } | |
123 | ||
124 | return $tags; | |
125 | } | |
6708bf23 KD |
126 | |
127 | /** | |
128 | * Retrieve a sorted list of tags used by a user. | |
129 | * | |
130 | * @param int $userId | |
131 | * | |
132 | * @return QueryBuilder | |
133 | */ | |
134 | private function getQueryBuilderByUser($userId) | |
135 | { | |
136 | return $this->createQueryBuilder('t') | |
137 | ->leftJoin('t.entries', 'e') | |
138 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
139 | ->groupBy('t.id') | |
140 | ->orderBy('t.slug'); | |
141 | } | |
b3dc0749 | 142 | } |