From 4fffaf41b6c62d9537ed714358cd94f592fb9382 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 14 Nov 2017 17:42:01 +0100 Subject: [PATCH] Add a small (private) support for geo query. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This use the small APIĀ of kiwix-lib and so, cannot search a text query and filter around a geo position in the same time. There is no way to do a search but than write directly the search url by hand. If the request is wrongly formatted, the search is simply not done without error message. --- src/server/kiwix-serve.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index bd57154..232adaf 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -453,6 +453,26 @@ static struct MHD_Response* handle_search(RequestContext* request_context) std::string patternString = kiwix::urlDecode(pattern == NULL ? "" : string(pattern)); + + /* Retrive geo search */ + bool has_geo_query = false; + const char* latitude = MHD_lookup_connection_value( + request_context->connection, MHD_GET_ARGUMENT_KIND, "latitude"); + const char* longitude = MHD_lookup_connection_value( + request_context->connection, MHD_GET_ARGUMENT_KIND, "longitude"); + const char* distance = MHD_lookup_connection_value( + request_context->connection, MHD_GET_ARGUMENT_KIND, "distance"); + + float latitudeFloat(0), longitudeFloat(0), distanceFloat(0); + if (latitude != nullptr && longitude != nullptr && distance != nullptr) { + try { + latitudeFloat = stof(string(latitude)); + longitudeFloat = stof(string(longitude)); + distanceFloat = stof(string(distance)); + has_geo_query = true; + } catch (...) {} + } + /* Search results for searches from the welcome page should not be cached */ @@ -491,7 +511,13 @@ static struct MHD_Response* handle_search(RequestContext* request_context) /* Get the results */ pthread_mutex_lock(&searchLock); try { - request_context->searcher->search(patternString, startNumber, endNumber, isVerbose.load()); + if (patternString.empty() && has_geo_query) { + request_context->searcher->geo_search(latitudeFloat, longitudeFloat, distanceFloat, + startNumber, endNumber, isVerbose.load()); + } else { + request_context->searcher->search(patternString, + startNumber, endNumber, isVerbose.load()); + } content = request_context->searcher->getHtml(); } catch (const std::exception& e) { std::cerr << e.what() << std::endl;