diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository')
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 49 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php | 8 |
2 files changed, 39 insertions, 18 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index f5089729..16c44885 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -9,6 +9,7 @@ use Pagerfanta\Adapter\DoctrineORMAdapter; | |||
9 | use Pagerfanta\Pagerfanta; | 9 | use Pagerfanta\Pagerfanta; |
10 | use Wallabag\CoreBundle\Entity\Entry; | 10 | use Wallabag\CoreBundle\Entity\Entry; |
11 | use Wallabag\CoreBundle\Entity\Tag; | 11 | use Wallabag\CoreBundle\Entity\Tag; |
12 | use Wallabag\CoreBundle\Helper\UrlHasher; | ||
12 | 13 | ||
13 | class EntryRepository extends EntityRepository | 14 | class EntryRepository extends EntityRepository |
14 | { | 15 | { |
@@ -139,15 +140,30 @@ class EntryRepository extends EntityRepository | |||
139 | * @param string $order | 140 | * @param string $order |
140 | * @param int $since | 141 | * @param int $since |
141 | * @param string $tags | 142 | * @param string $tags |
143 | * @param string $detail 'metadata' or 'full'. Include content field if 'full' | ||
144 | * | ||
145 | * @todo Breaking change: replace default detail=full by detail=metadata in a future version | ||
142 | * | 146 | * |
143 | * @return Pagerfanta | 147 | * @return Pagerfanta |
144 | */ | 148 | */ |
145 | public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '') | 149 | public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '', $detail = 'full') |
146 | { | 150 | { |
151 | if (!\in_array(strtolower($detail), ['full', 'metadata'], true)) { | ||
152 | throw new \Exception('Detail "' . $detail . '" parameter is wrong, allowed: full or metadata'); | ||
153 | } | ||
154 | |||
147 | $qb = $this->createQueryBuilder('e') | 155 | $qb = $this->createQueryBuilder('e') |
148 | ->leftJoin('e.tags', 't') | 156 | ->leftJoin('e.tags', 't') |
149 | ->where('e.user = :userId')->setParameter('userId', $userId); | 157 | ->where('e.user = :userId')->setParameter('userId', $userId); |
150 | 158 | ||
159 | if ('metadata' === $detail) { | ||
160 | $fieldNames = $this->getClassMetadata()->getFieldNames(); | ||
161 | $fields = array_filter($fieldNames, function ($k) { | ||
162 | return 'content' !== $k; | ||
163 | }); | ||
164 | $qb->select(sprintf('partial e.{%s}', implode(',', $fields))); | ||
165 | } | ||
166 | |||
151 | if (null !== $isArchived) { | 167 | if (null !== $isArchived) { |
152 | $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); | 168 | $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); |
153 | } | 169 | } |
@@ -329,21 +345,14 @@ class EntryRepository extends EntityRepository | |||
329 | * @param string $url | 345 | * @param string $url |
330 | * @param int $userId | 346 | * @param int $userId |
331 | * | 347 | * |
332 | * @return Entry|bool | 348 | * @return Entry|false |
333 | */ | 349 | */ |
334 | public function findByUrlAndUserId($url, $userId) | 350 | public function findByUrlAndUserId($url, $userId) |
335 | { | 351 | { |
336 | $res = $this->createQueryBuilder('e') | 352 | return $this->findByHashedUrlAndUserId( |
337 | ->where('e.url = :url')->setParameter('url', urldecode($url)) | 353 | UrlHasher::hashUrl($url), |
338 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | 354 | $userId |
339 | ->getQuery() | 355 | ); |
340 | ->getResult(); | ||
341 | |||
342 | if (\count($res)) { | ||
343 | return current($res); | ||
344 | } | ||
345 | |||
346 | return false; | ||
347 | } | 356 | } |
348 | 357 | ||
349 | /** | 358 | /** |
@@ -353,10 +362,11 @@ class EntryRepository extends EntityRepository | |||
353 | * @param string $hashedUrl Url hashed using sha1 | 362 | * @param string $hashedUrl Url hashed using sha1 |
354 | * @param int $userId | 363 | * @param int $userId |
355 | * | 364 | * |
356 | * @return Entry|bool | 365 | * @return Entry|false |
357 | */ | 366 | */ |
358 | public function findByHashedUrlAndUserId($hashedUrl, $userId) | 367 | public function findByHashedUrlAndUserId($hashedUrl, $userId) |
359 | { | 368 | { |
369 | // try first using hashed_url (to use the database index) | ||
360 | $res = $this->createQueryBuilder('e') | 370 | $res = $this->createQueryBuilder('e') |
361 | ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) | 371 | ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) |
362 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | 372 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
@@ -367,6 +377,17 @@ class EntryRepository extends EntityRepository | |||
367 | return current($res); | 377 | return current($res); |
368 | } | 378 | } |
369 | 379 | ||
380 | // then try using hashed_given_url (to use the database index) | ||
381 | $res = $this->createQueryBuilder('e') | ||
382 | ->where('e.hashedGivenUrl = :hashed_given_url')->setParameter('hashed_given_url', $hashedUrl) | ||
383 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | ||
384 | ->getQuery() | ||
385 | ->getResult(); | ||
386 | |||
387 | if (\count($res)) { | ||
388 | return current($res); | ||
389 | } | ||
390 | |||
370 | return false; | 391 | return false; |
371 | } | 392 | } |
372 | 393 | ||
diff --git a/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php b/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php index b2e212a4..aeadd770 100644 --- a/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php +++ b/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php | |||
@@ -19,16 +19,16 @@ class SiteCredentialRepository extends \Doctrine\ORM\EntityRepository | |||
19 | /** | 19 | /** |
20 | * Retrieve one username/password for the given host and userId. | 20 | * Retrieve one username/password for the given host and userId. |
21 | * | 21 | * |
22 | * @param string $host | 22 | * @param array $hosts An array of host to look for |
23 | * @param int $userId | 23 | * @param int $userId |
24 | * | 24 | * |
25 | * @return array|null | 25 | * @return array|null |
26 | */ | 26 | */ |
27 | public function findOneByHostAndUser($host, $userId) | 27 | public function findOneByHostsAndUser($hosts, $userId) |
28 | { | 28 | { |
29 | $res = $this->createQueryBuilder('s') | 29 | $res = $this->createQueryBuilder('s') |
30 | ->select('s.username', 's.password') | 30 | ->select('s.username', 's.password') |
31 | ->where('s.host = :hostname')->setParameter('hostname', $host) | 31 | ->where('s.host IN (:hosts)')->setParameter('hosts', $hosts) |
32 | ->andWhere('s.user = :userId')->setParameter('userId', $userId) | 32 | ->andWhere('s.user = :userId')->setParameter('userId', $userId) |
33 | ->setMaxResults(1) | 33 | ->setMaxResults(1) |
34 | ->getQuery() | 34 | ->getQuery() |