Add class NodaLinkedEntityGetter for getting linked entries

This commit is contained in:
Joshua Ramon Enslin 2023-08-30 17:39:25 +02:00
parent 2720adf9ed
commit 05fb965d8c
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -0,0 +1,120 @@
<?PHP
/**
* Contains a class for getting linked entities for given vocabulary data.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Contains a class for getting linked entities for given vocabulary data.
*/
final class NodaLinkedEntityGetter {
/**
* Returns a list of norm data repository links for an actor.
*
* @param MDMysqli $mysqli_noda DB connection.
* @param integer $persinst_id ID of the actor to get synonyms for.
*
* @return array<MDNodaLink>
*/
public static function listPersinstNodaLinks(MDMysqli $mysqli_noda, int $persinst_id):array {
$result = $mysqli_noda->query_by_stmt("SELECT `noda_source`, `noda_nrinsource`
FROM `noda`
WHERE `persinst_id` = ?", "i", $persinst_id);
$output = [];
if ($result->num_rows !== 0) {
while ($cur = $result->fetch_row()) {
$output[] = new MDNodaLink(MDNodaRepository::fromString($cur[0]), $cur[1]);
}
}
$result->close();
return $output;
}
/**
* Returns a list of norm data repository links for a place.
*
* @param MDMysqli $mysqli_noda DB connection.
* @param integer $place_id ID of the place to get synonyms for.
*
* @return array<MDNodaLink>
*/
public static function listPlaceNodaLinks(MDMysqli $mysqli_noda, int $place_id):array {
$result = $mysqli_noda->query_by_stmt("SELECT `noda_source`, `noda_nrinsource`
FROM `noda_orte`
WHERE `ort_id` = ?", "i", $place_id);
$output = [];
if ($result->num_rows !== 0) {
while ($cur = $result->fetch_row()) {
$output[] = new MDNodaLink(MDNodaRepository::fromString($cur[0]), $cur[1]);
}
}
$result->close();
return $output;
}
/**
* Returns a list of norm data repository links for a tag.
*
* @param MDMysqli $mysqli_noda DB connection.
* @param integer $tag_id ID of the tag to get synonyms for.
*
* @return array<MDNodaLink>
*/
public static function listTagNodaLinks(MDMysqli $mysqli_noda, int $tag_id):array {
$result = $mysqli_noda->query_by_stmt("SELECT `noda_source`, `noda_nrinsource`
FROM `noda_tag`
WHERE `tag_id` = ?", "i", $tag_id);
$output = [];
if ($result->num_rows !== 0) {
while ($cur = $result->fetch_row()) {
$output[] = new MDNodaLink(MDNodaRepository::fromString($cur[0]), $cur[1]);
}
}
$result->close();
return $output;
}
/**
* Returns synonyms of a place.
*
* @param MDMysqli $mysqli_noda DB connection.
* @param integer $place_id ID of the place to get synonyms for.
*
* @return array<int>
*/
public static function listPlaceSynonyms(MDMysqli $mysqli_noda, int $place_id):array {
$result = $mysqli_noda->do_read_query("SELECT `ort_syn_main`, `ort_syn_sub`
FROM `ort_syn`
WHERE `ort_syn_main` = " . $place_id . "
OR `ort_syn_sub` = " . $place_id . "
LIMIT 1");
$output = [];
while ($cur = $result->fetch_row()) {
// Use keys to prevent duplicates
$id1 = (int)$cur[0];
$id2 = (int)$cur[1];
if ($id1 !== $place_id) $output[$id1] = true;
if ($id2 !== $place_id) $output[$id2] = true;
}
$result->close();
return array_keys($output);
}
}