epntap.php 2.86 KB
<?php

include(realpath(dirname(__FILE__) . "/config.php"));
// include(CLASSPATH . "EpnTapMgr.php");
include(CLASSPATH . "VOTableMgr.php");

$action = preg_replace("/[^a-z_]+/", "", filter_var($_GET['action'], FILTER_SANITIZE_STRING));

switch ($action) {
	case 'resolver':
		echo json_encode(resolver(filter_var($_GET['input'], FILTER_SANITIZE_URL)));
		break;
	case 'get_services':
		echo json_encode(getServices());
		break;

	default:
		break;
}

function resolver($input) {
	$resolver_url = "http://voparis-registry.obspm.fr/ssodnet/1/autocomplete?q=%22$input%22";
	$result = json_decode(file_get_contents($resolver_url), true);

	$targets = array();
	foreach($result['hits'] as $e) {
		$aliases = '<li>' . join('</li><li>', $e['aliases']) . '</li>';
		$target = array('name' => $e['name'], 'type' => $e['type'], 'parent' => $e['parent'], 'aliases' => $aliases);
		array_push($targets, $target);
	}
	return $targets;
}

/* Return the list of available services by querying some usual registries. */
function getServices() {
	$registriesURL = ["http://registry.euro-vo.org/regtap/tap", "http://dc.zah.uni-heidelberg.de/tap", "http://gavo.aip.de/tap", "http://reg.g-vo.org/tap"];
	$columns = ['short_name', 'res_title', 'ivoid', 'access_url', 'table_name', 'content_type', 'creator_seq', 'content_level', 'reference_url', 'created', 'updated'];
	$getServicesQuery = "SELECT DISTINCT " . implode(', ', $columns) . " FROM rr.resource
			NATURAL JOIN rr.res_schema NATURAL JOIN rr.res_table NATURAL JOIN rr.interface NATURAL JOIN rr.res_detail NATURAL JOIN rr.capability
			WHERE standard_id='ivo://ivoa.net/std/tap' AND intf_type='vs:paramhttp' AND detail_xpath='/capability/dataModel/@ivo-id'
			AND 1=ivo_nocasematch(detail_value, 'ivo://vopdc.obspm/std/EpnCore%') AND table_name LIKE '%.epn_core' ORDER BY short_name, table_name";

	for($i=0; $i<count($registriesURL); $i++) {
		$services = request($registriesURL[$i], $getServicesQuery);
		if(! array_key_exists("error", $services)) {
			for($j=0; $j<count($services); $j++) {
				$services[$j]['id'] = generateServiceId($services[$j]);
				$services[$j]['nb_results'] = -1;
			}
			return $services;
		} else if($i === count($registriesURL)-1) {
			error_log("Can not access any of these services : " . implode(', ', $registriesURL) . ".");
			return;
		}
	}
}

/* Generate a unique service identifier from the service ivoid and the table name. */
function generateServiceId($service) {
	return str_replace(['ivo://', '.epn_core'], '', $service['ivoid'] . '/' . $service['table_name']);
}

function request($access_url, $query) {
	$votMgr = new VOTableMgr;
	$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
	$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode(preg_replace('/\s+/', ' ', $query));

	$votMgr->load($url);

	$result = $votMgr->parseStream();
	return $votMgr->getVotableError() ? array('error' => $votMgr->getVotableError()) : $result;
}
?>