# HG changeset patch # User cavaliet # Date 1390994176 -3600 # Node ID 085ea4dbfeee1ff9258235625ad8f4c628198080 # Parent ed31d6008d312db0bbb7a5552b9e39252a226468 dbpedia sparql query diff -r ed31d6008d31 -r 085ea4dbfeee README.md --- a/README.md Fri Jan 24 17:50:24 2014 +0100 +++ b/README.md Wed Jan 29 12:16:16 2014 +0100 @@ -83,7 +83,7 @@ wikipedia_api: 'http://de.wikipedia.org/w/api.php' wikipedia_permalink: 'http://de.wikipedia.org/w/index.php?oldid=%s' wikipedia_opensearch: 'http://de.wikipedia.org/w/index.php?search=' - dbpedia: 'http://de.dbpedia.org/resource/%s' + dbpedia_sparql: 'http://de.dbpedia.org/sparql' * Add the WikiTag routes to your routing.yml : diff -r ed31d6008d31 -r 085ea4dbfeee Utils/WikiTagUtils.php --- a/Utils/WikiTagUtils.php Fri Jan 24 17:50:24 2014 +0100 +++ b/Utils/WikiTagUtils.php Wed Jan 29 12:16:16 2014 +0100 @@ -145,6 +145,10 @@ $revision_id = $page['lastrevid']; + // Get the dbpedia uri by requesting dbpedia with sparkl + + + /* // process language to extract the english label $english_label = null; if($status==Tag::$TAG_URL_STATUS_DICT["match"] || $status==Tag::$TAG_URL_STATUS_DICT["redirection"]){ @@ -162,6 +166,8 @@ if($english_label!=null && strpos($english_label, '#')===false){ $dbpedia_uri = WikiTagUtils::getDbpediaUri($english_label); } + */ + $dbpedia_uri = WikiTagUtils::getDbpediaUri($new_label); $wp_response = array( 'new_label'=>$new_label, @@ -178,6 +184,48 @@ return $wp_response; } + /** + * Generic curl request + * + * @param string $url + * @return object (json decoded) + */ + private static function curlRequest($url) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + // default values + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0'); + curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000); + // Set options if they are set in the config.yml file, typically for proxy configuration. + // Thanks to the configuration file, it will execute commands like "curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);" or "curl_setopt($ch, CURLOPT_PROXY, "xxx.yyy.zzz:PORT");" + $curl_options = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.curl_options"); + foreach ($curl_options as $key => $value) { + if(strtoupper($value)=='TRUE'){ + $value = TRUE; + } + else if (strtoupper($value)=='FALSE'){ + $value = FALSE; + } + else if (defined($value)){ + $value = constant($value); + } + curl_setopt($ch, constant($key), $value); + } + // end of treatment + $res = curl_exec($ch); + $curl_errno = curl_errno($ch); + $curl_error = curl_error($ch); + curl_close($ch); + + if ($curl_errno > 0) { + throw new \Exception("$url\n request failed. cURLError #$curl_errno: $curl_error\n", $curl_errno, null); + } + + return $res; + } + /** * build and do the request to Wikipedia. @@ -197,41 +245,9 @@ } } - //$url = WikiTagUtils::$WIKIPEDIA_API_URL.'?'.$params_str; - //throw new \Exception($GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates"), 1, null); $url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["wikipedia_api"].'?'.$params_str; - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - // default values - curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.1) Gecko/20100101 Firefox/10.0.1'); - curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000); - // Set options if they are set in the config.yml file, typically for proxy configuration. - // Thanks to the configuration file, it will execute commands like "curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);" or "curl_setopt($ch, CURLOPT_PROXY, "xxx.yyy.zzz:PORT");" - $curl_options = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.curl_options"); - foreach ($curl_options as $key => $value) { - if(strtoupper($value)=='TRUE'){ - $value = TRUE; - } - else if (strtoupper($value)=='FALSE'){ - $value = FALSE; - } - else if (defined($value)){ - $value = constant($value); - } - curl_setopt($ch, constant($key), $value); - } - // end of treatment - $res = curl_exec($ch); - $curl_errno = curl_errno($ch); - $curl_error = curl_error($ch); - curl_close($ch); - - if ($curl_errno > 0) { - throw new \Exception("Wikipedia request failed. cURLError #$curl_errno: $curl_error\n", $curl_errno, null); - } - + $res = WikiTagUtils::curlRequest($url); $val = json_decode($res, true); $pages = $val["query"]["pages"]; return array($res, $pages); @@ -265,9 +281,39 @@ /** * Builds DbPedia URI */ - private static function getDbpediaUri($english_label) + private static function getDbpediaUri($label, $params=[]) { - return sprintf($GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia"], WikiTagUtils::urlize_for_wikipedia($english_label)); + // Get lang from url + $dbp_url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia_sparql"]; + $lang = substr($dbp_url, 7, 2); + $params = [ + "query" => 'select distinct * where {?s rdfs:label "'.$label.'"@'.$lang.'}', + "format" => 'application/json', + ]; + + $params_str = ''; + foreach ($params as $key => $value) { + if ($params_str==''){ + $params_str = $key.'='.urlencode($value); + } + else{ + $params_str .= '&'.$key.'='.urlencode($value); + } + } + + $url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia_sparql"].'?'.$params_str; + + $res = WikiTagUtils::curlRequest($url); + $val = json_decode($res, true); + $uri = ""; + if(array_key_exists("results", $val)){ + if(array_key_exists("bindings", $val["results"])){ + if(count($val["results"]["bindings"]) > 0){ + $uri = $val["results"]["bindings"][0]["s"]["value"]; + } + } + } + return $uri; } /**