X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FRepository%2FEntryRepository.php;h=3990932e14a1261dcf49b3b9283fc5ede0182caf;hb=92a66835624acf6fd14f5adc5f8aab399658592e;hp=6107e19ec65244f32efe322cfc51e2f533ae3795;hpb=f85d220c19872dd7e9199fd150060555584a2886;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 6107e19e..3990932e 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -3,6 +3,7 @@ namespace Wallabag\CoreBundle\Repository; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\NoResultException; use Doctrine\ORM\QueryBuilder; use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Pagerfanta; @@ -138,15 +139,30 @@ class EntryRepository extends EntityRepository * @param string $order * @param int $since * @param string $tags + * @param string $detail 'metadata' or 'full'. Include content field if 'full' + * + * @todo Breaking change: replace default detail=full by detail=metadata in a future version * * @return Pagerfanta */ - public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '') + public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '', $detail = 'full') { + if (!\in_array(strtolower($detail), ['full', 'metadata'], true)) { + throw new \Exception('Detail "' . $detail . '" parameter is wrong, allowed: full or metadata'); + } + $qb = $this->createQueryBuilder('e') ->leftJoin('e.tags', 't') ->where('e.user = :userId')->setParameter('userId', $userId); + if ('metadata' === $detail) { + $fieldNames = $this->getClassMetadata()->getFieldNames(); + $fields = array_filter($fieldNames, function ($k) { + return 'content' !== $k; + }); + $qb->select(sprintf('partial e.{%s}', implode(',', $fields))); + } + if (null !== $isArchived) { $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); } @@ -325,8 +341,8 @@ class EntryRepository extends EntityRepository * Find an entry by its url and its owner. * If it exists, return the entry otherwise return false. * - * @param $url - * @param $userId + * @param string $url + * @param int $userId * * @return Entry|bool */ @@ -345,6 +361,30 @@ class EntryRepository extends EntityRepository return false; } + /** + * Find an entry by its hashed url and its owner. + * If it exists, return the entry otherwise return false. + * + * @param string $hashedUrl Url hashed using sha1 + * @param int $userId + * + * @return Entry|bool + */ + public function findByHashedUrlAndUserId($hashedUrl, $userId) + { + $res = $this->createQueryBuilder('e') + ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) + ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) + ->getQuery() + ->getResult(); + + if (\count($res)) { + return current($res); + } + + return false; + } + /** * Count all entries for a user. * @@ -417,8 +457,8 @@ class EntryRepository extends EntityRepository /** * Find all entries by url and owner. * - * @param $url - * @param $userId + * @param string $url + * @param int $userId * * @return array */ @@ -434,46 +474,44 @@ class EntryRepository extends EntityRepository /** * Returns a random entry, filtering by status. * - * @param $userId - * @param string $status can be unread, archive or starred + * @param int $userId + * @param string $type Can be unread, archive, starred, etc * - * @throws \Doctrine\ORM\NoResultException - * @throws \Doctrine\ORM\NonUniqueResultException + * @throws NoResultException * * @return Entry */ - public function getRandomEntry($userId, $status = '') + public function getRandomEntry($userId, $type = '') { - $max = $this->getEntityManager() - ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') - ->setParameter('userId', $userId) - ->getSingleScalarResult(); - - $qb = $this->createQueryBuilder('e') - ->where('e.user = :user_id')->setParameter('user_id', $userId); + $qb = $this->getQueryBuilderByUser($userId) + ->select('e.id'); - if ('unread' === $status) { - $qb->andWhere('e.isArchived = false'); + switch ($type) { + case 'unread': + $qb->andWhere('e.isArchived = false'); + break; + case 'archive': + $qb->andWhere('e.isArchived = true'); + break; + case 'starred': + $qb->andWhere('e.isStarred = true'); + break; + case 'untagged': + $qb->leftJoin('e.tags', 't'); + $qb->andWhere('t.id is null'); + break; } - if ('archive' === $status) { - $qb->andWhere('e.isArchived = true'); - } + $ids = $qb->getQuery()->getArrayResult(); - if ('starred' === $status) { - $qb->andWhere('e.isStarred = true'); + if (empty($ids)) { + throw new NoResultException(); } - if ('untagged' === $status) { - $qb->leftJoin('e.tags', 't'); - $qb->andWhere('t.id is null'); - } + // random select one in the list + $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id']; - return $qb->andWhere('e.id >= :rand') - ->setParameter('rand', rand(0, $max)) - ->setMaxResults(1) - ->getQuery() - ->getSingleResult(); + return $this->find($randomId); } /**