-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.php
157 lines (130 loc) · 3.88 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
include_once 'citoid_ref.php';
define('WD_API', 'https://www.wikidata.org/w/api.php?');
$context = stream_context_create(
array(
"http" => array(
"header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
)
)
);
if(isset($_GET['url'])){
$url = urldecode(strip_tags($_GET['url']));
$ref = new citoidRef($url);
$date = '';
if ($ref->getPubDate() != null){
$date = $ref->getPubDate()->format("Y-m-d");
}
echo json_encode(array(
"url" => $ref->getURL(),
"title" => $ref->getTitle(),
"language" => $ref->getLanguage(),
"authors" => $ref->getAuthors(),
"pubdate" => $date
));
}
function getLabel($qid, $label_cache, $lang="en"){
if (isset($label_cache[$lang][$qid])){
return $label_cache[$lang][$qid];
}
$wbgetentities = array(
'format'=>'json',
'action'=>'wbgetentities',
'props'=>'labels',
'ids' => $qid
);
$query = json_decode(file_get_contents(WD_API.http_build_query($wbgetentities)), true);
if (isset($query['entities'][$qid]['labels'][$lang])){
$label = $query['entities'][$qid]['labels'][$lang]['value'];
}
else{
$label = $qid;
}
$label_cache[$lang][$qid] = $label;
return $label;
}
function country_substitute($countries){
$new_countries = array();
foreach ($countries as $country){
switch ($country) {
case 'Kingdom of the Netherlands':
array_push($new_countries, "the Netherlands");
break;
case 'United States of America':
array_push($new_countries, "the USA");
break;
default:
array_push($new_countries, $country);
break;
}
}
return $new_countries;
}
function get_date_string($datavalue){
//case 9
$precision_cut = 4;
switch($datavalue["precision"]){
case 10:
$precision_cut = 6;
break;
case 11:
$precision_cut = 10;
break;
}
return substr($datavalue["time"], 1, $precision_cut);
}
$search_vars = array(
'action'=>'query',
'list'=>'search',
'utf8'=>'true',
'format'=>'json',
'srlimit'=>'20');
$wbgetclaims = array('action'=> 'wbgetclaims','format'=>'json');
$label_cache = array("en" => array());
if(isset($_GET['srsearch'])){
$search_vars['srsearch'] = "haswbstatement:P31=Q5 '".trim($_GET['srsearch'])."'";
$query = json_decode(file_get_contents(WD_API.http_build_query($search_vars)), true)['query'];
if ($query['searchinfo']['totalhits'] >= 1){
$results = array();
foreach ($query['search'] as $key => $search_result) {
$result = array(
'qitem'=> $search_result['title'],
'itemLabel' => getLabel($search_result['title'], $label_cache));
$wbgetclaims['entity'] = $search_result['title'];
$claims = json_decode(file_get_contents(WD_API.http_build_query($wbgetclaims)), true)['claims'];
$occupations = array();
foreach ($claims['P106'] as $occupation) {
array_push($occupations, getLabel($occupation['mainsnak']["datavalue"]["value"]['id'], $label_cache));
}
$occupations = array_unique($occupations);
$result['occupation'] = implode("/­", $occupations);
$countries = array();
foreach ($claims['P27'] as $country) {
array_push($countries, getLabel($country['mainsnak']["datavalue"]["value"]['id'], $label_cache));
}
$countries = array_unique($countries);
$countries = country_substitute($countries);
$result['country'] = implode("/­", $countries);
if (isset($claims['P569'])){
$result['dateOfBirth'] = get_date_string($claims['P569'][0]["mainsnak"]["datavalue"]["value"]);
}
if (isset($claims['P570'])){
$result['dateOfDeath'] = get_date_string($claims['P570'][0]["mainsnak"]["datavalue"]["value"]);
}
if (isset($claims['P18'])){
$result['image'] = $claims['P18'][0]["mainsnak"]["datavalue"]["value"];
}
$results[] = $result;
}
if (isset($results) && count($results) > 0){
echo json_encode($results);
}
else{
echo json_encode('nee');
}
}
else{
echo json_encode('nee');
}
}
?>