Blame view

php/epntap.php 6.89 KB
8a0f1e9e   Nathanael Jourdane   Add autocompletio...
1
<?php
68664dca   Nathanael Jourdane   make epntap php f...
2

49401187   Nathanael Jourdane   epntap: Do not us...
3
4
5
6
include(realpath(dirname(__FILE__) . "/config.php"));
// include(CLASSPATH . "EpnTapMgr.php");
include(CLASSPATH . "VOTableMgr.php");

8c2fa14d   Nathanael Jourdane   Update services g...
7
$action = preg_replace("/[^a-zA-Z]+/", "", filter_var($_GET['action'], FILTER_SANITIZE_STRING));
68664dca   Nathanael Jourdane   make epntap php f...
8
9
10

switch ($action) {
	case 'resolver':
a8a12768   Nathanael Jourdane   Create the grid m...
11
		$response = json_encode(resolver());
68664dca   Nathanael Jourdane   make epntap php f...
12
		break;
8c2fa14d   Nathanael Jourdane   Update services g...
13
	case 'getServices':
a8a12768   Nathanael Jourdane   Create the grid m...
14
		$response = json_encode(getServices());
68664dca   Nathanael Jourdane   make epntap php f...
15
		break;
8c2fa14d   Nathanael Jourdane   Update services g...
16
	case 'getNbResults':
a8a12768   Nathanael Jourdane   Create the grid m...
17
		$response = getNbResults();
8c2fa14d   Nathanael Jourdane   Update services g...
18
		break;
d6674d39   Nathanael Jourdane   Add error and inf...
19
	case 'getGranules':
a8a12768   Nathanael Jourdane   Create the grid m...
20
		$response = json_encode(getGranules());
68664dca   Nathanael Jourdane   make epntap php f...
21
		break;
d6674d39   Nathanael Jourdane   Add error and inf...
22
	default:
a8a12768   Nathanael Jourdane   Create the grid m...
23
24
		$response = 'unknown action';
		break;
8a0f1e9e   Nathanael Jourdane   Add autocompletio...
25
}
a8a12768   Nathanael Jourdane   Create the grid m...
26
27
// error_log('epntap response: ' . $response);
echo $response;
68664dca   Nathanael Jourdane   make epntap php f...
28

8c2fa14d   Nathanael Jourdane   Update services g...
29
30
function resolver() {
	$input = filter_var($_GET['input'], FILTER_SANITIZE_URL);
68664dca   Nathanael Jourdane   make epntap php f...
31
32
33
	$resolver_url = "http://voparis-registry.obspm.fr/ssodnet/1/autocomplete?q=%22$input%22";
	$result = json_decode(file_get_contents($resolver_url), true);

49401187   Nathanael Jourdane   epntap: Do not us...
34
	$targets = array();
68664dca   Nathanael Jourdane   make epntap php f...
35
36
37
	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);
49401187   Nathanael Jourdane   epntap: Do not us...
38
39
40
41
42
43
44
45
46
		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'];
a8a12768   Nathanael Jourdane   Create the grid m...
47
	$query = "SELECT DISTINCT " . implode(', ', $columns) . " FROM rr.resource
49401187   Nathanael Jourdane   epntap: Do not us...
48
49
50
			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";
a8a12768   Nathanael Jourdane   Create the grid m...
51
	// error_log('getServices query: ' . $query);
49401187   Nathanael Jourdane   epntap: Do not us...
52
53

	for($i=0; $i<count($registriesURL); $i++) {
a8a12768   Nathanael Jourdane   Create the grid m...
54
		$services = request($registriesURL[$i], $query);
49401187   Nathanael Jourdane   epntap: Do not us...
55
56
57
58
		if(! array_key_exists("error", $services)) {
			for($j=0; $j<count($services); $j++) {
				$services[$j]['id'] = generateServiceId($services[$j]);
				$services[$j]['nb_results'] = -1;
d6674d39   Nathanael Jourdane   Add error and inf...
59
				$services[$j]['info'] = 'Please make a query first.';
1813a0ba   Nathanael Jourdane   Remove AMDA to th...
60
61
62
63
				if($services[$j]['id'] == 'cdpp/amda/amdadb') {
					array_splice($services, $j, 1);
					$j-=1;
				}
49401187   Nathanael Jourdane   epntap: Do not us...
64
65
			}
			return $services;
173951f6   Nathanael Jourdane   Display the getSe...
66
67
68
69
70
71
		} else {
			error_log('getServices error: ' . $services['error']);
			if($i === count($registriesURL)-1) {
				error_log("Can not access any of these registries : " . implode(', ', $registriesURL) . ", check the internet connexion.");
				return;
			}
49401187   Nathanael Jourdane   epntap: Do not us...
72
		}
68664dca   Nathanael Jourdane   make epntap php f...
73
	}
68664dca   Nathanael Jourdane   make epntap php f...
74
75
}

