Skip to content
Snippets Groups Projects
serviceModule.php 9.46 KiB
Newer Older
alvaro's avatar
alvaro committed
<?php

use uib\ub\loadspeakr\Convert;
use uib\ub\loadspeakr\HTTPStatus;
use uib\ub\loadspeakr\Logging;
use uib\ub\loadspeakr\Utils;

alangrafu's avatar
alangrafu committed
require_once('abstractModule.php');

class ServiceModule extends abstractModule {
alangrafu's avatar
alangrafu committed
  //Service module
  
  public function match($uri){
alangrafu's avatar
alangrafu committed
  	global $conf; 
alangrafu's avatar
alangrafu committed
  	global $acceptContentType; 
    global $localUri;
    
    $lodspk['model'] = null;
    $lodspk['view'] = null;
alangrafu's avatar
alangrafu committed
  	$q = preg_replace('|^'.$conf['basedir'].'|', '', $localUri);
alangrafu's avatar
alangrafu committed
 	$qArr = explode('/', $q);
alangrafu's avatar
alangrafu committed
  	if(sizeof($qArr)==0){
  	  return FALSE;
  	}
alvaro's avatar
alvaro committed
  	$extension = Utils::getExtension($acceptContentType);
  	$viewFile  = null;
  	$tokens = $qArr;
alvaro's avatar
alvaro committed
  	$arguments = array();
  	while(sizeof($tokens) > 0){
  	  $serviceName = join("%2F", $tokens);
  	  //Use .extension at the end of the service to force a particular content type
  	  $lastSegment = end($tokens);
  	  if(strpos($lastSegment, '.')>0){
  	    $aux = explode(".", $lastSegment);
  	    if(sizeof($aux)>1){
  	      $requestExtension = array_pop($aux);
  	      $contentTypes = $conf['http_accept'][$requestExtension];
  	      if($contentTypes != null){
  	        $acceptContentType = $contentTypes[0];
  	        $extension = $requestExtension;
  	      }
  	    }
  	    $serviceName = join(".",$aux);

      //checking default components  	  
alvaro's avatar
alvaro committed
  	  if(file_exists($conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/scaffold.ttl')){
  	    $subDir = $this->readScaffold($conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/scaffold.ttl', join("/", $arguments));
  	    $subDir.= '/';
  	    $lodspk['model'] = $conf['home'].$conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/'.$subDir;
alvaro's avatar
alvaro committed
  	    $lodspk['view'] = $conf['view']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/'.$subDir.$extension.'.template';  	    
  	  }elseif(file_exists($conf['home'].$conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName)){  	    
  	    $lodspk['model'] = $conf['home'].$conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/';
  	    $lodspk['view'] = $conf['home'].$conf['view']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/'.$extension.'.template';
  	  }else{
  	    if(is_null($lodspk['model']) && is_null($lodspk['view'])){
  	      //checking other components
  	      if(!empty($conf['components']['services']) && count($conf['components']['services'])>0){
alvaro's avatar
alvaro committed
  	        foreach($conf['components']['services'] as $service){
  	          $serviceArray = explode("/", $service);
  	          if($serviceName == end($serviceArray)){
  	            array_pop($serviceArray);
  	            $conf['service']['prefix'] = array_pop($serviceArray);
  	            $conf['model']['directory'] = join("/", $serviceArray);
  	            $conf['view']['directory'] = $conf['model']['directory'];
  	            if(file_exists($conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/scaffold.ttl')){
  	              $subDir = $this->readScaffold($conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/scaffold.ttl', join("/", $arguments));
  	              $subDir.= '/';
  	              $lodspk['model'] = $conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/'.$subDir;
  	              $lodspk['view'] = $conf['view']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/'.$subDir.$extension.'.template';  	    
  	            }elseif(file_exists($conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName)){ 
  	              $lodspk['model'] = $conf['model']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/';
  	              $lodspk['view'] = $conf['view']['directory'].'/'.$conf['service']['prefix'].'/'.$serviceName.'/'.$extension.'.template';
  	            }
alvaro's avatar
alvaro committed
  	  }
  	  $lodspk['serviceName'] = join("/", $tokens);
  	  $lodspk['componentName'] = $lodspk['serviceName'];
  	  $modelFile = $lodspk['model'].$extension.'.queries';
  	  if(file_exists($lodspk['model'].$extension.'.queries')){
  	    if(!file_exists($lodspk['view'])){
  	      $viewFile = null;
  	    }else{
  	      $viewFile = $lodspk['view'];
  	    }
  	    return array($modelFile, $viewFile);
  	  }elseif(file_exists($lodspk['model'].'queries')){
  	    $modelFile = $lodspk['model'].'queries';
  	    if(!file_exists($lodspk['view'])){
  	      $lodspk['resultRdf'] = true;
  	      $viewFile = null;
  	    }else{
  	      $viewFile = $lodspk['view'];
  	    }
  	    return array($modelFile, $viewFile);
  	  }elseif(file_exists($lodspk['model'])){
  	    HTTPStatus::send406($uri);
alvaro's avatar
alvaro committed
        return null;
alvaro's avatar
alvaro committed
  	  array_unshift($arguments, array_pop($tokens));
alangrafu's avatar
alangrafu committed
  	}
  public function execute($service){
  	global $conf;
  	global $localUri;
  	global $uri;
  	global $acceptContentType;
  	global $endpoints;
  	global $lodspk;
  	global $firstResults;
  	$context = array();
  	$context['contentType'] = $acceptContentType;
  	$context['endpoints'] = $endpoints;
  	//$f = $this->getFunction($localUri);
  	$params = $this->getParams($localUri);
  	//$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'] != false){
  	  	  $altUri = Utils::curie2uri($params[$i]);
  	  	  $altUri = preg_replace("|^".$conf['basedir']."|", $conf['ns']['local'], $altUri);
  	  	  $params[$i] = Utils::uri2curie($altUri);
  	  	}
alvaro's avatar
alvaro committed
  	  $segmentConnector = "";
  	  for($i=0;$i<sizeof($params);$i++){  
  	  	Utils::curie2uri($params[$i]);
  	  	//echo $params[$i]." ".Utils::curie2uri($params[$i]);exit(0);
  	  	$auxPrefix = Utils::getPrefix($params[$i]);
  	  	if($auxPrefix['ns'] != NULL){
  	  	  $prefixHeader[] = $auxPrefix;
  	  	}
  	  	$args["arg".$i]=$params[$i];
alvaro's avatar
alvaro committed
  	  	$args["all"] .= $segmentConnector.$params[$i];
  	  	if($segmentConnector == ""){
  	  	  $segmentConnector = "/";
  	  	}
alvaro's avatar
alvaro committed
  	  $lodspk['home'] = $conf['basedir'];
  	  $lodspk['baseUrl'] = $conf['basedir'];
  	  $lodspk['module'] = 'service';
  	  $lodspk['root'] = $conf['root'];
  	  $lodspk['contentType'] = $acceptContentType;
  	  $lodspk['ns'] = $conf['ns'];  	  	
  	  $lodspk['this']['value'] = $uri;
  	  $lodspk['this']['curie'] = Utils::uri2curie($uri);
  	  $lodspk['local']['value'] = $localUri;
  	  $lodspk['local']['curie'] = Utils::uri2curie($localUri);
  	  $lodspk['contentType'] = $acceptContentType;
  	  $lodspk['endpoint'] = $conf['endpoint'];
  	  
  	  $lodspk['type'] = $modelFile;
  	  $lodspk['header'] = $prefixHeader;
  	  $lodspk['args'] = $args;
  	  $lodspk['add_mirrored_uris'] = false;
  	  $lodspk['baseUrl'] = $conf['basedir'];
  	  $lodspk['this']['value'] = $uri;
  	  if($viewFile == null){
  	  	$lodspk['transform_select_query'] = true;
  	  }
alvaro's avatar
alvaro committed
  	//  chdir($lodspk['model']);
  	  Utils::queryFile($modelFile, $endpoints['local'], $results, $firstResults);
      if(!$lodspk['resultRdf']){
      	$results = Utils::internalize($results); 
alvaro's avatar
alvaro committed
      	$firstAux = Utils::getfirstResults($results);
alvaro's avatar
alvaro committed
    //  	chdir($conf['home']);
      	if(is_array($results)){
      	  $resultsObj = Convert::array_to_object($results);
      	}else{
      	  $resultsObj = $results;
      	}
alvaro's avatar
alvaro committed
      	$lodspk['firstResults'] = Convert::array_to_object($firstAux);
      }else{
      	$resultsObj = $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();
  	  if($conf['debug']){
  	    Logging::log($ex->getMessage(), E_ERROR);
  	  }
  	  HTTPStatus::send500($uri);
alangrafu's avatar
alangrafu committed
  }
  
  
  protected function getParams($uri){
  	global $conf;
  	global $lodspk;
  	$count = 1;
  	$prefixUri = $conf['basedir'];
  	$functionAndParams = explode('/', str_replace($prefixUri.$lodspk['serviceName'], '', $uri, $count));
  	if(sizeof($functionAndParams) > 1){
  	  array_shift($functionAndParams);
  	  return $functionAndParams;
  	}else{
  	  return array(null);
  	}
  }
alvaro's avatar
alvaro committed

  protected function readScaffold($scaffold, $serviceArgs){
    global $conf;
    require_once __DIR__ . '/../../vendor/semsol/arc2/ARC2.php';
alvaro's avatar
alvaro committed
    $parser = ARC2::getTurtleParser();
    $parser->parse($scaffold);
    $triples = $parser->getTriples();
    $aux=Utils::filterTriples($triples, array(null, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://lodspeakr.org/vocab/ScaffoldedService"));
    $scaffoldUri = $aux[0][0];
    $aux=Utils::filterTriples($triples, array($scaffoldUri, "http://lodspeakr.org/vocab/scaffold", null));
    foreach($aux as $r){
      $patterns = Utils::filterTriples($triples, array($r[2], "http://lodspeakr.org/vocab/uriPattern", null));
      $pattern = stripcslashes($patterns[0][2]);
      if(preg_match("|$pattern|", $serviceArgs) > 0){
//        echo "match ! \n ".$pattern."\n";
        $patternDir = Utils::filterTriples($triples, array($r[2], "http://lodspeakr.org/vocab/subComponent", null));
        return $patternDir[0][2];
      }
    }
//        exit(0);
    return "";
  }  
alangrafu's avatar
alangrafu committed
}