Blame view

php/classes/VOTableMgr.php 16.3 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	$this->is_little_endian = array_values(unpack('L1L', pack('V', 1)))[0] == 1;

	// see http://php.net/manual/en/domdocument.load.php#91384
	$options = array(
		'http' => array(
			'method' => 'GET',
			'timeout' => '5',
			'user_agent' => 'PHP libxml agent',
			// If the query is wrong, epn-tap service returns an HTTP error code 400, along with xml containing some usefull informations.
			'ignore_errors' => true
		)
	);
	$context = stream_context_create($options);
	libxml_set_streams_context($context);
	$this->xml = new DomDocument();

	if (!$this->xml->load($fileName)) {
		$this->votable_error = 'Can not load xml file.';
		return false;
	}
10200969   Roipoussiere   Remove whitespace...
53

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

30cd92df   Roipoussiere   Fix merge conflicts
56
57
58
	$rootNamespace = $this->xml->lookupNamespaceUri($this->xml->namespaceURI);
	$this->xp = new domxpath($this->xml);
	$this->xp->registerNameSpace('x', $rootNamespace);
10200969   Roipoussiere   Remove whitespace...
59

30cd92df   Roipoussiere   Fix merge conflicts
60
61
	return true;
  }
10200969   Roipoussiere   Remove whitespace...
62

30cd92df   Roipoussiere   Fix merge conflicts
63
64
  function getVotableError() {
	  return $this->votable_error;
16035364   Benjamin Renard   First commit
65
  }
10200969   Roipoussiere   Remove whitespace...
66

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


16035364   Benjamin Renard   First commit
87
88
  	 //ToDo - BRE - add validation!!
  	 return TRUE;
10200969   Roipoussiere   Remove whitespace...
89

16035364   Benjamin Renard   First commit
90
91
  	 if (DEBUG_MODE)
  	   libxml_use_internal_errors(true);
10200969   Roipoussiere   Remove whitespace...
92

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

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

16035364   Benjamin Renard   First commit
97
  	 $result = FALSE;
10200969   Roipoussiere   Remove whitespace...
98

16035364   Benjamin Renard   First commit
99
100
101
102
103
104
105
106
107
  	 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...
108

16035364   Benjamin Renard   First commit
109
110
111
  	 if (DEBUG_MODE)
  	 {
  	   $errors = libxml_get_errors();
10200969   Roipoussiere   Remove whitespace...
112
113

  	   foreach ($errors as $error)
16035364   Benjamin Renard   First commit
114
  	   {
10200969   Roipoussiere   Remove whitespace...
115
116
  	   	 $msg = '';

16035364   Benjamin Renard   First commit
117
118
119
120
121
122
123
124
125
126
127
128
129
        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...
130

16035364   Benjamin Renard   First commit
131
132
        $this->addLog($msg);
  	   }
10200969   Roipoussiere   Remove whitespace...
133

16035364   Benjamin Renard   First commit
134
135
  	   libxml_use_internal_errors(false);
  	 }
10200969   Roipoussiere   Remove whitespace...
136

16035364   Benjamin Renard   First commit
137
138
    return $result;
  }
10200969   Roipoussiere   Remove whitespace...
139

16035364   Benjamin Renard   First commit
140
141
142
143
  protected function queryResource()
  {
  	 return "//x:RESOURCE";
  }
10200969   Roipoussiere   Remove whitespace...
144

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

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

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

16035364   Benjamin Renard   First commit
159
160
161
162
  protected function queryFields()
  {
  	 return $this->queryTable()."/x:FIELD";
  }
10200969   Roipoussiere   Remove whitespace...
163

16035364   Benjamin Renard   First commit
164
165
166
167
168
169
170
171
172
173
174
175
176
  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...
177

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

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

16035364   Benjamin Renard   First commit
188
189
190
191
  protected function queryTR()
  {
  	 return $this->queryTableData()."/x:TR";
  }
10200969   Roipoussiere   Remove whitespace...
192

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

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

16035364   Benjamin Renard   First commit
202
203
204
205
206
  public function getVersion()
  {
  	 if (!$this->xml)
  	 		return '';
  	 $root = $this->xml->documentElement;
10200969   Roipoussiere   Remove whitespace...
207

16035364   Benjamin Renard   First commit
208
209
  	 return $root->getAttribute('version');
  }
10200969   Roipoussiere   Remove whitespace...
210

16035364   Benjamin Renard   First commit
211
212
213
214
  public function getDescription()
  {
  	 if (!$this->xp)
  	   return '';
10200969   Roipoussiere   Remove whitespace...
215

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

16035364   Benjamin Renard   First commit
218
219
  	 if ($desc->length < 1)
  	   return '';
10200969   Roipoussiere   Remove whitespace...
220

16035364   Benjamin Renard   First commit
221
222
  	 return $desc->item(0)->nodeValue;
  }
10200969   Roipoussiere   Remove whitespace...
223

bf74fc2d   Elena.Budnik   IMPEX
224
225
226
227
  public function isData()
  {
    $trs =  $this->xml->getElementsByTagName('TR');
    if ($trs->length > 0) return true;
10200969   Roipoussiere   Remove whitespace...
228

bf74fc2d   Elena.Budnik   IMPEX
229
230
    return false;
  }
10200969   Roipoussiere   Remove whitespace...
231

16035364   Benjamin Renard   First commit
232
233
234
235
  public function getFirstTR()
  {
  	 if (!$this->xp)
  	   return NULL;
10200969   Roipoussiere   Remove whitespace...
236
237
238

  	 /*$trs = $this->xp->query($this->queryTR());

16035364   Benjamin Renard   First commit
239
240
  	 if ($trs->length < 1)
  	   return NULL;
10200969   Roipoussiere   Remove whitespace...
241

16035364   Benjamin Renard   First commit
242
  	 return $trs->item(0);*/
10200969   Roipoussiere   Remove whitespace...
243

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

16035364   Benjamin Renard   First commit
246
247
  	 if ($tabledatas->length < 1)
  	   return NULL;
10200969   Roipoussiere   Remove whitespace...
248

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

16035364   Benjamin Renard   First commit
251
  	 $node = $tabledata->firstChild;
10200969   Roipoussiere   Remove whitespace...
252

16035364   Benjamin Renard   First commit
253
254
  	 while($node && ($node->nodeType != 1) && ($node->nodeName != "TR"))
      $node = $node->nextSibling;
10200969   Roipoussiere   Remove whitespace...
255

16035364   Benjamin Renard   First commit
256
257
    return $node;
  }
10200969   Roipoussiere   Remove whitespace...
258

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

16035364   Benjamin Renard   First commit
264
265
266
267
    while($tr->nextSibling && ($tr->nextSibling->nodeType != 1) && ($node->nodeName != "TR"))
      $tr = $tr->nextSibling;
				return $tr->nextSibling;
  }
10200969   Roipoussiere   Remove whitespace...
268

16035364   Benjamin Renard   First commit
269
270
271
272
  public function getTDValueByFieldIndex($tr,$field_index)
  {
  	 if (!$this->xp)
  	   return NULL;
10200969   Roipoussiere   Remove whitespace...
273

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

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

16035364   Benjamin Renard   First commit
279
280
  	 return $tds->item($field_index)->nodeValue;
  }
10200969   Roipoussiere   Remove whitespace...
281

16035364   Benjamin Renard   First commit
282
283
284
285
  protected function isTimeField($field)
  {
    if (!$this->xp)
  	   return FALSE;
10200969   Roipoussiere   Remove whitespace...
286

16035364   Benjamin Renard   First commit
287
288
  	 return (($field->getAttribute("ucd") == "time.epoch") && ($field->getAttribute("xtype") == "dateTime"));
  }
10200969   Roipoussiere   Remove whitespace...
289

