72 lines
1.8 KiB
PHP
72 lines
1.8 KiB
PHP
|
<?PHP
|
||
|
/**
|
||
|
* This file contains tools for fetching data from Wikidata.
|
||
|
*
|
||
|
* @file
|
||
|
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
||
|
*/
|
||
|
declare(strict_types = 1);
|
||
|
|
||
|
require_once __DIR__ . '/../src/NodaWikidataFetcher.php';
|
||
|
require_once __DIR__ . '/../../MD_STD/src/MD_STD.php';
|
||
|
|
||
|
/**
|
||
|
* Queries wikidata for instances of a Q-ID.
|
||
|
*
|
||
|
* @param string $lang Query language.
|
||
|
* @param string $instanceOf Q-ID.
|
||
|
*
|
||
|
* @return array<mixed>
|
||
|
*/
|
||
|
function query(string $lang, string $instanceOf):array {
|
||
|
|
||
|
$sparqlQueryString = 'SELECT ?item ?itemLabel
|
||
|
WHERE
|
||
|
{
|
||
|
?item wdt:P31/wdt:P279* wd:' . $instanceOf . '.
|
||
|
SERVICE wikibase:label { bd:serviceParam wikibase:language "' . $lang . ',[AUTO_LANGUAGE],en". } # Helps get the label in your language, if not, then en language
|
||
|
}';
|
||
|
|
||
|
return NodaWikidataFetcher::sparqlQuery($sparqlQueryString);
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns names from a query.
|
||
|
*
|
||
|
* @param array<mixed> $data Wikidata output values.
|
||
|
*
|
||
|
* @return array<string>
|
||
|
*/
|
||
|
function getNames(array $data):array {
|
||
|
|
||
|
$output = [];
|
||
|
|
||
|
foreach ($data['results']['bindings'] as $entry) {
|
||
|
$output[] = $entry['itemLabel']['value'];
|
||
|
}
|
||
|
|
||
|
return $output;
|
||
|
|
||
|
}
|
||
|
|
||
|
// Q6256 => country
|
||
|
|
||
|
$targets = [
|
||
|
'Q6256' => 'countries',
|
||
|
'Q3024240' => 'historical_countries',
|
||
|
'Q10864048' => 'first_lvl_administrative_units',
|
||
|
];
|
||
|
|
||
|
$langs = ['ar', 'bg', 'bn', 'cs', 'da', 'de', 'el', 'en', 'es', 'fa', 'fi', 'fr', 'ha', 'he', 'hi', 'hu', 'id', 'it', 'ja', 'ka', 'ko', 'nl', 'pl', 'pt', 'ro', 'ru', 'sv', 'sw', 'ta', 'th', 'tl', 'tr', 'uk', 'ur', 'vi', 'zh'];
|
||
|
|
||
|
foreach ($langs as $lang) {
|
||
|
foreach ($targets as $qid => $filename) {
|
||
|
|
||
|
$regionNames = getNames(query($lang, $qid));
|
||
|
file_put_contents(__DIR__ . '/../static/' . $filename . '.' . $lang . '.json', json_encode($regionNames));
|
||
|
echo "Fetched $lang : $filename ($qid)" . PHP_EOL;
|
||
|
|
||
|
}
|
||
|
}
|