Newer
Older
public static function uri2curie($uri){
global $conf;
$ns = $conf['ns'];
$curie = $uri;
$aux = preg_replace("|^$v|", "", $uri);
if($aux != $uri){
$uriSegments = explode("/", $aux);
$lastSegment = array_pop($uriSegments);
if(sizeof($uriSegments)>0){
$prefix = $k."_".(implode("_", $uriSegments));
//Adding "new" namespace
$conf['ns'][$prefix] = $v.implode("/", $uriSegments)."/";
}else{
$prefix = $k;
}
$curie = $prefix.":".$lastSegment;
public static function curie2uri($curie){
global $conf;
$ns = $conf['ns'];
$parts = explode(':', $curie);
//Avoid if we have a namespace prefix called 'http'
if(preg_match('|^//|', $parts[1])){
return $curie;
}
if(sizeof($parts)>1 ){
if(!isset($ns[$parts[0]])){
$prefixSegments = explode("_", $parts[0]);
$realPrefix = array_shift($prefixSegments);
$conf['ns'][$parts[0]] = $ns[$realPrefix].join("/", $prefixSegments);
return $ns[$realPrefix].join("/", $prefixSegments)."/".$parts[1];
}else{
return $ns[$parts[0]].$parts[1];
}
public static function getPrefix($curie){
global $conf;
$ns = $conf['ns'];
$parts = explode(':', $curie);
//Avoid if we have a namespace prefix called 'http'
if(preg_match('|^//|', $parts[1])){
return $curie;
}
return array('ns' => $ns[$parts[0]], 'prefix' => $parts[0]);
public static function getTemplate($uri){
$filename = str_replace(":", "_", $uri);
if(file_exists ($filename)){
include_once($filename);
}
}
private static function sparqlResult2Obj($data){
alangrafu
committed
global $conf;
foreach($data as $k => $v){
$obj[$k] = Utils::sparqlResult2Obj($v);
}
}else{
if(sizeof($aux)>0){
foreach($aux as $w){
$row = array();
foreach($w as $k => $v){
if($v['type'] == 'uri'){
$row[$k]['curie'] = Utils::uri2curie($v['value']);
$row[$k]['uri'] = 1;
}elseif($v['type'] == 'bnode'){
}
if(sizeof($row) >0){
array_push($obj, $row);
}
public static function getExtension($accept_string){
global $conf;
$extension = "html";
foreach($conf['http_accept'] as $ext => $accept_arr){
if(in_array($accept_string, $accept_arr)){
$extension = $ext;
}
public static function getBestContentType($accept_string){
global $conf;
alangrafu
committed
$a = explode(",", $accept_string);
$b = array();
foreach($a as $v){
foreach($conf['http_accept'] as $formatTypeArray){
if(strstr($v, ";")){
$aux = explode(";q=", $v);
if(in_array($aux[0], $formatTypeArray)){
$b[$aux[0]] = $aux[1];
}
}else{
if(in_array($v, $formatTypeArray)){
$b[$v] = 1;
}
}
}
}
$a = $b;
arsort($a);
foreach($a as $k => $v){
$ct = $k;
break;
if($ct == NULL || $ct == "" || $ct == "*/*"){
$ct = 'text/html';
private static function travelTree($tree){
$results = array();
if(is_string($tree)){
return $tree;
}
foreach($tree as $t){
$aux = Utils::travelTree($t);
if(is_array($aux)){
$results = array_merge($results, $aux);
}else{
array_push($results, $aux);
}
}
return $results;
}
public static function serializeRdf($data, $extension='rdf'){
Alvaro Graves
committed
global $conf;
global $lodspk;
require_once($conf['home'].'lib/arc2/ARC2.php');
$triples = array();
foreach($docs as $d){
$parser->parse($conf['basedir'], $d);
$t = $parser->getTriples();
$triples = array_merge($triples, $t);
}
$t['s_type'] = 'uri';
$t['p'] = "http://www.w3.org/2002/07/owl#sameAs";
$t['o_type'] = 'uri';
array_push($triples, $t);
$t['p'] = "http://www.w3.org/2000/10/swap/pim/contact#preferredURI";
array_push($triples, $t);
}
switch ($extension){
case 'ttl':
$ser = ARC2::getTurtleSerializer();
break;
case 'nt':
$ser = ARC2::getNTriplesSerializer();
break;
case 'json':
$ser = ARC2::getRDFJSONSerializer();
break;
case 'rdf':
$ser = ARC2::getRDFXMLSerializer();
break;
}else{
$doc = var_export($data, true);
}
public static function processDocument($viewFile, $lodspk, $data){
$extension = Utils::getExtension($contentType);
header('Content-Type: '.$contentType);
if(isset($lodspk['resultRdf']) && $lodspk['resultRdf'] == true){
echo Utils::serializeRdf($data, $extension);
}else{
Utils::showView($lodspk, $data, $viewFile);
}
}
public static function getResultsType($query){
global $conf;
if(preg_match("/select/i", $query)){
global $conf;
global $uri;
$strippedModelDir = str_replace('endpoint.', '', $modelDir);
$originalDir = getcwd();
trigger_error("Entering $strippedModelDir from ".getcwd(), E_USER_NOTICE);
chdir($modelDir);
$handle = opendir('.');
while (false !== ($modelFile = readdir($handle))) {
if($modelFile != "." && $modelFile != ".." && strpos($modelFile, ".") !== 0){
if(is_dir($modelFile)){
if(strpos('endpoint.', $modelFile) == 0){
trigger_error("Save $modelFile for later, after all the queries in the current directory has been resolved", E_USER_NOTICE);
$subDirs[]=$modelFile;
}
}else{
if(preg_match('/\.query$/', $modelFile)){
$e = null;
if(!isset($endpoints[$strippedModelDir])){
trigger_error("Creating endpoint for $strippedModelDir", E_USER_NOTICE);
if(!isset($conf['endpoint'][$strippedModelDir])){
trigger_error("Couldn't find $strippedModelDir as a list of available endpoints. Will continue using local", E_USER_WARNING);
$e = $endpoints['local'];
}else{
$endpoints[$strippedModelDir] = new Endpoint($conf['endpoint'][$strippedModelDir], $conf['endpoint']['config']);
$e = $endpoints[$strippedModelDir];
}
}else{
$e = $endpoints[$strippedModelDir];
}
if($modelDir != $lodspk['type']){
if(!isset($r[$strippedModelDir]) ){
$r[$strippedModelDir] = array();
$f[$strippedModelDir] = array();
}
Utils::queryFile($modelFile, $e, $r[$strippedModelDir], $f);
}else{
Utils::queryFile($modelFile, $e, $r, $f);
}
foreach($subDirs as $v){
if(!isset($r[$modelDir])){
$r[$modelDir] = array();
}
if($modelDir != $lodspk['type']){
Utils::queryDir($v, $r[$strippedModelDir], $f[$strippedModelDir]);
public static function queryFile($modelFile, $e, &$rPointer, &$fPointer){
global $firstResults;
$uri = $lodspk['this']['value'];
$strippedModelFile = str_replace('endpoint.', '', str_replace('.query', '',$modelFile));
if(!is_dir($modelFile)){
require_once($conf['home'].'lib/Haanga/lib/Haanga.php');
Haanga::configure(array(
'cache_dir' => $conf['home'].'cache/',
'autoescape' => FALSE,
));
//Haanga supports the dot (.) convention only for objects
if(is_array($lodspk)){
$lodspkObj = Convert::array_to_object($lodspk);
$lodspk = $lodspkObj;
$models = Convert::array_to_object($r2);
$f2 = Convert::array_copy($firstResults);
$first = Convert::array_to_object($f2);
$vars = compact('uri', 'lodspk', 'models', 'first');
$q = file_get_contents($modelFile);
if($q == false){
HTTPStatus::send500("I can't load ".$modelFile." in ".getcwd());
if(is_object($lodspk)){
$lodspkObj = Convert::object_to_array($lodspk);
$lodspk = $lodspkObj;
if($lodspk['transform_select_query']==true){
include_once($conf['home'].'lib/arc2/ARC2.php');
$parser = ARC2::getSPARQLParser();
$parser->parse($query);
$sparqlConstruct = array();
if (!$parser->getErrors()) {
$resultVars = array();
$q_infos = $parser->getQueryInfos();
foreach($q_infos['query']['result_vars'] as $v){
if($v['type'] == 'var'){
$resultVars[$v['value']] = 1;
}
};
$x = Utils::extractObj($q_infos['query']['pattern']);
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
foreach($x as $v){
if(($resultVars[$v['s']] && $v['s_type'] == 'var')
|| ($resultVars[$v['p']] && $v['p_type'] == 'var')
|| ($resultVars[$v['o']] && $v['o_type'] == 'var')){
array_push($sparqlConstruct, $v);
}
}
$construct = "";
foreach($sparqlConstruct as $v){
if($v['s_type'] == 'uri'){
$construct .= "<".$v['s']."> ";
}elseif($v['s_type'] == 'var'){
$construct .= '?'.$v['s'].' ';
}else{
$construct.= $v['s']." ";
}
if($v['p_type'] == 'uri'){
$construct .= "<".$v['p']."> ";
}elseif($v['p_type'] == 'var'){
$construct .= '?'.$v['p'].' ';
}else{
$construct.= $v['p']." ";
}
if($v['o_type'] == 'uri'){
$construct .= "<".$v['o']."> ";
}elseif($v['o_type'] == 'literal'){
$construct .= '"'.$v['o'].'" ';
}elseif($v['o_type'] == 'var'){
$construct .= '?'.$v['o'].' ';
}else{
$construct.= $v['o']." ";
}
$construct .= ".\n";
}
if($construct == ""){
if(sizeof($q_infos['query']['result_vars'])>0){
//For now, assuming variables are in the GRAPH ?g
$query = "CONSTRUCT {?g ?x ?y} WHERE{GRAPH ?g{?g ?x ?y}}";
}else{
$query = preg_replace('/select\n?.*\n?where/i', 'CONSTRUCT {'.$construct.'} WHERE', $query);
HTTPStatus::send500("invalid query: " . $parser->getErrors());
alangrafu
committed
if($conf['debug']){
echo "\n-------------------------------------------------\nIn ".getcwd()."\n";
echo "$modelFile (against ".$e->getSparqlUrl().")\n-------------------------------------------------\n\n";
alangrafu
committed
echo $query;
}
trigger_error("Running query from ".$modelFile." on endpoint ".$e->getSparqlURL(), E_USER_NOTICE);
$aux = $e->query($query, Utils::getResultsType($query));
if($modelFile != $lodspk['type']){
if(!isset($rPointer[$strippedModelFile])){
$rPointer[$strippedModelFile] = array();
$firstResults[$strippedModelFile] = array();
if(Utils::getResultsType($query) == $conf['output']['select']){
$rPointer[$strippedModelFile] = Utils::sparqlResult2Obj($aux);
$fPointer[$strippedModelFile] = $rPointer[$strippedModelFile][0];
/*if(sizeof($rPointer)>0){
$rPointer[$modelFile]['firstResults'] = $rPointer[$modelFile][0];
}*/
}
if(Utils::getResultsType($query) == $conf['output']['select']){
/*if(sizeof($rPointer)>0){
$rPointer['firstResults'] = $rPointer[0];
}*/
}
if(strpos('endpoint.', $modelFile) == 0){
trigger_error("$modelFile is a directory, will process it later", E_USER_NOTICE);
if($modelFile != $lodspk['type']){
if(!isset($rPointer[$strippedModelFile])){
$rPointer[$strippedModelFile] = array();
}
Utils::queryDir($modelFile, $rPointer[$strippedModelFile], $fPointer[$strippedModelFile]);
}else{
Utils::queryDir($modelFile, $rPointer, $fPointer);
public static function internalize($array){
global $conf;
foreach($array as $key => $value){
if(!isset($value['value'])){
$array[$key] = Utils::internalize($value);
/*if($firstResultsKeyAppearance){
$firstResultsKeyAppearance = false;
$array['_firstResults']=$array[$key];
if(isset($value['uri']) && $value['uri'] == 1){
if(isset($conf['mirror_external_uris']) && $conf['mirror_external_uris'] != false){
$value['mirroredUri'] = $value['value'];
if(is_bool($conf['mirror_external_uris'])){
$value['value'] = preg_replace("|^".$conf['ns']['local']."|", $conf['basedir'], $value['value']);
}elseif(is_string($conf['mirror_external_uris'])){
$value['value'] = preg_replace("|^".$conf['mirror_external_uris']."|", $conf['basedir'], $value['value']);
}else{
HTTPStatus::send500("Error in mirroring configuration");
global $conf;
foreach($array as $key => $value){
if(!isset($value['value'])){
if(isset($aux['0'])){
}
}
}
return $array;
}
public static function showView($lodspkData, $data, $view){
//$lodspk = $conf['view']['standard'];
$lodspk = $lodspkData;
if(isset($lodspkData['params'])){
$lodspk['this']['params'] = $lodspkData['params'];
}
require_once($conf['home'].'lib/Haanga/lib/Haanga.php');
$viewAux = explode("/",$view);
$viewFile = array_pop($viewAux);
$viewPath = join("/", $viewAux);
$first = $lodspk['firstResults'];
unset($lodspk['firstResults']);
$lodspk = $lodspk;
//unset($lodspk);
$vars = compact('uri','lodspk', 'models', 'first');
alangrafu
committed
if($conf['debug']){
alangrafu
committed
}
}elseif(is_file($conf['home'].$view)){
Haanga::Load($viewFile, $vars);
}elseif($view == null){
$fnc = Haanga::compile('{{models|safe}}');
$fnc($vars, TRUE);
}
private static function extractObj($obj, $term = 'triple'){
$triples = array();
if(is_array($obj)){
foreach($obj as $k => $v){
if($v['type'] != 'triple'){
if($aux['type'] != 'triple'){
$triples = array_merge($triples,$aux);
}else{
$triples = array_merge($triples, $aux);
}
}else{
array_push($triples, $v);
}
}
}
return $triples;
}