Merge pull request #204 from kiwix/library_filter_tag

Allow the library to be filtered by tags.
This commit is contained in:
Matthieu Gautier 2019-03-07 17:19:31 +01:00 committed by GitHub
commit f71f2935e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -194,6 +194,7 @@ class Library
const std::string& language = "", const std::string& language = "",
const std::string& creator = "", const std::string& creator = "",
const std::string& publisher = "", const std::string& publisher = "",
const std::vector<std::string>& tags = {},
size_t maxSize = 0); size_t maxSize = 0);
friend class OPDSDumper; friend class OPDSDumper;

View File

@ -27,6 +27,7 @@
#include <pugixml.hpp> #include <pugixml.hpp>
#include <algorithm> #include <algorithm>
#include <set>
namespace kiwix namespace kiwix
{ {
@ -250,6 +251,7 @@ std::vector<std::string> Library::listBooksIds(
const std::string& language, const std::string& language,
const std::string& creator, const std::string& creator,
const std::string& publisher, const std::string& publisher,
const std::vector<std::string>& tags,
size_t maxSize) { size_t maxSize) {
std::vector<std::string> bookIds; std::vector<std::string> bookIds;
@ -270,6 +272,23 @@ std::vector<std::string> Library::listBooksIds(
continue; continue;
if (mode & NOREMOTE && remote) if (mode & NOREMOTE && remote)
continue; continue;
if (!tags.empty()) {
auto vBookTags = split(book.getTags(), ";");
std::set<std::string> sBookTags(vBookTags.begin(), vBookTags.end());
bool ok = true;
for (auto& t: tags) {
if (sBookTags.find(t) == sBookTags.end()) {
// A "filter" tag is not in the book tag.
// No need to loop for all "filter" tags.
ok = false;
break;
}
}
if (! ok ) {
// Skip the book
continue;
}
}
if (maxSize != 0 && book.getSize() > maxSize) if (maxSize != 0 && book.getSize() > maxSize)
continue; continue;
if (!language.empty() && book.getLanguage() != language) if (!language.empty() && book.getLanguage() != language)