get_services.php 2.27 KB
<?php
include(realpath(dirname(__FILE__) . "/config.php"));
include(CLASSPATH . "EpnTapMgr.php");
$EpnTapMgr = new EpnTapMgr;

$registryURL = "http://dc.zah.uni-heidelberg.de/tap";
// $registryURL = "http://gavo.aip.de/tap";
// $registryURL = "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";

$services = $EpnTapMgr->request($registryURL, $getServicesQuery);

$services_info = array();

foreach($services as $service) {
	$service_id = str_replace(['ivo://', '.epn_core'], '', $service['ivoid'] . '/' . $service['table_name']);

	$query = "SELECT dataproduct_type as dp_type,
		target_class as t_class,
		target_name as t_name,
		COUNT(granule_uid) as nb_res,
		MIN(time_min) as time_min,
		MAX(time_max) as time_max
		FROM " . $service['table_name'] . " GROUP BY dp_type, t_class, t_name";

	$result = $EpnTapMgr->request($service['access_url'], $query);
	if(! array_key_exists("error", $result)) {
		$service['content'] = array_map('format_result', $result);
		// TODO: expected format: ["pr", "planet", "Mars", "spicam", 1232, "13/01/2004", "10/04/2006"]
	}
	$services_info[$service_id] = $service;
}

$servicesJsonFile = fopen(EpnTapDataPath . "new_services.json", "w");
fwrite($servicesJsonFile, json_encode(array('services' => $services_info), JSON_PARTIAL_OUTPUT_ON_ERROR));
fclose($servicesJsonFile);

$json_error = json_last_error();
if($json_error != 0) {
	print("json error: " . $json_error);
}

function format_result($result) {
	global $EpnTapMgr;
	return Array($result['dp_type'], $result['t_class'], $result['t_name'], $result['nb_res'], $EpnTapMgr->JDTodate($result['time_min']), $EpnTapMgr->JDTodate($result['time_max']));
}
?>