diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 5158171ea..08e6d6483 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -67,11 +67,24 @@ class EntryRestController extends WallabagRestController throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); } - $results = []; - foreach ($hashedUrls as $hashedUrlToSearch) { - $res = $repo->findByHashedUrlAndUserId($hashedUrlToSearch, $this->getUser()->getId()); + $results = array_fill_keys($hashedUrls, null); + $res = $repo->findByUserIdAndBatchHashedUrls($this->getUser()->getId(), $hashedUrls); + foreach ($res as $e) { + $_hashedUrl = array_keys($hashedUrls, 'blah', true); + if ([] !== array_keys($hashedUrls, $e['hashedUrl'], true)) { + $_hashedUrl = $e['hashedUrl']; + } elseif ([] !== array_keys($hashedUrls, $e['hashedGivenUrl'], true)) { + $_hashedUrl = $e['hashedGivenUrl']; + } else { + continue; + } + $results[$_hashedUrl] = $e['id']; + } - $results[$hashedUrlToSearch] = $this->returnExistInformation($res, $returnId); + if (false === $returnId) { + $results = array_map(function ($v) { + return null !== $v; + }, $results); } $results = $this->replaceUrlHashes($results, $urlHashMap); @@ -840,21 +853,4 @@ class EntryRestController extends WallabagRestController 'origin_url' => $request->request->get('origin_url', ''), ]; } - - /** - * Return information about the entry if it exist and depending on the id or not. - * - * @param Entry|bool|null $entry - * @param bool $returnId - * - * @return bool|int - */ - private function returnExistInformation($entry, $returnId) - { - if ($returnId) { - return $entry instanceof Entry ? $entry->getId() : null; - } - - return $entry instanceof Entry; - } } diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 3beda9251..ed950e974 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -421,6 +421,22 @@ class EntryRepository extends EntityRepository return false; } + public function findByUserIdAndBatchHashedUrls($userId, $hashedUrls) + { + $qb = $this->createQueryBuilder('e')->select(['e.id', 'e.hashedUrl', 'e.hashedGivenUrl']); + $res = $qb->where('e.user = :user_id')->setParameter('user_id', $userId) + ->andWhere( + $qb->expr()->orX( + $qb->expr()->in('e.hashedUrl', $hashedUrls), + $qb->expr()->in('e.hashedGivenUrl', $hashedUrls) + ) + ) + ->getQuery() + ->getResult(); + + return $res; + } + /** * Count all entries for a user. *