From d90f8b0f05fb8585f1d7190e457bc8790dc02c2f Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Sat, 10 Aug 2019 17:06:17 +0200 Subject: [PATCH] Add a name_mapper mapping the HumanReadable name to the id. --- include/name_mapper.h | 17 +++++++++++++ src/meson.build | 1 + src/name_mapper.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/name_mapper.cpp diff --git a/include/name_mapper.h b/include/name_mapper.h index 37c86e865..3df594c12 100644 --- a/include/name_mapper.h +++ b/include/name_mapper.h @@ -21,10 +21,13 @@ #define KIWIX_NAMEMAPPER_H #include +#include namespace kiwix { +class Library; + class NameMapper { public: virtual ~NameMapper() = default; @@ -39,6 +42,20 @@ class IdNameMapper : public NameMapper { virtual std::string getIdForName(const std::string& name) { return name; }; }; +class HumanReadableNameMapper : public NameMapper { + private: + std::map m_idToName; + std::map m_nameToId; + + public: + HumanReadableNameMapper(kiwix::Library& library, bool withAlias); + virtual ~HumanReadableNameMapper() = default; + virtual std::string getNameForId(const std::string& id); + virtual std::string getIdForName(const std::string& name); +}; + + + } #endif diff --git a/src/meson.build b/src/meson.build index 8b158ae46..08da8652a 100644 --- a/src/meson.build +++ b/src/meson.build @@ -20,6 +20,7 @@ kiwix_sources = [ 'tools/networkTools.cpp', 'tools/otherTools.cpp', 'kiwixserve.cpp', + 'name_mapper.cpp', 'server/request_context.cpp', 'server/response.cpp' ] diff --git a/src/name_mapper.cpp b/src/name_mapper.cpp new file mode 100644 index 000000000..1b749dd33 --- /dev/null +++ b/src/name_mapper.cpp @@ -0,0 +1,59 @@ +/* + * Copyright 2019 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. + */ + +#include "name_mapper.h" +#include "library.h" +#include "tools/regexTools.h" +#include + +namespace kiwix { + +HumanReadableNameMapper::HumanReadableNameMapper(kiwix::Library& library, bool withAlias) { + for (auto& bookId: library.filter(kiwix::Filter().local(true).valid(true))) { + auto& currentBook = library.getBookById(bookId); + auto bookName = currentBook.getHumanReadableIdFromPath(); + m_idToName[bookId] = bookName; + m_nameToId[bookName] = bookId; + + if (!withAlias) + continue; + + auto aliasName = replaceRegex(bookName, "", "_[[:digit:]]{4}-[[:digit:]]{2}$"); + if (m_nameToId.find(aliasName) == m_nameToId.end()) { + m_nameToId[aliasName] = bookId; + } else { + auto alreadyPresentPath = library.getBookById(aliasName).getPath(); + std::cerr << "Path collision: " << alreadyPresentPath + << " and " << currentBook.getPath() + << " can't share the same URL path '" << aliasName << "'." + << " Therefore, only " << alreadyPresentPath + << " will be served." << std::endl; + } + } +} + +std::string HumanReadableNameMapper::getNameForId(const std::string& id) { + return m_idToName.at(id); +} + +std::string HumanReadableNameMapper::getIdForName(const std::string& name) { + return m_nameToId.at(name); +} + +}