Merge pull request #74 from kiwix/get_content_title

getContent* methods also allow to get the title.
This commit is contained in:
Kelson 2017-08-02 21:02:27 +02:00 committed by GitHub
commit 44a282fa4c
5 changed files with 30 additions and 11 deletions

View File

@ -66,24 +66,29 @@ class Reader
bool getMimeTypeByUrl(const string& url, string& mimeType) const; bool getMimeTypeByUrl(const string& url, string& mimeType) const;
bool getContentByUrl(const string& url, bool getContentByUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType) const; string& contentType) const;
bool getContentByEncodedUrl(const string& url, bool getContentByEncodedUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType, string& contentType,
string& baseUrl) const; string& baseUrl) const;
bool getContentByEncodedUrl(const string& url, bool getContentByEncodedUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType) const; string& contentType) const;
bool getContentByDecodedUrl(const string& url, bool getContentByDecodedUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType, string& contentType,
string& baseUrl) const; string& baseUrl) const;
bool getContentByDecodedUrl(const string& url, bool getContentByDecodedUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType) const; string& contentType) const;
bool searchSuggestions(const string& prefix, bool searchSuggestions(const string& prefix,

View File

@ -323,9 +323,10 @@ Java_org_kiwix_kiwixlib_JNIKiwix_loadZIM(JNIEnv* env, jobject obj, jstring path)
} }
JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwix_getContent( JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwix_getContent(
JNIEnv* env, jobject obj, jstring url, jobject mimeTypeObj, jobject sizeObj) JNIEnv* env, jobject obj, jstring url, jobject titleObj, jobject mimeTypeObj, jobject sizeObj)
{ {
/* Default values */ /* Default values */
setStringObjValue("", titleObj, env);
setStringObjValue("", mimeTypeObj, env); setStringObjValue("", mimeTypeObj, env);
setIntObjValue(0, sizeObj, env); setIntObjValue(0, sizeObj, env);
jbyteArray data = env->NewByteArray(0); jbyteArray data = env->NewByteArray(0);
@ -334,16 +335,18 @@ JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwix_getContent(
if (reader != NULL) { if (reader != NULL) {
std::string cUrl = jni2c(url, env); std::string cUrl = jni2c(url, env);
std::string cData; std::string cData;
std::string cTitle;
std::string cMimeType; std::string cMimeType;
unsigned int cSize = 0; unsigned int cSize = 0;
pthread_mutex_lock(&readerLock); pthread_mutex_lock(&readerLock);
try { try {
if (reader->getContentByUrl(cUrl, cData, cSize, cMimeType)) { if (reader->getContentByUrl(cUrl, cData, cTitle, cSize, cMimeType)) {
data = env->NewByteArray(cSize); data = env->NewByteArray(cSize);
env->SetByteArrayRegion( env->SetByteArrayRegion(
data, 0, cSize, reinterpret_cast<const jbyte*>(cData.c_str())); data, 0, cSize, reinterpret_cast<const jbyte*>(cData.c_str()));
setStringObjValue(cMimeType, mimeTypeObj, env); setStringObjValue(cMimeType, mimeTypeObj, env);
setStringObjValue(cTitle, titleObj, env);
setIntObjValue(cSize, sizeObj, env); setIntObjValue(cSize, sizeObj, env);
} }
} catch (...) { } catch (...) {

View File

@ -38,7 +38,7 @@ public class JNIKiwix
public native boolean loadFulltextIndex(String path); public native boolean loadFulltextIndex(String path);
public native byte[] getContent(String url, JNIKiwixString mimeType, JNIKiwixInt size); public native byte[] getContent(String url, JNIKiwixString title, JNIKiwixString mimeType, JNIKiwixInt size);
public native boolean searchSuggestions(String prefix, int count); public native boolean searchSuggestions(String prefix, int count);

View File

@ -252,17 +252,18 @@ string Reader::getMainPageUrl() const
bool Reader::getFavicon(string& content, string& mimeType) const bool Reader::getFavicon(string& content, string& mimeType) const
{ {
unsigned int contentLength = 0; unsigned int contentLength = 0;
string title;
this->getContentByUrl("/-/favicon.png", content, contentLength, mimeType); this->getContentByUrl("/-/favicon.png", content, title, contentLength, mimeType);
if (content.empty()) { if (content.empty()) {
this->getContentByUrl("/I/favicon.png", content, contentLength, mimeType); this->getContentByUrl("/I/favicon.png", content, title, contentLength, mimeType);
if (content.empty()) { if (content.empty()) {
this->getContentByUrl("/I/favicon", content, contentLength, mimeType); this->getContentByUrl("/I/favicon", content, title, contentLength, mimeType);
if (content.empty()) { if (content.empty()) {
this->getContentByUrl("/-/favicon", content, contentLength, mimeType); this->getContentByUrl("/-/favicon", content, title, contentLength, mimeType);
} }
} }
} }
@ -279,8 +280,9 @@ bool Reader::getMetatag(const string& name, string& value) const
{ {
unsigned int contentLength = 0; unsigned int contentLength = 0;
string contentType = ""; string contentType = "";
string title;
return this->getContentByUrl("/M/" + name, value, contentLength, contentType); return this->getContentByUrl("/M/" + name, value, title, contentLength, contentType);
} }
string Reader::getTitle() const string Reader::getTitle() const
@ -467,30 +469,34 @@ bool Reader::getMimeTypeByUrl(const string& url, string& mimeType) const
/* Get a content from a zim file */ /* Get a content from a zim file */
bool Reader::getContentByUrl(const string& url, bool Reader::getContentByUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType) const string& contentType) const
{ {
return this->getContentByEncodedUrl(url, content, contentLength, contentType); return this->getContentByEncodedUrl(url, content, title, contentLength, contentType);
} }
bool Reader::getContentByEncodedUrl(const string& url, bool Reader::getContentByEncodedUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType, string& contentType,
string& baseUrl) const string& baseUrl) const
{ {
return this->getContentByDecodedUrl( return this->getContentByDecodedUrl(
kiwix::urlDecode(url), content, contentLength, contentType, baseUrl); kiwix::urlDecode(url), content, title, contentLength, contentType, baseUrl);
} }
bool Reader::getContentByEncodedUrl(const string& url, bool Reader::getContentByEncodedUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType) const string& contentType) const
{ {
std::string stubRedirectUrl; std::string stubRedirectUrl;
return this->getContentByEncodedUrl(kiwix::urlDecode(url), return this->getContentByEncodedUrl(kiwix::urlDecode(url),
content, content,
title,
contentLength, contentLength,
contentType, contentType,
stubRedirectUrl); stubRedirectUrl);
@ -498,12 +504,14 @@ bool Reader::getContentByEncodedUrl(const string& url,
bool Reader::getContentByDecodedUrl(const string& url, bool Reader::getContentByDecodedUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType) const string& contentType) const
{ {
std::string stubRedirectUrl; std::string stubRedirectUrl;
return this->getContentByDecodedUrl(kiwix::urlDecode(url), return this->getContentByDecodedUrl(kiwix::urlDecode(url),
content, content,
title,
contentLength, contentLength,
contentType, contentType,
stubRedirectUrl); stubRedirectUrl);
@ -511,6 +519,7 @@ bool Reader::getContentByDecodedUrl(const string& url,
bool Reader::getContentByDecodedUrl(const string& url, bool Reader::getContentByDecodedUrl(const string& url,
string& content, string& content,
string& title,
unsigned int& contentLength, unsigned int& contentLength,
string& contentType, string& contentType,
string& baseUrl) const string& baseUrl) const
@ -547,6 +556,7 @@ bool Reader::getContentByDecodedUrl(const string& url,
/* Get the data */ /* Get the data */
content = string(article.getData().data(), article.getArticleSize()); content = string(article.getData().data(), article.getArticleSize());
title = article.getTitle();
} }
/* Try to set a stub HTML header/footer if necesssary */ /* Try to set a stub HTML header/footer if necesssary */

View File

@ -194,10 +194,11 @@ std::string XapianResult::get_content()
return ""; return "";
} }
std::string content; std::string content;
std::string title;
unsigned int contentLength; unsigned int contentLength;
std::string contentType; std::string contentType;
searcher->reader->getContentByUrl( searcher->reader->getContentByUrl(
get_url(), content, contentLength, contentType); get_url(), content, title, contentLength, contentType);
return content; return content;
} }