Skip to content

Commit

Permalink
Add the language parameter in the xml result.
Browse files Browse the repository at this point in the history
As `language` is a declared argument for the search query, we need it in
`openSearch::Query` tag.

We are assuming that user follow the query described in
`searchdescription.xml` and that if `language` is specified, it is the
only book request argument provided.
  • Loading branch information
mgautierfr committed May 4, 2022
1 parent ec674ae commit baf1d03
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/search_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class SearchRenderer
*/
void setSearchPattern(const std::string& pattern);

/**
* Set the search lang used to do the search
*/
void setSearchLang(const std::string& lang);

/**
* Set the book names used to do the search.
*/
Expand Down Expand Up @@ -122,6 +127,7 @@ class SearchRenderer
Library* mp_library;
std::set<std::string> searchBookIds;
std::string searchPattern;
std::string searchLang;
std::string protocolPrefix;
std::string searchProtocolPrefix;
unsigned int pageLength;
Expand Down
16 changes: 14 additions & 2 deletions src/search_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ void SearchRenderer::setSearchPattern(const std::string& pattern)
searchPattern = pattern;
}

void SearchRenderer::setSearchLang(const std::string& lang)
{
searchLang = lang;
}

void SearchRenderer::setSearchBookIds(const std::set<std::string>& bookIds)
{
searchBookIds = bookIds;
Expand All @@ -90,14 +95,20 @@ kainjow::mustache::data buildQueryData
(
const std::string& searchProtocolPrefix,
const std::string& pattern,
const std::string& lang,
const std::set<std::string>& bookIds
) {
kainjow::mustache::data query;
query.set("pattern", kiwix::encodeDiples(pattern));
std::ostringstream ss;
ss << searchProtocolPrefix << "?pattern=" << urlEncode(pattern);
for (auto& bookId: bookIds) {
ss << "&books.id="<<urlEncode(bookId);
if(!lang.empty()) {
query.set("lang", kiwix::encodeDiples(lang));
ss << "?books.query.lang=" << urlEncode(lang);
} else {
for (auto& bookId: bookIds) {
ss << "&books.id="<<urlEncode(bookId);
}
}
query.set("unpaginatedQuery", ss.str());
return query;
Expand Down Expand Up @@ -197,6 +208,7 @@ std::string SearchRenderer::renderTemplate(const std::string& tmpl_str)
kainjow::mustache::data query = buildQueryData(
searchProtocolPrefix,
searchPattern,
searchLang,
searchBookIds
);

Expand Down
2 changes: 2 additions & 0 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
renderer.setSearchProtocolPrefix(m_root + "/search");
renderer.setPageLength(pageLength);
if (request.get_optional_param<std::string>("format", "") == "xml") {
auto lang = request.get_optional_param<std::string>("books.query.lang", "");
renderer.setSearchLang(lang);
return ContentResponse::build(*this, renderer.getXml(), "application/rss+xml; charset=utf-8",
/*isHomePage =*/false,
/*raw =*/true);
Expand Down
1 change: 1 addition & 0 deletions static/templates/search_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
searchTerms="{{query.pattern}}"
startIndex="{{results.start}}"
count="{{pagination.itemsPerPage}}"
{{#query.lang}}language="{{query.lang}}"{{/query.lang}}
/>
{{#results.items}}
<item>
Expand Down

0 comments on commit baf1d03

Please sign in to comment.