Utils/WikiTagUtils.php
changeset 116 a023e0185a02
parent 115 085ea4dbfeee
child 117 5771052a647a
equal deleted inserted replaced
115:085ea4dbfeee 116:a023e0185a02
   188      * Generic curl request
   188      * Generic curl request
   189      *
   189      *
   190      * @param string $url
   190      * @param string $url
   191      * @return object (json decoded)
   191      * @return object (json decoded)
   192      */
   192      */
   193     private static function curlRequest($url)
   193     private static function curlRequest($url, $throw_error=true)
   194     {
   194     {
   195     	$ch = curl_init();
   195     	$ch = curl_init();
   196     	curl_setopt($ch, CURLOPT_URL, $url);
   196     	curl_setopt($ch, CURLOPT_URL, $url);
   197     	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   197     	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   198     	// default values
   198     	// default values
   217     	$res = curl_exec($ch);
   217     	$res = curl_exec($ch);
   218     	$curl_errno = curl_errno($ch);
   218     	$curl_errno = curl_errno($ch);
   219     	$curl_error = curl_error($ch);
   219     	$curl_error = curl_error($ch);
   220     	curl_close($ch);
   220     	curl_close($ch);
   221     
   221     
   222     	if ($curl_errno > 0) {
   222     	if ($curl_errno > 0 && $throw_error) {
   223     		throw new \Exception("$url\n request failed. cURLError #$curl_errno: $curl_error\n", $curl_errno, null);
   223     		throw new \Exception("$url\n request failed. cURLError #$curl_errno: $curl_error\n", $curl_errno, null);
   224     	}
   224     	}
   225     	
   225     	
   226     	return $res;
   226     	return $res;
   227 	}
   227 	}
   279     }
   279     }
   280     
   280     
   281     /**
   281     /**
   282      * Builds DbPedia URI
   282      * Builds DbPedia URI
   283      */
   283      */
   284     private static function getDbpediaUri($label, $params=[])
   284     public static function getDbpediaUri($label, $params=[], $throw_error=true)
   285     {
   285     {
   286     	// Get lang from url
   286     	// Get lang from url
   287     	$dbp_url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia_sparql"];
   287     	$dbp_url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia_sparql"];
   288     	$lang = substr($dbp_url, 7, 2);
   288     	$lang = substr($dbp_url, 7, 2);
       
   289     	// filter with regexp to avoid results with "category:LABEL" or other "abc:LABEL"
       
   290     	//"query" => 'select distinct * where { ?s rdfs:label "'.$label.'"@'.$lang.' }',
       
   291     	//"query" => 'select distinct * where { ?s rdfs:label "'.$label.'"@'.$lang.' . FILTER (regex(?s, "^http\\\\://[^:]+$")) }',
   289     	$params = [
   292     	$params = [
   290     		"query" => 'select distinct * where {?s rdfs:label "'.$label.'"@'.$lang.'}',
   293     		"query" => 'select distinct * where { ?s rdfs:label "'.$label.'"@'.$lang.' . FILTER (regex(?s, "^http\\\\://[^:]+$")) }',
   291     		"format" => 'application/json',
   294     		"format" => 'application/json',
   292     	];
   295     	];
   293 
   296     	
   294     	$params_str = '';
   297     	$params_str = '';
   295     	foreach ($params as $key => $value) {
   298     	foreach ($params as $key => $value) {
   296     		if ($params_str==''){
   299     		if ($params_str==''){
   297     			$params_str = $key.'='.urlencode($value);
   300     			$params_str = $key.'='.urlencode($value);
   298     		}
   301     		}
   301     		}
   304     		}
   302     	}
   305     	}
   303     	
   306     	
   304     	$url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia_sparql"].'?'.$params_str;
   307     	$url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia_sparql"].'?'.$params_str;
   305     	
   308     	
   306     	$res = WikiTagUtils::curlRequest($url);
   309     	$res = WikiTagUtils::curlRequest($url, $throw_error);
   307     	$val = json_decode($res, true);
   310     	$val = json_decode($res, true);
   308     	$uri = "";
   311     	$uri = "";
   309     	if(array_key_exists("results", $val)){
   312     	if($val){
   310     		if(array_key_exists("bindings", $val["results"])){
   313 	    	if(array_key_exists("results", $val)){
   311     			if(count($val["results"]["bindings"]) > 0){
   314 	    		if(array_key_exists("bindings", $val["results"])){
   312     				$uri = $val["results"]["bindings"][0]["s"]["value"];
   315 	    			$len = count($val["results"]["bindings"]);
   313     			}
   316 	    			if($len > 0){
   314     		}
   317 	    				$uri = $val["results"]["bindings"][0]["s"]["value"];
       
   318 	    				if($len>1){
       
   319 	    					// If there are several results, we test the "url label" to see if it matches the label.
       
   320 	    					// Why ? Because, for example "1000" gets "Category:1000" and "1000" as result.
       
   321 	    					// We keep this code to be safe but the regexp in the sparql request normally avoids this problem.
       
   322 	    					for($i=0;$i<$len;$i++){
       
   323 	    						$res_uri = $val["results"]["bindings"][$i]["s"]["value"];
       
   324 	    						$url_label = substr( $res_uri, strrpos( $res_uri, '/' )+1 );
       
   325 	    						if(str_replace(" ", "_", $label) == $url_label){
       
   326 	    							$uri = $res_uri;
       
   327 	    						}
       
   328 	    					}
       
   329 	    				}
       
   330 	    			}
       
   331 	    		}
       
   332 	    	}
   315     	}
   333     	}
   316     	return $uri;
   334     	return $uri;
   317     }
   335     }
   318     
   336     
   319     /**
   337     /**