<commit_before>/** \brief Utility to automatically generate maps used for zotkat.
 *  \author <NAME>
 *
 *  \copyright 2020 Universitätsbibliothek Tübingen.  All rights reserved.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <unordered_set>
#include <cstdio>
#include <cstdlib>
#include "FileUtil.h"
#include "IniFile.h"
#include "MiscUtil.h"
#include "StringUtil.h"
#include "UBTools.h"
#include "util.h"
#include "ZoteroHarvesterConfig.h"
#include "ZoteroHarvesterUtil.h"
#include "ZoteroHarvesterZederInterop.h"


// avoid conflicts with LICENSE defined in mysql_version.h
#ifdef LICENSE
    #undef LICENSE
#endif


namespace {


[[noreturn]] void Usage() {
    ::Usage("[options] [zotero_enhancement_maps_directory]\n"
            "\n"
            "\tOptions:\n"
            "\t[--min-log-level=log_level]          Possible log levels are ERROR, WARNING (default), INFO and DEBUG\n"
            "\t[zotero_enhancement_maps_directory]  or default " + UBTools::GetTuelibPath() + "zotero-enhancement-maps" + "\n");
}


void DownloadZederInstanceEntries(const Zeder::Flavour flavour,
                                  std::unordered_map<Zeder::Flavour, Zeder::EntryCollection> * const downloaded_entries)
{
    LOG_INFO("Downloading Zeder entries for " + Zeder::FLAVOUR_TO_STRING_MAP.at(flavour));
    Zeder::EntryCollection downloaded_flavour_entries;

    const auto endpoint_url(Zeder::GetFullDumpEndpointPath(flavour));
    const std::unordered_set<unsigned> entries_to_download {}; // intentionally empty
    const std::unordered_set<std::string> columns_to_download {};  // intentionally empty
    const std::unordered_map<std::string, std::string> filter_regexps {}; // intentionally empty
    std::unique_ptr<Zeder::FullDumpDownloader::Params> downloader_params(new Zeder::FullDumpDownloader::Params(endpoint_url,
                                                                         entries_to_download, columns_to_download, filter_regexps));

    auto downloader(Zeder::FullDumpDownloader::Factory(Zeder::FullDumpDownloader::Type::FULL_DUMP, std::move(downloader_params)));
    if (not downloader->download(&downloaded_flavour_entries))
        LOG_ERROR("couldn't download full dump for " + Zeder::FLAVOUR_TO_STRING_MAP.at(flavour));

    downloaded_entries->emplace(flavour, downloaded_flavour_entries);
}


std::unordered_set<std::string> GetZederEntryISSNs(const Zeder::Entry &entry, const Zeder::Flavour flavour) {
    std::unordered_set<std::string> issns;

    std::unordered_set<ZoteroHarvester::Config::JournalParams::IniKey> ini_keys {
        ZoteroHarvester::Config::JournalParams::IniKey::ONLINE_ISSN,
        ZoteroHarvester::Config::JournalParams::IniKey::PRINT_ISSN
    };
    for (const auto ini_key : ini_keys) {
        const std::string issn(ZoteroHarvester::ZederInterop::GetJournalParamsIniValueFromZederEntry(entry, flavour,
                               ini_key));

        if (not issn.empty()) {
            if (MiscUtil::IsPossibleISSN(issn))
                issns.emplace(issn);
            else
                LOG_WARNING("Skipping invalid ISSN: " + issn);
        }
    }

    return issns;
}


void GenerateIssnToLicenseMap(const std::string &zotero_enhancement_maps_directory,
                              const std::unordered_map<Zeder::Flavour, Zeder::EntryCollection> &downloaded_entries)
{
    const std::string map_path(zotero_enhancement_maps_directory + "/ISSN_to_licence.map");<commit_msg>zotero_enhancement_maps_generator.cc: Added Zeder + instance ID's in warning on invalid ISSN<commit_after>/** \brief Utility to automatically generate maps used for zotkat.
 *  \author <NAME>
 *
 *  \copyright 2020 Universitätsbibliothek Tübingen.  All rights reserved.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <unordered_set>
#include <cstdio>
#include <cstdlib>
#include "FileUtil.h"
#include "IniFile.h"
#include "MiscUtil.h"
#include "StringUtil.h"
#include "UBTools.h"
#include "util.h"
#include "ZoteroHarvesterConfig.h"
#include "ZoteroHarvesterUtil.h"
#include "ZoteroHarvesterZederInterop.h"


// avoid conflicts with LICENSE defined in mysql_version.h
#ifdef LICENSE
    #undef LICENSE
#endif


namespace {


[[noreturn]] void Usage() {
    ::Usage("[options] [zotero_enhancement_maps_directory]\n"
            "\n"
            "\tOptions:\n"
            "\t[--min-log-level=log_level]          Possible log levels are ERROR, WARNING (default), INFO and DEBUG\n"
            "\t[zotero_enhancement_maps_directory]  or default " + UBTools::GetTuelibPath() + "zotero-enhancement-maps" + "\n");
}


void DownloadZederInstanceEntries(const Zeder::Flavour flavour,
                                  std::unordered_map<Zeder::Flavour, Zeder::EntryCollection> * const downloaded_entries)
{
    LOG_INFO("Downloading Zeder entries for " + Zeder::FLAVOUR_TO_STRING_MAP.at(flavour));
    Zeder::EntryCollection downloaded_flavour_entries;

    const auto endpoint_url(Zeder::GetFullDumpEndpointPath(flavour));
    const std::unordered_set<unsigned> entries_to_download {}; // intentionally empty
    const std::unordered_set<std::string> columns_to_download {};  // intentionally empty
    const std::unordered_map<std::string, std::string> filter_regexps {}; // intentionally empty
    std::unique_ptr<Zeder::FullDumpDownloader::Params> downloader_params(new Zeder::FullDumpDownloader::Params(endpoint_url,
                                                                         entries_to_download, columns_to_download, filter_regexps));

    auto downloader(Zeder::FullDumpDownloader::Factory(Zeder::FullDumpDownloader::Type::FULL_DUMP, std::move(downloader_params)));
    if (not downloader->download(&downloaded_flavour_entries))
        LOG_ERROR("couldn't download full dump for " + Zeder::FLAVOUR_TO_STRING_MAP.at(flavour));

    downloaded_entries->emplace(flavour, downloaded_flavour_entries);
}


std::unordered_set<std::string> GetZederEntryISSNs(const Zeder::Entry &entry, const Zeder::Flavour flavour) {
    std::unordered_set<std::string> issns;

    std::unordered_set<ZoteroHarvester::Config::JournalParams::IniKey> ini_keys {
        ZoteroHarvester::Config::JournalParams::IniKey::ONLINE_ISSN,
        ZoteroHarvester::Config::JournalParams::IniKey::PRINT_ISSN
    };
    for (const auto ini_key : ini_keys) {
        const std::string issn(ZoteroHarvester::ZederInterop::GetJournalParamsIniValueFromZederEntry(entry, flavour,
                               ini_key));

        if (not issn.empty()) {
            if (MiscUtil::IsPossibleISSN(issn))
                issns.emplace(issn);
            else {
                LOG_WARNING("Skipping invalid ISSN: " + issn
                            + " (Zeder ID: " + std::to_string(entry.getId())
                            + ", Instance: " + Zeder::FLAVOUR_TO_STRING_MAP.at(flavour) + ")");
            }
        }
    }

    return issns;
}


void GenerateIssnToLicenseMap(const std::string &zotero_enhancement_maps_directory,
                              const std::unordered_map<Zeder::Flavour, Zeder::EntryCollection> &downloaded_entries)
{
    const std::string map_path(zotero_enhancement_maps_directory + "/ISSN_to_licence.map");
{'fraction_non_alphanumeric': 0.10579641847313855, 'fraction_numerical': 0.00117813383600377, 'mean_word_length': 3.708264004437049, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2220778', 'n_tokens_mistral': 2657, 'n_tokens_neox': 2518, 'n_words': 585}