<?PHP
/**
 * Contains class NodaIDGetter.
 *
 * @author Joshua Ramon Enslin <joshua@museum-digital.de>
 */
declare(strict_types = 1);

/**
 * Contains static functions for getting IDs for noda entries by various means.
 */
final class NodaIDGetter {
    /**
     * Comparison operation that allows for equality but also returns true
     * if capitalization differs. If diacritics are available in one case
     * and not in the other, there will be no match.
     * This is needed to identify sufficiently exact matches despite a table
     * collation of *_ci in MySQL.
     *
     * @param string $string_one First string in comparison.
     * @param string $string_two Second string in comparison.
     *
     * @return boolean
     */
    private static function _stri_matches(string $string_one, string $string_two):bool {

        if ($string_one === $string_two) {
            return true;
        }
        if (\strtolower($string_one) === \strtolower($string_two)) {
            return true;
        }

        return false;

    }

    //
    // Actors
    //

    /**
     * Returns persinst ID by name, checking the different options for it.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the persinst to search for.
     *
     * @return integer
     */
    public static function getPersinstIDByName(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        if ($persinstByTransName = self::getPersinstIDByTransName($mysqli_noda, $lang, $name)) {
            return $persinstByTransName;
        }

        if ($persinstByBaseName = self::getPersinstIDByBaseName($mysqli_noda, $name)) {
            return $persinstByBaseName;
        }

        return 0;

    }

    /**
     * Returns persinst ID by name, checking the different options for it.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the persinst to search for.
     * @param string   $birth       Birth year.
     * @param string   $death       Death year.
     *
     * @return integer
     */
    public static function getPersinstIDByNamePlusYears(MDMysqli $mysqli_noda, string $lang, string $name, string $birth, string $death):int {

        if (empty($name)) return 0;

        if ($persinstByTransName = self::getPersinstIDByTransNamePlusYears($mysqli_noda, $lang, $name, $birth, $death)) {
            return $persinstByTransName;
        }

        if ($persinstByBaseName = self::getPersinstIDByBaseNamePlusYears($mysqli_noda, $name, $birth, $death)) {
            return $persinstByBaseName;
        }

        return 0;

    }

