Added a random article button for server mode

This commit is contained in:
Shivam 2014-05-25 12:25:13 -04:00
parent 5ef0a6e852
commit f26f60e3d7
3 changed files with 30 additions and 3 deletions

View File

@ -158,13 +158,14 @@ static int accessHandlerCallback(void *cls,
std::string httpRedirection;
unsigned int contentLength = 0;
bool found = true;
bool cacheEnabled = true;
int httpResponseCode = MHD_HTTP_OK;
std::string urlStr = string(url);
/* Get searcher and reader */
std::string humanReadableBookId = "";
if (!(urlStr.size() > 5 && urlStr.substr(0, 6) == "/skin/")) {
if (!strcmp(url, "/search") || !strcmp(url, "/suggest")) {
if (!strcmp(url, "/search") || !strcmp(url, "/suggest") || !strcmp(url, "/random")) {
const char* tmpGetValue = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "content");
humanReadableBookId = (tmpGetValue != NULL ? string(tmpGetValue) : "");
} else {
@ -270,6 +271,18 @@ static int accessHandlerCallback(void *cls,
}
}
/* Display a random article */
else if (!strcmp(url, "/random")) {
cacheEnabled = false;
std::string randomUrl;
if (reader != NULL) {
pthread_mutex_lock(&readerLock);
randomUrl = reader->getRandomPageUrl();
pthread_mutex_unlock(&readerLock);
httpRedirection="/" + humanReadableBookId + "/" + randomUrl;
}
}
/* Display the content of a ZIM article */
else if (reader != NULL) {
pthread_mutex_lock(&readerLock);
@ -387,8 +400,16 @@ static int accessHandlerCallback(void *cls,
/* Force to close the connection - cf. 100% CPU usage with v. 4.4 (in Lucid) */
MHD_add_response_header(response, MHD_HTTP_HEADER_CONNECTION, "close");
/* Force cache */
MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, "max-age=87840, must-revalidate");
if(cacheEnabled)
{
/* Force cache */
MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, "max-age=87840, must-revalidate");
}
else
{
/* Prevent cache (for random page) */
MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, "no-cache, no-store, must-revalidate");
}
/* Queue the response */
int ret = MHD_queue_response(connection,

View File

@ -19,6 +19,11 @@
float: left;
}
#kiwixrandom {
margin: 0px;
float: left;
}
#kiwixsearchbox {
margin: 0px;
float: right;

View File

@ -18,6 +18,7 @@
<span id="kiwixtoolbar" class="ui-widget-header ui-corner-all">
<form action="/" method="GET"><input type="submit" value="Library" id="kiwixlibrary" /></form>
<form action="/__CONTENT__/" method="GET"><input type="submit" value="Home" id="kiwixhome" /></form>
<form action="/random" method="GET"><input type="submit" value="Random" id="kiwixrandom" /><input type="hidden" name="content" value="__CONTENT__" /></form>
<form method="GET" action="/search" id="kiwixsearchform"><input type="hidden" name="content" value="__CONTENT__" /><input type="submit" id="kiwixsearch" value="Search" /><input type="text" id="kiwixsearchbox" name="pattern" /></form>
</span>
<div style="display: block; height: 45px;"></div>