diff options
author | Olivier Mehani <shtrom@ssji.net> | 2019-05-02 21:19:20 +1000 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2019-05-24 15:17:45 +0200 |
commit | 31e276fc1636b41b03b7c29127681de257c16b06 (patch) | |
tree | 7858796dcd094b5a61b38ddd3b7d84f5ea880b17 /src/Wallabag/ApiBundle | |
parent | 65b495e75b1d36894dd18133d3bf967a74e0b8de (diff) | |
download | wallabag-31e276fc1636b41b03b7c29127681de257c16b06.tar.gz wallabag-31e276fc1636b41b03b7c29127681de257c16b06.tar.zst wallabag-31e276fc1636b41b03b7c29127681de257c16b06.zip |
EntryRestController::getEntriesExistsAction: always find by hashed url
Simplify the logic from #3158 by hashing all the urls from the request,
and only doing a search by hash. This allows to get performance benefits
from the new indexed hash column even when using older clients that do
not hash the URL in the request.
Fixes: #3158, #3919
Signed-off-by: Olivier Mehani <shtrom@ssji.net>
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index aff0534a..17b53a01 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -43,50 +43,59 @@ class EntryRestController extends WallabagRestController | |||
43 | 43 | ||
44 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); | 44 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); |
45 | 45 | ||
46 | $urls = $request->query->get('urls', []); | ||
47 | $hashedUrls = $request->query->get('hashed_urls', []); | 46 | $hashedUrls = $request->query->get('hashed_urls', []); |
47 | $hashedUrl = $request->query->get('hashed_url', ''); | ||
48 | if (!empty($hashedUrl)) { | ||
49 | $hashedUrls[] = $hashedUrl; | ||
50 | } | ||
48 | 51 | ||
49 | // handle multiple urls first | 52 | $urls = $request->query->get('urls', []); |
50 | if (!empty($hashedUrls)) { | 53 | $url = $request->query->get('url', ''); |
51 | $results = []; | 54 | if (!empty($url)) { |
52 | foreach ($hashedUrls as $hashedUrl) { | 55 | $urls[] = $url; |
53 | $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId()); | 56 | } |
54 | |||
55 | $results[$hashedUrl] = $this->returnExistInformation($res, $returnId); | ||
56 | } | ||
57 | 57 | ||
58 | return $this->sendResponse($results); | 58 | $urlHashMap = []; |
59 | foreach($urls as $urlToHash) { | ||
60 | $urlHash = hash('sha1', $urlToHash); // XXX: the hash logic would better be in a separate util to avoid duplication with GenerateUrlHashesCommand::generateHashedUrls | ||
61 | $hashedUrls[] = $urlHash; | ||
62 | $urlHashMap[$urlHash] = $urlToHash; | ||
59 | } | 63 | } |
60 | 64 | ||
61 | // @deprecated, to be remove in 3.0 | 65 | if (empty($hashedUrls)) { |
62 | if (!empty($urls)) { | 66 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); |
63 | $results = []; | 67 | } |
64 | foreach ($urls as $url) { | ||
65 | $res = $repo->findByUrlAndUserId($url, $this->getUser()->getId()); | ||
66 | 68 | ||
67 | $results[$url] = $this->returnExistInformation($res, $returnId); | 69 | $results = []; |
68 | } | 70 | foreach ($hashedUrls as $hashedUrlToSearch) { |
71 | $res = $repo->findByHashedUrlAndUserId($hashedUrlToSearch, $this->getUser()->getId()); | ||
69 | 72 | ||
70 | return $this->sendResponse($results); | 73 | $results[$hashedUrlToSearch] = $this->returnExistInformation($res, $returnId); |
71 | } | 74 | } |
72 | 75 | ||
73 | // let's see if it is a simple url? | 76 | $results = $this->replaceUrlHashes($results, $urlHashMap); |
74 | $url = $request->query->get('url', ''); | ||
75 | $hashedUrl = $request->query->get('hashed_url', ''); | ||
76 | 77 | ||
77 | if (empty($url) && empty($hashedUrl)) { | 78 | if (!empty($url) || !empty($hashedUrl)) { |
78 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); | 79 | $hu = array_keys($results)[0]; |
80 | return $this->sendResponse(['exists' => $results[$hu]]); | ||
79 | } | 81 | } |
82 | return $this->sendResponse($results); | ||
83 | } | ||
80 | 84 | ||
81 | $method = 'findByUrlAndUserId'; | 85 | /** |
82 | if (!empty($hashedUrl)) { | 86 | * Replace the hashedUrl keys in $results with the unhashed URL from the |
83 | $method = 'findByHashedUrlAndUserId'; | 87 | * request, as recorded in $urlHashMap. |
84 | $url = $hashedUrl; | 88 | */ |
89 | private function replaceUrlHashes(array $results, array $urlHashMap) { | ||
90 | $newResults = []; | ||
91 | foreach($results as $hash => $res) { | ||
92 | if (isset($urlHashMap[$hash])) { | ||
93 | $newResults[$urlHashMap[$hash]] = $res; | ||
94 | } else { | ||
95 | $newResults[$hash] = $res; | ||
96 | } | ||
85 | } | 97 | } |
86 | 98 | return $newResults; | |
87 | $res = $repo->$method($url, $this->getUser()->getId()); | ||
88 | |||
89 | return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]); | ||
90 | } | 99 | } |
91 | 100 | ||
92 | /** | 101 | /** |