Blame view

php/classes/EpnTapMgr.php 2.87 KB
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
1
2
3
4
5
6
<?php
/**  @class EpnTapMgr
*    @brief Manager to communicates with EPN-TAP services.
*/

class EpnTapMgr {
ee761f1a   Nathanael Jourdane   Make the VOTable ...
7
8
9
10
	private $logEpnTap;

	function __construct()
    {
f740e66b   Nathanael Jourdane   Improve the VOTab...
11
    	$this->logEpnTap = fopen(LOG_DIR . "epnTap.log", "a");
ee761f1a   Nathanael Jourdane   Make the VOTable ...
12
13
14
15
16
		$this->addLog("\n---\n");
    }

	function addLog($msg)
    {
f740e66b   Nathanael Jourdane   Improve the VOTab...
17
  		fwrite($this->logEpnTap, date("h:i:s A") . ": " . $msg . "\n");
ee761f1a   Nathanael Jourdane   Make the VOTable ...
18
    }
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

	public function call($function_name, $args) {
		switch($function_name) {
			case 'getServiceNbResults':
				if(count($args) == 6)
					return $this->getServiceNbResults($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
				break;
			case 'getGranules':
				if(count($args) == 6)
					return $this->getGranules($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
				break;
			default:
				return array('success' => false, 'message' => $function_name.' is an unknown function.');
				break;
		}
	return array('success' => false, 'message' => 'The function do not have the required number of arguments');
	}

a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
37
38
39
40
41
42
43
44
45
46
47
	private function date2JD($date) {
		list($month, $day, $year) = split('[/.-]', $date);

		if($month == 1 || $month == 2) {
			$yearp = $year - 1;
			$monthp = $month + 12;
		} else {
			$yearp = $year;
			$monthp = $month;
		}

f740e66b   Nathanael Jourdane   Improve the VOTab...
48
		# this checks where we are in relation to October 15, 1582, the beginning of the Gregorian calendar.
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
49
50
51
52
53
54
55
56
57
58
59
60
61
		if (($year < 1582) ||
				($year == 1582 && $month < 10) ||
				($year == 1582 && $month == 10 && $day < 15))
			$j_day = 0;
		else
			$j_day = 2 - (int)($yearp / 100.0) + (int)((int)($yearp / 100.0) / 4.0);

		$j_day += $yearp < 0 ? (int)((365.25 * $yearp) - 0.75) : (int)(365.25 * $yearp);
		$j_day += (int)(30.6001 * ($monthp + 1)) + $day + 1720994.5;

		return $j_day;
	}

f740e66b   Nathanael Jourdane   Improve the VOTab...
62
	public function createFilter($target_name, $dataproduct_type, $time_min, $time_max) {
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
63
64
65
66
67
68
		$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)
f740e66b   Nathanael Jourdane   Improve the VOTab...
69
			array_push($filter, "time_min <= " . $this->date2JD($time_min));
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
70
71
72
		if($time_max)
			array_push($filter, "time_max >= " . $this->date2JD($time_max));

f740e66b   Nathanael Jourdane   Improve the VOTab...
73
74
		return (count($filter) > 0 ? ' WHERE '.join(' AND ', $filter) : '');
	}
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
75

f740e66b   Nathanael Jourdane   Improve the VOTab...
76
	public function request($access_url, $query) {
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
77
78
79
		$votMgr = new VOTableMgr;
		$params = 'FORMAT=votable&LANG=ADQL&REQUEST=doQuery';
		$url = $access_url . '/sync?' . $params . '&QUERY=' . urlencode($query);
f740e66b   Nathanael Jourdane   Improve the VOTab...
80
		$this->addLog("Query URL: " . $url);
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
81
		$votMgr->load($url);
f740e66b   Nathanael Jourdane   Improve the VOTab...
82
83
		$result = $votMgr->isValidSchema() ? $votMgr->parseStream() : NULL;
		return $result;
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
84
85
	}

ee761f1a   Nathanael Jourdane   Make the VOTable ...
86
	private function getGranules($table_name, $access_url, $target_name, $dataproduct_type, $time_min, $time_max) {
f740e66b   Nathanael Jourdane   Improve the VOTab...
87
88
89
		$filter = $this->createFilter($target_name, $dataproduct_type, $time_min, $time_max);
		$query = "SELECT dataproduct_type, target_name, time_min, time_max FROM $table_name.epn_core" . $filter;
		return $this->request($access_url, $query);
a79b0198   Nathanael Jourdane   Add EPN-TAP modul...
90
91
92
	}
}
?>