Browse Source

wikidata/wikipedia: create formatted entry from wikidata

master
parent
commit
531192a7be
  1. 62
      src/wikidata.php
  2. 2
      src/wikipedia.php

62
src/wikidata.php

@ -34,3 +34,65 @@ function wikidataGetLabel ($id, $lang) {
return array_values($data['labels'])[0]['value'];
}
}
function wikidataGetValues ($id, $property) {
$data = wikidataLoad($id);
if (!array_key_exists($property, $data['claims'])) {
return [];
}
return array_map(
function ($el) {
return $el['mainsnak']['datavalue']['value'];
},
$data['claims'][$property]
);
}
function wikidataFormatDate($value, $maxPrecision = 13) {
$v = new DateTime($value['time']);
$p = min($maxPrecision, $value['precision']);
if ($p < 9) {
} else {
$formats = [
9 => 'Y',
10 => 'M Y',
11 => 'j. M Y',
12 => 'j. M Y - G:00',
13 => 'j. M Y - G:i',
14 => 'j. M Y - G:i:s',
];
return $v->format($formats[$p]);
}
}
function wikidataFormat ($id, $lang) {
$ret = '<b>' . wikidataGetLabel($id, $lang) . '</b>';
$birthDate = wikidataGetValues($id, 'P569');
$deathDate = wikidataGetValues($id, 'P570');
if (sizeof($birthDate) && sizeof($deathDate)) {
$ret .= ' (' . wikidataFormatDate($birthDate[0], 11) . ' — ' . wikidataFormatDate($deathDate[0], 11) . ')';
}
elseif (sizeof($birthDate)) {
$ret .= ' (* ' . wikidataFormatDate($birthDate[0], 11) . ')';
}
elseif (sizeof($deathDate)) {
$ret .= ' († ' . wikidataFormatDate($birthDate[0], 11) . ')';
}
$occupation = wikidataGetValues($id, 'P106');
if (sizeof($occupation)) {
$ret .= ', ' . implode(', ', array_map(
function ($value) use ($lang) {
return wikidataGetLabel($value['id'], $lang);
},
$occupation
));
}
return $ret;
}

2
src/wikipedia.php

@ -27,7 +27,7 @@ function ajax_wikipedia ($param) {
$wp_url = $data['sitelinks'][$id]['url'];
}
else {
$content = "<div><div id='mw-content-text'><div><p>" . wikidataGetLabel($param['page'], $param['lang']) . "</p></div></div></div>";
$content = "<div><div id='mw-content-text'><div><p>" . wikidataFormat($param['page'], $param['lang']) . "</p></div></div></div>";
$url = "https://wikidata.org/wiki/{$param['page']}";
if (array_key_exists('commonswiki', $data['sitelinks'])) {
$url = $data['sitelinks']['commonswiki']['url'];

Loading…
Cancel
Save