Blame view

php/classes/EpnTapMgr.php 4.7 KB
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
1
2
<?php
/**  @class EpnTapMgr
210a3164   Nathanael Jourdane   Store actual func...
3
*	@brief Manager to communicates with EPN-TAP services.
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
4
5
6
*/

class EpnTapMgr {
f534c4a8   Nathanael Jourdane   Make the VOTable ...
7
8
	private $logEpnTap;

210a3164   Nathanael Jourdane   Store actual func...
9
	private function dateToJD($gregorian_date) {
2b6b4308   Nathanael Jourdane   Make the epntap m...
10
		list($day, $month, $year, $hours, $minutes, $seconds) = preg_split('/[\s\/:]+/', $gregorian_date);
67a4e0da   Nathanael Jourdane   Add a function to...
11
		return GregorianToJD($month, $day, $year) + $hours/24 + $minutes/(24*60) + $seconds/(24*60*60);
210a3164   Nathanael Jourdane   Store actual func...
12
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
13

210a3164   Nathanael Jourdane   Store actual func...
14
	public function JDTodate($jd) {
e461c01f   Nathanael Jourdane   Improve VOTable p...
15
16
17
18
		if(!is_numeric($jd)) {
			return '';
		}
		list($month, $day, $year) = preg_split('/[\s\/:]+/', JDToGregorian(intval($jd)));
67a4e0da   Nathanael Jourdane   Add a function to...
19
20
		$date = sprintf('%02d', $day) . '/' . sprintf('%02d', $month) . '/' . sprintf('%04d', $year);
		return ($date == '00/00/0000') ? '' : $date;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
21
22
	}

47a2829d   Nathanael Jourdane   Fix the paginatio...
23
	public function createFilter($dataproduct_type, $target_name, $time_min, $time_max) {
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
24
		$filter = array();
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
25
26
		if($dataproduct_type)
			array_push($filter, "dataproduct_type = '$dataproduct_type'");
47a2829d   Nathanael Jourdane   Fix the paginatio...
27
28
		if($target_name)
			array_push($filter, "target_name = '$target_name'");
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
29
		if($time_min)
d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
30
			array_push($filter, "time_min >= " . $this->dateToJD($time_min));
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
31
		if($time_max)
d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
32
			array_push($filter, "time_max <= " . $this->dateToJD($time_max));
210a3164   Nathanael Jourdane   Store actual func...
33
		return (count($filter) > 0 ? ' WHERE ' . join(' AND ', $filter) : '');
5de62950   Nathanael Jourdane   Improve the VOTab...
34
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
35

5de62950   Nathanael Jourdane   Improve the VOTab...
36
	public function request($access_url, $query) {
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
37
38
		$votMgr = new VOTableMgr;
		$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
e461c01f   Nathanael Jourdane   Improve VOTable p...
39
		$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode(preg_replace('/\s+/', ' ', $query)); // remove also multiple whitespaces
210a3164   Nathanael Jourdane   Store actual func...
40

8d5016bc   Nathanael Jourdane   bugFix: do not us...
41
42
		$votMgr->load($url);

d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
43
44
		$result = $votMgr->parseStream();
		return $votMgr->getVotableError() ? array('error' => $votMgr->getVotableError()) : $result;
67a4e0da   Nathanael Jourdane   Add a function to...
45
46
	}

47a2829d   Nathanael Jourdane   Fix the paginatio...
47
	/* filter order: product type, target name, time min, time max */
8d5016bc   Nathanael Jourdane   bugFix: do not us...
48
49
50
	public function getGranules($table_name, $access_url, $filter, $limit, $offset) {

		$query = "SELECT TOP {$limit} * FROM {$table_name} " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]) . " OFFSET {$offset}";
67a4e0da   Nathanael Jourdane   Add a function to...
51
		$result = $this->request($access_url, $query);
8d5016bc   Nathanael Jourdane   bugFix: do not us...
52

6df8a37c   Nathanael Jourdane   getGranules only ...
53
54
55
56
57
58
59
		$result_path = EpnTapDataPath . "test_result.json";
		$servicesJsonFile = fopen($result_path, "w");

		if(array_key_exists("error", $result)) {
			return $result;
		}

e4b5c8c0   Nathanael Jourdane   getGranules: refa...
60
		$non_empty_values = Array();
6df8a37c   Nathanael Jourdane   getGranules only ...
61
62
63
64
65
66
		for ($i = 0 ; $i < count($result) ; $i++) {
			$result[$i]['num'] = $i + $offset + 1;
			$result[$i]['time_min'] = $this->JDTodate($result[$i]['time_min']);
			$result[$i]['time_max'] = $this->JDTodate($result[$i]['time_max']);

			foreach($result[$i] as $key => $value) {
e4b5c8c0   Nathanael Jourdane   getGranules: refa...
67
				$non_empty_values[$key] = $non_empty_values[$key] || $value === '' || $value === 'NAN';
6df8a37c   Nathanael Jourdane   getGranules only ...
68
69
70
			}
		}

e4b5c8c0   Nathanael Jourdane   getGranules: refa...
71
72
73
		$non_null_cols = array_keys(array_filter($non_empty_values));
		for ($i = 0 ; $i < count($result) ; $i++) {
			$result[$i] = array_diff_key($result[$i], array_flip($non_null_cols));
210a3164   Nathanael Jourdane   Store actual func...
74
		}
494949bb   Nathanael Jourdane   Remove dataproduc...
75

5de62950   Nathanael Jourdane   Improve the VOTab...
76
		return $result;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
77
78
	}

8e8f453c   Nathanael Jourdane   VOTable parser: r...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
	/* Return the list of available services by querying some usual registries. */
	public 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 = $this->request($registriesURL[$i], $getServicesQuery);
			if(! array_key_exists("error", $services)) {
				for($j=0; $j<count($services); $j++) {
					$services[$j]['id'] = $this->generateServiceId($services[$j]);
				}
				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. */
	public function generateServiceId($service) {
		return str_replace(['ivo://', '.epn_core'], '', $service['ivoid'] . '/' . $service['table_name']);
	}

47a2829d   Nathanael Jourdane   Fix the paginatio...
107
	/* filter order: product type, target name, time min, time max */
67a4e0da   Nathanael Jourdane   Add a function to...
108
	public function getNbRows($table_name, $access_url, $filter) {
8d5016bc   Nathanael Jourdane   bugFix: do not us...
109
110
111
		$query = "SELECT COUNT(*) AS nb_rows FROM {$table_name} " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]);
		$result = $this->request($access_url, $query)[0]['nb_rows'];
		return $result;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
112
	}
67a4e0da   Nathanael Jourdane   Add a function to...
113

3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
114
115
}
?>