*/ 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 */ 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 $data Wikidata output values. * * @return array */ 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; } }