Skip to content
Snippets Groups Projects
Commit e3089512 authored by Alvaro Graves's avatar Alvaro Graves
Browse files

Adding query conversion to services

parent 7eb5b6dd
No related branches found
No related tags found
No related merge requests found
...@@ -355,7 +355,7 @@ class Utils{ ...@@ -355,7 +355,7 @@ class Utils{
$resultVars[$v['value']] = 1; $resultVars[$v['value']] = 1;
} }
}; };
$x = Utils::extractTriples($q_infos['query']['pattern']); $x = Utils::extractObj($q_infos['query']['pattern']);
foreach($x as $v){ foreach($x as $v){
if(($resultVars[$v['s']] && $v['s_type'] == 'var') if(($resultVars[$v['s']] && $v['s_type'] == 'var')
|| ($resultVars[$v['p']] && $v['p_type'] == 'var') || ($resultVars[$v['p']] && $v['p_type'] == 'var')
...@@ -393,7 +393,16 @@ class Utils{ ...@@ -393,7 +393,16 @@ class Utils{
$construct .= ".\n"; $construct .= ".\n";
} }
$query = preg_replace('/select.*where/i', 'CONSTRUCT {'.$construct.'} WHERE', $query); 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{
Utils::send500();
}
}else{
$query = preg_replace('/select.*where/i', 'CONSTRUCT {'.$construct.'} WHERE', $query);
}
}else { }else {
Utils::send500("invalid query: " . $parser->getErrors()); Utils::send500("invalid query: " . $parser->getErrors());
} }
...@@ -519,12 +528,12 @@ class Utils{ ...@@ -519,12 +528,12 @@ class Utils{
} }
private static function extractTriples($obj){ private static function extractObj($obj, $term = 'triple'){
$triples = array(); $triples = array();
if(is_array($obj)){ if(is_array($obj)){
foreach($obj as $k => $v){ foreach($obj as $k => $v){
if($v['type'] != 'triple'){ if($v['type'] != 'triple'){
$aux = Utils::extractTriples($v); $aux = Utils::extractObj($v);
if($aux['type'] != 'triple'){ if($aux['type'] != 'triple'){
$triples = array_merge($triples,$aux); $triples = array_merge($triples,$aux);
}else{ }else{
......
...@@ -13,38 +13,128 @@ class ServiceModule extends abstractModule{ ...@@ -13,38 +13,128 @@ class ServiceModule extends abstractModule{
return FALSE; return FALSE;
} }
$extension = Utils::getExtension($acceptContentType); $extension = Utils::getExtension($acceptContentType);
$auxViewFile = $conf['home'].$conf['view']['directory'].$conf['service']['prefix'].$qArr[0]."/".$extension.".template"; $viewFile = $conf['service']['prefix'].$qArr[0]."/".$extension.".template";
$auxModelFile = $conf['home'].$conf['model']['directory'].$conf['service']['prefix'].$qArr[0]."/".$extension.".queries"; $modelFile = $conf['service']['prefix'].$qArr[0]."/".$extension.".queries";
if(is_dir($auxModelFile) && is_file($auxViewFile)){ if(file_exists($conf['home'].$conf['model']['directory'].$modelFile) && file_exists($conf['home'].$conf['view']['directory'].$viewFile) && $qArr[0] != null){
return $uri;// $qArr[0]; return array($modelFile, $viewFile);
}elseif($extension != 'html' && file_exists($conf['model']['directory'].$conf['service']['prefix'].$qArr[0].'/html.queries')){
$modelFile = $conf['service']['prefix'].$qArr[0].'/html.queries';
$viewFile = null;
return array($modelFile, $viewFile);
} }
$auxViewFile = $conf['home'].$conf['view']['directory'].$conf['service']['prefix'].$qArr[0];
$auxModelFile = $conf['home'].$conf['model']['directory'].$conf['service']['prefix'].$qArr[0]; return FALSE;
if(is_dir($auxModelFile) && is_dir($auxViewFile)){ }
Utils::send406($uri);// $qArr[0];
public function execute($service){
global $conf;
global $localUri;
global $uri;
global $acceptContentType;
global $endpoints;
global $lodspk;
$context = array();
$context['contentType'] = $acceptContentType;
$context['endpoints'] = $endpoints;
//$sp = new SpecialFunction();
//$sp->execute($localUri, $context);
$f = $this->getFunction($uri);
$params = array();
$params = $this->getParams($uri);
//$params[] = $context;
$acceptContentType = Utils::getBestContentType($_SERVER['HTTP_ACCEPT']);
$extension = Utils::getExtension($acceptContentType);
$args = array();
list($modelFile, $viewFile) = $service;
try{
$prefixHeader = array();
for($i=0;$i<sizeof($params);$i++){
if($conf['mirror_external_uris']){
$altUri = Utils::curie2uri($params[$i]);
$altUri = preg_replace("|^".$conf['basedir']."|", $conf['ns']['local'], $altUri);
$params[$i] = Utils::uri2curie($altUri);
}
} }
if(is_dir($auxModelFile) && is_file($auxViewFile)){
return $localUri;// $qArr[0]; for($i=0;$i<sizeof($params);$i++){
$auxPrefix = Utils::getPrefix($params[$i]);
if($auxPrefix['ns'] != NULL){
$prefixHeader[] = $auxPrefix;
}
$args["arg".$i]=$params[$i];
} }
$results['params'] = $params;
return FALSE; $lodspk = $conf['view']['standard'];
$lodspk['type'] = $modelFile;
$lodspk['root'] = $conf['root'];
$lodspk['home'] = $conf['basedir'];
$lodspk['this']['value'] = $uri;
$lodspk['this']['curie'] = Utils::uri2curie($uri);
$lodspk['this']['contentType'] = $acceptContentType;
$lodspk['model']['directory'] = $conf['model']['directory'];
$lodspk['view']['directory'] = $conf['view']['directory'];
$lodspk['ns'] = $conf['ns'];
$lodspk['endpoint'] = $conf['endpoint'];
$lodspk['type'] = $modelFile;
$lodspk['header'] = $prefixHeader;
$lodspk['args'] = $args;
$lodspk['module'] = 'service';
$lodspk['add_mirrored_uris'] = false;
$lodspk['baseUrl'] = $conf['basedir'];
$lodspk['this']['value'] = $uri;
$lodspk['this']['contentType'] = $acceptContentType;
$lodspk['view']['directory'] = $conf['home'].$conf['view']['directory'].$conf['service']['prefix'].$f.'/';
$lodspk['model']['directory'] = $conf['home'].$conf['model']['directory'];
if($viewFile == null){
$lodspk['transform_select_query'] = true;
}
chdir($conf['model']['directory']);
$first = array();
Utils::queryFile($modelFile, $endpoints['local'], $results, $first);
chdir($conf['home']);
$results = Utils::internalize($results);
if(is_array($results)){
$results = Convert::array_to_object($results);
}
//Need to redefine viewFile as 'local' i.e., inside service.foo/ so I can load files with the relative path correctly
$viewFile = $extension.".template";
Utils::processDocument($viewFile, $lodspk, $results);
}catch (Exception $ex){
echo $ex->getMessage();
trigger_error($ex->getMessage(), E_ERROR);
Utils::send500($uri);
} }
exit(0);
public function execute($service){ }
global $conf;
global $localUri;
global $uri; protected function getFunction($uri){
global $acceptContentType; global $conf;
global $endpoints; $count = 1;
global $lodspk; $prefixUri = $conf['basedir'];
require_once($conf['home'].$conf['service']['class']); $aux = str_replace($prefixUri, '', $uri, $count);
$context = array(); $functionAndParams = explode('/', $aux);
$context['contentType'] = $acceptContentType; return $functionAndParams[0];
$context['endpoints'] = $endpoints; }
$sp = new SpecialFunction();
$sp->execute($localUri, $context); protected function getParams($uri){
exit(0); global $conf;
$count = 1;
$prefixUri = $conf['basedir'];
$functionAndParams = explode('/', str_replace($prefixUri, '', $uri, $count));
if(sizeof($functionAndParams) > 1){
array_shift($functionAndParams);
return $functionAndParams;
}else{
return array(null);
} }
}
} }
?> ?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment