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 */
|
/* Constructor */
|
||||||
Library::Library() {
|
Library::Library():
|
||||||
|
current(""),
|
||||||
|
version(KIWIX_LIBRARY_VERSION) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Destructor */
|
/* Destructor */
|
||||||
|
|
|
@ -20,6 +20,8 @@
|
||||||
#ifndef KIWIX_LIBRARY_H
|
#ifndef KIWIX_LIBRARY_H
|
||||||
#define KIWIX_LIBRARY_H
|
#define KIWIX_LIBRARY_H
|
||||||
|
|
||||||
|
#define KIWIX_LIBRARY_VERSION "20110503"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -57,6 +59,7 @@ namespace kiwix {
|
||||||
~Library();
|
~Library();
|
||||||
|
|
||||||
string current;
|
string current;
|
||||||
|
string version;
|
||||||
bool addBook(const Book &book);
|
bool addBook(const Book &book);
|
||||||
bool removeBookByIndex(const unsigned int bookIndex);
|
bool removeBookByIndex(const unsigned int bookIndex);
|
||||||
vector <kiwix::Book> books;
|
vector <kiwix::Book> books;
|
||||||
|
|
|
@ -37,6 +37,8 @@ namespace kiwix {
|
||||||
if (result) {
|
if (result) {
|
||||||
pugi::xml_node libraryNode = doc.child("library");
|
pugi::xml_node libraryNode = doc.child("library");
|
||||||
library.current = libraryNode.attribute("current").value();
|
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")) {
|
for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode; bookNode = bookNode.next_sibling("book")) {
|
||||||
kiwix::Book book;
|
kiwix::Book book;
|
||||||
|
@ -53,8 +55,17 @@ namespace kiwix {
|
||||||
book.url = bookNode.attribute("url").value();
|
book.url = bookNode.attribute("url").value();
|
||||||
book.articleCount = bookNode.attribute("articleCount").value();
|
book.articleCount = bookNode.attribute("articleCount").value();
|
||||||
book.mediaCount = bookNode.attribute("mediaCount").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);
|
library.addBook(book);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,6 +81,9 @@ namespace kiwix {
|
||||||
if (library.current != "")
|
if (library.current != "")
|
||||||
libraryNode.append_attribute("current") = library.current.c_str();
|
libraryNode.append_attribute("current") = library.current.c_str();
|
||||||
|
|
||||||
|
if (library.version != "")
|
||||||
|
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 ) {
|
||||||
|
@ -123,7 +137,18 @@ namespace kiwix {
|
||||||
bool Manager::addBookFromPath(const string path, const string url) {
|
bool Manager::addBookFromPath(const string path, const string url) {
|
||||||
kiwix::Book book;
|
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);
|
kiwix::Reader reader = kiwix::Reader(path);
|
||||||
book.path = path;
|
book.path = path;
|
||||||
book.id = reader.getId();
|
book.id = reader.getId();
|
||||||
|
@ -132,7 +157,6 @@ namespace kiwix {
|
||||||
book.language = reader.getLanguage();
|
book.language = reader.getLanguage();
|
||||||
book.date = reader.getDate();
|
book.date = reader.getDate();
|
||||||
book.creator = reader.getCreator();
|
book.creator = reader.getCreator();
|
||||||
book.url = url;
|
|
||||||
|
|
||||||
std::ostringstream articleCountStream;
|
std::ostringstream articleCountStream;
|
||||||
articleCountStream << reader.getArticleCount();
|
articleCountStream << reader.getArticleCount();
|
||||||
|
@ -141,8 +165,9 @@ namespace kiwix {
|
||||||
std::ostringstream mediaCountStream;
|
std::ostringstream mediaCountStream;
|
||||||
mediaCountStream << reader.getMediaCount();
|
mediaCountStream << reader.getMediaCount();
|
||||||
book.mediaCount = mediaCountStream.str();
|
book.mediaCount = mediaCountStream.str();
|
||||||
|
} catch (...) {
|
||||||
library.addBook(book);
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -151,6 +176,17 @@ namespace kiwix {
|
||||||
return this->library.removeBookByIndex(bookIndex);
|
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() {
|
kiwix::Library Manager::cloneLibrary() {
|
||||||
return this->library;
|
return this->library;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,11 +39,14 @@ namespace kiwix {
|
||||||
bool readFile(const string path);
|
bool readFile(const string path);
|
||||||
bool writeFile(const string path);
|
bool writeFile(const string path);
|
||||||
bool removeBookByIndex(const unsigned int bookIndex);
|
bool removeBookByIndex(const unsigned int bookIndex);
|
||||||
|
bool removeBookById(const string id);
|
||||||
bool addBookFromPath(const string path, const string url = "");
|
bool addBookFromPath(const string path, const string url = "");
|
||||||
kiwix::Library cloneLibrary();
|
Library cloneLibrary();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
kiwix::Library library;
|
kiwix::Library library;
|
||||||
|
|
||||||
|
bool readBookFromPath(const string path, kiwix::Book &book);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue