diff --git a/src/common/kiwix/library.h b/src/common/kiwix/library.h index 130890a8d..5631fe88d 100644 --- a/src/common/kiwix/library.h +++ b/src/common/kiwix/library.h @@ -67,6 +67,7 @@ namespace kiwix { string publisher; string date; string url; + string origID; string articleCount; string mediaCount; bool readOnly; @@ -76,7 +77,7 @@ namespace kiwix { }; class Library { - + public: Library(); ~Library(); diff --git a/src/common/kiwix/manager.cpp b/src/common/kiwix/manager.cpp index abf2d2868..1cd59ce97 100644 --- a/src/common/kiwix/manager.cpp +++ b/src/common/kiwix/manager.cpp @@ -25,19 +25,19 @@ namespace kiwix { Manager::Manager() : writableLibraryPath("") { } - + /* Destructor */ Manager::~Manager() { } bool Manager::parseXmlDom(const pugi::xml_document &doc, const bool readOnly, const string libraryPath) { pugi::xml_node libraryNode = doc.child("library"); - + if (strlen(libraryNode.attribute("current").value())) this->setCurrentBookId(libraryNode.attribute("current").value()); string libraryVersion = libraryNode.attribute("version").value(); - + for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode; bookNode = bookNode.next_sibling("book")) { bool ok = true; kiwix::Book book; @@ -45,7 +45,7 @@ namespace kiwix { book.readOnly = readOnly; book.id = bookNode.attribute("id").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() : ""); book.indexPath = bookNode.attribute("indexPath").value(); book.indexType = (std::string(bookNode.attribute("indexType").value()) == "xapian" ? XAPIAN : CLUCENE); @@ -56,14 +56,15 @@ namespace kiwix { book.creator = bookNode.attribute("creator").value(); book.publisher = bookNode.attribute("publisher").value(); book.url = bookNode.attribute("url").value(); + book.origID = bookNode.attribute("origId").value(); book.articleCount = bookNode.attribute("articleCount").value(); book.mediaCount = bookNode.attribute("mediaCount").value(); book.size = bookNode.attribute("size").value(); book.favicon = bookNode.attribute("favicon").value(); book.faviconMimeType = bookNode.attribute("faviconMimeType").value(); - + /* Check absolute and relative paths */ - this->checkAndCleanBookPaths(book, libraryPath); + this->checkAndCleanBookPaths(book, libraryPath); /* Update the book properties with the new importer */ if (libraryVersion.empty() || atoi(libraryVersion.c_str()) <= atoi(KIWIX_LIBRARY_VERSION)) { @@ -76,7 +77,7 @@ namespace kiwix { library.addBook(book); } } - + return true; } @@ -128,7 +129,7 @@ namespace kiwix { if (!library.version.empty()) libraryNode.append_attribute("version") = library.version.c_str(); - + /* Add each book */ std::vector::iterator itr; for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { @@ -141,11 +142,11 @@ namespace kiwix { if (!itr->path.empty()) bookNode.append_attribute("path") = itr->path.c_str(); - + if (!itr->last.empty() && itr->last != "undefined") { bookNode.append_attribute("last") = itr->last.c_str(); } - + if (!itr->indexPath.empty()) bookNode.append_attribute("indexPath") = itr->indexPath.c_str(); @@ -155,31 +156,34 @@ namespace kiwix { else if (itr->indexType == CLUCENE) bookNode.append_attribute("indexType") = "clucene"; } - + if (!itr->title.empty()) bookNode.append_attribute("title") = itr->title.c_str(); - + if (itr->description != "") bookNode.append_attribute("description") = itr->description.c_str(); - + if (itr->language != "") bookNode.append_attribute("language") = itr->language.c_str(); - + if (itr->date != "") bookNode.append_attribute("date") = itr->date.c_str(); - + if (itr->creator != "") bookNode.append_attribute("creator") = itr->creator.c_str(); if (itr->publisher != "") bookNode.append_attribute("publisher") = itr->publisher.c_str(); - + if (itr->url != "") bookNode.append_attribute("url") = itr->url.c_str(); - + + if (itr->origID != "") + bookNode.append_attribute("origId") = itr->origID.c_str(); + if (itr->articleCount != "") bookNode.append_attribute("articleCount") = itr->articleCount.c_str(); - + if (itr->mediaCount != "") bookNode.append_attribute("mediaCount") = itr->mediaCount.c_str(); @@ -211,13 +215,13 @@ namespace kiwix { } string Manager::getCurrentBookId() { - return library.current.empty() ? + return library.current.empty() ? "" : library.current.top(); } /* Add a book to the library. Return empty string if failed, book id otherwise */ - string Manager::addBookFromPathAndGetId(const string pathToOpen, const string pathToSave, - const string url, const bool checkMetaData) { + string Manager::addBookFromPathAndGetId(const string pathToOpen, const string pathToSave, + const string url, const string origId, const bool checkMetaData) { kiwix::Book book; if (this->readBookFromPath(pathToOpen, &book)) { @@ -228,9 +232,10 @@ namespace kiwix { computeAbsolutePath(removeLastPathElement(writableLibraryPath, true, false), pathToSave) : pathToSave; } - if (!checkMetaData || + if (!checkMetaData || (checkMetaData && !book.title.empty() && !book.language.empty() && !book.date.empty())) { book.url = url; + book.origID=origId; library.addBook(book); return book.id; } @@ -238,16 +243,16 @@ namespace kiwix { return ""; } - + /* 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) { - return !(this->addBookFromPathAndGetId(pathToOpen, pathToSave, url, checkMetaData).empty()); + bool Manager::addBookFromPath(const string pathToOpen, const string pathToSave, const string url, const string origId, const bool checkMetaData) { + return !(this->addBookFromPathAndGetId(pathToOpen, pathToSave, url, origId, checkMetaData).empty()); } bool Manager::readBookFromPath(const string path, kiwix::Book *book) { try { kiwix::Reader *reader = new kiwix::Reader(path); - + if (book != NULL) { book->path = path; book->pathAbsolute = path; @@ -258,18 +263,18 @@ namespace kiwix { book->creator = reader->getCreator(); book->publisher = reader->getPublisher(); book->title = reader->getTitle(); - + std::ostringstream articleCountStream; articleCountStream << reader->getArticleCount(); book->articleCount = articleCountStream.str(); - + std::ostringstream mediaCountStream; mediaCountStream << reader->getMediaCount(); book->mediaCount = mediaCountStream.str(); - + ostringstream convert; convert << reader->getFileSize(); book->size = convert.str(); - + string favicon; string faviconMimeType; if (reader->getFavicon(favicon, faviconMimeType)) { @@ -277,7 +282,7 @@ namespace kiwix { book->faviconMimeType = faviconMimeType; } } - + delete reader; } catch (const std::exception& e) { std::cerr << e.what() << std::endl; @@ -294,7 +299,7 @@ namespace kiwix { bool Manager::removeBookById(const string id) { unsigned int bookIndex = 0; std::vector::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) { return this->library.removeBookByIndex(bookIndex); } @@ -308,14 +313,14 @@ namespace kiwix { std::vector::iterator itr; std::map 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 ) { if (booksLanguagesMap.find(itr->language) == booksLanguagesMap.end()) { booksLanguagesMap[itr->language] = true; booksLanguages.push_back(itr->language); } } - + return booksLanguages; } @@ -324,14 +329,14 @@ namespace kiwix { std::vector::iterator itr; std::map 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 ) { if (booksCreatorsMap.find(itr->creator) == booksCreatorsMap.end()) { booksCreatorsMap[itr->creator] = true; booksCreators.push_back(itr->creator); } } - + return booksCreators; } @@ -343,7 +348,7 @@ namespace kiwix { for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { booksIds.push_back(itr->id); } - + return booksIds; } @@ -352,14 +357,14 @@ namespace kiwix { std::vector::iterator itr; std::map 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 ) { if (booksPublishersMap.find(itr->publisher) == booksPublishersMap.end()) { booksPublishersMap[itr->publisher] = true; booksPublishers.push_back(itr->publisher); } } - + return booksPublishers; } @@ -379,7 +384,7 @@ namespace kiwix { bool Manager::getBookById(const string id, Book &book) { std::vector::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) { book = *itr; return true; @@ -390,7 +395,7 @@ namespace kiwix { bool Manager::updateBookLastOpenDateById(const string id) { std::vector::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) { char unixdate[12]; sprintf (unixdate, "%d", (int)time(NULL)); @@ -404,7 +409,7 @@ namespace kiwix { bool Manager::setBookIndex(const string id, const string path, const supportedIndexType type) { std::vector::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) { itr->indexPath = path; itr->indexPathAbsolute = isRelativePath(path) ? @@ -419,7 +424,7 @@ namespace kiwix { bool Manager::setBookPath(const string id, const string path) { std::vector::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) { itr->path = path; itr->pathAbsolute = isRelativePath(path) ? @@ -433,7 +438,7 @@ namespace kiwix { void Manager::removeBookPaths() { std::vector::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->pathAbsolute = ""; } @@ -449,7 +454,7 @@ namespace kiwix { 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 string publisher, const string search) { this->bookIdList.clear(); @@ -457,7 +462,7 @@ namespace kiwix { /* Sort */ 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) { std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortBySize); } else if (sortBy == DATE) { @@ -467,7 +472,7 @@ namespace kiwix { } else if (sortBy == PUBLISHER) { std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByPublisher); } - + /* Special sort for LASTOPEN */ if (mode == LASTOPEN) { std::sort(library.books.begin(), library.books.end(), kiwix::Book::sortByLastOpen); @@ -482,9 +487,9 @@ namespace kiwix { if (mode == LOCAL && itr->path.empty()) ok = false; - + 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) ok = false; @@ -497,7 +502,7 @@ namespace kiwix { if (ok == true && !publisher.empty() && itr->publisher != publisher) ok = false; - + if ((ok == true && !search.empty()) && !(matchRegex(itr->title, search) || matchRegex(itr->description, search))) ok = false; @@ -506,7 +511,7 @@ namespace kiwix { } } } - + return true; } @@ -519,14 +524,14 @@ namespace kiwix { book.path = computeRelativePath(removeLastPathElement(libraryPath, true, false), book.pathAbsolute); } } - + if (!book.indexPath.empty()) { if (isRelativePath(book.indexPath)) { - book.indexPathAbsolute = + book.indexPathAbsolute = computeAbsolutePath(removeLastPathElement(libraryPath, true, false), book.indexPath); } else { book.indexPathAbsolute = book.indexPath; - book.indexPath = + book.indexPath = computeRelativePath(removeLastPathElement(libraryPath, true, false), book.indexPathAbsolute); } } diff --git a/src/common/kiwix/manager.h b/src/common/kiwix/manager.h index 6253eece6..063fca0bd 100644 --- a/src/common/kiwix/manager.h +++ b/src/common/kiwix/manager.h @@ -40,7 +40,7 @@ namespace kiwix { enum supportedListSortBy { TITLE, SIZE, DATE, CREATOR, PUBLISHER }; class Manager { - + public: Manager(); ~Manager(); @@ -55,9 +55,9 @@ namespace kiwix { string getCurrentBookId(); bool setBookIndex(const string id, const string path, const supportedIndexType type); bool setBookPath(const string id, const string path); - string addBookFromPathAndGetId(const string pathToOpen, const string pathToSave = "", const string url = "", + string addBookFromPathAndGetId(const string pathToOpen, const string pathToSave = "", const string url = "", const string origID="", const bool checkMetaData = false); - bool addBookFromPath(const string pathToOpen, const string pathToSave = "", const string url = "", + bool addBookFromPath(const string pathToOpen, const string pathToSave = "", const string url = "", const string origID="", const bool checkMetaData = false); Library cloneLibrary(); bool getBookById(const string id, Book &book); @@ -65,7 +65,7 @@ namespace kiwix { unsigned int getBookCount(const bool localBooks, const bool remoteBooks); bool updateBookLastOpenDateById(const string id); void removeBookPaths(); - bool listBooks(const supportedListMode mode, const supportedListSortBy sortBy, const unsigned int maxSize, + bool listBooks(const supportedListMode mode, const supportedListSortBy sortBy, const unsigned int maxSize, const string language, const string creator, const string publisher, const string search); vector getBooksLanguages(); vector getBooksCreators(); @@ -75,10 +75,10 @@ namespace kiwix { string writableLibraryPath; vector bookIdList; - + protected: kiwix::Library library; - + bool readBookFromPath(const string path, Book *book = NULL); bool parseXmlDom(const pugi::xml_document &doc, const bool readOnly, const string libraryPath);