diff --git a/src/server.cpp b/src/server.cpp index eb2354356..75861b27a 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -530,18 +530,14 @@ Response InternalServer::handle_search(const RequestContext& request) printf("** running handle_search\n"); } - std::string content; - std::string mimeType; - std::string httpRedirection; - std::string bookName; - std::string patternString; std::string bookId; try { bookName = request.get_argument("content"); bookId = mp_nameMapper->getIdForName(bookName); } catch (const std::out_of_range&) {} + std::string patternString; try { patternString = request.get_argument("pattern"); } 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_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; if (reader) { searcher.add_reader(reader.get()); } else { - if (bookName.empty()) { - for (auto& bookId: m_library.filter(kiwix::Filter().local(true).valid(true))) { - auto currentReader = m_library.getReaderById(bookId); - if (currentReader) { - searcher.add_reader(currentReader.get()); - } + for (auto& bookId: m_library.filter(kiwix::Filter().local(true).valid(true))) { + auto currentReader = m_library.getReaderById(bookId); + if (currentReader) { + searcher.add_reader(currentReader.get()); } - } else { - response.set_content("\n
There is no article with the title \"" + kiwix::encodeDiples(patternString) + "\" and the fulltext search engine is not available for this content.
"); - response.set_code(MHD_HTTP_NOT_FOUND); } } - if (!patternString.empty() || has_geo_query) { - auto start = 0; - try { - start = request.get_argumentThere is no article with the title \"" + kiwix::encodeDiples(patternString) + "\" and the fulltext search engine is not available for this content.
"); - response.set_code(MHD_HTTP_NOT_FOUND); + 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; } return response; } diff --git a/static/resources_list.txt b/static/resources_list.txt index 43c2619d4..62cfe15de 100644 --- a/static/resources_list.txt +++ b/static/resources_list.txt @@ -21,6 +21,7 @@ skin/caret.png skin/taskbar.js skin/taskbar.css templates/search_result.html +templates/no_search_result.html templates/404.html templates/index.html templates/suggestion.json diff --git a/static/templates/no_search_result.html b/static/templates/no_search_result.html new file mode 100644 index 000000000..2346ac5b1 --- /dev/null +++ b/static/templates/no_search_result.html @@ -0,0 +1,103 @@ + + + + + ++ There is no article with the title "{{pattern}}" + and the fulltext search engine is not available for this content. +
+ +