16035364   Benjamin Renard   First commit
290
291
292
293
  public function getTimeFieldIndex()
  {
  	 if (!$this->xp)
  	   return -1;
10200969   Roipoussiere   Remove whitespace...
294

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

16035364   Benjamin Renard   First commit
297
298
  	 if ($fields->length < 1)
  	   return -1;
10200969   Roipoussiere   Remove whitespace...
299

16035364   Benjamin Renard   First commit
300
301
302
  	 for ($i = 0; $i < $fields->length; $i++)
  	   if ($this->isTimeField($fields->item($i)))
  	     return $i;
10200969   Roipoussiere   Remove whitespace...
303

16035364   Benjamin Renard   First commit
304
305
  	 return -1;
  }
10200969   Roipoussiere   Remove whitespace...
306

bf74fc2d   Elena.Budnik   IMPEX
307
308
309
310
	protected function getFieldByID($field_id)
	{
		if (!$this->xp)
			return NULL;
16035364   Benjamin Renard   First commit
311

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

bf74fc2d   Elena.Budnik   IMPEX
314
315
		if ($fields->length < 1)
			return NULL;
10200969   Roipoussiere   Remove whitespace...
316

bf74fc2d   Elena.Budnik   IMPEX
317
318
319
		foreach ($fields as $field)
			if ($field->getAttribute("ID") == $field_id)
				return $field;
10200969   Roipoussiere   Remove whitespace...
320

bf74fc2d   Elena.Budnik   IMPEX
321
322
		return NULL;
	}
16035364   Benjamin Renard   First commit
323

bf74fc2d   Elena.Budnik   IMPEX
324
325
326
327
	protected function getFieldByName($field_id)
	{
		if (!$this->xp)
			return NULL;
10200969   Roipoussiere   Remove whitespace...
328

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

bf74fc2d   Elena.Budnik   IMPEX
331
332
		if ($fields->length < 1)
			return NULL;
10200969   Roipoussiere   Remove whitespace...
333

bf74fc2d   Elena.Budnik   IMPEX
334
335
336
		foreach ($fields as $field)
			if ($field->getAttribute("name") == $field_id)
			return $field;
10200969   Roipoussiere   Remove whitespace...
337

bf74fc2d   Elena.Budnik   IMPEX
338
		return NULL;
16035364   Benjamin Renard   First commit
339
	}
16035364   Benjamin Renard   First commit
340

bf74fc2d   Elena.Budnik   IMPEX
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
	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
356
357
358
359
360

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

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

16035364   Benjamin Renard   First commit
364
365
  	 if ($fields->length < 1)
  	   return -1;
10200969   Roipoussiere   Remove whitespace...
366

16035364   Benjamin Renard   First commit
367
368
369
  	 for ($i = 0; $i < $fields->length; $i++)
  	   if ($fields->item($i)->getAttribute("ID") == $field_id)
  	     return $i;
10200969   Roipoussiere   Remove whitespace...
370

16035364   Benjamin Renard   First commit
371
372
  	 return -1;
  }
10200969   Roipoussiere   Remove whitespace...
373
374

  public function getStartStop()
