222 else{ |
222 else{ |
223 $params_str .= '&'.$key.'='.$value; |
223 $params_str .= '&'.$key.'='.$value; |
224 } |
224 } |
225 } |
225 } |
226 |
226 |
227 $url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["wikipedia_api"].'?'.$params_str; |
227 $url_templates = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates"); |
|
228 $url = $url_templates["wikipedia_api"].'?'.$params_str; |
228 |
229 |
229 $res = WikiTagUtils::curlRequest($url); |
230 $res = WikiTagUtils::curlRequest($url); |
230 $val = json_decode($res, true); |
231 $val = json_decode($res, true); |
231 $pages = $val["query"]["pages"]; |
232 $pages = $val["query"]["pages"]; |
232 return array($res, $pages); |
233 return array($res, $pages); |
258 } |
259 } |
259 |
260 |
260 /** |
261 /** |
261 * Builds DbPedia URI |
262 * Builds DbPedia URI |
262 */ |
263 */ |
263 public static function getDbpediaUri($label, $params=[], $throw_error=true, $req_param="label") |
264 public static function getDbpediaUri($label, $params=array(), $throw_error=true, $req_param="label") |
264 { |
265 { |
265 // Get lang from url |
266 // Get lang from url |
266 $dbp_url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia_sparql"]; |
267 $url_templates = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates"); |
|
268 $dbp_url = $url_templates["dbpedia_sparql"]; |
267 $lang = substr($dbp_url, 7, 2); |
269 $lang = substr($dbp_url, 7, 2); |
268 // filter with regexp to avoid results with "category:LABEL" or other "abc:LABEL" |
270 // filter with regexp to avoid results with "category:LABEL" or other "abc:LABEL" |
269 $query = 'select distinct * where { ?s rdfs:label "'.$label.'"@'.$lang.' . FILTER (regex(?s, "^http\\\\://[^:]+$")) }'; |
271 $query = 'select distinct * where { ?s rdfs:label "'.$label.'"@'.$lang.' . FILTER (regex(?s, "^http\\\\://[^:]+$")) }'; |
270 if($req_param=="pageid"){ |
272 if($req_param=="pageid"){ |
271 $query = 'select distinct * where { ?s dbpedia-owl:wikiPageID '.$label.' }'; |
273 $query = 'select distinct * where { ?s dbpedia-owl:wikiPageID '.$label.' }'; |
272 } |
274 } |
273 elseif ($req_param=="wikiurl"){ |
275 elseif ($req_param=="wikiurl"){ |
274 $query = 'select distinct * where { ?s foaf:isPrimaryTopicOf <'.$label.'> }'; |
276 $query = 'select distinct * where { ?s foaf:isPrimaryTopicOf <'.$label.'> }'; |
275 } |
277 } |
276 |
278 |
277 $params = [ |
279 $params = array( |
278 "query" => $query, |
280 "query" => $query, |
279 "format" => 'application/json', |
281 "format" => 'application/json', |
280 ]; |
282 ); |
281 |
283 |
282 $params_str = ''; |
284 $params_str = ''; |
283 foreach ($params as $key => $value) { |
285 foreach ($params as $key => $value) { |
284 if ($params_str==''){ |
286 if ($params_str==''){ |
285 $params_str = $key.'='.urlencode($value); |
287 $params_str = $key.'='.urlencode($value); |
287 else{ |
289 else{ |
288 $params_str .= '&'.$key.'='.urlencode($value); |
290 $params_str .= '&'.$key.'='.urlencode($value); |
289 } |
291 } |
290 } |
292 } |
291 |
293 |
292 $url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia_sparql"].'?'.$params_str; |
294 $url_templates = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates"); |
|
295 $url = $url_templates["dbpedia_sparql"].'?'.$params_str; |
293 |
296 |
294 $res = WikiTagUtils::curlRequest($url, $throw_error); |
297 $res = WikiTagUtils::curlRequest($url, $throw_error); |
295 $val = json_decode($res, true); |
298 $val = json_decode($res, true); |
296 $uri = ""; |
299 $uri = ""; |
297 if($val){ |
300 if($val){ |