Allow a special wikidata results list for actors, making suggestions

based on birth and death dates
This commit is contained in:
Joshua Ramon Enslin 2021-08-07 17:38:49 +02:00
parent e69be5b2b1
commit e773bab7ce
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -532,13 +532,7 @@ final class NodaWikidataFetcher {
// Try to get birth date // Try to get birth date
if (!empty($data['claims']['P569']) and !empty($data['claims']['P569']['0']['mainsnak']['datavalue']['value']['time'])) { if (!empty($data['claims']['P569']) and !empty($data['claims']['P569']['0']['mainsnak']['datavalue']['value']['time'])) {
$birth_date_int = strtotime(substr($data['claims']['P569']['0']['mainsnak']['datavalue']['value']['time'], 1, 4)); $birth_date = self::wikidataBirthDeathToYear($data['claims']['P569']['0']['mainsnak']['datavalue']['value']['time']);
if ($birth_date_int) {
$birth_date = date("Y", $birth_date_int);
if ($birth_date === date("Y")) {
$birth_date = date("Y", strtotime($data['claims']['P569']['0']['mainsnak']['datavalue']['value']['time']));
}
}
} }
if (!empty($birth_date)) { if (!empty($birth_date)) {
@ -558,13 +552,7 @@ final class NodaWikidataFetcher {
// Try to get birth date // Try to get birth date
if (!empty($data['claims']['P570']) and !empty($data['claims']['P570']['0']['mainsnak']['datavalue']['value']['time'])) { if (!empty($data['claims']['P570']) and !empty($data['claims']['P570']['0']['mainsnak']['datavalue']['value']['time'])) {
$death_date_int = strtotime(substr($data['claims']['P570']['0']['mainsnak']['datavalue']['value']['time'], 1, 4)); $death_date = self::wikidataBirthDeathToYear($data['claims']['P570']['0']['mainsnak']['datavalue']['value']['time']);
if ($death_date_int) {
$death_date = date("Y", $death_date_int);
if ($death_date === date("Y")) {
$death_date = date("Y", strtotime($data['claims']['P570']['0']['mainsnak']['datavalue']['value']['time']));
}
}
} }
if (!empty($death_date)) { if (!empty($death_date)) {
@ -1587,6 +1575,33 @@ final class NodaWikidataFetcher {
} }
/**
* Generates the HTML for an entry in the general wikidata search results list.
*
* @param string $link Links.
* @param string $searchTerm Search term.
* @param string $lang Language.
* @param array<mixed> $result Single result to display.
*
* @return string
*/
public static function generateWikidataResultsListEntry(string $link, string $searchTerm, string $lang, array $result):string {
if ((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')) {
return '';
}
$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>';
return $output;
}
/** /**
* Function for generating a wikidata results list. * Function for generating a wikidata results list.
* *
@ -1598,31 +1613,116 @@ final class NodaWikidataFetcher {
*/ */
public static function generateWikidataResultsList(string $link, string $searchTerm, string $lang):string { public static function generateWikidataResultsList(string $link, string $searchTerm, string $lang):string {
$wikidata_data = self::searchWikidataForString($searchTerm); if (empty($wikidata_data = self::searchWikidataForString($searchTerm))) {
$output = ''; return '<p class="icons iconsAlert alert"><b>' . ucfirst($searchTerm) . '</b> not found in Wikidata</p>';
if (empty($wikidata_data)) {
$output .= '<p class="icons iconsAlert alert"><b>' . ucfirst($searchTerm) . '</b> not found in Wikidata</p>';
return $output;
} }
$output .= ' $output = '
<main id="wikidataResultsList">'; <main id="wikidataResultsList">';
foreach ($wikidata_data as $result) { foreach ($wikidata_data as $result) {
$output .= self::generateWikidataResultsListEntry($link, $searchTerm, $lang, $result);
}
$output .= '
</main>';
if ((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; return $output;
$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>';
} }
/**
* Attempts to parse birth or death years from the data returned by wikidata.
*
* @param string $inputTime Input time in the format delivered by wikidata.
*
* @return string
*/
public static function wikidataBirthDeathToYear(string $inputTime):string {
$birth_date_int = strtotime(substr($inputTime, 1, 4));
if ($birth_date_int) {
$birth_date = date("Y", $birth_date_int);
if ($birth_date === date("Y") and $tTime = strtotime($inputTime)) {
$birth_date = date("Y", $tTime);
}
return $birth_date;
}
return '';
}
/**
* Function for generating a wikidata results list for actors, keeping track of life dates.
*
* @param string $link Links.
* @param string $searchTerm Search term.
* @param string $lang Language.
* @param integer $yearOfBirth Year of birth.
* @param integer $yearOfDeath Year of death.
*
* @return string
*/
public static function generateWikidataResultsListForActors(string $link, string $searchTerm, string $lang, int $yearOfBirth, int $yearOfDeath):string {
if (empty($wikidata_data = self::searchWikidataForString($searchTerm))) {
return '<p class="icons iconsAlert alert"><b>' . ucfirst($searchTerm) . '</b> not found in Wikidata</p>';
}
$qLinksToCheck = [];
foreach ($wikidata_data as $entry) {
$qLinksToCheck[$entry['id']] = "https://www.wikidata.org/wiki/Special:EntityData/" . $entry['id'] . ".json";
}
$fetched = MD_STD::runCurlMulti($qLinksToCheck, 10000);
$yearsOfBirthList = $yearsOfDeathList = [];
foreach ($fetched as $qId => $data) {
if (!($jsonData = json_decode($data, true))) {
continue;
}
if (empty($jsonData['entities'][$qId])) {
continue;
}
$data = $jsonData['entities'][$qId];
if (!empty($data['claims']['P569']) and !empty($data['claims']['P569']['0']['mainsnak']['datavalue']['value']['time'])) {
$yearsOfBirthList[$qId] = (int)self::wikidataBirthDeathToYear($data['claims']['P569']['0']['mainsnak']['datavalue']['value']['time']);
}
if (!empty($data['claims']['P570']) and !empty($data['claims']['P570']['0']['mainsnak']['datavalue']['value']['time'])) {
$yearsOfDeathList[$qId] = (int)self::wikidataBirthDeathToYear($data['claims']['P570']['0']['mainsnak']['datavalue']['value']['time']);
}
}
$output = '
<main id="wikidataResultsList">';
foreach ($wikidata_data as $result) {
if (empty($result['id'])) continue;
if (!empty($yearsOfBirthList[$result['id']])) {
if (empty($result['description'])) {
$result['description'] = 'Born: ' . $yearsOfBirthList[$result['id']];
}
else $result['description'] .= '<br/>Born: ' . $yearsOfBirthList[$result['id']];
}
if (!empty($yearsOfDeathList[$result['id']])) {
if (empty($result['description'])) {
$result['description'] = 'Death: ' . $yearsOfDeathList[$result['id']];
}
else $result['description'] .= '<br/>Death: ' . $yearsOfDeathList[$result['id']];
}
if (!empty($yearsOfBirthList[$result['id']]) && !empty($yearsOfDeathList[$result['id']])) {
if ($yearsOfBirthList[$result['id']] === $yearOfBirth
&& $yearsOfDeathList[$result['id']] === $yearOfDeath
) {
$result['description'] .= '<br/><span class="buttonLike">Suggestion!</span>';
}
}
$output .= self::generateWikidataResultsListEntry($link, $searchTerm, $lang, $result);
}
$output .= ' $output .= '
</main>'; </main>';