+ in the fulltext search, if pattern match exactly a title, then load automatical the article

pull/9/head
kelson42 14 years ago
parent 284085e3e1
commit 717b7a902a

@ -176,12 +176,13 @@ static int accessHandlerCallback(void *cls,
/* Prepare the variables */ /* Prepare the variables */
struct MHD_Response *response; struct MHD_Response *response;
string content = ""; std::string content;
string mimeType = ""; std::string mimeType;
std::string httpRedirection;
unsigned int contentLength = 0; unsigned int contentLength = 0;
bool found = true; bool found = true;
int httpResponseCode = MHD_HTTP_OK; int httpResponseCode = MHD_HTTP_OK;
std::string urlStr = string(url); std::string urlStr = string(url);
/* Get searcher and reader */ /* Get searcher and reader */
std::string humanReadableBookId = ""; std::string humanReadableBookId = "";
@ -202,6 +203,10 @@ static int accessHandlerCallback(void *cls,
searchers.find(humanReadableBookId)->second : NULL; searchers.find(humanReadableBookId)->second : NULL;
kiwix::Reader *reader = readers.find(humanReadableBookId) != readers.end() ? kiwix::Reader *reader = readers.find(humanReadableBookId) != readers.end() ?
readers.find(humanReadableBookId)->second : NULL; readers.find(humanReadableBookId)->second : NULL;
if (reader == NULL) {
humanReadableBookId="";
}
pthread_mutex_unlock(&mapLock); pthread_mutex_unlock(&mapLock);
/* Get suggestions */ /* Get suggestions */
@ -224,7 +229,6 @@ static int accessHandlerCallback(void *cls,
} }
content += (content == "[" ? "" : ","); content += (content == "[" ? "" : ",");
content += "{\"value\":\"" + std::string(term) + " \", \"label\":\"containing '" + std::string(term) + "'...\"}]"; content += "{\"value\":\"" + std::string(term) + " \", \"label\":\"containing '" + std::string(term) + "'...\"}]";
std::cout << content << std::endl;
mimeType = "text/x-json; charset=utf-8"; mimeType = "text/x-json; charset=utf-8";
} }
@ -234,34 +238,54 @@ static int accessHandlerCallback(void *cls,
} }
/* Display the search restults */ /* Display the search restults */
else if (!strcmp(url, "/search") && searcher != NULL) { else if (!strcmp(url, "/search")) {
/* Retrieve the pattern to search */
const char* pattern = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "pattern"); const char* pattern = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "pattern");
const char* start = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "start"); if (pattern == NULL)
const char* end = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "end"); pattern = "";
unsigned int startNumber = 0;
unsigned int endNumber = 25; /* Try first to load directly the article if exactly matching the pattern */
std::string patternCorrespondingUrl;
if (reader != NULL) {
pthread_mutex_lock(&readerLock);
reader->getPageUrlFromTitle(pattern, patternCorrespondingUrl);
pthread_mutex_unlock(&readerLock);
if (!patternCorrespondingUrl.empty()) {
std::cout << "Search url:" << patternCorrespondingUrl << std::endl;
httpRedirection="/" + humanReadableBookId + "/" + patternCorrespondingUrl;
}
}
if (start != NULL) /* Make the search */
startNumber = atoi(start); if (patternCorrespondingUrl.empty() && searcher != NULL) {
const char* start = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "start");
const char* end = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "end");
unsigned int startNumber = 0;
unsigned int endNumber = 25;
if (end != NULL) if (start != NULL)
endNumber = atoi(end); startNumber = atoi(start);
if (pattern == NULL) if (end != NULL)
pattern = ""; endNumber = atoi(end);
/* Get the results */ /* Get the results */
pthread_mutex_lock(&searcherLock); pthread_mutex_lock(&searcherLock);
try { try {
std::string patternString = string(pattern); std::string patternString = string(pattern);
searcher->search(patternString, startNumber, endNumber, isVerbose()); searcher->search(patternString, startNumber, endNumber, isVerbose());
content = searcher->getHtml(); content = searcher->getHtml();
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
pthread_mutex_unlock(&searcherLock); pthread_mutex_unlock(&searcherLock);
mimeType = "text/html; charset=utf-8"; mimeType = "text/html; charset=utf-8";
} else {
content = "<html><head><title>Fulltext search unavailable</title></head><body><h1>Not Found</h1><p>There is no article with the title <b>\"" + string(pattern) + "\"</b> and the fulltext search engine is not available for this content.</p></body></html>";
mimeType = "text/html";
httpResponseCode = MHD_HTTP_NOT_FOUND;
}
} }
/* Display the content of a ZIM article */ /* Display the content of a ZIM article */
@ -335,13 +359,19 @@ static int accessHandlerCallback(void *cls,
MHD_NO, MHD_NO,
MHD_YES); MHD_YES);
/* Add if necessary the content-encoding */ /* Make a redirection if necessary otherwise send the content */
if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) { if (!httpRedirection.empty()) {
MHD_add_response_header(response, "Content-encoding", "deflate"); MHD_add_response_header(response, "Location", httpRedirection.c_str());
} httpResponseCode = MHD_HTTP_FOUND;
} else {
/* Add if necessary the content-encoding */
if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) {
MHD_add_response_header(response, "Content-encoding", "deflate");
}
/* Specify the mime type */ /* Specify the mime type */
MHD_add_response_header(response, "Content-Type", mimeType.c_str()); MHD_add_response_header(response, "Content-Type", mimeType.c_str());
}
/* clear context pointer */ /* clear context pointer */
*ptr = NULL; *ptr = NULL;

Loading…
Cancel
Save