Blame view

php/classes/EpnTapMgr.php 3.23 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)));
b37c65e6   Nathanael Jourdane   EpnTapMgr: Displa...
19
20
		$date = sprintf('%04d', $year) . '/' . sprintf('%02d', $month) . '/' . sprintf('%02d', $day);
		return ($date == '0000/00/00') ? '' : $date;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
21
22
	}

30281591   Nathanael Jourdane   [WIP] Send reques...
23
	public function createFilter($targetName, $productTypes, $timeMin, $timeMax) {
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
24
		$filter = array();
30281591   Nathanael Jourdane   [WIP] Send reques...
25
26
27
28
29
30
31
32
33
34
35
36
		if($targetName) {
			array_push($filter, "target_name = '$targetName'");
		}
		if($productTypes) {
			array_push($filter, "dataproduct_type IN ('" . join("', '", $productTypes) . "')");
		}
		if($timeMin) {
			array_push($filter, "time_min >= " . $this->dateToJD($timeMin));
		}
		if($timeMax) {
			array_push($filter, "time_max <= " . $this->dateToJD($timeMax));
		}
210a3164   Nathanael Jourdane   Store actual func...
37
		return (count($filter) > 0 ? ' WHERE ' . join(' AND ', $filter) : '');
5de62950   Nathanael Jourdane   Improve the VOTab...
38
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
39

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

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

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

47a2829d   Nathanael Jourdane   Fix the paginatio...
51
	/* filter order: product type, target name, time min, time max */
8d5016bc   Nathanael Jourdane   bugFix: do not us...
52
53
54
	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...
55
		$result = $this->request($access_url, $query);
8d5016bc   Nathanael Jourdane   bugFix: do not us...
56

6df8a37c   Nathanael Jourdane   getGranules only ...
57
58
59
60
61
62
63
		$result_path = EpnTapDataPath . "test_result.json";
		$servicesJsonFile = fopen($result_path, "w");

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

e4b5c8c0   Nathanael Jourdane   getGranules: refa...
64
		$non_empty_values = Array();
6df8a37c   Nathanael Jourdane   getGranules only ...
65
66
67
68
69
70
		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) {
cef7375b   Nathanael Jourdane   EpnTapMgr: Fix wa...
71
				$non_empty_values[$key] = (array_key_exists($key, $non_empty_values) ? $non_empty_values[$key] : False) || $value === '' || $value === 'NAN';
6df8a37c   Nathanael Jourdane   getGranules only ...
72
73
74
			}
		}

e4b5c8c0   Nathanael Jourdane   getGranules: refa...
75
76
77
		$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...
78
		}
cef7375b   Nathanael Jourdane   EpnTapMgr: Fix wa...
79
		// error_log('Query result: ' . json_encode($result));
5de62950   Nathanael Jourdane   Improve the VOTab...
80
		return $result;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
81
82
	}

30281591   Nathanael Jourdane   [WIP] Send reques...
83
84
85
86
87
	public function getNbResults($url, $tableName, $targetName, $productTypes, $timeMin, $timeMax) {
		$query = "SELECT COUNT(*) AS nb_rows FROM $tableName" . $this->createFilter($targetName, $productTypes, $timeMin, $timeMax);
		// error_log('Query: ' . $query);
		$result = $this->request($url, $query)[0]['nb_rows'];
		// error_log('Query result: ' . json_encode($result));
8d5016bc   Nathanael Jourdane   bugFix: do not us...
88
		return $result;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
89
	}
67a4e0da   Nathanael Jourdane   Add a function to...
90

3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
91
92
}
?>