-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.php
132 lines (124 loc) · 5.07 KB
/
query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
$srsearch = trim($_GET['srsearch']);
$query = file_get_contents('https://www.wikidata.org/w/api.php?action=query&list=search&utf8=true&format=json&srlimit=15&srsearch=haswbstatement:"P31=Q5"%20'.urlencode("'".$srsearch."'"));
$query = json_decode($query, true);
$query = $query['query'];
if ($query['searchinfo']['totalhits'] >= 1){
$results = array();
foreach ($query['search'] as $key => $value) {
$query = "
SELECT ?itemLabel ?image ?dateOfBirth ?dateOfDeath ?occupationLabel ?countryLabel ?sitelinks ?isSportsPerson ?cattitle ?subcat WHERE {
{
{
BIND(wd:".$value['title']." AS ?item).
?item wdt:P31 wd:Q5
}
OPTIONAL { ?item wdt:P18 ?image. }
OPTIONAL { ?item wdt:P569 ?dateOfBirth.}
OPTIONAL { ?item wdt:P570 ?dateOfDeath. }
OPTIONAL { ?item wdt:P106 ?occupation .}
OPTIONAL { ?item wdt:P27 ?country .}
OPTIONAL { ?link schema:about ?item; schema:isPartOf <https://commons.wikimedia.org/>; schema:name ?cattitle1 . }
OPTIONAL { ?item wdt:P373 ?_cattitle2
BIND(CONCAT(\"Category:\", ?_cattitle2) as ?cattitle2)}
BIND(COALESCE( ?cattitle2, ?cattitle1) as ?cattitle)
OPTIONAL{
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:api \"Generator\" .
bd:serviceParam wikibase:endpoint \"commons.wikimedia.org\" .
bd:serviceParam mwapi:gcmtitle ?cattitle .
bd:serviceParam mwapi:generator \"categorymembers\" .
bd:serviceParam mwapi:gcmlimit \"max\" .
bd:serviceParam mwapi:gcmtype \"subcat\" .
# out
?subcat wikibase:apiOutput mwapi:title .
?ns wikibase:apiOutput \"@ns\" .
?witem wikibase:apiOutputItem mwapi:item .
}
}
OPTIONAL {
?item wdt:P31 wd:Q5.
{ ?item wdt:P641 ?sport. } UNION
{ ?item wdt:P1532 ?countryForSport. } UNION
{ ?item wdt:P106/wdt:P279* wd:Q2066131. }
BIND(true AS ?isSportsPerson_)
}
BIND(COALESCE(?isSportsPerson_, false) AS ?isSportsPerson).
?item wikibase:sitelinks ?sitelinks
service wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en,de\". }
}.filter(NOT EXISTS { ?item wdt:P570 [] }
|| ?dateOfDeath >= \"".(date("Y")-5)."-01-01T00:00:00Z\"^^xsd:dateTime )
.filter(NOT EXISTS { ?item wdt:P569 [] }
|| ?dateOfBirth >=\"1910-01-01T00:00:00Z\"^^xsd:dateTime )
}";
$data = sparqlQuery ($query);
$occupations = array();
$categories = array();
foreach ($data['results']['bindings'] as $key => $item) {
if(isset($item['occupationLabel'])){
$occupations[] = $item['occupationLabel']['value'];
}
if(isset($item['subcat'])){
$categories[] = str_replace("Category:", "", $item['subcat']['value']);
}
}
$occupations = array_unique($occupations);
$categories = array_unique($categories);
if (isset($data['results']['bindings'][0]) ) {
$result = array();
$item = $data['results']['bindings'][0];
$result['qitem'] = $value['title'];
$result['itemLabel'] = $item['itemLabel']['value'];
$result['sitelinks'] = $item['sitelinks']['value'];
$result['isSportsPerson'] = $item['isSportsPerson']['value'];
if (isset($item['image'])){
$result['image'] = $item['image']['value']."?width=100";
}
if (isset($item['dateOfBirth'])){
$result['dateOfBirth'] = $item['dateOfBirth']['value'];
}
if (isset($item['dateOfDeath'])){
$result['dateOfDeath'] = $item['dateOfDeath']['value'];
}
if (isset($item['occupationLabel'])){
$result['occupation'] = implode("/­", $occupations);
}
if (isset($item['countryLabel'])){
$result['country'] = $item['countryLabel']['value'];
}
if (isset($item['cattitle'])){
$categories[] = str_replace("Category:", "", $item['cattitle']['value']);
$result['categories'] = "-incategory:\"".implode("\" -incategory:\"", $categories)."\"";
}
$results[] = $result;
}
}
if (isset($results) && count($results) > 0){
echo json_encode($results);
}
else{
echo json_encode('nee');
}
}
else{
echo json_encode('nee');
}
function sparqlQuery(string $sparqlQuery): array
{
$opts = [
'http' => [
'method' => 'GET',
'header' => [
"Accept: application/sparql-results+json\r\n".
"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net
"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n"
],
],
];
$context = stream_context_create($opts);
$url = 'https://query.wikidata.org/sparql?query=' . urlencode($sparqlQuery);
$response = file_get_contents($url, false, $context);
return json_decode($response, true);
}
?>