]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/TagRepository.php
TagRestController: rewrite delete actions to only retrieve tags related to the user
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / TagRepository.php
index 005142fc568707a9b2e807f390dbf2cca8814fb2..8464a6a55004c27cce23c5a2283af01dd56c1a74 100644 (file)
 namespace Wallabag\CoreBundle\Repository;
 
 use Doctrine\ORM\EntityRepository;
-use Doctrine\ORM\Tools\Pagination\Paginator;
+use Doctrine\ORM\QueryBuilder;
+use Wallabag\CoreBundle\Entity\Tag;
 
-class EntryRepository extends EntityRepository
+class TagRepository extends EntityRepository
 {
     /**
-     * Retrieves unread entries for a user
+     * Count all tags per user.
      *
      * @param int $userId
-     * @param int $firstResult
-     * @param int $maxResults
+     * @param int $cacheLifeTime Duration of the cache for this query
      *
-     * @return Paginator
+     * @return int
      */
-    public function findUnreadByUser($userId, $firstResult, $maxResults = 12)
+    public function countAllTags($userId, $cacheLifeTime = null)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->setFirstResult($firstResult)
-            ->setMaxResults($maxResults)
-            ->leftJoin('e.user', 'u')
-            ->where('e.isArchived = false')
-            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
-            ->orderBy('e.createdAt', 'desc')
+        $query = $this->createQueryBuilder('t')
+            ->select('t.slug')
+            ->leftJoin('t.entries', 'e')
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->groupBy('t.slug')
             ->getQuery();
 
-        $paginator = new Paginator($qb);
+        if (null !== $cacheLifeTime) {
+            $query->useQueryCache(true);
+            $query->useResultCache(true);
+            $query->setResultCacheLifetime($cacheLifeTime);
+        }
 
-        return $paginator;
+        return \count($query->getArrayResult());
     }
 
     /**
-     * Retrieves read entries for a user
+     * Find all tags per user.
+     * Instead of just left joined on the Entry table, we select only id and group by id to avoid tag multiplication in results.
+     * Once we have all tags id, we can safely request them one by one.
+     * This'll still be fastest than the previous query.
      *
      * @param int $userId
-     * @param int $firstResult
-     * @param int $maxResults
      *
-     * @return Paginator
+     * @return array
      */
-    public function findArchiveByUser($userId, $firstResult, $maxResults = 12)
+    public function findAllTags($userId)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->select('e')
-            ->setFirstResult($firstResult)
-            ->setMaxResults($maxResults)
-            ->leftJoin('e.user', 'u')
-            ->where('e.isArchived = true')
-            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
-            ->orderBy('e.createdAt', 'desc')
-            ->getQuery();
+        $ids = $this->getQueryBuilderByUser($userId)
+            ->select('t.id')
+            ->getQuery()
+            ->getArrayResult();
 
-        $paginator = new Paginator($qb);
+        $tags = [];
+        foreach ($ids as $id) {
+            $tags[] = $this->find($id);
+        }
 
-        return $paginator;
+        return $tags;
     }
 
     /**
-     * Retrieves starred entries for a user
+     * Find all tags (flat) per user with nb entries.
      *
      * @param int $userId
-     * @param int $firstResult
-     * @param int $maxResults
      *
-     * @return Paginator
+     * @return array
      */
-    public function findStarredByUser($userId, $firstResult, $maxResults = 12)
+    public function findAllFlatTagsWithNbEntries($userId)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->select('e')
-            ->setFirstResult($firstResult)
-            ->setMaxResults($maxResults)
-            ->leftJoin('e.user', 'u')
-            ->where('e.isStarred = true')
-            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
-            ->orderBy('e.createdAt', 'desc')
-            ->getQuery();
+        return $this->getQueryBuilderByUser($userId)
+            ->select('t.id, t.label, t.slug, count(e.id) as nbEntries')
+            ->distinct(true)
+            ->getQuery()
+            ->getArrayResult();
+    }
+
+    public function findByLabelsAndUser($labels, $userId)
+    {
+        $qb = $this->getQueryBuilderByUser($userId)
+            ->select('t.id');
 
-        $paginator = new Paginator($qb);
+        $ids = $qb->andWhere($qb->expr()->in('t.label', $labels))
+            ->getQuery()
+            ->getArrayResult();
 
-        return $paginator;
+        $tags = [];
+        foreach ($ids as $id) {
+            $tags[] = $this->find($id);
+        }
+
+        return $tags;
     }
 
     /**
-     * Find Entries
-     *
-     * @param int    $userId
-     * @param bool   $isArchived
-     * @param bool   $isStarred
-     * @param string $sort
-     * @param string $order
+     * Used only in test case to get a tag for our entry.
      *
-     * @return array
+     * @return Tag
      */
-    public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
+    public function findOneByEntryAndTagLabel($entry, $label)
     {
-        $qb = $this->createQueryBuilder('e')
-            ->where('e.user =:userId')->setParameter('userId', $userId);
+        return $this->createQueryBuilder('t')
+            ->leftJoin('t.entries', 'e')
+            ->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
+            ->andWhere('t.label = :label')->setParameter('label', $label)
+            ->setMaxResults(1)
+            ->getQuery()
+            ->getSingleResult();
+    }
 
-        if (null !== $isArchived) {
-            $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
-        }
+    public function findForArchivedArticlesByUser($userId)
+    {
+        $ids = $this->getQueryBuilderByUser($userId)
+            ->select('t.id')
+            ->andWhere('e.isArchived = true')
+            ->getQuery()
+            ->getArrayResult();
 
-        if (null !== $isStarred) {
-            $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
+        $tags = [];
+        foreach ($ids as $id) {
+            $tags[] = $this->find($id);
         }
 
-        if ('created' === $sort) {
-            $qb->orderBy('e.createdAt', $order);
-        } elseif ('updated' === $sort) {
-            $qb->orderBy('e.updatedAt', $order);
-        }
+        return $tags;
+    }
 
-        return $qb
-            ->getQuery()
-            ->getResult();
+    /**
+     * Retrieve a sorted list of tags used by a user.
+     *
+     * @param int $userId
+     *
+     * @return QueryBuilder
+     */
+    private function getQueryBuilderByUser($userId)
+    {
+        return $this->createQueryBuilder('t')
+            ->leftJoin('t.entries', 'e')
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->groupBy('t.id')
+            ->orderBy('t.slug');
     }
 }