Blame view

php/classes/VOTableMgr.php 16.2 KB
16035364   Benjamin Renard   First commit
1
2
3
<?php
/**
 * @class VOTableMgr
10200969   Roipoussiere   Remove whitespace...
4
 * @version $Id: VOTableMgr.php 2916 2015-05-19 13:08:33Z elena $
16035364   Benjamin Renard   First commit
5
6
 */

8e8f453c   Nathanael Jourdane   VOTable parser: r...
7
8
//set DEBUG_MODE to TRUE to have some log information
define("DEBUG_MODE", FALSE);
16035364   Benjamin Renard   First commit
9
10

class VOTableMgr {
30cd92df   Roipoussiere   Fix merge conflicts
11
12
13
	public  $xml = null;
	private $log;
	private $xp;
5de62950   Nathanael Jourdane   Improve the VOTab...
14
15
	private $stream; // The stream in the VOTable
	private $c; // Current character position on the stream
e581690d   Nathanael Jourdane   Fix VOtable conve...
16
	private $is_little_endian;
b97c59e9   Nathanael Jourdane   Provide to the us...
17
	private $votable_error = false;
10200969   Roipoussiere   Remove whitespace...
18
19

  function __construct()
16035364   Benjamin Renard   First commit
20
21
22
23
  {
  	 if (DEBUG_MODE)
  	   $this->log = fopen(USERDATADIR."logVOTable","w");
  }
10200969   Roipoussiere   Remove whitespace...
24

16035364   Benjamin Renard   First commit
25
26
27
28
29
  function addLog($msg)
  {
  	 if (DEBUG_MODE)
  	   fprintf($this->log,$msg);
  }
10200969   Roipoussiere   Remove whitespace...
30

16035364   Benjamin Renard   First commit
31
32
  function load($fileName)
  {
30cd92df   Roipoussiere   Fix merge conflicts
33
34
	$this->is_little_endian = array_values(unpack('L1L', pack('V', 1)))[0] == 1;

e57cb025   Benjamin Renard   Fix most of error...
35
	// BRE - Add proxy host if exists
ba35574d   Benjamin Renard   Uniformize proxy ...
36
37
38
39
        $context = ProxyUtils::getStreamContextWithProxy();
        if (isset($context)) {
                libxml_set_streams_context($context);
        }
30cd92df   Roipoussiere   Fix merge conflicts
40
41
	$this->xml = new DomDocument();

e57cb025   Benjamin Renard   Fix most of error...
42
	if (!@$this->xml->load($fileName)) {
30cd92df   Roipoussiere   Fix merge conflicts
43
44
45
		$this->votable_error = 'Can not load xml file.';
		return false;
	}
10200969   Roipoussiere   Remove whitespace...
46

30cd92df   Roipoussiere   Fix merge conflicts
47
	$this->checkIDAttribute();
10200969   Roipoussiere   Remove whitespace...
48

30cd92df   Roipoussiere   Fix merge conflicts
49
50
51
	$rootNamespace = $this->xml->lookupNamespaceUri($this->xml->namespaceURI);
	$this->xp = new domxpath($this->xml);
	$this->xp->registerNameSpace('x', $rootNamespace);
10200969   Roipoussiere   Remove whitespace...
52

30cd92df   Roipoussiere   Fix merge conflicts
53
54
	return true;
  }
10200969   Roipoussiere   Remove whitespace...
55

30cd92df   Roipoussiere   Fix merge conflicts
56
57
  function getVotableError() {
	  return $this->votable_error;
16035364   Benjamin Renard   First commit
58
  }
10200969   Roipoussiere   Remove whitespace...
59

16035364   Benjamin Renard   First commit
60
61
  function isValidSchema()
  {
30cd92df   Roipoussiere   Fix merge conflicts
62
63
64
65
66
67
68
69
70
71
72
73
74
	if ($this->votable_error != false) {
		return false;
	}

	if (!$this->xml) {
		$this->votable_error = "The returned file is not XML.";
		return false;
	}

	$infos = $this->xp->query($this->queryResourceInfo());
	foreach($infos as $info) {
		if($info->getAttribute('value') == 'ERROR') {
			$this->votable_error = $info->textContent;
5de62950   Nathanael Jourdane   Improve the VOTab...
75
			return false;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
76
		}
30cd92df   Roipoussiere   Fix merge conflicts
77
	}
10200969   Roipoussiere   Remove whitespace...
78
79


16035364   Benjamin Renard   First commit
80
81
  	 //ToDo - BRE - add validation!!
  	 return TRUE;
10200969   Roipoussiere   Remove whitespace...
82

16035364   Benjamin Renard   First commit
83
84
  	 if (DEBUG_MODE)
  	   libxml_use_internal_errors(true);
10200969   Roipoussiere   Remove whitespace...
85

16035364   Benjamin Renard   First commit
86
  	 $vers = $this->getVersion();
10200969   Roipoussiere   Remove whitespace...
87

16035364   Benjamin Renard   First commit
88
  	 $this->addLog("VOTable version : ".$vers."\n");
10200969   Roipoussiere   Remove whitespace...
89

16035364   Benjamin Renard   First commit
90
  	 $result = FALSE;
10200969   Roipoussiere   Remove whitespace...
91

16035364   Benjamin Renard   First commit
92
93
94
95
96
97
98
99
100
  	 switch ($vers)
  	 {
  	 	 case '1.2' :
  	 	 	 $result = $this->xml->schemaValidate(XMLPATH.'VOTable-1.2.xsd');
  	 	 case '1.0' :
  	 	 	 $result = $this->xml->schemaValidate(XMLPATH.'VOTable-1.0.xsd');
  	 	 default :
  	 	   $result = $this->xml->schemaValidate(XMLPATH.'VOTable-1.1.xsd');
  	 }
10200969   Roipoussiere   Remove whitespace...
101

16035364   Benjamin Renard   First commit
102
103
104
  	 if (DEBUG_MODE)
  	 {
  	   $errors = libxml_get_errors();
10200969   Roipoussiere   Remove whitespace...
105
106

  	   foreach ($errors as $error)
16035364   Benjamin Renard   First commit
107
  	   {
10200969   Roipoussiere   Remove whitespace...
108
109
  	   	 $msg = '';

16035364   Benjamin Renard   First commit
110
111
112
113
114
115
116
117
118
119
120
121
122
        switch ($error->level)
        {
          case LIBXML_ERR_WARNING:
            $msg .= ("WARNING ".$error->code.": ");
            break;
          case LIBXML_ERR_ERROR:
            $msg .= ("ERROR ".$error->code.": ");
            break;
          case LIBXML_ERR_FATAL:
            $msg .= ("FATAL ".$error->code.": ");
            break;
        }
        $msg .= ($error->message." - In line : ".$error->line." - Of file : ".$error->file."\n");
10200969   Roipoussiere   Remove whitespace...
123

16035364   Benjamin Renard   First commit
124
125
        $this->addLog($msg);
  	   }
10200969   Roipoussiere   Remove whitespace...
126

16035364   Benjamin Renard   First commit
127
128
  	   libxml_use_internal_errors(false);
  	 }
10200969   Roipoussiere   Remove whitespace...
129

16035364   Benjamin Renard   First commit
130
131
    return $result;
  }
10200969   Roipoussiere   Remove whitespace...
132

16035364   Benjamin Renard   First commit
133
134
135
136
  protected function queryResource()
  {
  	 return "//x:RESOURCE";
  }
10200969   Roipoussiere   Remove whitespace...
137

30cd92df   Roipoussiere   Fix merge conflicts
138
	protected function queryResourceInfo() {
5de62950   Nathanael Jourdane   Improve the VOTab...
139
140
141
		return $this->queryResource()."/x:INFO";
	}

16035364   Benjamin Renard   First commit
142
143
144
145
  protected function queryTable()
  {
  	 return $this->queryResource()."/x:TABLE";
  }
10200969   Roipoussiere   Remove whitespace...
146

16035364   Benjamin Renard   First commit
147
148
149
150
  protected function queryDescription()
  {
  	 return $this->queryTable()."/x:DESCRIPTION";
  }
10200969   Roipoussiere   Remove whitespace...
151

16035364   Benjamin Renard   First commit
152
153
154
155
  protected function queryFields()
  {
  	 return $this->queryTable()."/x:FIELD";
  }
10200969   Roipoussiere   Remove whitespace...
156

16035364   Benjamin Renard   First commit
157
158
159
160
161
162
163
164
165
166
167
168
169
  protected function queryField($field_id)
  {
  	 return $this->queryFields()."[@ID='".$field_id."']";
  }

  protected function queryFieldByName($field_id)
  {
  	 return $this->queryFields()."[@name='".$field_id."']";
  }
  protected function queryFieldDescription($field_id)
  {
  	 return $this->queryField($field_id)."/x:DESCRIPTION";
  }
10200969   Roipoussiere   Remove whitespace...
170

16035364   Benjamin Renard   First commit
171
172
173
174
  protected function queryData()
  {
  	 return $this->queryTable()."/x:DATA";
  }
10200969   Roipoussiere   Remove whitespace...
175

16035364   Benjamin Renard   First commit
176
177
178
179
  protected function queryTableData()
  {
  	 return $this->queryData()."/x:TABLEDATA";
  }
10200969   Roipoussiere   Remove whitespace...
180

16035364   Benjamin Renard   First commit
181
182
183
184
  protected function queryTR()
  {
  	 return $this->queryTableData()."/x:TR";
  }
10200969   Roipoussiere   Remove whitespace...
185

30cd92df   Roipoussiere   Fix merge conflicts
186
187
	protected function queryBinaryData() {
		return $this->queryData()."/x:BINARY";
5de62950   Nathanael Jourdane   Improve the VOTab...
188
189
	}

30cd92df   Roipoussiere   Fix merge conflicts
190
191
	protected function queryStream() {
		return $this->queryBinaryData()."/x:STREAM";
5de62950   Nathanael Jourdane   Improve the VOTab...
192
	}
16035364   Benjamin Renard   First commit
193
  //
10200969   Roipoussiere   Remove whitespace...
194

16035364   Benjamin Renard   First commit
195
196
197
198
199
  public function getVersion()
  {
  	 if (!$this->xml)
  	 		return '';
  	 $root = $this->xml->documentElement;
10200969   Roipoussiere   Remove whitespace...
200

16035364   Benjamin Renard   First commit
201
202
  	 return $root->getAttribute('version');
  }
10200969   Roipoussiere   Remove whitespace...
203

16035364   Benjamin Renard   First commit
204
205
206
207
  public function getDescription()
  {
  	 if (!$this->xp)
  	   return '';
10200969   Roipoussiere   Remove whitespace...
208

16035364   Benjamin Renard   First commit
209
  	 $desc = $this->xp->query($this->queryDescription());
10200969   Roipoussiere   Remove whitespace...
210

16035364   Benjamin Renard   First commit
211
212
  	 if ($desc->length < 1)
  	   return '';
10200969   Roipoussiere   Remove whitespace...
213

16035364   Benjamin Renard   First commit
214
215
  	 return $desc->item(0)->nodeValue;
  }
10200969   Roipoussiere   Remove whitespace...
216

bf74fc2d   Elena.Budnik   IMPEX
217
218
219
220
  public function isData()
  {
    $trs =  $this->xml->getElementsByTagName('TR');
    if ($trs->length > 0) return true;
10200969   Roipoussiere   Remove whitespace...
221

bf74fc2d   Elena.Budnik   IMPEX
222
223
    return false;
  }
10200969   Roipoussiere   Remove whitespace...
224

16035364   Benjamin Renard   First commit
225
226
  public function getFirstTR()
  {
f60f0bd9   Benjamin Renard   Fix upload from s...
227
  	 if (!isset($this->xp))
16035364   Benjamin Renard   First commit
228
  	   return NULL;
10200969   Roipoussiere   Remove whitespace...
229

16035364   Benjamin Renard   First commit
230
  	 $tabledatas = $this->xp->query($this->queryTableData());
10200969   Roipoussiere   Remove whitespace...
231

16035364   Benjamin Renard   First commit
232
233
  	 if ($tabledatas->length < 1)
  	   return NULL;
10200969   Roipoussiere   Remove whitespace...
234

16035364   Benjamin Renard   First commit
235
  	 $tabledata = $tabledatas->item(0);
10200969   Roipoussiere   Remove whitespace...
236

16035364   Benjamin Renard   First commit
237
  	 $node = $tabledata->firstChild;
f60f0bd9   Benjamin Renard   Fix upload from s...
238
239
	 if (!isset($node))
		return NULL;
10200969   Roipoussiere   Remove whitespace...
240

f60f0bd9   Benjamin Renard   Fix upload from s...
241
242
	 if (($node->nodeType != XML_ELEMENT_NODE) || ($node->nodeName != "TR"))
		return $this->getNextTR($node);
10200969   Roipoussiere   Remove whitespace...
243

f60f0bd9   Benjamin Renard   Fix upload from s...
244
    	return $node;
16035364   Benjamin Renard   First commit
245
  }
10200969   Roipoussiere   Remove whitespace...
246

16035364   Benjamin Renard   First commit
247
248
  public function getNextTR($tr)
  {
f60f0bd9   Benjamin Renard   Fix upload from s...
249
  	 if (!isset($this->xp) || !isset($tr) || !isset($tr->nextSibling))
16035364   Benjamin Renard   First commit
250
  	   return NULL;
10200969   Roipoussiere   Remove whitespace...
251

f60f0bd9   Benjamin Renard   Fix upload from s...
252
253
254
	if (($tr->nextSibling->nodeType != XML_ELEMENT_NODE) || ($tr->nextSibling->nodeName != "TR"))
		return $this->getNextTR($tr->nextSibling);
	return $tr->nextSibling;
16035364   Benjamin Renard   First commit
255
  }
10200969   Roipoussiere   Remove whitespace...
256

16035364   Benjamin Renard   First commit
257
258
259
260
  public function getTDValueByFieldIndex($tr,$field_index)
  {
  	 if (!$this->xp)
  	   return NULL;
10200969   Roipoussiere   Remove whitespace...
261

16035364   Benjamin Renard   First commit
262
  	 $tds = $tr->getElementsByTagName("TD");
10200969   Roipoussiere   Remove whitespace...
263

16035364   Benjamin Renard   First commit
264
265
  	 if (($tds->length < 1) || ($field_index >= $tds->length))
  	   return NULL;
10200969   Roipoussiere   Remove whitespace...
266

16035364   Benjamin Renard   First commit
267
268
  	 return $tds->item($field_index)->nodeValue;
  }
10200969   Roipoussiere   Remove whitespace...
269

16035364   Benjamin Renard   First commit
270
271
272
273
  protected function isTimeField($field)
  {
    if (!$this->xp)
  	   return FALSE;
10200969   Roipoussiere   Remove whitespace...
274

6de48c3d   Benjamin Renard   Implement data up...
275
276
  	 return (($field->getAttribute("ucd") == "time.epoch") && ($field->getAttribute("xtype") == "dateTime") ||
		 ($field->getAttribute("ucd") == "TIME") && ($field->getAttribute("unit") == "iso-8601"));
16035364   Benjamin Renard   First commit
277
  }
10200969   Roipoussiere   Remove whitespace...
278

16035364   Benjamin Renard   First commit
279
280
281
282
  public function getTimeFieldIndex()
  {
  	 if (!$this->xp)
  	   return -1;
10200969   Roipoussiere   Remove whitespace...
283

16035364   Benjamin Renard   First commit
284
  	 $fields = $this->xp->query($this->queryFields());
10200969   Roipoussiere   Remove whitespace...
285

16035364   Benjamin Renard   First commit
286
287
  	 if ($fields->length < 1)
  	   return -1;
10200969   Roipoussiere   Remove whitespace...
288

16035364   Benjamin Renard   First commit
289
290
291
  	 for ($i = 0; $i < $fields->length; $i++)
  	   if ($this->isTimeField($fields->item($i)))
  	     return $i;
10200969   Roipoussiere   Remove whitespace...
292

16035364   Benjamin Renard   First commit
293
294
  	 return -1;
  }
10200969   Roipoussiere   Remove whitespace...
295

bf74fc2d   Elena.Budnik   IMPEX
296
297
298
299
	protected function getFieldByID($field_id)
	{
		if (!$this->xp)
			return NULL;
16035364   Benjamin Renard   First commit
300

bf74fc2d   Elena.Budnik   IMPEX
301
		$fields = $this->xp->query($this->queryFields());
10200969   Roipoussiere   Remove whitespace...
302

bf74fc2d   Elena.Budnik   IMPEX
303
304
		if ($fields->length < 1)
			return NULL;
10200969   Roipoussiere   Remove whitespace...
305

bf74fc2d   Elena.Budnik   IMPEX
306
307
308
		foreach ($fields as $field)
			if ($field->getAttribute("ID") == $field_id)
				return $field;
10200969   Roipoussiere   Remove whitespace...
309

bf74fc2d   Elena.Budnik   IMPEX
310
311
		return NULL;
	}
16035364   Benjamin Renard   First commit
312

bf74fc2d   Elena.Budnik   IMPEX
313
314
315
316
	protected function getFieldByName($field_id)
	{
		if (!$this->xp)
			return NULL;
10200969   Roipoussiere   Remove whitespace...
317

bf74fc2d   Elena.Budnik   IMPEX
318
		$fields = $this->xp->query($this->queryFieldByName($field_id));
10200969   Roipoussiere   Remove whitespace...
319

bf74fc2d   Elena.Budnik   IMPEX
320
321
		if ($fields->length < 1)
			return NULL;
10200969   Roipoussiere   Remove whitespace...
322

bf74fc2d   Elena.Budnik   IMPEX
323
324
325
		foreach ($fields as $field)
			if ($field->getAttribute("name") == $field_id)
			return $field;
10200969   Roipoussiere   Remove whitespace...
326

bf74fc2d   Elena.Budnik   IMPEX
327
		return NULL;
16035364   Benjamin Renard   First commit
328
	}
16035364   Benjamin Renard   First commit
329

bf74fc2d   Elena.Budnik   IMPEX
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
	protected function checkIDAttribute()
	{

		$fields = $this->xml->getElementsByTagName('FIELD');
		$i = 0;
		foreach ($fields as $field)
		{
			$i++;
			if (!$field->hasAttribute("ID"))
			{
				$field->setAttribute("ID", "col".$i);
			}
		}
		$this->xml->saveXML();
	}
16035364   Benjamin Renard   First commit
345
346
347
348
349

  public function getFieldIndexByID($field_id)
  {
  	 if (!$this->xp)
  	   return -1;
10200969   Roipoussiere   Remove whitespace...
350

16035364   Benjamin Renard   First commit
351
  	 $fields = $this->xp->query($this->queryFields());
10200969   Roipoussiere   Remove whitespace...
352

16035364   Benjamin Renard   First commit
353
354
  	 if ($fields->length < 1)
  	   return -1;
10200969   Roipoussiere   Remove whitespace...
355

16035364   Benjamin Renard   First commit
356
357
358
  	 for ($i = 0; $i < $fields->length; $i++)
  	   if ($fields->item($i)->getAttribute("ID") == $field_id)
  	     return $i;
10200969   Roipoussiere   Remove whitespace...
359

16035364   Benjamin Renard   First commit
360
361
  	 return -1;
  }
10200969   Roipoussiere   Remove whitespace...
362
363

  public function getStartStop()
16035364   Benjamin Renard   First commit
364
365
366
  {
  	 if (!$this->xp)
  	   return '0 0';
10200969   Roipoussiere   Remove whitespace...
367

16035364   Benjamin Renard   First commit
368
369
370
    $timeIndex = $this->getTimeFieldIndex();
    if ($timeIndex < 0)
      return '0 0';
10200969   Roipoussiere   Remove whitespace...
371

16035364   Benjamin Renard   First commit
372
    $tr = $this->getFirstTR();
10200969   Roipoussiere   Remove whitespace...
373

f60f0bd9   Benjamin Renard   Fix upload from s...
374
    if (!isset($tr))
16035364   Benjamin Renard   First commit
375
      return '0 0';
10200969   Roipoussiere   Remove whitespace...
376

16035364   Benjamin Renard   First commit
377
    $start = $this->getTDValueByFieldIndex($tr,$timeIndex);
10200969   Roipoussiere   Remove whitespace...
378

16035364   Benjamin Renard   First commit
379
    $stop = $start;
f60f0bd9   Benjamin Renard   Fix upload from s...
380
    while (isset($tr))
16035364   Benjamin Renard   First commit
381
382
383
384
    {
      $stop = $this->getTDValueByFieldIndex($tr,$timeIndex);
      $tr = $this->getNextTR($tr);
    }
10200969   Roipoussiere   Remove whitespace...
385

f60f0bd9   Benjamin Renard   Fix upload from s...
386
    if (!isset($start))
16035364   Benjamin Renard   First commit
387
388
389
      $start = 0;
    else
      $start = strtotime($start);
10200969   Roipoussiere   Remove whitespace...
390

f60f0bd9   Benjamin Renard   Fix upload from s...
391
    if (!isset($stop))
16035364   Benjamin Renard   First commit
392
393
394
      $stop = 0;
    else
      $stop = strtotime($stop);
10200969   Roipoussiere   Remove whitespace...
395
396

    return $start." ".$stop;
16035364   Benjamin Renard   First commit
397
  }
10200969   Roipoussiere   Remove whitespace...
398

bf74fc2d   Elena.Budnik   IMPEX
399
400
401
402
	public function getFieldInfoByID($field_id)
	{
		if (!$this->xp)
			return array("id"    => $field_id, "error" => "No file loaded");
10200969   Roipoussiere   Remove whitespace...
403

bf74fc2d   Elena.Budnik   IMPEX
404
		$field = $this->getFieldByID($field_id);
10200969   Roipoussiere   Remove whitespace...
405

bf74fc2d   Elena.Budnik   IMPEX
406
407
		if (!$field)
			$field = $this->getFieldByName($field_id);
16035364   Benjamin Renard   First commit
408

bf74fc2d   Elena.Budnik   IMPEX
409
410
		if (!$field)
			return array("id" => $field_id, "error" => "This field doesn't exist");
10200969   Roipoussiere   Remove whitespace...
411

bf74fc2d   Elena.Budnik   IMPEX
412
413
		return $this->getFieldInfo($field);
	}
10200969   Roipoussiere   Remove whitespace...
414

30cd92df   Roipoussiere   Fix merge conflicts
415
	/** Get the size of a row according to datatype array length. */
e581690d   Nathanael Jourdane   Fix VOtable conve...
416
417
418
419
420
421
422
423
	private function get_row_size($field_node) {
		$datatype = $field_node->getAttribute("datatype");

		if($datatype == 'boolean') {
			return 1;
		}

		switch($datatype) {
5de62950   Nathanael Jourdane   Improve the VOTab...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
			case 'unsignedByte':
			case 'char':
				$block_size = 1;
				break;
			case 'unicodeChar':
			case 'short':
				$block_size = 2;
				break;
			case 'int':
			case 'float':
				$block_size = 4;
				break;
			case 'long':
			case 'double':
			case 'float_complex':
				$block_size = 8;
				break;
			case 'double_complex':
e581690d   Nathanael Jourdane   Fix VOtable conve...
442
				$block_size = 16;
5de62950   Nathanael Jourdane   Improve the VOTab...
443
444
445
446
			default:
				$block_size = 0;
				break;
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
447

5de62950   Nathanael Jourdane   Improve the VOTab...
448
449
450
		if($field_node->getAttribute("arraysize") == NULL) {
			$array_size = $block_size;
		} else if("*" == $field_node->getAttribute("arraysize")) {
5de62950   Nathanael Jourdane   Improve the VOTab...
451
			$array_size = unpack("Ns", substr($this->stream, $this->c, 4))["s"] * $block_size;
5de62950   Nathanael Jourdane   Improve the VOTab...
452
453
454
455
456
457
			$this->c+=4;
		} else {
			$array_size = (int)($field_node->getAttribute("arraysize")) * $block_size;
		}
		return $array_size;
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
458
459

	/** Get the VOTable stream content.*/
5de62950   Nathanael Jourdane   Improve the VOTab...
460
	public function parseStream() {
b97c59e9   Nathanael Jourdane   Provide to the us...
461
		if (! $this->isValidSchema()) {
8d5016bc   Nathanael Jourdane   bugFix: do not us...
462
			error_log('There is an error on the VOTable: ' . $this->votable_error);
b97c59e9   Nathanael Jourdane   Provide to the us...
463
			return null;
b97c59e9   Nathanael Jourdane   Provide to the us...
464
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
465
466
		$data = Array();
		$fields = $this->xp->query($this->queryFields());
8d5016bc   Nathanael Jourdane   bugFix: do not us...
467
		$resource = $this->xp->query($this->queryResource());
5de62950   Nathanael Jourdane   Improve the VOTab...
468
469
470
471
472
		$nb_columns = $fields->length;
		$row = Array();
		$n_value = 0; // index of current value
		$this->c = 0; // initialize cursor position.
		$query_stream = $this->xp->query($this->queryStream())->item(0);
b97c59e9   Nathanael Jourdane   Provide to the us...
473
474
		if($query_stream == NULL) {
			$this->votable_error = "There is no STREAM node in the VOTable file.";
8d5016bc   Nathanael Jourdane   bugFix: do not us...
475
			return null;
b97c59e9   Nathanael Jourdane   Provide to the us...
476
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
477
		$this->stream = base64_decode($query_stream->textContent);
5de62950   Nathanael Jourdane   Improve the VOTab...
478
		$stream_len = strlen($this->stream);
b97c59e9   Nathanael Jourdane   Provide to the us...
479
		if($stream_len == 0) {
58423118   Nathanael Jourdane   Prompt an error m...
480
			$this->votable_error = "no result";
8d5016bc   Nathanael Jourdane   bugFix: do not us...
481
			return null;
b97c59e9   Nathanael Jourdane   Provide to the us...
482
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
483
484
		while($this->c < strlen($this->stream)) {
			$col_id = $n_value % $nb_columns;
57d96667   Nathanael Jourdane   Compatibility wit...
485
			$field_node = $fields->item($col_id);
5de62950   Nathanael Jourdane   Improve the VOTab...
486

5de62950   Nathanael Jourdane   Improve the VOTab...
487
488
489
490
491
492
			if($col_id == 0) {
				$row = Array();
			}
			$row[$field_node->getAttribute("ID")] = $this->process_datablock($field_node);
			if($col_id == $nb_columns-1) {
				array_push($data, $row);
f534c4a8   Nathanael Jourdane   Make the VOTable ...
493
			}
5de62950   Nathanael Jourdane   Improve the VOTab...
494
			$n_value+=1;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
495
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
496
497
		return $data;
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
498

e581690d   Nathanael Jourdane   Fix VOtable conve...
499
500
501
502
503
	private function JDTodate($jd) {
		list($month, $day, $year) = split('/', JDToGregorian($jd));
		return "$day/$month/$year";
	}

5de62950   Nathanael Jourdane   Improve the VOTab...
504
505
506
	private function process_datablock($field_node) {
		$data_type = $field_node->getAttribute("datatype");
		$row_size = $this->get_row_size($field_node);
e581690d   Nathanael Jourdane   Fix VOtable conve...
507
508
		$substr = substr($this->stream, $this->c, $row_size);

5de62950   Nathanael Jourdane   Improve the VOTab...
509
510
511
		switch ($data_type) {
			case 'boolean':
			case 'unsignedByte':
e581690d   Nathanael Jourdane   Fix VOtable conve...
512
				$b = $substr;
cdf35aa6   Nathanael Jourdane   Votable parser: c...
513
				$res = $b == 'T' || $b == 't' || $b == '1';
5de62950   Nathanael Jourdane   Improve the VOTab...
514
515
				break;
			case 'char':
cdf35aa6   Nathanael Jourdane   Votable parser: c...
516
				$res = $row_size !=0 ? utf8_encode($substr) : NULL;
5de62950   Nathanael Jourdane   Improve the VOTab...
517
			case 'unicodeChar':
cdf35aa6   Nathanael Jourdane   Votable parser: c...
518
				$res = $row_size !=0 ? utf8_encode(str_replace("\0", '', $substr)) : NULL;
5de62950   Nathanael Jourdane   Improve the VOTab...
519
520
				break;
			case 'short':
cdf35aa6   Nathanael Jourdane   Votable parser: c...
521
522
				$res = unpack('ss', $substr)['s'];
				$res = is_nan($res) ? NULL : $res;
5de62950   Nathanael Jourdane   Improve the VOTab...
523
524
				break;
			case 'int':
cdf35aa6   Nathanael Jourdane   Votable parser: c...
525
526
				$res = unpack('Ns', $substr)['s'];
				$res = is_nan($res) ? NULL : $res;
5de62950   Nathanael Jourdane   Improve the VOTab...
527
528
				break;
			case 'long':
cdf35aa6   Nathanael Jourdane   Votable parser: c...
529
530
				$res = unpack('Js', $substr)['s'];  // /!\ J -> PHP 5.6 only
				$res = is_nan($res) ? NULL : $res;
5de62950   Nathanael Jourdane   Improve the VOTab...
531
532
				break;
			case 'float':
cdf35aa6   Nathanael Jourdane   Votable parser: c...
533
				$res = unpack('fs', $substr)['s'];
e581690d   Nathanael Jourdane   Fix VOtable conve...
534
535
				// If machine is little endian:
				if($this->is_little_endian) {
cdf35aa6   Nathanael Jourdane   Votable parser: c...
536
					$res = unpack('f1f', strrev(pack('f', $res)))['f'];
e581690d   Nathanael Jourdane   Fix VOtable conve...
537
				}
cdf35aa6   Nathanael Jourdane   Votable parser: c...
538
				$res = is_nan($res) ? NULL : $res;
5de62950   Nathanael Jourdane   Improve the VOTab...
539
540
				break;
			case 'double':
cdf35aa6   Nathanael Jourdane   Votable parser: c...
541
				$res = unpack('ds', $substr)['s'];
e581690d   Nathanael Jourdane   Fix VOtable conve...
542
543
				// If machine is little endian:
				if($this->is_little_endian) {
cdf35aa6   Nathanael Jourdane   Votable parser: c...
544
					$res = unpack('d1d', strrev(pack('d', $res)))['d'];
e581690d   Nathanael Jourdane   Fix VOtable conve...
545
				}
cdf35aa6   Nathanael Jourdane   Votable parser: c...
546
				$res = is_nan($res) ? NULL : $res;
5de62950   Nathanael Jourdane   Improve the VOTab...
547
548
549
				break;
			default:
				$res = NULL;
cdf35aa6   Nathanael Jourdane   Votable parser: c...
550
				error_log("Unknown datatype: $data_type");
5de62950   Nathanael Jourdane   Improve the VOTab...
551
552
553
				break;
		}
		$this->c+=$row_size;
cdf35aa6   Nathanael Jourdane   Votable parser: c...
554
		return $res;
5de62950   Nathanael Jourdane   Improve the VOTab...
555
	}
10200969   Roipoussiere   Remove whitespace...
556

bf74fc2d   Elena.Budnik   IMPEX
557
558
559
560
	public function getFieldInfo($field)
	{
		if (!$this->xp)
			return array("id"    => $field_id, "error" => "No file loaded");
10200969   Roipoussiere   Remove whitespace...
561

bf74fc2d   Elena.Budnik   IMPEX
562
563
564
565
		$description = '';
		$desc = $field->getElementsByTagName("DESCRIPTION");
		if ($desc->length >= 1)
			$description = $desc->item(0)->nodeValue;
16035364   Benjamin Renard   First commit
566

bf74fc2d   Elena.Budnik   IMPEX
567
568
569
570
571
		$size = $field->getAttribute("arraysize");
		if ($size == '')
			$size = 1;
		else
			$size = intval($size);
16035364   Benjamin Renard   First commit
572

bf74fc2d   Elena.Budnik   IMPEX
573
574
575
576
577
578
579
580
581
582
583
584
585
		switch ($field->getAttribute("datatype"))
		{
			case "short" :
				$type = "SHORT";
				break;
			case "int" :
				$type = "INTEGER";
				break;
			case "long"   :
			case "double" :
				$type = "DOUBLE";
				break;
			default :
10200969   Roipoussiere   Remove whitespace...
586
				$type = "FLOAT";
bf74fc2d   Elena.Budnik   IMPEX
587
		}
10200969   Roipoussiere   Remove whitespace...
588

bf74fc2d   Elena.Budnik   IMPEX
589
590
		if (!$field->getAttribute("ID"))
			$id = "col".$n;
10200969   Roipoussiere   Remove whitespace...
591
		else
bf74fc2d   Elena.Budnik   IMPEX
592
			$id = $field->getAttribute("ID");
10200969   Roipoussiere   Remove whitespace...
593

bf74fc2d   Elena.Budnik   IMPEX
594
595
596
597
598
599
600
601
602
		return array("id"     => $field->getAttribute("ID"),
						"type"        => $type,
						"name"        => $field->getAttribute("name"),
						"ucd"         => $field->getAttribute("ucd"),
						"unit"        => $field->getAttribute("unit"),
						"size"        => $size,
						"description" => $description
						);
	}
10200969   Roipoussiere   Remove whitespace...
603

16035364   Benjamin Renard   First commit
604
605
606
607
  public function getFieldsInfo()
  {
   	 if (!$this->xp)
  	   return array("error" => "No file loaded");
10200969   Roipoussiere   Remove whitespace...
608

16035364   Benjamin Renard   First commit
609
    $fields_info = array();
10200969   Roipoussiere   Remove whitespace...
610

16035364   Benjamin Renard   First commit
611
  	 $fields = $this->xp->query($this->queryFields());
10200969   Roipoussiere   Remove whitespace...
612

16035364   Benjamin Renard   First commit
613
614
  	 if ($fields->length < 1)
  	   return $fields_info;
10200969   Roipoussiere   Remove whitespace...
615

16035364   Benjamin Renard   First commit
616
617
618
619
    foreach ($fields as $field)
    {
     	if ($this->isTimeField($field))
        continue;
10200969   Roipoussiere   Remove whitespace...
620

16035364   Benjamin Renard   First commit
621
622
      array_push($fields_info,$this->getFieldInfo($field));
    }
10200969   Roipoussiere   Remove whitespace...
623

16035364   Benjamin Renard   First commit
624
625
    return $fields_info;
  }
10200969   Roipoussiere   Remove whitespace...
626
627

  public function getSamplings()
16035364   Benjamin Renard   First commit
628
629
630
631
  {
  	 if (!$this->xp)
  	   return array("minSampling" => 0,
                   "maxSampling" => 0);
10200969   Roipoussiere   Remove whitespace...
632

16035364   Benjamin Renard   First commit
633
634
635
636
  	 $timeIndex = $this->getTimeFieldIndex();
    if ($timeIndex < 0)
      return array("minSampling" => 0,
                   "maxSampling" => 0);
10200969   Roipoussiere   Remove whitespace...
637

16035364   Benjamin Renard   First commit
638
639
640
641
642
643
644
    $tr = $this->getFirstTR();

    if (!$tr)
      return array("minSampling" => 0,
                   "maxSampling" => 0);

    $prevTime = 0;
f60f0bd9   Benjamin Renard   Fix upload from s...
645
    while (isset($tr))
16035364   Benjamin Renard   First commit
646
647
    {
      $time = $this->getTDValueByFieldIndex($tr,$timeIndex);
10200969   Roipoussiere   Remove whitespace...
648

f60f0bd9   Benjamin Renard   Fix upload from s...
649
      if (isset($time))
16035364   Benjamin Renard   First commit
650
651
652
653
654
655
      {
        $time = strtotime($time);
        if (($prevTime > 0) && ($time-$prevTime > 0))
          $deltaT[$time-$prevTime]++;
        $prevTime = $time;
      }
10200969   Roipoussiere   Remove whitespace...
656

16035364   Benjamin Renard   First commit
657
658
      $tr = $this->getNextTR($tr);
    }
10200969   Roipoussiere   Remove whitespace...
659

16035364   Benjamin Renard   First commit
660
661
    $minSampling = +1.e31;
    $maxSampling = 0.0;
10200969   Roipoussiere   Remove whitespace...
662
663

    foreach ($deltaT as $key => $value)
16035364   Benjamin Renard   First commit
664
    {
10200969   Roipoussiere   Remove whitespace...
665

16035364   Benjamin Renard   First commit
666
667
     if ($value/count($deltaT) < 0.10)
        continue;
10200969   Roipoussiere   Remove whitespace...
668

16035364   Benjamin Renard   First commit
669
670
671
      if ($key < $minSampling) $minSampling = $key;
      if ($key > $maxSampling) $maxSampling = $key;
    }
10200969   Roipoussiere   Remove whitespace...
672

16035364   Benjamin Renard   First commit
673
674
675
    return array("minSampling" => $minSampling,
                 "maxSampling" => $maxSampling);
  }
10200969   Roipoussiere   Remove whitespace...
676

bf74fc2d   Elena.Budnik   IMPEX
677
678
679
680
681
682
  /*
	* Add vector data made from components to IMPEX VOT
	*/
	public function addVectorToVot($paramID, $fileName)
	{
		$argsArr = explode(',', $paramID);
10200969   Roipoussiere   Remove whitespace...
683

bf74fc2d   Elena.Budnik   IMPEX
684
685
686
		$fields = $this->xml->getElementsByTagName('FIELD');
		$table = $this->xml->getElementsByTagName('TABLE')->item(0);
		$data = $this->xml->getElementsByTagName('DATA')->item(0);
10200969   Roipoussiere   Remove whitespace...
687

bf74fc2d   Elena.Budnik   IMPEX
688
		$i=0;
10200969   Roipoussiere   Remove whitespace...
689
		$find = false;
bf74fc2d   Elena.Budnik   IMPEX
690
		foreach ($fields as $field)
10200969   Roipoussiere   Remove whitespace...
691
		{
bf74fc2d   Elena.Budnik   IMPEX
692
			if ($field->getAttribute('name') == $argsArr[0])
10200969   Roipoussiere   Remove whitespace...
693
			{
bf74fc2d   Elena.Budnik   IMPEX
694
695
696
697
698
699
700
				$unit = $field->getAttribute('unit');
				$ucd  = $field->getAttribute('ucd');
				$datatype  = $field->getAttribute('datatype');
				$firstTD = $i;
				$find = true;
				break;
			}
10200969   Roipoussiere   Remove whitespace...
701
			$i++;
bf74fc2d   Elena.Budnik   IMPEX
702
		}
16035364   Benjamin Renard   First commit
703

bf74fc2d   Elena.Budnik   IMPEX
704
		if ($find)
10200969   Roipoussiere   Remove whitespace...
705
		{
bf74fc2d   Elena.Budnik   IMPEX
706
707
708
709
710
711
			$new_field = $this->xml->createElement('FIELD');
			$new_field->setAttribute('ID', $paramID);
			$new_field->setAttribute('name', $paramID);
			$new_field->setAttribute('datatype', $datatype);
			$new_field->setAttribute('arraysize', '3');
			$new_field->setAttribute('unit', $unit);
10200969   Roipoussiere   Remove whitespace...
712
713
			$new_field->setAttribute('ucd', $ucd);
			$colN = $fields->length + 1;
bf74fc2d   Elena.Budnik   IMPEX
714
			$new_field->setAttribute('ID', 'col'.$colN);
10200969   Roipoussiere   Remove whitespace...
715

bf74fc2d   Elena.Budnik   IMPEX
716
			$table->insertBefore($new_field,$data);
10200969   Roipoussiere   Remove whitespace...
717

bf74fc2d   Elena.Budnik   IMPEX
718
719
720
721
722
723
724
725
			$trs = $this->xml->getElementsByTagName('TR');
			foreach($trs as $tr)
			{
				$tds = $tr->getElementsByTagName('TD');
				$value = trim($tds->item($firstTD)->nodeValue).' '.trim($tds->item($firstTD + 1)->nodeValue).' '.trim($tds->item($firstTD + 2)->nodeValue);
				$td = $this->xml->createElement('TD', $value);
				$tr->appendChild($td);
			}
10200969   Roipoussiere   Remove whitespace...
726
727

			$this->xml->save($fileName);
bf74fc2d   Elena.Budnik   IMPEX
728
		}
16035364   Benjamin Renard   First commit
729
	}
16035364   Benjamin Renard   First commit
730
}
10200969   Roipoussiere   Remove whitespace...
731
?>