mirror of https://github.com/kiwix/libkiwix.git
+ new dev in content manager core
This commit is contained in:
parent
6b8112b88b
commit
6cfc716c84
|
@ -43,7 +43,9 @@ namespace kiwix {
|
|||
}
|
||||
|
||||
/* Constructor */
|
||||
Library::Library() {
|
||||
Library::Library():
|
||||
current(""),
|
||||
version(KIWIX_LIBRARY_VERSION) {
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
#ifndef KIWIX_LIBRARY_H
|
||||
#define KIWIX_LIBRARY_H
|
||||
|
||||
#define KIWIX_LIBRARY_VERSION "20110503"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
@ -57,6 +59,7 @@ namespace kiwix {
|
|||
~Library();
|
||||
|
||||
string current;
|
||||
string version;
|
||||
bool addBook(const Book &book);
|
||||
bool removeBookByIndex(const unsigned int bookIndex);
|
||||
vector <kiwix::Book> books;
|
||||
|
|
|
@ -37,6 +37,8 @@ namespace kiwix {
|
|||
if (result) {
|
||||
pugi::xml_node libraryNode = doc.child("library");
|
||||
library.current = libraryNode.attribute("current").value();
|
||||
string libraryVersion = libraryNode.attribute("version").value();
|
||||
bool ok = true;
|
||||
|
||||
for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode; bookNode = bookNode.next_sibling("book")) {
|
||||
kiwix::Book book;
|
||||
|
@ -53,8 +55,17 @@ namespace kiwix {
|
|||
book.url = bookNode.attribute("url").value();
|
||||
book.articleCount = bookNode.attribute("articleCount").value();
|
||||
book.mediaCount = bookNode.attribute("mediaCount").value();
|
||||
|
||||
/* Update the book properties with the new importer */
|
||||
if (libraryVersion.empty() || atoi(libraryVersion.c_str()) < atoi(KIWIX_LIBRARY_VERSION)) {
|
||||
if (!book.path.empty())
|
||||
ok = this->readBookFromPath(book.path, book);
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
library.addBook(book);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -70,6 +81,9 @@ namespace kiwix {
|
|||
if (library.current != "")
|
||||
libraryNode.append_attribute("current") = library.current.c_str();
|
||||
|
||||
if (library.version != "")
|
||||
libraryNode.append_attribute("version") = library.version.c_str();
|
||||
|
||||
/* Add each book */
|
||||
std::vector<kiwix::Book>::iterator itr;
|
||||
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
|
||||
|
@ -123,7 +137,18 @@ namespace kiwix {
|
|||
bool Manager::addBookFromPath(const string path, const string url) {
|
||||
kiwix::Book book;
|
||||
|
||||
/* Open the ZIM file */
|
||||
if (this->readBookFromPath(path, book)) {
|
||||
book.url = url;
|
||||
library.addBook(book);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Manager::readBookFromPath(const string path, kiwix::Book &book) {
|
||||
|
||||
try {
|
||||
kiwix::Reader reader = kiwix::Reader(path);
|
||||
book.path = path;
|
||||
book.id = reader.getId();
|
||||
|
@ -132,7 +157,6 @@ namespace kiwix {
|
|||
book.language = reader.getLanguage();
|
||||
book.date = reader.getDate();
|
||||
book.creator = reader.getCreator();
|
||||
book.url = url;
|
||||
|
||||
std::ostringstream articleCountStream;
|
||||
articleCountStream << reader.getArticleCount();
|
||||
|
@ -141,8 +165,9 @@ namespace kiwix {
|
|||
std::ostringstream mediaCountStream;
|
||||
mediaCountStream << reader.getMediaCount();
|
||||
book.mediaCount = mediaCountStream.str();
|
||||
|
||||
library.addBook(book);
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -151,6 +176,17 @@ namespace kiwix {
|
|||
return this->library.removeBookByIndex(bookIndex);
|
||||
}
|
||||
|
||||
bool Manager::removeBookById(const string id) {
|
||||
unsigned int bookIndex = 0;
|
||||
std::vector<kiwix::Book>::iterator itr;
|
||||
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
|
||||
if ( itr->id == id)
|
||||
return this->library.removeBookByIndex(bookIndex);
|
||||
bookIndex++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
kiwix::Library Manager::cloneLibrary() {
|
||||
return this->library;
|
||||
}
|
||||
|
|
|
@ -39,11 +39,14 @@ namespace kiwix {
|
|||
bool readFile(const string path);
|
||||
bool writeFile(const string path);
|
||||
bool removeBookByIndex(const unsigned int bookIndex);
|
||||
bool removeBookById(const string id);
|
||||
bool addBookFromPath(const string path, const string url = "");
|
||||
kiwix::Library cloneLibrary();
|
||||
Library cloneLibrary();
|
||||
|
||||
protected:
|
||||
kiwix::Library library;
|
||||
|
||||
bool readBookFromPath(const string path, kiwix::Book &book);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue