Fixed extern compile (static) on linux and removed Clucene dependency

This commit is contained in:
rgaudin 2013-12-09 10:51:06 +00:00
parent 8bb7c040b1
commit fcf84811c6
2 changed files with 54 additions and 56 deletions

View File

@ -36,7 +36,7 @@ using namespace std;
namespace kiwix { namespace kiwix {
enum supportedIndexType { UNKNOWN, XAPIAN, CLUCENE }; enum supportedIndexType { UNKNOWN, XAPIAN };
class Book { class Book {
@ -76,7 +76,7 @@ namespace kiwix {
}; };
class Library { class Library {
public: public:
Library(); Library();
~Library(); ~Library();

View File

@ -25,19 +25,19 @@ namespace kiwix {
Manager::Manager() : Manager::Manager() :
writableLibraryPath("") { writableLibraryPath("") {
} }
/* Destructor */ /* Destructor */
Manager::~Manager() { Manager::~Manager() {
} }
bool Manager::parseXmlDom(const pugi::xml_document &doc, const bool readOnly, const string libraryPath) { bool Manager::parseXmlDom(const pugi::xml_document &doc, const bool readOnly, const string libraryPath) {
pugi::xml_node libraryNode = doc.child("library"); pugi::xml_node libraryNode = doc.child("library");
if (strlen(libraryNode.attribute("current").value())) if (strlen(libraryNode.attribute("current").value()))
this->setCurrentBookId(libraryNode.attribute("current").value()); this->setCurrentBookId(libraryNode.attribute("current").value());
string libraryVersion = libraryNode.attribute("version").value(); string libraryVersion = libraryNode.attribute("version").value();
for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode; bookNode = bookNode.next_sibling("book")) { for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode; bookNode = bookNode.next_sibling("book")) {
bool ok = true; bool ok = true;
kiwix::Book book; kiwix::Book book;
@ -45,10 +45,10 @@ namespace kiwix {
book.readOnly = readOnly; book.readOnly = readOnly;
book.id = bookNode.attribute("id").value(); book.id = bookNode.attribute("id").value();
book.path = bookNode.attribute("path").value(); book.path = bookNode.attribute("path").value();
book.last = (std::string(bookNode.attribute("last").value()) != "undefined" ? book.last = (std::string(bookNode.attribute("last").value()) != "undefined" ?
bookNode.attribute("last").value() : ""); bookNode.attribute("last").value() : "");
book.indexPath = bookNode.attribute("indexPath").value(); book.indexPath = bookNode.attribute("indexPath").value();
book.indexType = (std::string(bookNode.attribute("indexType").value()) == "xapian" ? XAPIAN : CLUCENE); book.indexType = XAPIAN;
book.title = bookNode.attribute("title").value(); book.title = bookNode.attribute("title").value();
book.description = bookNode.attribute("description").value(); book.description = bookNode.attribute("description").value();
book.language = bookNode.attribute("language").value(); book.language = bookNode.attribute("language").value();
@ -61,9 +61,9 @@ namespace kiwix {
book.size = bookNode.attribute("size").value(); book.size = bookNode.attribute("size").value();
book.favicon = bookNode.attribute("favicon").value(); book.favicon = bookNode.attribute("favicon").value();
book.faviconMimeType = bookNode.attribute("faviconMimeType").value(); book.faviconMimeType = bookNode.attribute("faviconMimeType").value();
/* Check absolute and relative paths */ /* Check absolute and relative paths */
this->checkAndCleanBookPaths(book, libraryPath); this->checkAndCleanBookPaths(book, libraryPath);
/* Update the book properties with the new importer */ /* Update the book properties with the new importer */
if (libraryVersion.empty() || atoi(libraryVersion.c_str()) <= atoi(KIWIX_LIBRARY_VERSION)) { if (libraryVersion.empty() || atoi(libraryVersion.c_str()) <= atoi(KIWIX_LIBRARY_VERSION)) {
@ -76,7 +76,7 @@ namespace kiwix {
library.addBook(book); library.addBook(book);
} }
} }
return true; return true;
} }
@ -128,7 +128,7 @@ namespace kiwix {
if (!library.version.empty()) if (!library.version.empty())
libraryNode.append_attribute("version") = library.version.c_str(); libraryNode.append_attribute("version") = library.version.c_str();
/* Add each book */ /* Add each book */
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
@ -141,45 +141,43 @@ namespace kiwix {
if (!itr->path.empty()) if (!itr->path.empty())
bookNode.append_attribute("path") = itr->path.c_str(); bookNode.append_attribute("path") = itr->path.c_str();
if (!itr->last.empty() && itr->last != "undefined") { if (!itr->last.empty() && itr->last != "undefined") {
bookNode.append_attribute("last") = itr->last.c_str(); bookNode.append_attribute("last") = itr->last.c_str();
} }
if (!itr->indexPath.empty()) if (!itr->indexPath.empty())
bookNode.append_attribute("indexPath") = itr->indexPath.c_str(); bookNode.append_attribute("indexPath") = itr->indexPath.c_str();
if (!itr->indexPath.empty() || !itr->indexPathAbsolute.empty()) { if (!itr->indexPath.empty() || !itr->indexPathAbsolute.empty()) {
if (itr->indexType == XAPIAN) if (itr->indexType == XAPIAN)
bookNode.append_attribute("indexType") = "xapian"; bookNode.append_attribute("indexType") = "xapian";
else if (itr->indexType == CLUCENE)
bookNode.append_attribute("indexType") = "clucene";
} }
if (!itr->title.empty()) if (!itr->title.empty())
bookNode.append_attribute("title") = itr->title.c_str(); bookNode.append_attribute("title") = itr->title.c_str();
if (itr->description != "") if (itr->description != "")
bookNode.append_attribute("description") = itr->description.c_str(); bookNode.append_attribute("description") = itr->description.c_str();
if (itr->language != "") if (itr->language != "")
bookNode.append_attribute("language") = itr->language.c_str(); bookNode.append_attribute("language") = itr->language.c_str();
if (itr->date != "") if (itr->date != "")
bookNode.append_attribute("date") = itr->date.c_str(); bookNode.append_attribute("date") = itr->date.c_str();
if (itr->creator != "") if (itr->creator != "")
bookNode.append_attribute("creator") = itr->creator.c_str(); bookNode.append_attribute("creator") = itr->creator.c_str();
if (itr->publisher != "") if (itr->publisher != "")
bookNode.append_attribute("publisher") = itr->publisher.c_str(); bookNode.append_attribute("publisher") = itr->publisher.c_str();
if (itr->url != "") if (itr->url != "")
bookNode.append_attribute("url") = itr->url.c_str(); bookNode.append_attribute("url") = itr->url.c_str();
if (itr->articleCount != "") if (itr->articleCount != "")
bookNode.append_attribute("articleCount") = itr->articleCount.c_str(); bookNode.append_attribute("articleCount") = itr->articleCount.c_str();
if (itr->mediaCount != "") if (itr->mediaCount != "")
bookNode.append_attribute("mediaCount") = itr->mediaCount.c_str(); bookNode.append_attribute("mediaCount") = itr->mediaCount.c_str();
@ -211,12 +209,12 @@ namespace kiwix {
} }
string Manager::getCurrentBookId() { string Manager::getCurrentBookId() {
return library.current.empty() ? return library.current.empty() ?
"" : library.current.top(); "" : library.current.top();
} }
/* Add a book to the library. Return empty string if failed, book id otherwise */ /* Add a book to the library. Return empty string if failed, book id otherwise */
string Manager::addBookFromPathAndGetId(const string pathToOpen, const string pathToSave, string Manager::addBookFromPathAndGetId(const string pathToOpen, const string pathToSave,
const string url, const bool checkMetaData) { const string url, const bool checkMetaData) {
kiwix::Book book; kiwix::Book book;
@ -228,7 +226,7 @@ namespace kiwix {
computeAbsolutePath(removeLastPathElement(writableLibraryPath, true, false), pathToSave) : pathToSave; computeAbsolutePath(removeLastPathElement(writableLibraryPath, true, false), pathToSave) : pathToSave;
} }
if (!checkMetaData || if (!checkMetaData ||
(checkMetaData && !book.title.empty() && !book.language.empty() && !book.date.empty())) { (checkMetaData && !book.title.empty() && !book.language.empty() && !book.date.empty())) {
book.url = url; book.url = url;
library.addBook(book); library.addBook(book);
@ -238,7 +236,7 @@ namespace kiwix {
return ""; return "";
} }
/* Wrapper over Manager::addBookFromPath which return a bool instead of a string */ /* Wrapper over Manager::addBookFromPath which return a bool instead of a string */
bool Manager::addBookFromPath(const string pathToOpen, const string pathToSave, const string url, const bool checkMetaData) { bool Manager::addBookFromPath(const string pathToOpen, const string pathToSave, const string url, const bool checkMetaData) {
return !(this->addBookFromPathAndGetId(pathToOpen, pathToSave, url, checkMetaData).empty()); return !(this->addBookFromPathAndGetId(pathToOpen, pathToSave, url, checkMetaData).empty());
@ -247,7 +245,7 @@ namespace kiwix {
bool Manager::readBookFromPath(const string path, kiwix::Book *book) { bool Manager::readBookFromPath(const string path, kiwix::Book *book) {
try { try {
kiwix::Reader *reader = new kiwix::Reader(path); kiwix::Reader *reader = new kiwix::Reader(path);
if (book != NULL) { if (book != NULL) {
book->path = path; book->path = path;
book->pathAbsolute = path; book->pathAbsolute = path;
@ -258,18 +256,18 @@ namespace kiwix {
book->creator = reader->getCreator(); book->creator = reader->getCreator();
book->publisher = reader->getPublisher(); book->publisher = reader->getPublisher();
book->title = reader->getTitle(); book->title = reader->getTitle();
std::ostringstream articleCountStream; std::ostringstream articleCountStream;
articleCountStream << reader->getArticleCount(); articleCountStream << reader->getArticleCount();
book->articleCount = articleCountStream.str(); book->articleCount = articleCountStream.str();
std::ostringstream mediaCountStream; std::ostringstream mediaCountStream;
mediaCountStream << reader->getMediaCount(); mediaCountStream << reader->getMediaCount();
book->mediaCount = mediaCountStream.str(); book->mediaCount = mediaCountStream.str();
ostringstream convert; convert << reader->getFileSize(); ostringstream convert; convert << reader->getFileSize();
book->size = convert.str(); book->size = convert.str();
string favicon; string favicon;
string faviconMimeType; string faviconMimeType;
if (reader->getFavicon(favicon, faviconMimeType)) { if (reader->getFavicon(favicon, faviconMimeType)) {
@ -277,7 +275,7 @@ namespace kiwix {
book->faviconMimeType = faviconMimeType; book->faviconMimeType = faviconMimeType;
} }
} }
delete reader; delete reader;
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
@ -294,7 +292,7 @@ namespace kiwix {
bool Manager::removeBookById(const string id) { bool Manager::removeBookById(const string id) {
unsigned int bookIndex = 0; unsigned int bookIndex = 0;
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if ( itr->id == id) { if ( itr->id == id) {
return this->library.removeBookByIndex(bookIndex); return this->library.removeBookByIndex(bookIndex);
} }
@ -308,14 +306,14 @@ namespace kiwix {
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
std::map<string, bool> booksLanguagesMap; std::map<string, bool> booksLanguagesMap;
std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByLanguage); std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByLanguage);
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if (booksLanguagesMap.find(itr->language) == booksLanguagesMap.end()) { if (booksLanguagesMap.find(itr->language) == booksLanguagesMap.end()) {
booksLanguagesMap[itr->language] = true; booksLanguagesMap[itr->language] = true;
booksLanguages.push_back(itr->language); booksLanguages.push_back(itr->language);
} }
} }
return booksLanguages; return booksLanguages;
} }
@ -324,14 +322,14 @@ namespace kiwix {
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
std::map<string, bool> booksCreatorsMap; std::map<string, bool> booksCreatorsMap;
std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByCreator); std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByCreator);
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if (booksCreatorsMap.find(itr->creator) == booksCreatorsMap.end()) { if (booksCreatorsMap.find(itr->creator) == booksCreatorsMap.end()) {
booksCreatorsMap[itr->creator] = true; booksCreatorsMap[itr->creator] = true;
booksCreators.push_back(itr->creator); booksCreators.push_back(itr->creator);
} }
} }
return booksCreators; return booksCreators;
} }
@ -343,7 +341,7 @@ namespace kiwix {
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
booksIds.push_back(itr->id); booksIds.push_back(itr->id);
} }
return booksIds; return booksIds;
} }
@ -352,14 +350,14 @@ namespace kiwix {
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
std::map<string, bool> booksPublishersMap; std::map<string, bool> booksPublishersMap;
std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByPublisher); std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByPublisher);
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if (booksPublishersMap.find(itr->publisher) == booksPublishersMap.end()) { if (booksPublishersMap.find(itr->publisher) == booksPublishersMap.end()) {
booksPublishersMap[itr->publisher] = true; booksPublishersMap[itr->publisher] = true;
booksPublishers.push_back(itr->publisher); booksPublishers.push_back(itr->publisher);
} }
} }
return booksPublishers; return booksPublishers;
} }
@ -379,7 +377,7 @@ namespace kiwix {
bool Manager::getBookById(const string id, Book &book) { bool Manager::getBookById(const string id, Book &book) {
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if ( itr->id == id) { if ( itr->id == id) {
book = *itr; book = *itr;
return true; return true;
@ -390,7 +388,7 @@ namespace kiwix {
bool Manager::updateBookLastOpenDateById(const string id) { bool Manager::updateBookLastOpenDateById(const string id) {
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if ( itr->id == id) { if ( itr->id == id) {
char unixdate[12]; char unixdate[12];
sprintf (unixdate, "%d", (int)time(NULL)); sprintf (unixdate, "%d", (int)time(NULL));
@ -404,7 +402,7 @@ namespace kiwix {
bool Manager::setBookIndex(const string id, const string path, const supportedIndexType type) { bool Manager::setBookIndex(const string id, const string path, const supportedIndexType type) {
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if ( itr->id == id) { if ( itr->id == id) {
itr->indexPath = path; itr->indexPath = path;
itr->indexPathAbsolute = isRelativePath(path) ? itr->indexPathAbsolute = isRelativePath(path) ?
@ -419,7 +417,7 @@ namespace kiwix {
bool Manager::setBookPath(const string id, const string path) { bool Manager::setBookPath(const string id, const string path) {
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if ( itr->id == id) { if ( itr->id == id) {
itr->path = path; itr->path = path;
itr->pathAbsolute = isRelativePath(path) ? itr->pathAbsolute = isRelativePath(path) ?
@ -433,7 +431,7 @@ namespace kiwix {
void Manager::removeBookPaths() { void Manager::removeBookPaths() {
std::vector<kiwix::Book>::iterator itr; std::vector<kiwix::Book>::iterator itr;
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
itr->path = ""; itr->path = "";
itr->pathAbsolute = ""; itr->pathAbsolute = "";
} }
@ -449,7 +447,7 @@ namespace kiwix {
return result; return result;
} }
bool Manager::listBooks(const supportedListMode mode, const supportedListSortBy sortBy, bool Manager::listBooks(const supportedListMode mode, const supportedListSortBy sortBy,
const unsigned int maxSize, const string language, const string creator, const unsigned int maxSize, const string language, const string creator,
const string publisher, const string search) { const string publisher, const string search) {
this->bookIdList.clear(); this->bookIdList.clear();
@ -457,7 +455,7 @@ namespace kiwix {
/* Sort */ /* Sort */
if (sortBy == TITLE) { if (sortBy == TITLE) {
std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByTitle); std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByTitle);
} else if (sortBy == SIZE) { } else if (sortBy == SIZE) {
std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortBySize); std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortBySize);
} else if (sortBy == DATE) { } else if (sortBy == DATE) {
@ -467,7 +465,7 @@ namespace kiwix {
} else if (sortBy == PUBLISHER) { } else if (sortBy == PUBLISHER) {
std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByPublisher); std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByPublisher);
} }
/* Special sort for LASTOPEN */ /* Special sort for LASTOPEN */
if (mode == LASTOPEN) { if (mode == LASTOPEN) {
std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByLastOpen); std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByLastOpen);
@ -482,9 +480,9 @@ namespace kiwix {
if (mode == LOCAL && itr->path.empty()) if (mode == LOCAL && itr->path.empty())
ok = false; ok = false;
if (ok == true && mode == REMOTE && (!itr->path.empty() || itr->url.empty())) if (ok == true && mode == REMOTE && (!itr->path.empty() || itr->url.empty()))
ok = false; ok = false;
if (ok == true && maxSize != 0 && (unsigned int)atoi(itr->size.c_str()) > maxSize * 1024 * 1024) if (ok == true && maxSize != 0 && (unsigned int)atoi(itr->size.c_str()) > maxSize * 1024 * 1024)
ok = false; ok = false;
@ -497,7 +495,7 @@ namespace kiwix {
if (ok == true && !publisher.empty() && itr->publisher != publisher) if (ok == true && !publisher.empty() && itr->publisher != publisher)
ok = false; ok = false;
if ((ok == true && !search.empty()) && !(matchRegex(itr->title, search) || matchRegex(itr->description, search))) if ((ok == true && !search.empty()) && !(matchRegex(itr->title, search) || matchRegex(itr->description, search)))
ok = false; ok = false;
@ -506,7 +504,7 @@ namespace kiwix {
} }
} }
} }
return true; return true;
} }
@ -519,14 +517,14 @@ namespace kiwix {
book.path = computeRelativePath(removeLastPathElement(libraryPath, true, false), book.pathAbsolute); book.path = computeRelativePath(removeLastPathElement(libraryPath, true, false), book.pathAbsolute);
} }
} }
if (!book.indexPath.empty()) { if (!book.indexPath.empty()) {
if (isRelativePath(book.indexPath)) { if (isRelativePath(book.indexPath)) {
book.indexPathAbsolute = book.indexPathAbsolute =
computeAbsolutePath(removeLastPathElement(libraryPath, true, false), book.indexPath); computeAbsolutePath(removeLastPathElement(libraryPath, true, false), book.indexPath);
} else { } else {
book.indexPathAbsolute = book.indexPath; book.indexPathAbsolute = book.indexPath;
book.indexPath = book.indexPath =
computeRelativePath(removeLastPathElement(libraryPath, true, false), book.indexPathAbsolute); computeRelativePath(removeLastPathElement(libraryPath, true, false), book.indexPathAbsolute);
} }
} }