Import place hierarchy from Wikidata

Close #5
This commit is contained in:
Joshua Ramon Enslin 2021-05-11 01:37:49 +02:00
parent 874cfb8a6f
commit 2a8ba31410
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -910,6 +910,66 @@ final class NodaWikidataFetcher {
}
/**
* Function for retrieving a superordinate place relation from Wikidata information
* for places
*
* @param integer $onum Place ID.
* @param array<mixed> $data Wikidata information (P131 claim).
*
* @return void
*/
public function retrieveSuperordinateAdministrativePlace(int $onum, array $data):void {
if (!empty($data[0]["mainsnak"]["datavalue"]["value"]["id"])) {
// Check if there already is a superordinate of the current place
$result = $this->_mysqli_noda->query_by_stmt("SELECT 1
FROM `ort_relation`
WHERE `ort_menor_id` = ?
LIMIT 1", "i", $onum);
if ($result->num_rows !== 0) {
$result->close();
$result = null;
return;
}
$result->close();
$result = null;
// If there is no superordinate, check if the identified superordinate
// is known in the noda DB.
$superordinateId = $data[0]["mainsnak"]["datavalue"]["value"]["id"];
$result = $this->_mysqli_noda->query_by_stmt("SELECT `ort_id`
FROM `noda_orte`
WHERE `noda_source` = 'wikidata'
AND `noda_nrinsource` = ?", "s", $superordinateId);
if (!($superordinateData = $result->fetch_row())) {
$result->close();
$result = null;
return;
}
$result->close();
$result = null;
$topPlaceId = $superordinateData[0];
// Enter superordinate place by Wikidata
$insertStmt = $this->_mysqli_noda->do_prepare("INSERT INTO `ort_relation`
(`ort_mayor_id`, `ort_menor_id`, `ort_relation`)
VALUES
(?, ?, 1)");
$insertStmt->bind_param("ii", $topPlaceId, $onum);
$insertStmt->execute();
$insertStmt->close();
$insertStmt = null;
}
}
/**
* Function for retrieving place information based on a Wikidata ID.
*
@ -941,6 +1001,11 @@ final class NodaWikidataFetcher {
$alreadyEntered = false;
// P131: Located in administrative unit
if (isset($data['claims']['P131'])) {
$this->retrieveSuperordinateAdministrativePlace($onum, $data['claims']['P131']);
}
if (!empty($wikilink[$lang])) {
$datafromwiki = MD_STD::runCurl("https://" . urlencode($lang) . ".wikipedia.org/w/api.php?action=parse&page=" . urlencode($wikilinkterm[$lang]) . "&prop=text&section=0&format=json", 10000);