Blame view

php/classes/EpnTapMgr.php 2.58 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
9
	private $logEpnTap;

	function __construct()
210a3164   Nathanael Jourdane   Store actual func...
10
11
	{
		$this->logEpnTap = fopen(LOG_DIR . "epnTap.log", "a");
f534c4a8   Nathanael Jourdane   Make the VOTable ...
12
		$this->addLog("\n---\n");
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
13
14
	}

210a3164   Nathanael Jourdane   Store actual func...
15
16
17
18
	private function addLog($msg)
	{
		fwrite($this->logEpnTap, date("h:i:s A") . ": " . $msg . "\n");
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
19

210a3164   Nathanael Jourdane   Store actual func...
20
	private function dateToJD($gregorian_date) {
67a4e0da   Nathanael Jourdane   Add a function to...
21
22
		list($day, $month, $year, $hours, $minutes, $seconds) = split('[/ :]', $gregorian_date);
		return GregorianToJD($month, $day, $year) + $hours/24 + $minutes/(24*60) + $seconds/(24*60*60);
210a3164   Nathanael Jourdane   Store actual func...
23
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
24

210a3164   Nathanael Jourdane   Store actual func...
25
26
	public function JDTodate($jd) {
		list($month, $day, $year) = split('/', JDToGregorian($jd));
67a4e0da   Nathanael Jourdane   Add a function to...
27
28
		$date = sprintf('%02d', $day) . '/' . sprintf('%02d', $month) . '/' . sprintf('%04d', $year);
		return ($date == '00/00/0000') ? '' : $date;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
29
30
	}

5de62950   Nathanael Jourdane   Improve the VOTab...
31
	public function createFilter($target_name, $dataproduct_type, $time_min, $time_max) {
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
32
33
34
35
36
37
		$filter = array();
		if($target_name)
			array_push($filter, "target_name = '$target_name'");
		if($dataproduct_type)
			array_push($filter, "dataproduct_type = '$dataproduct_type'");
		if($time_min)
210a3164   Nathanael Jourdane   Store actual func...
38
			array_push($filter, "time_min <= " . $this->dateToJD($time_min));
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
39
		if($time_max)
210a3164   Nathanael Jourdane   Store actual func...
40
			array_push($filter, "time_max >= " . $this->dateToJD($time_max));
210a3164   Nathanael Jourdane   Store actual func...
41
		return (count($filter) > 0 ? ' WHERE ' . join(' AND ', $filter) : '');
5de62950   Nathanael Jourdane   Improve the VOTab...
42
	}
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
43

5de62950   Nathanael Jourdane   Improve the VOTab...
44
	public function request($access_url, $query) {
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
45
46
47
		$votMgr = new VOTableMgr;
		$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
		$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode($query);
210a3164   Nathanael Jourdane   Store actual func...
48

5de62950   Nathanael Jourdane   Improve the VOTab...
49
		$this->addLog("Query URL: " . $url);
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
50
		$votMgr->load($url);
5de62950   Nathanael Jourdane   Improve the VOTab...
51
		$result = $votMgr->isValidSchema() ? $votMgr->parseStream() : NULL;
67a4e0da   Nathanael Jourdane   Add a function to...
52
53
54
55
56
57
58
59
		return $result;
	}

	/* filter order: $target_name, $time_min, $time_max, $table_name, $access_url */
	public function getGranules($table_name, $access_url, $filter, $select, $limit, $offset) {
		$query = "SELECT TOP $limit " . join(', ', $select) . " FROM $table_name.epn_core " . $this->createFilter($filter[0], $filter[1], $filter[2], $filter[3]) . " OFFSET $offset";
		// return $query;
		$result = $this->request($access_url, $query);
210a3164   Nathanael Jourdane   Store actual func...
60
		for ($i = 0 ; $i < sizeof($result) ; $i++) {
67a4e0da   Nathanael Jourdane   Add a function to...
61
			$result[$i]['time_min'] = $this->JDTodate($result[$i]['time_min']);
210a3164   Nathanael Jourdane   Store actual func...
62
63
			$result[$i]['time_max'] = $this->JDTodate($result[$i]['time_max']);
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
64
		return $result;
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
65
66
	}

67a4e0da   Nathanael Jourdane   Add a function to...
67
68
	public function getNbRows($table_name, $access_url, $filter) {
		$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...
69
		// return $query;
67a4e0da   Nathanael Jourdane   Add a function to...
70
		return $this->request($access_url, $query)[0]['nb_rows'];
3fc0b658   Nathanael Jourdane   Add EPN-TAP modul...
71
	}
67a4e0da   Nathanael Jourdane   Add a function to...
72

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