8c2fa14d   Nathanael Jourdane   Update services g...
76
77
78
79
80
81
82
83
84
function getNbResults() {
	$url = filter_var($_GET['url'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$tableName = filter_var($_GET['tableName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$targetName = filter_var($_GET['targetName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$productTypes = filter_var($_GET['productTypes'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$timeMin = filter_var($_GET['timeMin'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$timeMax = filter_var($_GET['timeMax'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);

	$query = "SELECT COUNT(*) AS nb_rows FROM $tableName" . createFilter($targetName, $productTypes, $timeMin, $timeMax);
a8a12768   Nathanael Jourdane   Create the grid m...
85
	// error_log('getNbResults query: ' . $query);
8c2fa14d   Nathanael Jourdane   Update services g...
86
87
88
89
90
	$result = request($url, $query);
	if(count($result) < 1) {
		return 'Too few returned raws.';
	} else if(count($result) > 1) {
		return 'Too many returned raws.';
87467502   Nathanael Jourdane   Replace page butt...
91
92
	} else if(!array_key_exists(0, $result)) {
		return 'cant find raw item 0';
8c2fa14d   Nathanael Jourdane   Update services g...
93
94
95
96
97
98
99
100
101
	} else if(is_null($result[0])) {
		return 'The returned raw is null.';
	} else if(!array_key_exists("nb_rows", $result[0])) {
		return 'cant find nb_rows.';
	} else if(!is_numeric($result[0]['nb_rows'])) {
		return 'The returned value is not a number.';
	} else {
		return (int)($result[0]['nb_rows']);
	}
49401187   Nathanael Jourdane   epntap: Do not us...
102
103
}

d6674d39   Nathanael Jourdane   Add error and inf...
104
function getGranules() {
a8a12768   Nathanael Jourdane   Create the grid m...
105
	// error_log('getGranules GET: ' . json_encode($_GET));
d6674d39   Nathanael Jourdane   Add error and inf...
106
107
108
109
110
111
	$url = filter_var($_GET['url'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$tableName = filter_var($_GET['tableName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$targetName = filter_var($_GET['targetName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$productTypes = filter_var($_GET['productTypes'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$timeMin = filter_var($_GET['timeMin'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$timeMax = filter_var($_GET['timeMax'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
87467502   Nathanael Jourdane   Replace page butt...
112
113
114
	$start = filter_var($_GET['start'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$limit = filter_var($_GET['limit'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
	$nbRes = filter_var($_GET['nbRes'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
d6674d39   Nathanael Jourdane   Add error and inf...
115

9b2dbf25   Nathanael Jourdane   Add limits to the...
116
	$filter = createFilter($targetName, $productTypes, $timeMin, $timeMax);
a8a12768   Nathanael Jourdane   Create the grid m...
117
118
	$query = "SELECT TOP $limit * FROM $tableName $filter OFFSET $start";
	// error_log('getGranules query: ' . $query);
87467502   Nathanael Jourdane   Replace page butt...
119
	$rows = request($url, $query);
a8a12768   Nathanael Jourdane   Create the grid m...
120
121
122
123
124
125
126
127
128
129

	$fields = array();
	$columns = array();
	foreach($rows[0] as $key => $value) {
		$fields[] = ['name' => $key, 'type' => 'string'];
		$columns[] = ['text' => ucfirst(str_replace('_', ' ', $key)), 'dataIndex' => $key];
	}

	$metadata = ['fields' => $fields, 'columns' => $columns, 'root' => 'data'];
	return ['data' => $rows, 'total' => $nbRes, 'metaData' => $metadata];
d6674d39   Nathanael Jourdane   Add error and inf...
130
131
}

8c2fa14d   Nathanael Jourdane   Update services g...
132
133
// ----- utils -----

49401187   Nathanael Jourdane   epntap: Do not us...
134
135
136
function request($access_url, $query) {
	$votMgr = new VOTableMgr;
	$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
8c2fa14d   Nathanael Jourdane   Update services g...
137
	$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode(preg_replace('/\s+/', ' ', $query)); // remove also multiple whitespaces
49401187   Nathanael Jourdane   epntap: Do not us...
138
139

	$votMgr->load($url);
49401187   Nathanael Jourdane   epntap: Do not us...
140
141
142
	$result = $votMgr->parseStream();
	return $votMgr->getVotableError() ? array('error' => $votMgr->getVotableError()) : $result;
}
8c2fa14d   Nathanael Jourdane   Update services g...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

function createFilter($targetName, $productTypes, $timeMin, $timeMax) {
	$filter = array();
	if($targetName) {
		array_push($filter, "target_name = '$targetName'");
	}
	if($productTypes) {
		array_push($filter, "dataproduct_type IN ('" . join("', '", explode(';', $productTypes)) . "')");
	}
	if($timeMin) {
		array_push($filter, "time_min >= " . dateToJD($timeMin));
	}
	if($timeMax) {
		array_push($filter, "time_max <= " . dateToJD($timeMax));
	}
	return (count($filter) > 0 ? ' WHERE ' . join(' AND ', $filter) : '');
}

/* 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 dateToJD($gregorian_date) {
	list($day, $month, $year, $hours, $minutes, $seconds) = preg_split('/[\s\/:]+/', $gregorian_date);
	return GregorianToJD($month, $day, $year) + $hours/24 + $minutes/(24*60) + $seconds/(24*60*60);
}
8a0f1e9e   Nathanael Jourdane   Add autocompletio...
170
?>