Don't propose a fulltext search if no fulltext index available

This commit is contained in:
kelson42 2014-08-31 12:27:00 -06:00
parent e629f59f6e
commit b38c4306d1

View File

@ -94,9 +94,10 @@ static pthread_mutex_t mimeTypeLock = PTHREAD_MUTEX_INITIALIZER;
/* Try to get the mimeType from the file extension */ /* Try to get the mimeType from the file extension */
static std::string getMimeTypeForFile(const std::string& filename) { static std::string getMimeTypeForFile(const std::string& filename) {
std::string mimeType = "text/plain"; std::string mimeType = "text/plain";
unsigned int pos = filename.find_last_of(".");
if (filename.find_last_of(".") != std::string::npos) { if (pos != std::string::npos) {
std::string extension = filename.substr(filename.find_last_of(".")+1); std::string extension = filename.substr(pos+1);
pthread_mutex_lock(&mimeTypeLock); pthread_mutex_lock(&mimeTypeLock);
if (extMimeTypes.find(extension) != extMimeTypes.end()) { if (extMimeTypes.find(extension) != extMimeTypes.end()) {
@ -180,7 +181,6 @@ static int accessHandlerCallback(void *cls,
std::string mimeType; std::string mimeType;
std::string httpRedirection; std::string httpRedirection;
unsigned int contentLength = 0; unsigned int contentLength = 0;
bool found = true;
bool cacheEnabled = true; bool cacheEnabled = true;
int httpResponseCode = MHD_HTTP_OK; int httpResponseCode = MHD_HTTP_OK;
std::string urlStr = string(url); std::string urlStr = string(url);
@ -234,8 +234,13 @@ static int accessHandlerCallback(void *cls,
suggestionCount++; suggestionCount++;
} }
content += (suggestionCount == 0 ? "" : ","); /* Propose the fulltext search if possible */
content += "{\"value\":\"" + std::string(term) + " \", \"label\":\"containing '" + std::string(term) + "'...\"}]"; if (searcher != NULL) {
content += (suggestionCount == 0 ? "" : ",");
content += "{\"value\":\"" + std::string(term) + " \", \"label\":\"containing '" + std::string(term) + "'...\"}";
}
content += "]";
mimeType = "application/json; charset=utf-8"; mimeType = "application/json; charset=utf-8";
} }
@ -312,7 +317,7 @@ static int accessHandlerCallback(void *cls,
try { try {
pthread_mutex_lock(&readerLock); pthread_mutex_lock(&readerLock);
found = reader->getContentByDecodedUrl(urlStr, content, contentLength, mimeType, baseUrl); bool found = reader->getContentByDecodedUrl(urlStr, content, contentLength, mimeType, baseUrl);
pthread_mutex_unlock(&readerLock); pthread_mutex_unlock(&readerLock);
if (found) { if (found) {
@ -584,10 +589,9 @@ int main(int argc, char **argv) {
readers[humanReadableId] = reader; readers[humanReadableId] = reader;
/* Instanciate the ZIM index (if necessary) */ /* Instanciate the ZIM index (if necessary) */
kiwix::Searcher *searcher = NULL;
if (!indexPath.empty()) { if (!indexPath.empty()) {
try { try {
searcher = new kiwix::XapianSearcher(indexPath); kiwix::Searcher *searcher = new kiwix::XapianSearcher(indexPath);
searcher->setProtocolPrefix("/"); searcher->setProtocolPrefix("/");
searcher->setSearchProtocolPrefix("/search?"); searcher->setSearchProtocolPrefix("/search?");
searcher->setContentHumanReadableId(humanReadableId); searcher->setContentHumanReadableId(humanReadableId);
@ -602,7 +606,7 @@ int main(int argc, char **argv) {
/* Compute the Welcome HTML */ /* Compute the Welcome HTML */
string welcomeBooksHtml; string welcomeBooksHtml;
for ( itr = booksIds.begin(); itr != booksIds.end(); ++itr ) { for (itr = booksIds.begin(); itr != booksIds.end(); ++itr) {
libraryManager.getBookById(*itr, currentBook); libraryManager.getBookById(*itr, currentBook);
if (!currentBook.path.empty() && readers.find(currentBook.getHumanReadableIdFromPath()) != readers.end()) { if (!currentBook.path.empty() && readers.find(currentBook.getHumanReadableIdFromPath()) != readers.end()) {