Blame view

php/classes/EpnTapMgr.php 2.74 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
10
	private function addLog($msg)
	{
2b6b4308   Nathanael Jourdane   Make the epntap m...
11
12
13
		// fwrite($this->logEpnTap, date("h:i:s A") . ": " . $msg . "\n");
		error_log("INFO " . $msg);
		// print "test";
210a3164   Nathanael Jourdane   Store actual func...
14
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
15

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

210a3164   Nathanael Jourdane   Store actual func...
21
	public function JDTodate($jd) {
2b6b4308   Nathanael Jourdane   Make the epntap m...
22
		list($month, $day, $year) = preg_split('/[\s\/:]+/', JDToGregorian($jd));
67a4e0da   Nathanael Jourdane   Add a function to...
23
24
		$date = sprintf('%02d', $day) . '/' . sprintf('%02d', $month) . '/' . sprintf('%04d', $year);
		return ($date == '00/00/0000') ? '' : $date;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
25
26
	}

47a2829d   Nathanael Jourdane   Fix the paginatio...
27
	public function createFilter($dataproduct_type, $target_name, $time_min, $time_max) {
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
28
		$filter = array();
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
29
30
		if($dataproduct_type)
			array_push($filter, "dataproduct_type = '$dataproduct_type'");
47a2829d   Nathanael Jourdane   Fix the paginatio...
31
32
		if($target_name)
			array_push($filter, "target_name = '$target_name'");
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
33
		if($time_min)
d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
34
			array_push($filter, "time_min >= " . $this->dateToJD($time_min));
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
35
		if($time_max)
d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
36
			array_push($filter, "time_max <= " . $this->dateToJD($time_max));
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
43
		$votMgr = new VOTableMgr;
		$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
		$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode($query);
210a3164   Nathanael Jourdane   Store actual func...
44

d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
45
		$res = $votMgr->load($url);
5de62950   Nathanael Jourdane   Improve the VOTab...
46
		$this->addLog("Query URL: " . $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 */
67a4e0da   Nathanael Jourdane   Add a function to...
52
	public function getGranules($table_name, $access_url, $filter, $select, $limit, $offset) {
b185823c   Nathanael Jourdane   Use IntervalUI mo...
53
		$query = "SELECT TOP {$limit} " . join(', ', $select) . " FROM {$table_name}.epn_core " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]) . " OFFSET {$offset}";
67a4e0da   Nathanael Jourdane   Add a function to...
54
55
		// return $query;
		$result = $this->request($access_url, $query);
d53e0fe6   Nathanael Jourdane   Move EPN-TAP modu...
56
57
58
59
60
61
		if(! array_key_exists("error", $result)) {
			for ($i = 0 ; $i < sizeof($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']);
			}
210a3164   Nathanael Jourdane   Store actual func...
62
		}
494949bb   Nathanael Jourdane   Remove dataproduc...
63

5de62950   Nathanael Jourdane   Improve the VOTab...
64
		return $result;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
65
66
	}

47a2829d   Nathanael Jourdane   Fix the paginatio...
67
	/* filter order: product type, target name, time min, time max */
67a4e0da   Nathanael Jourdane   Add a function to...
68
	public function getNbRows($table_name, $access_url, $filter) {
b185823c   Nathanael Jourdane   Use IntervalUI mo...
69
		$query = "SELECT COUNT(*) AS nb_rows FROM {$table_name}.epn_core " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]);
210a3164   Nathanael Jourdane   Store actual func...
70
		// return $query;
67a4e0da   Nathanael Jourdane   Add a function to...
71
		return $this->request($access_url, $query)[0]['nb_rows'];
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
72
	}
67a4e0da   Nathanael Jourdane   Add a function to...
73

3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
74
75
}
?>