From f1873876b2a4675369c0e044e5924542a39ac406 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Fri, 3 Mar 2023 00:30:58 +0530 Subject: [PATCH] Extract LibraryDumper from OPDSDumper This change creates a new common class for dumping the library into various formats: LibraryDumper --- include/library_dumper.h | 91 +++++++++++++++++++++++++++++ include/opds_dumper.h | 38 +----------- src/library_dumper.cpp | 121 +++++++++++++++++++++++++++++++++++++++ src/meson.build | 1 + src/opds_dumper.cpp | 94 ++---------------------------- 5 files changed, 220 insertions(+), 125 deletions(-) create mode 100644 include/library_dumper.h create mode 100644 src/library_dumper.cpp diff --git a/include/library_dumper.h b/include/library_dumper.h new file mode 100644 index 000000000..3438ef711 --- /dev/null +++ b/include/library_dumper.h @@ -0,0 +1,91 @@ +/* + * Copyright 2023 Nikhil Tanwar <2002nikhiltanwar@gmail.com> + * Copyright 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#ifndef KIWIX_LIBRARY_DUMPER_H +#define KIWIX_LIBRARY_DUMPER_H + +#include + + +#include "library.h" +#include "name_mapper.h" +#include + +namespace kiwix +{ + +/** + * A base class to dump Library in various formats. + * + */ +class LibraryDumper +{ + public: + LibraryDumper(const Library* library, const NameMapper* NameMapper); + ~LibraryDumper(); + + void setLibraryId(const std::string& id) { this->libraryId = id;} + + /** + * Set the root location used when generating url. + * + * @param rootLocation the root location to use. + */ + void setRootLocation(const std::string& rootLocation) { this->rootLocation = rootLocation; } + + /** + * Set some informations about the search results. + * + * @param totalResult the total number of results of the search. + * @param startIndex the start index of the result. + * @param count the number of result of the current set (or page). + */ + void setOpenSearchInfo(int totalResult, int startIndex, int count); + + /** + * Sets user default language + * + * @param userLang the user language to be set + */ + void setUserLanguage(std::string userLang) { this->m_userLang = userLang; } + + /** + * Get the data of categories + */ + kainjow::mustache::list getCategoryData() const; + + /** + * Get the data of languages + */ + kainjow::mustache::list getLanguageData() const; + + protected: + const kiwix::Library* const library; + const kiwix::NameMapper* const nameMapper; + std::string libraryId; + std::string rootLocation; + std::string m_userLang; + int m_totalResults; + int m_startIndex; + int m_count; +}; +} + +#endif // KIWIX_LIBRARY_DUMPER_H diff --git a/include/opds_dumper.h b/include/opds_dumper.h index cb6566f4f..c2f5ff182 100644 --- a/include/opds_dumper.h +++ b/include/opds_dumper.h @@ -28,6 +28,7 @@ #include "library.h" #include "name_mapper.h" +#include "library_dumper.h" using namespace std; @@ -38,11 +39,10 @@ namespace kiwix * A tool to dump a `Library` into a opds stream. * */ -class OPDSDumper +class OPDSDumper : public LibraryDumper { public: - OPDSDumper() = default; - OPDSDumper(Library* library, NameMapper* NameMapper); + OPDSDumper(const Library* library, const NameMapper* NameMapper); ~OPDSDumper(); /** @@ -85,38 +85,6 @@ class OPDSDumper * @return The OPDS feed. */ std::string languagesOPDSFeed() const; - - /** - * Set the id of the library. - * - * @param id the id to use. - */ - void setLibraryId(const std::string& id) { this->libraryId = id;} - - /** - * Set the root location used when generating url. - * - * @param rootLocation the root location to use. - */ - void setRootLocation(const std::string& rootLocation) { this->rootLocation = rootLocation; } - - /** - * Set some informations about the search results. - * - * @param totalResult the total number of results of the search. - * @param startIndex the start index of the result. - * @param count the number of result of the current set (or page). - */ - void setOpenSearchInfo(int totalResult, int startIndex, int count); - - protected: - kiwix::Library* library; - kiwix::NameMapper* nameMapper; - std::string libraryId; - std::string rootLocation; - int m_totalResults; - int m_startIndex; - int m_count; }; } diff --git a/src/library_dumper.cpp b/src/library_dumper.cpp new file mode 100644 index 000000000..81880db3b --- /dev/null +++ b/src/library_dumper.cpp @@ -0,0 +1,121 @@ +#include "library_dumper.h" +#include "tools/stringTools.h" +#include "tools/otherTools.h" +#include "tools.h" +#include "tools/regexTools.h" + +namespace kiwix +{ +/* Constructor */ +LibraryDumper::LibraryDumper(const Library* library, const NameMapper* nameMapper) + : library(library), + nameMapper(nameMapper) +{ +} +/* Destructor */ +LibraryDumper::~LibraryDumper() +{ +} + +void LibraryDumper::setOpenSearchInfo(int totalResults, int startIndex, int count) +{ + m_totalResults = totalResults; + m_startIndex = startIndex, + m_count = count; +} + +namespace { + +std::map iso639_3 = { + {"atj", "atikamekw"}, + {"azb", "آذربایجان دیلی"}, + {"bcl", "central bikol"}, + {"bgs", "tagabawa"}, + {"bxr", "буряад хэлэн"}, + {"cbk", "chavacano"}, + {"cdo", "閩東語"}, + {"dag", "Dagbani"}, + {"diq", "dimli"}, + {"dty", "डोटेली"}, + {"eml", "emiliân-rumagnōl"}, + {"fbs", "српскохрватски"}, + {"hbs", "srpskohrvatski"}, + {"ido", "ido"}, + {"kbp", "kabɩyɛ"}, + {"kld", "Gamilaraay"}, + {"lbe", "лакку маз"}, + {"lbj", "ལ་དྭགས་སྐད་"}, + {"map", "Austronesian"}, + {"mhr", "марий йылме"}, + {"mnw", "ဘာသာမန်"}, + {"myn", "mayan"}, + {"nah", "nahuatl"}, + {"nai", "north American Indian"}, + {"nds", "plattdütsch"}, + {"nrm", "bhasa narom"}, + {"olo", "livvi"}, + {"pih", "Pitcairn-Norfolk"}, + {"pnb", "Western Panjabi"}, + {"rmr", "Caló"}, + {"rmy", "romani shib"}, + {"roa", "romance languages"}, + {"twi", "twi"}, +}; + +std::once_flag fillLanguagesFlag; + +void fillLanguagesMap() +{ + for (auto icuLangPtr = icu::Locale::getISOLanguages(); *icuLangPtr != NULL; ++icuLangPtr) { + const ICULanguageInfo lang(*icuLangPtr); + iso639_3.insert({lang.iso3Code(), lang.selfName()}); + } +} + +std::string getLanguageSelfName(const std::string& lang) { + const auto itr = iso639_3.find(lang); + if (itr != iso639_3.end()) { + return itr->second; + } + return lang; +}; + +} // unnamed namespace + +kainjow::mustache::list LibraryDumper::getCategoryData() const +{ + const auto now = gen_date_str(); + kainjow::mustache::list categoryData; + for ( const auto& category : library->getBooksCategories() ) { + const auto urlencodedCategoryName = urlEncode(category); + categoryData.push_back(kainjow::mustache::object{ + {"name", category}, + {"urlencoded_name", urlencodedCategoryName}, + {"updated", now}, + {"id", gen_uuid(libraryId + "/categories/" + urlencodedCategoryName)} + }); + } + return categoryData; +} + +kainjow::mustache::list LibraryDumper::getLanguageData() const +{ + const auto now = gen_date_str(); + kainjow::mustache::list languageData; + std::call_once(fillLanguagesFlag, fillLanguagesMap); + for ( const auto& langAndBookCount : library->getBooksLanguagesWithCounts() ) { + const std::string languageCode = langAndBookCount.first; + const int bookCount = langAndBookCount.second; + const auto languageSelfName = getLanguageSelfName(languageCode); + languageData.push_back(kainjow::mustache::object{ + {"lang_code", languageCode}, + {"lang_self_name", languageSelfName}, + {"book_count", to_string(bookCount)}, + {"updated", now}, + {"id", gen_uuid(libraryId + "/languages/" + languageCode)} + }); + } + return languageData; +} + +} // namespace kiwix diff --git a/src/meson.build b/src/meson.build index 3c486fb18..3bbb24f98 100644 --- a/src/meson.build +++ b/src/meson.build @@ -5,6 +5,7 @@ kiwix_sources = [ 'manager.cpp', 'libxml_dumper.cpp', 'opds_dumper.cpp', + 'library_dumper.cpp', 'downloader.cpp', 'server.cpp', 'search_renderer.cpp', diff --git a/src/opds_dumper.cpp b/src/opds_dumper.cpp index 1104029ac..a8b1be9ed 100644 --- a/src/opds_dumper.cpp +++ b/src/opds_dumper.cpp @@ -30,9 +30,8 @@ namespace kiwix { /* Constructor */ -OPDSDumper::OPDSDumper(Library* library, NameMapper* nameMapper) - : library(library), - nameMapper(nameMapper) +OPDSDumper::OPDSDumper(const Library* library, const NameMapper* nameMapper) + : LibraryDumper(library, nameMapper) { } /* Destructor */ @@ -40,13 +39,6 @@ OPDSDumper::~OPDSDumper() { } -void OPDSDumper::setOpenSearchInfo(int totalResults, int startIndex, int count) -{ - m_totalResults = totalResults; - m_startIndex = startIndex, - m_count = count; -} - namespace { @@ -133,60 +125,6 @@ BooksData getBooksData(const Library* library, const NameMapper* nameMapper, con return booksData; } -std::map iso639_3 = { - {"atj", "atikamekw"}, - {"azb", "آذربایجان دیلی"}, - {"bcl", "central bikol"}, - {"bgs", "tagabawa"}, - {"bxr", "буряад хэлэн"}, - {"cbk", "chavacano"}, - {"cdo", "閩東語"}, - {"dag", "Dagbani"}, - {"diq", "dimli"}, - {"dty", "डोटेली"}, - {"eml", "emiliân-rumagnōl"}, - {"fbs", "српскохрватски"}, - {"hbs", "srpskohrvatski"}, - {"ido", "ido"}, - {"kbp", "kabɩyɛ"}, - {"kld", "Gamilaraay"}, - {"lbe", "лакку маз"}, - {"lbj", "ལ་དྭགས་སྐད་"}, - {"map", "Austronesian"}, - {"mhr", "марий йылме"}, - {"mnw", "ဘာသာမန်"}, - {"myn", "mayan"}, - {"nah", "nahuatl"}, - {"nai", "north American Indian"}, - {"nds", "plattdütsch"}, - {"nrm", "bhasa narom"}, - {"olo", "livvi"}, - {"pih", "Pitcairn-Norfolk"}, - {"pnb", "Western Panjabi"}, - {"rmr", "Caló"}, - {"rmy", "romani shib"}, - {"roa", "romance languages"}, - {"twi", "twi"}, -}; - -std::once_flag fillLanguagesFlag; - -void fillLanguagesMap() -{ - for (auto icuLangPtr = icu::Locale::getISOLanguages(); *icuLangPtr != NULL; ++icuLangPtr) { - const ICULanguageInfo lang(*icuLangPtr); - iso639_3.insert({lang.iso3Code(), lang.selfName()}); - } -} - -std::string getLanguageSelfName(const std::string& lang) { - const auto itr = iso639_3.find(lang); - if (itr != iso639_3.end()) { - return itr->second; - } - return lang; -}; - } // unnamed namespace string OPDSDumper::dumpOPDSFeed(const std::vector& bookIds, const std::string& query) const @@ -240,17 +178,7 @@ std::string OPDSDumper::dumpOPDSCompleteEntry(const std::string& bookId) const std::string OPDSDumper::categoriesOPDSFeed() const { const auto now = gen_date_str(); - kainjow::mustache::list categoryData; - for ( const auto& category : library->getBooksCategories() ) { - const auto urlencodedCategoryName = urlEncode(category); - categoryData.push_back(kainjow::mustache::object{ - {"name", category}, - {"urlencoded_name", urlencodedCategoryName}, - {"updated", now}, - {"id", gen_uuid(libraryId + "/categories/" + urlencodedCategoryName)} - }); - } - + kainjow::mustache::list categoryData = getCategoryData(); return render_template( RESOURCE::templates::catalog_v2_categories_xml, kainjow::mustache::object{ @@ -265,21 +193,7 @@ std::string OPDSDumper::categoriesOPDSFeed() const std::string OPDSDumper::languagesOPDSFeed() const { const auto now = gen_date_str(); - kainjow::mustache::list languageData; - std::call_once(fillLanguagesFlag, fillLanguagesMap); - for ( const auto& langAndBookCount : library->getBooksLanguagesWithCounts() ) { - const std::string languageCode = langAndBookCount.first; - const int bookCount = langAndBookCount.second; - const auto languageSelfName = getLanguageSelfName(languageCode); - languageData.push_back(kainjow::mustache::object{ - {"lang_code", languageCode}, - {"lang_self_name", languageSelfName}, - {"book_count", to_string(bookCount)}, - {"updated", now}, - {"id", gen_uuid(libraryId + "/languages/" + languageCode)} - }); - } - + kainjow::mustache::list languageData = getLanguageData(); return render_template( RESOURCE::templates::catalog_v2_languages_xml, kainjow::mustache::object{