    /**
     * Returns persinst ID by entry in persinst name rewriting table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the persinst to search for.
     *
     * @return integer
     */
    public static function getPersinstIDByRewrite(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `persinst_id`, `input_name`
            FROM `persinst_rewriting`
            WHERE `language` = ?
                AND `input_name` = ?
            LIMIT 1", "ss", $lang, $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns persinst ID by entry in persinst translations table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the persinst to search for.
     *
     * @return integer
     */
    public static function getPersinstIDByTransName(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `persinst_id`, `trans_name`
            FROM `persinst_translation`
            WHERE `trans_name` = ?
                AND `trans_language` = ?
            LIMIT 2", "ss", $name, $lang);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns persinst ID by entry in persinst translations table,
     * irrespective of language.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $name        Name of the persinst to search for.
     *
     * @return integer
     */
    public static function getPersinstIDByAnyTransName(MDMysqli $mysqli_noda, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `persinst_id`, `trans_name`
            FROM `persinst_translation`
            WHERE `trans_name` = ?
            LIMIT 2", "s", $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns persinst ID by entry in persinst translations table
     * plus birth and death.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the persinst to search for.
     * @param string   $birth       Birth year.
     * @param string   $death       Death year.
     *
     * @return integer
     */
    public static function getPersinstIDByTransNamePlusYears(MDMysqli $mysqli_noda, string $lang, string $name, string $birth, string $death):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `persinst_translation`.`persinst_id`, `trans_name`
            FROM `persinst_translation`, `persinst`
            WHERE `persinst_translation`.`persinst_id` = `persinst`.`persinst_id`
                AND `trans_name` = ?
                AND `trans_language` = ?
                AND `persinst_geburtsjahr` = ?
                AND `persinst_sterbejahr` = ?
            LIMIT 2", "ssss", $name, $lang, $birth, $death);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns persinst ID by base name.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $name        Name of the persinst to search for.
     *
     * @return integer
     */
    public static function getPersinstIDByBaseName(MDMysqli $mysqli_noda, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `persinst_id`, `persinst_anzeigename`, `persinst_name`, CONCAT(`persinst_name`, ' (', `persinst_geburtsjahr`, '-', `persinst_sterbejahr`, ')')
            FROM `persinst`
            WHERE `persinst_anzeigename` = ?
                OR `persinst_name` = ?
                OR CONCAT(`persinst_name`, ' (', `persinst_geburtsjahr`, '-', `persinst_sterbejahr`, ')') = ?
            LIMIT 2", "sss", $name, $name, $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name) || self::_stri_matches($cur[2], $name) || self::_stri_matches($cur[3], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns persinst ID by base name.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $name        Name of the persinst to search for.
     * @param string   $birth       Birth year.
     * @param string   $death       Death year.
     *
     * @return integer
     */
    public static function getPersinstIDByBaseNamePlusYears(MDMysqli $mysqli_noda, string $name, string $birth, string $death):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `persinst_id`, `persinst_anzeigename`, `persinst_name`, CONCAT(`persinst_name`, ' (', `persinst_geburtsjahr`, '-', `persinst_sterbejahr`, ')')
            FROM `persinst`
            WHERE (
                    `persinst_anzeigename` = ?
                    OR `persinst_name` = ?
                    OR CONCAT(`persinst_name`, ' (', `persinst_geburtsjahr`, '-', `persinst_sterbejahr`, ')') = ?
                )
                AND `persinst_geburtsjahr` = ?
                AND `persinst_sterbejahr` = ?
            LIMIT 2", "sssss", $name, $name, $name, $birth, $death);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name) || self::_stri_matches($cur[2], $name) || self::_stri_matches($cur[3], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns place ID by entry in place noda table.
     *
     * @param MDMysqli   $mysqli_noda Database connection.
     * @param MDNodaLink $noda_link   Language to check in.
     *
     * @return integer
     */
    public static function getPersinstIDByNodaLink(MDMysqli $mysqli_noda, MDNodaLink $noda_link):int {

        if (empty($noda_link->id)) return 0;

        $noda_source = $noda_link->source->toDbName();
        $noda_nrinsource = $noda_link->id;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `persinst_id`
            FROM `noda`
            WHERE `noda_source` = ?
                AND `noda_nrinsource` = ?
            LIMIT 2", "ss", $noda_source, $noda_nrinsource);

        if ($data = $result->fetch_row()) {
            $output = $data[0];
        }
        else $output = 0;

        $result->close();

        return $output;

    }

    /**
     * Returns persinst ID by entry in persinst translations table.
     *
     * @param MDMysqli $mysqli_noda    Database connection.
     * @param string   $instance       Instance in which the import was run.
     * @param integer  $institution_id ID of the importing institution.
     * @param string   $name           Name of the persinst to search for.
     * @param string   $birthYear      Year of birth. Optional.
     * @param string   $deathYear      Year of death. Optional.
     *
     * @return integer
     */
    public static function getPersinstIDByImportLog(MDMysqli $mysqli_noda, string $instance, int $institution_id, string $name, string $birthYear = "", string $deathYear = ""):int {

        if (empty($name)) return 0;

        $lookUpName = $name . $birthYear . $deathYear;
        $result = $mysqli_noda->query_by_stmt("
            SELECT `persinst_id`, `input_string`
            FROM `persinst_logged_imports`
            WHERE `instance` = ?
                AND `institution_id` = ?
                AND `input_string` = ?
            LIMIT 2", "sis", $instance, $institution_id, $lookUpName);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $lookUpName)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    //
    // Places
    //

    /**
     * Returns place ID by name, checking the different options for it.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the place to search for.
     *
     * @return integer
     */
    public static function getPlaceIDByName(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        if ($placeByTransName = self::getPlaceIDByTransName($mysqli_noda, $lang, $name)) {
            return $placeByTransName;
        }

        if ($placeByBaseName = self::getPlaceIDByBaseName($mysqli_noda, $name)) {
            return $placeByBaseName;
        }

        return 0;

    }

    /**
     * Returns place ID by entry in place name rewriting table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the place to search for.
     *
     * @return integer
     */
    public static function getPlaceIDByRewrite(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `ort_id`, `input_name`
            FROM `ort_rewriting`
            WHERE `language` = ?
                AND `input_name` = ?
            LIMIT 1", "ss", $lang, $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns place ID by base name.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $name        Name of the place to search for.
     *
     * @return integer
     */
    public static function getPlaceIDByBaseName(MDMysqli $mysqli_noda, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `ort_id`, `ort_name`
            FROM `orte`
            WHERE `ort_name` = ?
            LIMIT 2", "s", $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns place ID by entry in place translations table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the place to search for.
     *
     * @return integer
     */
    public static function getPlaceIDByTransName(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `ort_id`, `trans_name`
            FROM `ort_translation`
            WHERE `trans_name` = ?
                AND `trans_language` = ?
            LIMIT 2", "ss", $name, $lang);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns place ID by entry in place translations table, irrespective of
     * language.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $name        Name of the place to search for.
     *
     * @return integer
     */
    public static function getPlaceIDByAnyTransName(MDMysqli $mysqli_noda, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `ort_id`, `trans_name`
            FROM `ort_translation`
            WHERE `trans_name` = ?
            LIMIT 2", "s", $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns place ID by entry in place noda table.
     *
     * @param MDMysqli   $mysqli_noda Database connection.
     * @param MDNodaLink $noda_link   Language to check in.
     *
     * @return integer
     */
    public static function getPlaceIDByNodaLink(MDMysqli $mysqli_noda, MDNodaLink $noda_link):int {

        if (empty($noda_link->id)) return 0;

        $noda_source = $noda_link->source->toDbName();
        $noda_nrinsource = $noda_link->id;

        $placeByNodaResult = $mysqli_noda->query_by_stmt("
            SELECT `ort_id`
            FROM `noda_orte`
            WHERE `noda_source` = ?
                AND `noda_nrinsource` = ?
            LIMIT 2", "ss", $noda_source, $noda_nrinsource);

        if ($data = $placeByNodaResult->fetch_row()) {
            $output = $data[0];
        }
        else $output = 0;

        $placeByNodaResult->close();

        return $output;

    }

    /**
     * Returns place ID by entry in place translations table.
     *
     * @param MDMysqli $mysqli_noda    Database connection.
     * @param string   $instance       Instance in which the import was run.
     * @param integer  $institution_id ID of the importing institution.
     * @param string   $name           Name of the place to search for.
     *
     * @return integer
     */
    public static function getPlaceIDByImportLog(MDMysqli $mysqli_noda, string $instance, int $institution_id, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `ort_id`, `input_string`
            FROM `orte_logged_imports`
            WHERE `instance` = ?
                AND `institution_id` = ?
                AND `input_string` = ?
            LIMIT 2", "sis", $instance, $institution_id, $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($cur[1], $name)) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    //
    // Tags
    //

    /**
     * Returns tag ID by name, checking the different options for it.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the tag to search for.
     *
     * @return integer
     */
    public static function getTagIDByName(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        if ($tagByTransName = self::getTagIDByTransName($mysqli_noda, $lang, $name)) {
            return $tagByTransName;
        }

        if ($tagByBaseName = self::getTagIDByBaseName($mysqli_noda, $name)) {
            return $tagByBaseName;
        }

        return 0;

    }

    /**
     * Returns tag ID by entry in tag name rewriting table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the tag to search for.
     *
     * @return array<integer>
     */
    public static function getTagIDByRewrite(MDMysqli $mysqli_noda, string $lang, string $name):array {

        if (empty($name)) return [];

        $output = [];

        $result = $mysqli_noda->query_by_stmt("
            SELECT `tag_id`, `input_name`
            FROM `tag_rewriting`
            WHERE `tag_language` = ?
                AND `input_name` = ?", "ss", $lang, $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $output[] = $cur[0];
            }
        }

        $result->close();

        return $output;

    }

    /**
     * Returns tag ID by entry in tag translations table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $name        Name of the tag to search for.
     *
     * @return integer
     */
    public static function getTagIDByBaseName(MDMysqli $mysqli_noda, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `tag_id`, `tag_name`
            FROM `tag`
            WHERE `tag_name` = ?
            LIMIT 2", "s", $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns tag ID by entry in tag translations table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the tag to search for.
     *
     * @return integer
     */
    public static function getTagIDByTransName(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `tag_id`, `trans_name`
            FROM `tag_translation`
            WHERE `trans_name` = ?
                AND `trans_language` = ?
            LIMIT 2", "ss", $name, $lang);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns tag ID by entry in tag translations table,
     * irrespective of language.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $name        Name of the tag to search for.
     *
     * @return integer
     */
    public static function getTagIDByAnyTransName(MDMysqli $mysqli_noda, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `tag_id`, `trans_name`
            FROM `tag_translation`
            WHERE `trans_name` = ?
            LIMIT 2", "s", $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns tag ID by entry in tag noda table.
     *
     * @param MDMysqli   $mysqli_noda Database connection.
     * @param MDNodaLink $noda_link   Language to check in.
     *
     * @return integer
     */
    public static function getTagIDByNodaLink(MDMysqli $mysqli_noda, MDNodaLink $noda_link):int {

        if (empty($noda_link->id)) return 0;

        $noda_source = $noda_link->source->toDbName();
        $noda_nrinsource = $noda_link->id;

        $tagByNodaResult = $mysqli_noda->query_by_stmt("
            SELECT `tag_id`
            FROM `noda_tag`
            WHERE `noda_source` = ?
                AND `noda_nrinsource` = ?
            LIMIT 2", "ss", $noda_source, $noda_nrinsource);

        if ($tagByNodaData = $tagByNodaResult->fetch_row()) {
            $output = $tagByNodaData[0];
        }
        else $output = 0;

        $tagByNodaResult->close();

        return $output;

    }

    /**
     * Returns tag ID by entry in tag translations table.
     *
     * @param MDMysqli $mysqli_noda    Database connection.
     * @param string   $instance       Instance in which the import was run.
     * @param integer  $institution_id ID of the importing institution.
     * @param string   $name           Name of the tag to search for.
     *
     * @return integer
     */
    public static function getTagIDByImportLog(MDMysqli $mysqli_noda, string $instance, int $institution_id, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `tag_id`, `input_string`
            FROM `tag_logged_imports`
            WHERE `instance` = ?
                AND `institution_id` = ?
                AND `input_string` = ?
            LIMIT 2", "sis", $instance, $institution_id, $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    //
    // Times
    //

    /**
     * Returns time ID by name, checking the different options for it.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the time to search for.
     *
     * @return integer
     */
    public static function getTimeIDByName(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        if ($timeByTransName = self::getTimeIDByTransName($mysqli_noda, $lang, $name)) {
            return $timeByTransName;
        }

        if ($timeByBaseName = self::getTimeIDByBaseName($mysqli_noda, $name)) {
            return $timeByBaseName;
        }

        return 0;

    }

    /**
     * Returns time ID by entry in time name rewriting table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the time to search for.
     *
     * @return integer
     */
    public static function getTimeIDByRewrite(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        $output = [];

        $result = $mysqli_noda->query_by_stmt("
            SELECT `zeit_id`, `input_name`
            FROM `zeit_rewriting`
            WHERE `language` = ?
                AND `input_name` = ?", "ss", $lang, $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns time ID by base name.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $name        Name of the time to search for.
     *
     * @return integer
     */
    public static function getTimeIDByBaseName(MDMysqli $mysqli_noda, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `zeit_id`, `zeit_name`
            FROM `zeiten`
            WHERE `zeit_name` = ?
            LIMIT 2", "s", $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns time ID by entry in time translations table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $lang        Language to check in.
     * @param string   $name        Name of the time to search for.
     *
     * @return integer
     */
    public static function getTimeIDByTransName(MDMysqli $mysqli_noda, string $lang, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `zeit_id`, `trans_name`
            FROM `zeit_translation`
            WHERE `trans_name` = ?
                AND `trans_language` = ?
            LIMIT 2", "ss", $name, $lang);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns time ID by entry in time translations table.
     *
     * @param MDMysqli $mysqli_noda Database connection.
     * @param string   $name        Name of the time to search for.
     *
     * @return integer
     */
    public static function getTimeIDByAnyTransName(MDMysqli $mysqli_noda, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `zeit_id`, `trans_name`
            FROM `zeit_translation`
            WHERE `trans_name` = ?
            LIMIT 2", "s", $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /**
     * Returns time ID by entry in time translations table.
     *
     * @param MDMysqli $mysqli_noda    Database connection.
     * @param string   $instance       Instance in which the import was run.
     * @param integer  $institution_id ID of the importing institution.
     * @param string   $name           Name of the time to search for.
     *
     * @return integer
     */
    public static function getTimeIDByImportLog(MDMysqli $mysqli_noda, string $instance, int $institution_id, string $name):int {

        if (empty($name)) return 0;

        $result = $mysqli_noda->query_by_stmt("
            SELECT `zeit_id`, `input_string`
            FROM `zeiten_logged_imports`
            WHERE `instance` = ?
                AND `institution_id` = ?
                AND `input_string` = ?
            LIMIT 2", "sis", $instance, $institution_id, $name);

        while ($cur = $result->fetch_row()) {
            if (self::_stri_matches($name, $cur[1])) {
                $result->close();
                return (int)$cur[0];
            }
        }
        $result->close();

        return 0;

    }

    /** Wrappers */

    /**
     * Gets a actor ID to check pre-existence for insertion.
     *
     * @param MDMysqli $mysqli_noda    Database connection.
     * @param string   $lang           Language to check in.
     * @param string   $name           Name of the actor to search for.
     * @param string   $birthYear      Year of birth. Optional.
     * @param string   $deathYear      Year of death. Optional.
     * @param string   $instance       Instance for checking import log. Optional.
     * @param integer  $institution_id Institution ID for checking import log. Optional.
     *
     * @return integer
     */
    public static function getPersinstIDByNamesAndRewrites(MDMysqli $mysqli_noda, string $lang, string $name, string $birthYear, string $deathYear, string $instance = "", int $institution_id = 0):int {

        if (($persinstIdByName = self::getPersinstIDByName($mysqli_noda, $lang, $name)) !== 0) {
            return $persinstIdByName;
        }

        if (($persinstIdByRewrite = self::getPersinstIDByRewrite($mysqli_noda, $lang, $name)) !== 0) {
            return $persinstIdByRewrite;
        }

        if ($instance !== "") {
            if (($persinstIdByImportLog = self::getPersinstIDByImportLog($mysqli_noda, $instance, $institution_id, $name, $birthYear, $deathYear)) !== 0) {
                return $persinstIdByImportLog;
            }
        }

        return 0;

    }

    /**
     * Gets a place ID to check pre-existence for insertion.
     *
     * @param MDMysqli $mysqli_noda    Database connection.
     * @param string   $lang           Language to check in.
     * @param string   $name           Name of the place to search for.
     * @param string   $instance       Instance for checking import log. Optional.
     * @param integer  $institution_id Institution ID for checking import log. Optional.
     *
     * @return integer
     */
    public static function getPlaceIDByNamesAndRewrites(MDMysqli $mysqli_noda, string $lang, string $name, string $instance = "", int $institution_id = 0):int {

        if (($placeIdByName = self::getPlaceIDByName($mysqli_noda, $lang, $name)) !== 0) {
            return $placeIdByName;
        }

        if (($placeIdByRewrite = self::getPlaceIDByRewrite($mysqli_noda, $lang, $name)) !== 0) {
            return $placeIdByRewrite;
        }

        if ($instance !== "") {
            if (($placeIdByImportLog = self::getPlaceIDByImportLog($mysqli_noda, $instance, $institution_id, $name)) !== 0) {
                return $placeIdByImportLog;
            }
        }

        return 0;

    }

    /**
     * Gets a tag ID to check pre-existence for insertion.
     *
     * @param MDMysqli $mysqli_noda    Database connection.
     * @param string   $lang           Language to check in.
     * @param string   $name           Name of the tag to search for.
     * @param string   $instance       Instance for checking import log. Optional.
     * @param integer  $institution_id Institution ID for checking import log. Optional.
     *
     * @return integer
     */
    public static function getTagIDByNamesAndRewrites(MDMysqli $mysqli_noda, string $lang, string $name, string $instance = "", int $institution_id = 0):int {

        if (($tagIdByName = self::getTagIDByName($mysqli_noda, $lang, $name)) !== 0) {
            return $tagIdByName;
        }

        if (!empty($tagIdByRewrite = self::getTagIDByRewrite($mysqli_noda, $lang, $name))) {
            return end($tagIdByRewrite);
        }

        if ($instance !== "") {
            if (($tagIdByImportLog = self::getTagIDByImportLog($mysqli_noda, $instance, $institution_id, $name)) !== 0) {
                return $tagIdByImportLog;
            }
        }

        return 0;

    }

    /**
     * Gets a time ID to check pre-existence for insertion.
     *
     * @param MDMysqli $mysqli_noda    Database connection.
     * @param string   $lang           Language to check in.
     * @param string   $name           Name of the time to search for.
     * @param string   $instance       Instance for checking import log. Optional.
     * @param integer  $institution_id Institution ID for checking import log. Optional.
     *
     * @return integer
     */
    public static function getTimeIDByNamesAndRewrites(MDMysqli $mysqli_noda, string $lang, string $name, string $instance = "", int $institution_id = 0):int {

        if (($timeIdByName = self::getTimeIDByName($mysqli_noda, $lang, $name)) !== 0) {
            return $timeIdByName;
        }

        if (!empty($timeIdByRewrite = self::getTimeIDByRewrite($mysqli_noda, $lang, $name))) {
            return $timeIdByRewrite;
        }

        if ($instance !== "") {
            if (($timeIdByImportLog = self::getTimeIDByImportLog($mysqli_noda, $instance, $institution_id, $name)) !== 0) {
                return $timeIdByImportLog;
            }
        }

        return 0;

    }

    /**
     * Checks each string in a list of strings for its existence as a tag name.
     *
     * @param MDMysqli                $mysqli_noda Database connection.
     * @param string                  $lang        Language to check in.
     * @param non-empty-array<string> $phrases     List of phrases to check.
     *
     * @return array{count: int, tag: integer[], actor: integer[], time: integer[], place: integer[]}
     */
    public static function searchEntryNamesByList(MDMysqli $mysqli_noda, string $lang, array $phrases):array {

        $output = [
            'count' => 0,
            'tag' => [],
            'actor' => [],
            'time' => [],
            'place' => [],
        ];

        foreach ($phrases as $phrase) {

            if (($tag_id = NodaIDGetter::getTagIDByNamesAndRewrites($mysqli_noda, $lang, $phrase)) !== 0 && !in_array($tag_id, $output['tag'], true)) {
                $output['tag'][] = $tag_id;
                ++$output['count'];
            }
            else if (($tag_id_by_tl = NodaIDGetter::getTagIDByAnyTransName($mysqli_noda, $phrase)) !== 0 && !in_array($tag_id_by_tl, $output['tag'], true)) {
                $output['tag'][] = $tag_id_by_tl;
                ++$output['count'];
            }
            else if (($place_id = NodaIDGetter::getPlaceIDByNamesAndRewrites($mysqli_noda, $lang, $phrase)) !== 0 && !in_array($place_id, $output['place'], true)) {
                $output['place'][] = $place_id;
                ++$output['count'];
            }
            else if (($place_id = NodaIDGetter::getPlaceIDByAnyTransName($mysqli_noda, $phrase)) !== 0 && !in_array($place_id, $output['place'], true)) {
                $output['place'][] = $place_id;
                ++$output['count'];
            }
            else if (($persinst_id = NodaIDGetter::getPersinstIDByNamesAndRewrites($mysqli_noda, $lang, $phrase, '', '')) !== 0 && !in_array($persinst_id, $output['actor'], true)) {
                $output['actor'][] = $persinst_id;
                ++$output['count'];
            }
            else if (($persinst_id = NodaIDGetter::getPersinstIDByAnyTransName($mysqli_noda, $phrase)) !== 0 && !in_array($persinst_id, $output['actor'], true)) {
                $output['actor'][] = $persinst_id;
                ++$output['count'];
            }
            else if (($time_id = NodaIDGetter::getTimeIDByNamesAndRewrites($mysqli_noda, $lang, $phrase)) !== 0 && !in_array($time_id, $output['time'], true)) {
                $output['time'][] = $time_id;
                ++$output['count'];
            }
            else if (($time_id = NodaIDGetter::getTimeIDByAnyTransName($mysqli_noda, $phrase)) !== 0 && !in_array($time_id, $output['time'], true)) {
                $output['time'][] = $time_id;
                ++$output['count'];
            }

        }

        if (count($phrases) !== $output['count']) {
            return [
                'count' => 0,
                'tag' => [],
                'actor' => [],
                'time' => [],
                'place' => [],
            ];
        }

        if (!empty($output['tag'])) sort($output['tag']);
        if (!empty($output['actor'])) sort($output['actor']);
        if (!empty($output['time'])) sort($output['time']);
        if (!empty($output['place'])) sort($output['place']);

        return $output;

    }
}
