Skip to content
Snippets Groups Projects
uriModule.php 5.15 KiB
Newer Older
alangrafu's avatar
alangrafu committed
<?
require_once('abstractModule.php');
class UriModule extends abstractModule{
  //Uri module
  
  public function match($uri){
  	global $conf;
  	global $localUri;
  	global $uri;
  	global $acceptContentType;
  	global $endpoints;
alangrafu's avatar
alangrafu committed
  	
  	if($conf['disableComponents'] == true){
  	  return FALSE;
  	}
alangrafu's avatar
alangrafu committed
  	require_once('classes/MetaDb.php');
  	$metaDb = new MetaDb($conf['metadata']['db']['location']);
  	$pair = Queries::getMetadata($localUri, $acceptContentType, $metaDb);
  	if($pair == NULL){ // Original URI is not in metadata
  	  if(Queries::uriExist($uri, $endpoints['local'])){
  	  	$page = Queries::createPage($uri, $localUri, $acceptContentType, $metaDb);
  	  	if($page == NULL){
  	  	  Utils::send500("Can't write sqlite database.");
  	  	}
  	  	Utils::send303($page, $acceptContentType);
  	  	exit(0);
  	  }else{
  	  	return false; //Utils::send404($uri);
  	  }
  	}
  	$extension = Utils::getExtension($pair[2]); 
  	$curie = Utils::uri2curie($pair[0]);
  	list($modelFile, $viewFile) = $this->getModelandView($curie, $extension);
alangrafu's avatar
alangrafu committed
  	if($modelFile == NULL){
  	  return FALSE;
  	}
  	$result = array( 'res' => $pair[0],
  	  					 'page' => $pair[1], 
  	  					 'format' => $pair[2], 
  	  					 'modelFile' => $modelFile, 
  	  					 'viewFile' => $viewFile);

  	return $result;
alangrafu's avatar
alangrafu committed
  }
  
  public function execute($p){
alangrafu's avatar
alangrafu committed
  	global $conf;
  	global $localUri;
  	global $uri;
  	global $acceptContentType;
  	global $endpoints;
alangrafu's avatar
alangrafu committed
  	global $results;
  	global $firstResults;
  	$res = $p['res'];
  	$page = $p['page'];
  	$format = $p['format'];
  	$modelFile = $p['modelFile'];
  	$viewFile = $p['viewFile'];
alangrafu's avatar
alangrafu committed
  	$uri = $res;
  	$curie = Utils::uri2curie($res);
alangrafu's avatar
alangrafu committed
  	//If resource is not the page, send a 303 to the document
  	if($res == $localUri){
  	  Utils::send303($page, $acceptContentType);
  	}
  	
  	$uri = $res;
  	if($conf['mirror_external_uris'] != false){
alangrafu's avatar
alangrafu committed
  	  $localUri = preg_replace("|^".$conf['ns']['local']."|", $conf['basedir'], $res);
  	}
  	
  	$extension = Utils::getExtension($format); 
  	
  	/*Redefine Content type based on the
  	* dcterms:format for this page
  	*/
  	$acceptContentType = $format;
alangrafu's avatar
alangrafu committed
  	$curie = Utils::uri2curie($uri);
  	if($modelFile == NULL){
  	  return;
  	}
  	
  	//$lodspk = $conf['view']['standard'];

  	$lodspk['type'] = $modelFile;
alvaro's avatar
alvaro committed
  	  	$lodspk['home'] = $conf['basedir'];

  	$lodspk['module'] = 'uri';
  	$lodspk['add_mirrored_uris'] = true;
  	$lodspk['this']['value'] = $uri;
  	$lodspk['this']['curie'] = Utils::uri2curie($uri);
alvaro's avatar
alvaro committed
  	$lodspk['local']['value'] = $localUri;
  	$lodspk['local']['curie'] = Utils::uri2curie($localUri);
alvaro's avatar
alvaro committed
  	$lodspk['contentType'] = $acceptContentType;
alvaro's avatar
alvaro committed
  	$lodspk['model'] = $conf['model']['directory'];
  	$lodspk['view'] = $conf['view']['directory'];
  	$lodspk['ns'] = $conf['ns'];
alangrafu's avatar
alangrafu committed
  	
  	
alvaro's avatar
alvaro committed
  	//chdir($conf['home'].$conf['model']['directory']);
  	Utils::queryFile($modelFile, $endpoints['local'], $results, $firstResults);
  	if(!$lodspk['resultRdf']){
  	  $results = Utils::internalize($results); 
alvaro's avatar
alvaro committed
  	  $firstAux = Utils::getfirstResults($results);
  	  
  	  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);
alangrafu's avatar
alangrafu committed
  	}else{
  	  $resultsObj = $results;
  	}
alvaro's avatar
alvaro committed
  	//chdir($conf['home']);
  	if($conf['debug']){
  	  trigger_error("Using template ".$viewFile, E_USER_NOTICE);
  	  echo("TEMPLATE: ".$viewFile."\n\n");
  	}
  	Utils::processDocument($viewFile, $lodspk, $resultsObj);
alangrafu's avatar
alangrafu committed
  	
  }
  
  private static function getModelandView($uri, $extension){  	
  	global $conf;
  	global $lodspk;
alvaro's avatar
alvaro committed
  	$auxViewFile  = $conf['view']['directory'].'/'.$conf['uri']['prefix'].'/'.$uri.'/'.$extension.'.template';
  	$auxModelFile = $conf['model']['directory'].'/'.$conf['uri']['prefix'].'/'.$uri.'/'.$extension.'.queries';
  	if(file_exists($auxModelFile)){
 	  //Model exists
alvaro's avatar
alvaro committed
  	  $modelFile = $auxModelFile;//$conf['uri']['prefix'].$uri.'/'.$extension.'.queries';
  	  if(file_exists($auxViewFile) ){
  	  	//View exists, everything is fine
alvaro's avatar
alvaro committed
  	  	$viewFile = $conf['model']['directory'].'/'.$conf['uri']['prefix'].'/'.$uri.'/'.$extension.'.template';
  	  }elseif($extension != 'html'){
  	  	//View doesn't exists (and is not HTML)
  	  	$viewFile = null;  	  	
alvaro's avatar
alvaro committed
  	  }else{
  	  	//No HTML representation as fallback, then not recognized by URI module 
  	  	return array(null, null);
  	  }
  	  return array($modelFile, $viewFile);
alvaro's avatar
alvaro committed
  	}elseif(file_exists($conf['model']['directory'].'/'.$conf['uri']['prefix'].'/'.$uri.'/queries')){
  	  $modelFile = $conf['model']['directory'].'/'.$conf['uri']['prefix'].'/'.$uri.'/queries';//$conf['uri']['prefix'].$uri.'/html.queries';
	  if(file_exists($auxViewFile) ){
  	  	//View exists, everything is fine
alvaro's avatar
alvaro committed
  	  	$viewFile = $conf['model']['directory'].'/'.$conf['uri']['prefix'].'/'.$uri.'/'.$extension.'.template';
  	  }elseif($extension != 'html'){
  	  	//View doesn't exists (and is not HTML)
  	  	$lodspk['transform_select_query'] = true;
  	  	$viewFile = null;  	  	
  	  }
alangrafu's avatar
alangrafu committed
  	  return array($modelFile, $viewFile);
  	}
alangrafu's avatar
alangrafu committed
  	return array(NULL, NULL);
  }
  
}
?>