TestDownloadBase.php
3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
require_once "TestAbstract.php";
abstract class TestDownloadBase extends TestAbstract
{
abstract protected function checkResultInfo($info);
protected function checkRESTResult($result) {
if (empty($result)) {
return array(
'success' => FALSE,
'message' => 'Empty result',
);
}
$json_res = json_decode($result);
if (!$json_res) {
return array(
'success' => FALSE,
'message' => 'Result is not in JSON format',
);
}
$info = $this->getResultInfo($json_res);
if (!$info['success']) {
return $info;
}
return $this->checkResultInfo($info);
}
protected function checkSOAPResult($result) {
$info = $this->getResultInfo($result);
if (!$info['success']) {
return $info;
}
return $this->checkResultInfo($info);
}
private function gz_get_contents($path) {
$gzip = file_get_contents($path);
$rest = substr($gzip, -4);
$unpacked = unpack("V", $rest);
$uncompressedSize = end($unpacked);
// read the gzipped content, specifying the exact length
$handle = gzopen($path, "rb");
$contents = gzread($handle, $uncompressedSize);
gzclose($handle);
return $contents;
}
private function isCompressed($path) {
return (substr($path, -3) == ".gz");
}
private function isVOTable($path, $compressed) {
if ($compressed) {
$path = substr($path, 0, strlen($path)-3);
}
return (substr($path, -4) == ".vot");
}
private function getResultInfo($obj) {
if (empty($obj) || empty($obj->status)) {
return array(
'success' => FALSE,
'message' => 'Bad result format',
);
}
if ($obj->status != 'done') {
return array(
'success' => FALSE,
'message' => 'Status is not done ('.$obj->status.')',
);
}
if (empty($obj->dataFileURLs)) {
return array(
'success' => FALSE,
'message' => 'Missing data file URL in result',
);
}
$result = array(
'success' => TRUE,
);
$result['compressed'] = $this->isCompressed($obj->dataFileURLs);
if ($result['compressed']) {
$data = $this->gz_get_contents($obj->dataFileURLs);
}
else {
$data = file_get_contents($obj->dataFileURLs);
}
if (!$data) {
return array(
'success' => FALSE,
'message' => 'Cannot load data result file',
);
}
$result['isVOTable'] = $this->isVOTable($obj->dataFileURLs, $result['compressed']);
$request_info = array();
foreach (explode(PHP_EOL, $data) as $line) {
$pattern = $result['isVOTable'] ? "REQUEST_" : "# REQUEST_";
if (strpos($line, $pattern) === 0) {
$sep_pos = strpos($line, ":");
if ($result['isVOTable'])
$key = trim(substr($line, 0, $sep_pos));
else
$key = trim(substr($line, 2, $sep_pos-2));
$value = trim(substr($line, $sep_pos+1));
$request_info[$key] = $value;
}
}
$result['parameters'] = array();
if (!empty($request_info['REQUEST_OUTPUT_PARAMS'])) {
$result['parameters'] = explode(',',$request_info['REQUEST_OUTPUT_PARAMS']);
}
$result['structure'] = !empty($request_info['REQUEST_STRUCTURE']) ? $request_info['REQUEST_STRUCTURE'] : '';
$result['timeFormat'] = !empty($request_info['REQUEST_TIME_FORMAT']) ? $request_info['REQUEST_TIME_FORMAT'] : '';
$result['sampling'] = !empty($request_info['REQUEST_TIME_RESOLUTION']) ? doubleval($request_info['REQUEST_TIME_RESOLUTION']) : NULL;
return $result;
}
}
?>