diff --git a/src/NodaWikidataFetcher.php b/src/NodaWikidataFetcher.php index e5f2803..9c7c0ec 100644 --- a/src/NodaWikidataFetcher.php +++ b/src/NodaWikidataFetcher.php @@ -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 $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§ion=0&format=json", 10000);