Blame view

php/classes/VOTableMgr.php 16.7 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
	$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
		)
	);
e57cb025   Benjamin Renard   Fix most of error...
45
46
47
48
49
50
51
52
53
54

	// BRE - Add proxy host if exists
	if (defined('HTTP_PROXY_HOST')) {
		$options['http']['proxy'] = 'tcp://'.HTTP_PROXY_HOST;
		$options['http']['request_fulluri'] = TRUE;
		if (defined('HTTP_PROXY_USER')) {
			$options['http']['header'] = "Proxy-Authorization: Basic ".base64_encode(HTTP_PROXY_USER);
		}
	}

30cd92df   Roipoussiere   Fix merge conflicts
55
56
57
58
	$context = stream_context_create($options);
	libxml_set_streams_context($context);
	$this->xml = new DomDocument();

e57cb025   Benjamin Renard   Fix most of error...
59
	if (!@$this->xml->load($fileName)) {
30cd92df   Roipoussiere   Fix merge conflicts
60
61
62
		$this->votable_error = 'Can not load xml file.';
		return false;
	}
10200969   Roipoussiere   Remove whitespace...
63

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

30cd92df   Roipoussiere   Fix merge conflicts
66
67
68
	$rootNamespace = $this->xml->lookupNamespaceUri($this->xml->namespaceURI);
	$this->xp = new domxpath($this->xml);
	$this->xp->registerNameSpace('x', $rootNamespace);
10200969   Roipoussiere   Remove whitespace...
69

30cd92df   Roipoussiere   Fix merge conflicts
70
71
	return true;
  }
10200969   Roipoussiere   Remove whitespace...
72

30cd92df   Roipoussiere   Fix merge conflicts
73
74
  function getVotableError() {
	  return $this->votable_error;
16035364   Benjamin Renard   First commit
75
  }
10200969   Roipoussiere   Remove whitespace...
76

16035364   Benjamin Renard   First commit
77
78
  function isValidSchema()
  {
30cd92df   Roipoussiere   Fix merge conflicts
79
80
81
82
83
84
85
86
87
88
89
90
91
	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...
92
			return false;
f534c4a8   Nathanael Jourdane   Make the VOTable ...
93
		}
30cd92df   Roipoussiere   Fix merge conflicts
94
	}
10200969   Roipoussiere   Remove whitespace...
95
96


16035364   Benjamin Renard   First commit
97
98
  	 //ToDo - BRE - add validation!!
  	 return TRUE;
10200969   Roipoussiere   Remove whitespace...
99

16035364   Benjamin Renard   First commit
100
101
  	 if (DEBUG_MODE)
  	   libxml_use_internal_errors(true);
10200969   Roipoussiere   Remove whitespace...
102

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

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

16035364   Benjamin Renard   First commit
107
  	 $result = FALSE;
10200969   Roipoussiere   Remove whitespace...
108

16035364   Benjamin Renard   First commit
109
110
111
112
113
114
115
116
117
  	 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...
118

16035364   Benjamin Renard   First commit
119
120
121
  	 if (DEBUG_MODE)
  	 {
  	   $errors = libxml_get_errors();
10200969   Roipoussiere   Remove whitespace...
122
123

  	   foreach ($errors as $error)
16035364   Benjamin Renard   First commit
124
  	   {
10200969   Roipoussiere   Remove whitespace...
125
126
  	   	 $msg = '';

16035364   Benjamin Renard   First commit
127
128
129
130
131
132
133
134
135
136
137
138
139
        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...
140

16035364   Benjamin Renard   First commit
141
142
        $this->addLog($msg);
  	   }
10200969   Roipoussiere   Remove whitespace...
143

16035364   Benjamin Renard   First commit
144
145
  	   libxml_use_internal_errors(false);
  	 }
10200969   Roipoussiere   Remove whitespace...
146

16035364   Benjamin Renard   First commit
147
148
    return $result;
  }
10200969   Roipoussiere   Remove whitespace...
149

16035364   Benjamin Renard   First commit
150
151
152
153
  protected function queryResource()
  {
  	 return "//x:RESOURCE";
  }
10200969   Roipoussiere   Remove whitespace...
154

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

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

16035364   Benjamin Renard   First commit
164
165
166
167
  protected function queryDescription()
  {
  	 return $this->queryTable()."/x:DESCRIPTION";
  }
10200969   Roipoussiere   Remove whitespace...
168

16035364   Benjamin Renard   First commit
169
170
171
172
  protected function queryFields()
  {
  	 return $this->queryTable()."/x:FIELD";
  }
10200969   Roipoussiere   Remove whitespace...
173

16035364   Benjamin Renard   First commit
174
175
176
177
178
179
180
181
182
183
184
185
186
  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...
187

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

16035364   Benjamin Renard   First commit
193
194
195
196
  protected function queryTableData()
  {
  	 return $this->queryData()."/x:TABLEDATA";
  }
10200969   Roipoussiere   Remove whitespace...
197

16035364   Benjamin Renard   First commit
198
199
200
201
  protected function queryTR()
  {
  	 return $this->queryTableData()."/x:TR";
  }
10200969   Roipoussiere   Remove whitespace...
202