16035364   Benjamin Renard   First commit
375
376
377
  {
  	 if (!$this->xp)
  	   return '0 0';
10200969   Roipoussiere   Remove whitespace...
378

16035364   Benjamin Renard   First commit
379
380
381
    $timeIndex = $this->getTimeFieldIndex();
    if ($timeIndex < 0)
      return '0 0';
10200969   Roipoussiere   Remove whitespace...
382

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

16035364   Benjamin Renard   First commit
385
386
    if (!$tr)
      return '0 0';
10200969   Roipoussiere   Remove whitespace...
387

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

16035364   Benjamin Renard   First commit
390
391
392
393
394
395
    $stop = $start;
    while ($tr)
    {
      $stop = $this->getTDValueByFieldIndex($tr,$timeIndex);
      $tr = $this->getNextTR($tr);
    }
10200969   Roipoussiere   Remove whitespace...
396

16035364   Benjamin Renard   First commit
397
398
399
400
    if (!$start)
      $start = 0;
    else
      $start = strtotime($start);
10200969   Roipoussiere   Remove whitespace...
401

16035364   Benjamin Renard   First commit
402
403
404
405
    if (!$stop)
      $stop = 0;
    else
      $stop = strtotime($stop);
10200969   Roipoussiere   Remove whitespace...
406
407

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

bf74fc2d   Elena.Budnik   IMPEX
410
411
412
413
	public function getFieldInfoByID($field_id)
	{
		if (!$this->xp)
			return array("id"    => $field_id, "error" => "No file loaded");
10200969   Roipoussiere   Remove whitespace...
414

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

bf74fc2d   Elena.Budnik   IMPEX
417
418
		if (!$field)
			$field = $this->getFieldByName($field_id);
16035364   Benjamin Renard   First commit
419

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

bf74fc2d   Elena.Budnik   IMPEX
423
424
		return $this->getFieldInfo($field);
	}
10200969   Roipoussiere   Remove whitespace...
425

30cd92df   Roipoussiere   Fix merge conflicts
426
	/** Get the size of a row according to datatype array length. */
e581690d   Nathanael Jourdane   Fix VOtable conve...
427
428
429
430
431
432
433
434
	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...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
			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...
453
				$block_size = 16;
5de62950   Nathanael Jourdane   Improve the VOTab...
454
455
456
457
			default:
				$block_size = 0;
				break;
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
458

5de62950   Nathanael Jourdane   Improve the VOTab...
459
460
461
		if($field_node->getAttribute("arraysize") == NULL) {
			$array_size = $block_size;
		} else if("*" == $field_node->getAttribute("arraysize")) {
5de62950   Nathanael Jourdane   Improve the VOTab...
462
			$array_size = unpack("Ns", substr($this->stream, $this->c, 4))["s"] * $block_size;
5de62950   Nathanael Jourdane   Improve the VOTab...
463
464
465
466
467
468
			$this->c+=4;
		} else {
			$array_size = (int)($field_node->getAttribute("arraysize")) * $block_size;
		}
		return $array_size;
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
469
470

	/** Get the VOTable stream content.*/
5de62950   Nathanael Jourdane   Improve the VOTab...
471
	public function parseStream() {
b97c59e9   Nathanael Jourdane   Provide to the us...
472
		if (! $this->isValidSchema()) {
8d5016bc   Nathanael Jourdane   bugFix: do not us...
473
			error_log('There is an error on the VOTable: ' . $this->votable_error);
b97c59e9   Nathanael Jourdane   Provide to the us...
474
			return null;
b97c59e9   Nathanael Jourdane   Provide to the us...
475
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
476
477
		$data = Array();
		$fields = $this->xp->query($this->queryFields());
8d5016bc   Nathanael Jourdane   bugFix: do not us...
478
		$resource = $this->xp->query($this->queryResource());
5de62950   Nathanael Jourdane   Improve the VOTab...
479
480
481
482
483
		$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...
484
485
		if($query_stream == NULL) {
			$this->votable_error = "There is no STREAM node in the VOTable file.";
8d5016bc   Nathanael Jourdane   bugFix: do not us...
486
			return null;
b97c59e9   Nathanael Jourdane   Provide to the us...
487
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
488
		$this->stream = base64_decode($query_stream->textContent);
5de62950   Nathanael Jourdane   Improve the VOTab...
489
		$stream_len = strlen($this->stream);
b97c59e9   Nathanael Jourdane   Provide to the us...
490
		if($stream_len == 0) {
58423118   Nathanael Jourdane   Prompt an error m...
491
			$this->votable_error = "no result";
8d5016bc   Nathanael Jourdane   bugFix: do not us...
492
			return null;
b97c59e9   Nathanael Jourdane   Provide to the us...
493
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
494
495
		while($this->c < strlen($this->stream)) {
			$col_id = $n_value % $nb_columns;
57d96667   Nathanael Jourdane   Compatibility wit...
496
			$field_node = $fields->item($col_id);
5de62950   Nathanael Jourdane   Improve the VOTab...
497

5de62950   Nathanael Jourdane   Improve the VOTab...
498
499
500
501
502
503
			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 ...
504
			}
5de62950   Nathanael Jourdane   Improve the VOTab...
505
			$n_value+=1;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
506
		}
5de62950   Nathanael Jourdane   Improve the VOTab...
507
508
		return $data;
	}
f534c4a8   Nathanael Jourdane   Make the VOTable ...
509

e581690d   Nathanael Jourdane   Fix VOtable conve...
510
511
512
513
514
	private function JDTodate($jd) {
		list($month, $day, $year) = split('/', JDToGregorian($jd));
		return "$day/$month/$year";
	}

5de62950   Nathanael Jourdane   Improve the VOTab...
515
516
517
	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...
518
519
		$substr = substr($this->stream, $this->c, $row_size);

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

bf74fc2d   Elena.Budnik   IMPEX
568
569
570
571
	public function getFieldInfo($field)
	{
		if (!$this->xp)
			return array("id"    => $field_id, "error" => "No file loaded");
10200969   Roipoussiere   Remove whitespace...
572

bf74fc2d   Elena.Budnik   IMPEX
573
574
575
576
		$description = '';
		$desc = $field->getElementsByTagName("DESCRIPTION");
		if ($desc->length >= 1)
			$description = $desc->item(0)->nodeValue;
16035364   Benjamin Renard   First commit
577

bf74fc2d   Elena.Budnik   IMPEX
578
579
580
581
582
		$size = $field->getAttribute("arraysize");
		if ($size == '')
			$size = 1;
		else
			$size = intval($size);
16035364   Benjamin Renard   First commit
583

bf74fc2d   Elena.Budnik   IMPEX
584
585
586
587
588
589
590
591
592
593
594
595
596
		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...
597
				$type = "FLOAT";
bf74fc2d   Elena.Budnik   IMPEX
598
		}
10200969   Roipoussiere   Remove whitespace...
599

bf74fc2d   Elena.Budnik   IMPEX
600
601
		if (!$field->getAttribute("ID"))
			$id = "col".$n;
10200969   Roipoussiere   Remove whitespace...
602
		else
bf74fc2d   Elena.Budnik   IMPEX
603
			$id = $field->getAttribute("ID");
10200969   Roipoussiere   Remove whitespace...
604

bf74fc2d   Elena.Budnik   IMPEX
605
606
607
608
609
610
611
612
613
		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...
614

16035364   Benjamin Renard   First commit
615
616
617
618
  public function getFieldsInfo()
  {
   	 if (!$this->xp)
  	   return array("error" => "No file loaded");
10200969   Roipoussiere   Remove whitespace...
619

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

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

16035364   Benjamin Renard   First commit
624
625
  	 if ($fields->length < 1)
  	   return $fields_info;
10200969   Roipoussiere   Remove whitespace...
626

16035364   Benjamin Renard   First commit
627
628
629
630
    foreach ($fields as $field)
    {
     	if ($this->isTimeField($field))
        continue;
10200969   Roipoussiere   Remove whitespace...
631

16035364   Benjamin Renard   First commit
632
633
      array_push($fields_info,$this->getFieldInfo($field));
    }
10200969   Roipoussiere   Remove whitespace...
634

16035364   Benjamin Renard   First commit
635
636
    return $fields_info;
  }
10200969   Roipoussiere   Remove whitespace...
637
638

  public function getSamplings()
16035364   Benjamin Renard   First commit
639
640
641
642
  {
  	 if (!$this->xp)
  	   return array("minSampling" => 0,
                   "maxSampling" => 0);
10200969   Roipoussiere   Remove whitespace...
643

16035364   Benjamin Renard   First commit
644
645
646
647
  	 $timeIndex = $this->getTimeFieldIndex();
    if ($timeIndex < 0)
      return array("minSampling" => 0,
                   "maxSampling" => 0);
10200969   Roipoussiere   Remove whitespace...
648

16035364   Benjamin Renard   First commit
649
650
651
652
653
654
655
656
657
658
    $tr = $this->getFirstTR();

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

    $prevTime = 0;
    while ($tr)
    {
      $time = $this->getTDValueByFieldIndex($tr,$timeIndex);
10200969   Roipoussiere   Remove whitespace...
659

16035364   Benjamin Renard   First commit
660
661
662
663
664
665
666
      if ($time)
      {
        $time = strtotime($time);
        if (($prevTime > 0) && ($time-$prevTime > 0))
          $deltaT[$time-$prevTime]++;
        $prevTime = $time;
      }
10200969   Roipoussiere   Remove whitespace...
667

16035364   Benjamin Renard   First commit
668
669
      $tr = $this->getNextTR($tr);
    }
10200969   Roipoussiere   Remove whitespace...
670

16035364   Benjamin Renard   First commit
671
672
    $minSampling = +1.e31;
    $maxSampling = 0.0;
10200969   Roipoussiere   Remove whitespace...
673
674

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

16035364   Benjamin Renard   First commit
677
678
     if ($value/count($deltaT) < 0.10)
        continue;
10200969   Roipoussiere   Remove whitespace...
679

16035364   Benjamin Renard   First commit
680
681
682
      if ($key < $minSampling) $minSampling = $key;
      if ($key > $maxSampling) $maxSampling = $key;
    }
10200969   Roipoussiere   Remove whitespace...
683

16035364   Benjamin Renard   First commit
684
685
686
    return array("minSampling" => $minSampling,
                 "maxSampling" => $maxSampling);
  }
10200969   Roipoussiere   Remove whitespace...
687

bf74fc2d   Elena.Budnik   IMPEX
688
689
690
691
692
693
  /*
	* Add vector data made from components to IMPEX VOT
	*/
	public function addVectorToVot($paramID, $fileName)
	{
		$argsArr = explode(',', $paramID);
10200969   Roipoussiere   Remove whitespace...
694

bf74fc2d   Elena.Budnik   IMPEX
695
696
697
		$fields = $this->xml->getElementsByTagName('FIELD');
		$table = $this->xml->getElementsByTagName('TABLE')->item(0);
		$data = $this->xml->getElementsByTagName('DATA')->item(0);
10200969   Roipoussiere   Remove whitespace...
698

bf74fc2d   Elena.Budnik   IMPEX
699
		$i=0;
10200969   Roipoussiere   Remove whitespace...
700
		$find = false;
bf74fc2d   Elena.Budnik   IMPEX
701
		foreach ($fields as $field)
10200969   Roipoussiere   Remove whitespace...
702
		{
bf74fc2d   Elena.Budnik   IMPEX
703
			if ($field->getAttribute('name') == $argsArr[0])
10200969   Roipoussiere   Remove whitespace...
704
			{
bf74fc2d   Elena.Budnik   IMPEX
705
706
707
708
709
710
711
				$unit = $field->getAttribute('unit');
				$ucd  = $field->getAttribute('ucd');
				$datatype  = $field->getAttribute('datatype');
				$firstTD = $i;
				$find = true;
				break;
			}
10200969   Roipoussiere   Remove whitespace...
712
			$i++;
bf74fc2d   Elena.Budnik   IMPEX
713
		}
16035364   Benjamin Renard   First commit
714

bf74fc2d   Elena.Budnik   IMPEX
715
		if ($find)
10200969   Roipoussiere   Remove whitespace...
716
		{
bf74fc2d   Elena.Budnik   IMPEX
717
718
719
720
721
722
			$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...
723
724
			$new_field->setAttribute('ucd', $ucd);
			$colN = $fields->length + 1;
bf74fc2d   Elena.Budnik   IMPEX
725
			$new_field->setAttribute('ID', 'col'.$colN);
10200969   Roipoussiere   Remove whitespace...
726

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

bf74fc2d   Elena.Budnik   IMPEX
729
730
731
732
733
734
735
736
			$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...
737
738

			$this->xml->save($fileName);
bf74fc2d   Elena.Budnik   IMPEX
739
		}
16035364   Benjamin Renard   First commit
740
	}
16035364   Benjamin Renard   First commit
741
}
10200969   Roipoussiere   Remove whitespace...
742
?>