From c6254d95043e76295713f563e5d163311edf1fa0 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Thu, 7 Mar 2019 17:08:39 +0100 Subject: [PATCH] Allow the library to be filtered by tags. This add an argument to `listBooksIds` to filter by tags. So, this is an API break. --- include/library.h | 1 + src/library.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/library.h b/include/library.h index 24f8fe5cd..890d140d5 100644 --- a/include/library.h +++ b/include/library.h @@ -194,6 +194,7 @@ class Library const std::string& language = "", const std::string& creator = "", const std::string& publisher = "", + const std::vector& tags = {}, size_t maxSize = 0); friend class OPDSDumper; diff --git a/src/library.cpp b/src/library.cpp index ac8adb8fb..58c6d8b48 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -27,6 +27,7 @@ #include #include +#include namespace kiwix { @@ -250,6 +251,7 @@ std::vector Library::listBooksIds( const std::string& language, const std::string& creator, const std::string& publisher, + const std::vector& tags, size_t maxSize) { std::vector bookIds; @@ -270,6 +272,23 @@ std::vector Library::listBooksIds( continue; if (mode & NOREMOTE && remote) continue; + if (!tags.empty()) { + auto vBookTags = split(book.getTags(), ";"); + std::set 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) continue; if (!language.empty() && book.getLanguage() != language)