Compare commits

..

2 Commits

2 changed files with 100 additions and 61 deletions

View File

@ -143,6 +143,54 @@ final class NodaNameGetter {
} }
/**
* Static function for getting persinst names in bulk.
*
* @param MDMysqli $mysqli_noda DB connection.
* @param string $lang Language.
* @param array<integer> $persinst_ids Persinst IDs.
*
* @return array<int, string>
*/
public static function getBatchPersinstNamesWithLifeDates(MDMysqli $mysqli_noda, string $lang, array $persinst_ids):array {
$output = [];
// Get translations
$result = $mysqli_noda->do_read_query("SELECT `persinst`.`persinst_id`, `trans_name`, `persinst_geburtsjahr`, `persinst_sterbejahr`
FROM `persinst_translation`, `persinst`
WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
AND `persinst`.`persinst_id` = `persinst_translation`.`persinst_id`
AND `persinst`.`persinst_id` IN (" . implode(', ', $persinst_ids) . ")");
while ($cur = $result->fetch_row()) {
$name = (string)$cur[1];
if (!empty($cur[2]) || !empty($cur[3])) {
$name .= ' (' . $cur[2] . '-' . $cur[3] . ')';
}
$output[(int)$cur[0]] = $name;
}
$result->close();
if (!empty($persinst_ids_left = array_diff($persinst_ids, array_keys($output)))) {
$result = $mysqli_noda->do_read_query("SELECT `persinst_id`, `persinst_anzeigename`
FROM `persinst`
WHERE `persinst_id` IN (" . implode(', ', $persinst_ids_left) . ")");
while ($cur = $result->fetch_row()) {
$output[(int)$cur[0]] = (string)$cur[1];
}
$result->close();
}
if (count($output) > 1) {
asort($output);
}
return $output;
}
/** /**
* Static function for getting time names in bulk. * Static function for getting time names in bulk.
* *

View File

@ -91,6 +91,52 @@ final class NodaWikidataFetcher {
} }
/**
* Parses wikidata results to MDNodaLink entries.
*
* @param 'tag'|'persinst'|'place' $target Target vocabulary type.
* @param string $wikidata_id Wikidata ID.
* @param array<mixed> $data Wikidata result.
*
* @return list<MDNodaLink>
*/
public function _getNodaLinksFromWikidataResult(string $target, string $wikidata_id, array $data):array {
$linkableVocabularies = match($target) {
'tag' => MDNodaRepositoriesSet::REPOSITORIES_TAG,
'persinst' => MDNodaRepositoriesSet::REPOSITORIES_ACTOR,
'place' => MDNodaRepositoriesSet::REPOSITORIES_PLACE,
};
$output = [
new MDNodaLink(MDNodaRepository::wikidata, $wikidata_id)
];
foreach (self::P_IDS_NODA_TAGS as $vocabName => $pId) {
if (!isset($data['claims'][$pId])) {
continue;
}
if (empty($data['claims'][$pId][0]['mainsnak']['datavalue'])) {
continue;
}
$url = $data['claims'][$pId][0]['mainsnak']['datavalue']['value'];
if ($vocabName === 'loc' || ($vocabName === 'lcsh')) {
$vocabName = $this->_determineLocRefMode($url);
if (empty($vocabName)) continue;
}
if (!in_array($vocabName, $linkableVocabularies, true)) continue;
$output[] = new MDNodaLink(MDNodaRepository::fromString($vocabName), $url);
}
return $output;
}
/** /**
* Checks if a vocabulary link to the library * Checks if a vocabulary link to the library
* of congress is a valid LCSH or LOC link or * of congress is a valid LCSH or LOC link or
@ -963,30 +1009,10 @@ final class NodaWikidataFetcher {
// Get links to other norm data sources // Get links to other norm data sources
$nodaLinks = [ if (!empty($nodaLinks = $this->_getNodaLinksFromWikidataResult('persinst', $wikidata_id, $data))) {
new MDNodaLink(MDNodaRepository::wikidata, $wikidata_id) NodaBatchInserter::linkNodaForPersinst($this->_mysqli_noda, $persinst_id, $nodaLinks, $erfasst_von);
];
foreach (self::P_IDS_NODA_TAGS as $vocabName => $pId) {
if (isset($data['claims'][$pId])) {
if (empty($data['claims'][$pId][0]['mainsnak']['datavalue'])) continue;
$url = $data['claims'][$pId][0]['mainsnak']['datavalue']['value'];
if ($vocabName === 'loc' || ($vocabName === 'lcsh')) {
$vocabName = $this->_determineLocRefMode($url);
if (empty($vocabName)) continue;
}
if (!in_array($vocabName, MDNodaRepositoriesSet::REPOSITORIES_ACTOR, true)) continue;
$nodaLinks[] = new MDNodaLink(MDNodaRepository::fromString($vocabName), $url);
}
} }
// GET links to other noda entries.
NodaBatchInserter::linkNodaForPersinst($this->_mysqli_noda, $persinst_id, $nodaLinks, $erfasst_von);
$this->getWikidataTranslationsForPersinst($data, $persinst_id); $this->getWikidataTranslationsForPersinst($data, $persinst_id);
NodaLogEdit::logPersinstEdit($this->_mysqli_noda, $persinst_id, "wikidata-fetcher", $erfasst_von, 'update', 'synchronize'); NodaLogEdit::logPersinstEdit($this->_mysqli_noda, $persinst_id, "wikidata-fetcher", $erfasst_von, 'update', 'synchronize');
@ -1264,23 +1290,8 @@ final class NodaWikidataFetcher {
if (isset($data['claims']['P1566'])) $geonames_id = filter_var($data['claims']['P1566'][0]['mainsnak']['datavalue']['value'], FILTER_VALIDATE_INT); if (isset($data['claims']['P1566'])) $geonames_id = filter_var($data['claims']['P1566'][0]['mainsnak']['datavalue']['value'], FILTER_VALIDATE_INT);
if (isset($data['claims']['P1667'])) $tgn_id = filter_var($data['claims']['P1667'][0]['mainsnak']['datavalue']['value'], FILTER_VALIDATE_INT); if (isset($data['claims']['P1667'])) $tgn_id = filter_var($data['claims']['P1667'][0]['mainsnak']['datavalue']['value'], FILTER_VALIDATE_INT);
$nodaLinks = [ if (!empty($nodaLinks = $this->_getNodaLinksFromWikidataResult('place', $wikidata_id, $data))) {
new MDNodaLink(MDNodaRepository::wikidata, $wikidata_id) NodaBatchInserter::linkNodaForPlace($this->_mysqli_noda, $onum, $nodaLinks, $erfasst_von);
];
foreach (self::P_IDS_NODA_TAGS as $vocabName => $pId) {
if (isset($data['claims'][$pId])) {
if (empty($data['claims'][$pId][0]['mainsnak']['datavalue'])) continue;
$url = $data['claims'][$pId][0]['mainsnak']['datavalue']['value'];
if ($vocabName === 'loc' || ($vocabName === 'lcsh')) {
$vocabName = $this->_determineLocRefMode($url);
if (empty($vocabName)) continue;
}
if (!in_array($vocabName, MDNodaRepositoriesSet::REPOSITORIES_PLACE, true)) continue;
$nodaLinks[] = new MDNodaLink(MDNodaRepository::fromString($vocabName), $url);
}
} }
if (isset($data['claims']['P625'])) { if (isset($data['claims']['P625'])) {
@ -1288,8 +1299,6 @@ final class NodaWikidataFetcher {
$longitude_wd = \filter_var($data['claims']['P625'][0]['mainsnak']['datavalue']['value']['longitude'], FILTER_VALIDATE_FLOAT); $longitude_wd = \filter_var($data['claims']['P625'][0]['mainsnak']['datavalue']['value']['longitude'], FILTER_VALIDATE_FLOAT);
} }
NodaBatchInserter::linkNodaForPlace($this->_mysqli_noda, $onum, $nodaLinks, $erfasst_von);
$this->_mysqli_noda->autocommit(false); $this->_mysqli_noda->autocommit(false);
if (!empty($tgn_id)) { if (!empty($tgn_id)) {
@ -1521,28 +1530,10 @@ final class NodaWikidataFetcher {
} }
$nodaLinks = [ if (!empty($nodaLinks = $this->_getNodaLinksFromWikidataResult('tag', $wikidata_id, $data))) {
new MDNodaLink(MDNodaRepository::wikidata, $wikidata_id) NodaBatchInserter::linkNodaForTag($this->_mysqli_noda, $tag_id, $nodaLinks, $erfasst_von);
];
foreach (self::P_IDS_NODA_TAGS as $vocabName => $pId) {
if (isset($data['claims'][$pId])) {
if (empty($data['claims'][$pId][0]['mainsnak']['datavalue'])) continue;
$url = $data['claims'][$pId][0]['mainsnak']['datavalue']['value'];
if ($vocabName === 'loc' || ($vocabName === 'lcsh')) {
$vocabName = $this->_determineLocRefMode($url);
if (empty($vocabName)) continue;
}
if (!in_array($vocabName, MDNodaRepositoriesSet::REPOSITORIES_TAG, true)) continue;
$nodaLinks[] = new MDNodaLink(MDNodaRepository::fromString($vocabName), $url);
}
} }
NodaBatchInserter::linkNodaForTag($this->_mysqli_noda, $tag_id, $nodaLinks, $erfasst_von);
// Get translations // Get translations
if (!empty($data)) $this->getWikidataTranslationsForTag($data, $tag_id); if (!empty($data)) $this->getWikidataTranslationsForTag($data, $tag_id);