From db62ff440ad5e1fb56b4194ccafa8b57ac7c6238 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Mon, 16 Apr 2012 23:21:37 +0000 Subject: [PATCH] + last improvement of the new kiwix-serve --- src/server/kiwix-serve.cpp | 119 +++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 50 deletions(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index a7da0e8..a15a707 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -137,7 +137,7 @@ else if (document.getElementById) \n \ "; static const string HTMLDiv = " \ -
\n \ +
\n \ "; // Urlencode @@ -229,7 +229,18 @@ static int accessHandlerCallback(void *cls, std::string urlStr = string(url); /* Get searcher and reader */ - std::string humanReadableBookId = urlStr.substr(1, urlStr.find("/", 1) != string::npos ? urlStr.find("/", 1) - 1 : urlStr.size() - 2); + std::string humanReadableBookId = ""; + if (!strcmp(url, "/search")) { + const char* tmpGetValue = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "content"); + humanReadableBookId = (tmpGetValue != NULL ? string(tmpGetValue) : ""); + cout << humanReadableBookId << endl; + } else { + humanReadableBookId = urlStr.substr(1, urlStr.find("/", 1) != string::npos ? urlStr.find("/", 1) - 1 : urlStr.size() - 2); + if (!humanReadableBookId.empty()) { + urlStr = urlStr.substr(urlStr.find("/", 1) != string::npos ? urlStr.find("/", 1) : humanReadableBookId.size()); + } + } + pthread_mutex_lock(&mapLock); kiwix::Searcher *searcher = searchers.find(humanReadableBookId) != searchers.end() ? searchers.find(humanReadableBookId)->second : NULL; @@ -237,10 +248,6 @@ static int accessHandlerCallback(void *cls, readers.find(humanReadableBookId)->second : NULL; pthread_mutex_unlock(&mapLock); - if (!humanReadableBookId.empty()) { - urlStr = urlStr.substr(urlStr.find("/", 1) != string::npos ? urlStr.find("/", 1) : humanReadableBookId.size()); - } - /* Display the search restults */ if (!strcmp(url, "/search") && searcher != NULL) { if (searcher != NULL) { @@ -306,8 +313,10 @@ static int accessHandlerCallback(void *cls, replaceRegex(content, "$1=\"/" + humanReadableBookId + "/$3/", "(href|src)(=\"/)([A-Z|\-])/"); if (searcher != NULL) { + std::string HTMLDivRewrited = HTMLDiv; + replaceRegex(HTMLDivRewrited, humanReadableBookId, "__CONTENT__"); appendToFirstOccurence(content, "", HTMLScripts); - appendToFirstOccurence(content, "]*>", HTMLDiv); + appendToFirstOccurence(content, "]*>", HTMLDivRewrited); } } } @@ -506,61 +515,71 @@ int main(int argc, char **argv) { libraryManager.getBookById(*itr, currentBook); string humanReadableId = currentBook.getHumanReadableIdFromPath(); zimPath = currentBook.path; - indexPath = currentBook.indexPath; - /* Instanciate the ZIM file handler */ - kiwix::Reader *reader = NULL; - try { - reader = new kiwix::Reader(zimPath); - } catch (...) { - cerr << "Unable to open the ZIM file '" << zimPath << "'." << endl; - exit(1); - } - readers[humanReadableId] = reader; - - /* Instanciate the ZIM index (if necessary) */ - kiwix::Searcher *searcher = NULL; - if (indexPath != "") { - bool hasSearchIndex = false; - - /* Try with the XapianSearcher */ - try { - searcher = new kiwix::XapianSearcher(indexPath); - hasSearchIndex = true; - } catch (...) { - cerr << "Unable to open the search index '" << zimPath << "' with the XapianSearcher." << endl; - } + if (!zimPath.empty()) { + indexPath = currentBook.indexPath; -#ifndef _WIN32 - /* Try with the CluceneSearcher */ - if (!hasSearchIndex) { - try { - searcher = new kiwix::CluceneSearcher(indexPath); - } catch (...) { - cerr << "Unable to open the search index '" << zimPath << "' with the CluceneSearcher." << endl; - exit(1); - } + /* Instanciate the ZIM file handler */ + kiwix::Reader *reader = NULL; + try { + reader = new kiwix::Reader(zimPath); + } catch (...) { + cerr << "Unable to open the ZIM file '" << zimPath << "'." << endl; + exit(1); } + readers[humanReadableId] = reader; + + /* Instanciate the ZIM index (if necessary) */ + kiwix::Searcher *searcher = NULL; + if (indexPath != "") { + bool hasSearchIndex = false; + + /* Try with the XapianSearcher */ + try { + searcher = new kiwix::XapianSearcher(indexPath); + hasSearchIndex = true; + } catch (...) { + cerr << "Unable to open the search index '" << zimPath << "' with the XapianSearcher." << endl; + } + +#ifndef _WIN32 + /* Try with the CluceneSearcher */ + if (!hasSearchIndex) { + try { + searcher = new kiwix::CluceneSearcher(indexPath); + } catch (...) { + cerr << "Unable to open the search index '" << zimPath << "' with the CluceneSearcher." << endl; + exit(1); + } + } #endif - searcher->setProtocolPrefix("/"); - searcher->setSearchProtocolPrefix("/search?"); - searcher->setResultTemplatePath(templatePath); - searchers[humanReadableId] = searcher; + searcher->setProtocolPrefix("/"); + searcher->setSearchProtocolPrefix("/search"); + searcher->setContentHumanReadableId(humanReadableId); + searcher->setResultTemplatePath(templatePath); + searchers[humanReadableId] = searcher; + } } } /* Compute the Welcome HTML */ - welcomeHTML = "Welcome to Kiwix Server
    "; + welcomeHTML = "Welcome to Kiwix Server"; for ( itr = booksIds.begin(); itr != booksIds.end(); ++itr ) { libraryManager.getBookById(*itr, currentBook); string humanReadableId = currentBook.getHumanReadableIdFromPath(); - welcomeHTML += "

    "; - welcomeHTML += "

    " + currentBook.title + "

    "; - welcomeHTML += "


    "; - } - welcomeHTML += ""; - + if (!currentBook.path.empty()) { + welcomeHTML += "

    "; + welcomeHTML += "

    " + currentBook.title + "(" + currentBook.creator + "/" + currentBook.publisher + ")

    "; + welcomeHTML += "

    " + currentBook.description + "

    "; + welcomeHTML += "

      "; + welcomeHTML += "
    • Number of articles: " + currentBook.articleCount + "
    • "; + welcomeHTML += "
    • Number of pictures: " + currentBook.mediaCount + "
    • "; + welcomeHTML += "

    "; + welcomeHTML += "


    "; + } + } + welcomeHTML += ""; #ifndef _WIN32 /* Fork if necessary */