mirror of https://github.com/kiwix/libkiwix.git
Reduce complexity of handle_search.
This commit is contained in:
parent
fae0918f49
commit
ce09375c6c
|
@ -530,18 +530,14 @@ Response InternalServer::handle_search(const RequestContext& request)
|
||||||
printf("** running handle_search\n");
|
printf("** running handle_search\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string content;
|
|
||||||
std::string mimeType;
|
|
||||||
std::string httpRedirection;
|
|
||||||
|
|
||||||
std::string bookName;
|
std::string bookName;
|
||||||
std::string patternString;
|
|
||||||
std::string bookId;
|
std::string bookId;
|
||||||
try {
|
try {
|
||||||
bookName = request.get_argument("content");
|
bookName = request.get_argument("content");
|
||||||
bookId = mp_nameMapper->getIdForName(bookName);
|
bookId = mp_nameMapper->getIdForName(bookName);
|
||||||
} catch (const std::out_of_range&) {}
|
} catch (const std::out_of_range&) {}
|
||||||
|
|
||||||
|
std::string patternString;
|
||||||
try {
|
try {
|
||||||
patternString = request.get_argument("pattern");
|
patternString = request.get_argument("pattern");
|
||||||
} catch (const std::out_of_range&) {}
|
} catch (const std::out_of_range&) {}
|
||||||
|
@ -595,62 +591,61 @@ Response InternalServer::handle_search(const RequestContext& request)
|
||||||
response.set_taskbar(bookName, reader ? reader->getTitle() : "");
|
response.set_taskbar(bookName, reader ? reader->getTitle() : "");
|
||||||
response.set_compress(true);
|
response.set_compress(true);
|
||||||
|
|
||||||
|
if ( (!reader && !bookName.empty())
|
||||||
|
|| (patternString.empty() && ! has_geo_query) ) {
|
||||||
|
auto data = get_default_data();
|
||||||
|
data.set("pattern", encodeDiples(patternString));
|
||||||
|
response.set_template(RESOURCE::templates::no_search_result_html, data);
|
||||||
|
response.set_code(MHD_HTTP_NOT_FOUND);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
Searcher searcher;
|
Searcher searcher;
|
||||||
if (reader) {
|
if (reader) {
|
||||||
searcher.add_reader(reader.get());
|
searcher.add_reader(reader.get());
|
||||||
} else {
|
} else {
|
||||||
if (bookName.empty()) {
|
for (auto& bookId: m_library.filter(kiwix::Filter().local(true).valid(true))) {
|
||||||
for (auto& bookId: m_library.filter(kiwix::Filter().local(true).valid(true))) {
|
auto currentReader = m_library.getReaderById(bookId);
|
||||||
auto currentReader = m_library.getReaderById(bookId);
|
if (currentReader) {
|
||||||
if (currentReader) {
|
searcher.add_reader(currentReader.get());
|
||||||
searcher.add_reader(currentReader.get());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
response.set_content("<!DOCTYPE html>\n<html><head><meta content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\" /><title>Fulltext search unavailable</title></head><body><h1>Not Found</h1><p>There is no article with the title <b>\"" + kiwix::encodeDiples(patternString) + "\"</b> and the fulltext search engine is not available for this content.</p></body></html>");
|
|
||||||
response.set_code(MHD_HTTP_NOT_FOUND);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!patternString.empty() || has_geo_query) {
|
auto start = 0;
|
||||||
auto start = 0;
|
try {
|
||||||
try {
|
start = request.get_argument<unsigned int>("start");
|
||||||
start = request.get_argument<unsigned int>("start");
|
} catch (const std::exception&) {}
|
||||||
} catch (const std::exception&) {}
|
auto end = 25;
|
||||||
auto end = 25;
|
try {
|
||||||
try {
|
end = request.get_argument<unsigned int>("end");
|
||||||
end = request.get_argument<unsigned int>("end");
|
} catch (const std::exception&) {}
|
||||||
} catch (const std::exception&) {}
|
if (start>end) {
|
||||||
if (start>end) {
|
auto tmp = start;
|
||||||
auto tmp = start;
|
start = end;
|
||||||
start = end;
|
end = tmp;
|
||||||
end = tmp;
|
}
|
||||||
}
|
if (end > start + MAX_SEARCH_LEN) {
|
||||||
if (end > start + MAX_SEARCH_LEN) {
|
end = start + MAX_SEARCH_LEN;
|
||||||
end = start + MAX_SEARCH_LEN;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Get the results */
|
/* Get the results */
|
||||||
try {
|
try {
|
||||||
if (patternString.empty()) {
|
if (patternString.empty()) {
|
||||||
searcher.geo_search(latitude, longitude, distance,
|
searcher.geo_search(latitude, longitude, distance,
|
||||||
start, end, m_verbose.load());
|
start, end, m_verbose.load());
|
||||||
} else {
|
} else {
|
||||||
searcher.search(patternString,
|
searcher.search(patternString,
|
||||||
start, end, m_verbose.load());
|
start, end, m_verbose.load());
|
||||||
}
|
|
||||||
SearchRenderer renderer(&searcher, mp_nameMapper);
|
|
||||||
renderer.setSearchPattern(patternString);
|
|
||||||
renderer.setSearchContent(bookName);
|
|
||||||
renderer.setProtocolPrefix(m_root + "/");
|
|
||||||
renderer.setSearchProtocolPrefix(m_root + "/search?");
|
|
||||||
response.set_content(renderer.getHtml());
|
|
||||||
} catch (const std::exception& e) {
|
|
||||||
std::cerr << e.what() << std::endl;
|
|
||||||
}
|
}
|
||||||
} else {
|
SearchRenderer renderer(&searcher, mp_nameMapper);
|
||||||
response.set_content("<!DOCTYPE html>\n<html><head><meta content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\" /><title>Fulltext search unavailable</title></head><body><h1>Not Found</h1><p>There is no article with the title <b>\"" + kiwix::encodeDiples(patternString) + "\"</b> and the fulltext search engine is not available for this content.</p></body></html>");
|
renderer.setSearchPattern(patternString);
|
||||||
response.set_code(MHD_HTTP_NOT_FOUND);
|
renderer.setSearchContent(bookName);
|
||||||
|
renderer.setProtocolPrefix(m_root + "/");
|
||||||
|
renderer.setSearchProtocolPrefix(m_root + "/search?");
|
||||||
|
response.set_content(renderer.getHtml());
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ skin/caret.png
|
||||||
skin/taskbar.js
|
skin/taskbar.js
|
||||||
skin/taskbar.css
|
skin/taskbar.css
|
||||||
templates/search_result.html
|
templates/search_result.html
|
||||||
|
templates/no_search_result.html
|
||||||
templates/404.html
|
templates/404.html
|
||||||
templates/index.html
|
templates/index.html
|
||||||
templates/suggestion.json
|
templates/suggestion.json
|
||||||
|
|
|
@ -0,0 +1,103 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
|
||||||
|
<style type="text/css">
|
||||||
|
body{
|
||||||
|
color: #00000;
|
||||||
|
font: small/normal Arial,Helvetica,Sans-Serif;
|
||||||
|
margin-top: 0.5em;
|
||||||
|
font-size: 90%;
|
||||||
|
}
|
||||||
|
|
||||||
|
a{
|
||||||
|
color: #04c;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:visited {
|
||||||
|
color: #639
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover {
|
||||||
|
text-decoration: underline
|
||||||
|
}
|
||||||
|
|
||||||
|
.header {
|
||||||
|
font-size: 120%;
|
||||||
|
}
|
||||||
|
|
||||||
|
ul {
|
||||||
|
margin:0;
|
||||||
|
padding:0
|
||||||
|
}
|
||||||
|
|
||||||
|
.results {
|
||||||
|
font-size: 110%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.results li {
|
||||||
|
list-style-type:none;
|
||||||
|
margin-top: 0.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.results a {
|
||||||
|
font-size: 110%;
|
||||||
|
text-decoration: underline
|
||||||
|
}
|
||||||
|
|
||||||
|
cite {
|
||||||
|
font-style:normal;
|
||||||
|
word-wrap:break-word;
|
||||||
|
display: block;
|
||||||
|
font-size: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.informations {
|
||||||
|
color: #388222;
|
||||||
|
font-size: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer {
|
||||||
|
padding: 0;
|
||||||
|
margin-top: 1em;
|
||||||
|
width: 100%;
|
||||||
|
float: left
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer a, .footer span {
|
||||||
|
display: block;
|
||||||
|
padding: .3em .7em;
|
||||||
|
margin: 0 .38em 0 0;
|
||||||
|
text-align:center;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer a:hover {
|
||||||
|
background: #ededed;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer ul, .footer li {
|
||||||
|
list-style:none;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer li {
|
||||||
|
float: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
.selected {
|
||||||
|
background: #ededed;
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
<title>Fulltext search unavailable</title>
|
||||||
|
</head>
|
||||||
|
<body bgcolor="white">
|
||||||
|
<div class="header">Not found</div>
|
||||||
|
<p>
|
||||||
|
There is no article with the title <b> "{{pattern}}"</b>
|
||||||
|
and the fulltext search engine is not available for this content.
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue