Blame view

php/classes/VOTableMgr.php 16.4 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;

7dd0f3cd   NathanaĆ«l Jourdane   Fix bug when a re...
35
	libxml_set_streams_context(stream_context_create([ 'http' => [ 'method' => 'GET', 'timeout' => '5' ]]));
e57cb025   Benjamin Renard   Fix most of error...
36
	// BRE - Add proxy host if exists
ba35574d   Benjamin Renard   Uniformize proxy ...
37
38
39
40
        $context = ProxyUtils::getStreamContextWithProxy();
        if (isset($context)) {
                libxml_set_streams_context($context);
        }
30cd92df   Roipoussiere   Fix merge conflicts
41
42
	$this->xml = new DomDocument();

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

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

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

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

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

16035364   Benjamin Renard   First commit
61
62
  function isValidSchema()
  {
30cd92df   Roipoussiere   Fix merge conflicts
63
64
65
66
67
68
69
70
71
72
73
74
75
	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...
76
			return false;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
77
		}
30cd92df   Roipoussiere   Fix merge conflicts
78
	}
10200969   Roipoussiere   Remove whitespace...
79
80


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

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

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

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

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

16035364   Benjamin Renard   First commit
93
94
95
96
97
98
99
100
101
  	 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...
102

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

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

16035364   Benjamin Renard   First commit
111
112
113
114
115
116
117
118
119
120
121
122
123
        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...
124

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

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

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

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

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

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

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

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

16035364   Benjamin Renard   First commit
158
159
160
161
162
163
164
165
166
167
168
169
170
  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...
171

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

bf74fc2d   Elena.Budnik   IMPEX
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
	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
346
347
348
349
350

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

30cd92df   Roipoussiere   Fix merge conflicts
416
	/** Get the size of a row according to datatype array length. */
e581690d   Nathanael Jourdane   Fix VOtable conve...
417
418
419
420
421
422
423
424
	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...
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
			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...
443
				$block_size = 16;
5de62950   Nathanael Jourdane   Improve the VOTab...
444
445
446
447
			default:
				$block_size = 0;
				break;
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
448

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

	/** Get the VOTable stream content.*/
5de62950   Nathanael Jourdane   Improve the VOTab...
461
	public function parseStream() {
b97c59e9   Nathanael Jourdane   Provide to the us...
462
		if (! $this->isValidSchema()) {
8d5016bc   Nathanael Jourdane   bugFix: do not us...
463
			error_log('There is an error on the VOTable: ' . $this->votable_error);
b97c59e9   Nathanael Jourdane   Provide to the us...
464
			return null;
b97c59e9   Nathanael Jourdane   Provide to the us...
465
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
466
467
		$data = Array();
		$fields = $this->xp->query($this->queryFields());
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
  /*
	* Add vector data made from components to IMPEX VOT
	*/
	public function addVectorToVot($paramID, $fileName)
	{
955c4995   Elena.Budnik   add vector to VOT...
682
683
684
685
		if (strpos($paramID, ',') === false)  // LATMOS Ganymede components are defined with spaces!
			$argsArr = explode(' ',$paramID); 
		else
			$argsArr = explode(',',$paramID);	
10200969   Roipoussiere   Remove whitespace...
686

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

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

bf74fc2d   Elena.Budnik   IMPEX
707
		if ($find)
10200969   Roipoussiere   Remove whitespace...
708
		{
bf74fc2d   Elena.Budnik   IMPEX
709
710
711
712
713
714
			$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...
715
716
			$new_field->setAttribute('ucd', $ucd);
			$colN = $fields->length + 1;
bf74fc2d   Elena.Budnik   IMPEX
717
			$new_field->setAttribute('ID', 'col'.$colN);
10200969   Roipoussiere   Remove whitespace...
718

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

bf74fc2d   Elena.Budnik   IMPEX
721
722
723
724
725
726
727
728
			$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...
729
730

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