Use separate function for generating overview lists in

NodaWikidataFetcher
This commit is contained in:
Joshua Ramon Enslin 2021-06-30 22:55:37 +02:00
parent 2c0d8e041e
commit bd3851ccf4
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -1540,42 +1540,93 @@ final class NodaWikidataFetcher {
}
/**
* Function for generating a wikidata results list.
* Searches Wikidata for a string.
*
* @param array<array<mixed>> $wikidata_data Wikidata data.
* @param string $link Links.
* @param string $searchTerm Search term.
* @param string $lang Language.
* @param string $searchTerm Search string.
* @param string $lang Searched language. Defaults to German.
*
* @return void
* @return array<mixed>
*/
public static function generateWikidataResultsList(array $wikidata_data, string $link, string $searchTerm, string $lang):void {
public static function searchWikidataForString(string $searchTerm, string $lang = "de"):array {
if (count($wikidata_data['search']) === 0) {
echo '<p class="icons iconsAlert alert"><b>' . ucfirst($searchTerm) . '</b> not found in Wikidata</p>';
return;
$wikidata_data = MD_STD::runCurl("https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&search=" . urlencode($searchTerm) . "&language=" . urlencode($lang) . "&limit=20", 10000);
if (($wikidata_data = json_decode($wikidata_data, true)) === false) {
return [];
}
echo '
<main id="wikidataResultsList">';
$output = [];
foreach ($wikidata_data['search'] as $result) {
if ((isset($result['description']) and $result['description'] == '') or (isset($result['label']) and $result['label'] == '') or !isset($result['label']) or (isset($result['description']) and $result['description'] == 'Wikipedia disambiguation page') or (isset($result['description']) and $result['description'] == 'Wikimedia disambiguation page')) continue;
if (empty($result['description'])
or empty($result['label'])
or (isset($result['description']) and $result['description'] == 'Wikipedia disambiguation page')
or (isset($result['description']) and $result['description'] == 'Wikimedia disambiguation page')
) continue;
echo '<div><a href="' . $link . 'suchbegriff=' . $searchTerm . '&wikidata_id=' . $result['id'] . '&lang=' . $lang . '">
<h4 class="icons iconsTag">' . $result['id'] . '</h4>';
echo '<p class="wikidataSummary">' . $result['label'];
if (!empty($result['match'])) echo " (<span class='icons iconsTranslate'>{$result['match']['language']}: {$result['match']['text']}</span>)";
echo '</p>';
if (!empty($result['description'])) echo '<p>' . $result['description'] . '</p>';
echo '</a><a class="icons iconsEye" target="_blank" href="https://www.wikidata.org/wiki/' . $result['id'] . '">Wikidata page</a></div>';
$cur = [
'id' => $result['id'],
'label' => $result['label'],
'label_ext' => '',
'description' => '',
];
if (!empty($result['match'])) {
$cur['label_ext'] = "{$result['match']['language']}: {$result['match']['text']}";
}
if (!empty($result['description'])) {
$cur['description'] = $result['description'];
}
$output[] = $cur;
}
echo '
return $output;
}
/**
* Function for generating a wikidata results list.
*
* @param string $link Links.
* @param string $searchTerm Search term.
* @param string $lang Language.
*
* @return string
*/
public static function generateWikidataResultsList(string $link, string $searchTerm, string $lang):string {
$wikidata_data = self::searchWikidataForString($searchTerm);
$output = '';
if (empty($wikidata_data)) {
$output .= '<p class="icons iconsAlert alert"><b>' . ucfirst($searchTerm) . '</b> not found in Wikidata</p>';
return $output;
}
$output .= '
<main id="wikidataResultsList">';
foreach ($wikidata_data as $result) {
if ((isset($result['description']) and $result['description'] == '') or (isset($result['label']) and $result['label'] == '') or !isset($result['label']) or (isset($result['description']) and $result['description'] == 'Wikipedia disambiguation page') or (isset($result['description']) and $result['description'] == 'Wikimedia disambiguation page')) continue;
$output .= '<div><a href="' . $link . 'suchbegriff=' . $searchTerm . '&wikidata_id=' . $result['id'] . '&lang=' . $lang . '">
<h4 class="icons iconsTag">' . $result['id'] . '</h4>';
$output .= '<p class="wikidataSummary">' . $result['label'];
if (!empty($result['label_ext'])) $output .= " (<span class='icons iconsTranslate'>{$result['label_ext']}</span>)";
$output .= '</p>';
if (!empty($result['description'])) $output .= '<p>' . $result['description'] . '</p>';
$output .= '</a><a class="icons iconsEye" target="_blank" href="https://www.wikidata.org/wiki/' . $result['id'] . '">Wikidata page</a></div>';
}
$output .= '
</main>';
return $output;
}
/**