sparql_connection::errno()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
###############################
4
# Christopher Gutteridge 2010
5
#  [email protected]
6
#  LGPL License 
7
#  http://graphite.ecs.soton.ac.uk/sparqllib/
8
#  https://github.com/cgutteridge/PHP-SPARQL-Lib
9
###############################
10
11
# to document: CGIParams
12
13
function sparql_connect( $endpoint ) { return new sparql_connection( $endpoint ); }
14
15
function sparql_ns( $short, $long, $db = null ) { return _sparql_a_connection( $db )->ns( $short, $long ); }
16
function sparql_query( $sparql, $db = null ) { return _sparql_a_connection( $db )->query( $sparql ); }
17
function sparql_errno( $db = null ) { return _sparql_a_connection( $db )->errno(); }
18
function sparql_error( $db = null ) { return _sparql_a_connection( $db )->error(); }
19
20
function sparql_fetch_array( $result ) { return $result->fetch_array(); }
21
function sparql_num_rows( $result ) { return $result->num_rows(); }
22
function sparql_field_array( $result ) { return $result->field_array(); }
23
function sparql_field_name( $result, $i ) { return $result->field_name( $i ); }
24
25
function sparql_fetch_all( $result ) { return $result->fetch_all(); }
26
27
function sparql_get( $endpoint, $sparql ) 
28
{ 
29
	$db = sparql_connect( $endpoint );
30
	if( !$db ) { return; }
31
	$result = $db->query( $sparql );
32
	if( !$result ) { return; }
33
	return $result->fetch_all(); 
34
}
35
36
function _sparql_a_connection( $db )
37
{
38
	global $sparql_last_connection;
39
	if( !isset( $db ) )
40
	{
41
		if( !isset( $sparql_last_connection ) )
42
		{
43
			print( "No currect SPARQL connection (connection) in play!" );
44
			return;
45
		}
46
		$db = $sparql_last_connection;
47
	}
48
	return $db;
49
}
50
		
51
52
#	$timeout = 20;
53
#	$old = ini_set('default_socket_timeout', $timeout);
54
#	ini_set('default_socket_timeout', $old);
55
class sparql_connection
56
{
57
	var $db;
58
	var $debug = false;
59
	var $errno = null;
60
	var $error = null;
61
	var $ns = array();
62
	var $params = null;
63
	# capabilities are either true, false or null if not yet tested.
64
65
	function __construct( $endpoint )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
	{
67
		$this->endpoint = $endpoint;
0 ignored issues
show
Bug introduced by
The property endpoint does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
		global $sparql_last_connection;
69
		$sparql_last_connection = $this;
70
	}
71
72
	function ns( $short, $long )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
	{
74
		$this->ns[$short] = $long;
75
	}
76
77
	function errno() { return $this->errno; }
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
	function error() { return $this->error; }
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
79
80
	function cgiParams( $params = null )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
81
	{
82
		if( $params === null ) { return $this->params; }
83
		if( $params === "" ) { $this->params = null; return; }
84
		$this->params = $params;
85
	}
86
87
	function query( $query, $timeout=null )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
	{	
89
		$prefixes = "";
90
		foreach( $this->ns as $k=>$v )
91
		{
92
			$prefixes .= "PREFIX $k: <$v>\n";
93
		}
94
		$output = $this->dispatchQuery( $prefixes.$query, $timeout );
95
		if( $this->errno ) { return; }
96
		$parser = new xx_xml($output, 'contents');
97
		if( $parser->error() ) 
98
		{ 
99
			$this->errno = -1; # to not clash with CURLOPT return; }
100
			$this->error = $parser->error();
101
			return;
102
		}
103
		return new sparql_result( $this, $parser->rows, $parser->fields );
0 ignored issues
show
Bug introduced by
The property rows does not seem to exist in xx_xml.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property fields does not seem to exist in xx_xml.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
104
	}
105
106
	function alive( $timeout=3 )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
107
	{
108
		$result = $this->query( "SELECT * WHERE { ?s ?p ?o } LIMIT 1", $timeout );
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
110
		if( $this->errno ) { return false; }
111
112
		return true;
113
	}	
114
115
	function dispatchQuery( $sparql, $timeout=null )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
	{
117
		$url = $this->endpoint."?query=".urlencode( $sparql );
118
		if( $this->params !== null )
119
		{
120
			$url .= "&".$this->params;
121
		}
122
		if( $this->debug ) { print "<div class='debug'><a href='".htmlspecialchars($url)."'>".htmlspecialchars($prefixes.$query)."</a></div>\n"; }
0 ignored issues
show
Bug introduced by
The variable $prefixes does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $query does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
123
		$this->errno = null;
124
		$this->error = null;
125
		$ch = curl_init($url);
126
		#curl_setopt($ch, CURLOPT_HEADER, 1);
127
		if( $timeout !== null )
128
		{
129
			curl_setopt($ch, CURLOPT_TIMEOUT, $timeout );
130
		}
131
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
132
		curl_setopt($ch, CURLOPT_HTTPHEADER,array (
133
			"Accept: application/sparql-results+xml"
134
		));
135
136
		$output = curl_exec($ch);      
137
		$info = curl_getinfo($ch);
138
		if(curl_errno($ch))
139
		{
140
			$this->errno = curl_errno( $ch );
141
			$this->error = 'Curl error: ' . curl_error($ch);
142
			return;
143
		}
144
		if( $output === '' )
145
		{
146
			$this->errno = "-1";
147
			$this->error = 'URL returned no data';
148
			return;
149
		}
150
		if( $info['http_code'] != 200) 
151
		{
152
			$this->errno = $info['http_code'];
153
			$this->error = 'Bad response, '.$info['http_code'].': '.$output;
154
			return;
155
		}
156
		curl_close($ch);
157
158
		return $output;
159
	}
160
161
	####################################
162
	# Endpoint Capability Testing
163
	####################################
164
165
	# This section is very limited right now. I plan, in time, to
166
	# caching so it can save results to a cache to save re-doing them 
167
	# and many more capability options (suggestions to [email protected])
168
169
	var $caps = array();
170
	var $caps_desc = array(
171
		"select"=>"Basic SELECT",
172
		"constant_as"=>"SELECT (\"foo\" AS ?bar)",
173
		"math_as"=>"SELECT (2+3 AS ?bar)",
174
		"count"=>"SELECT (COUNT(?a) AS ?n) ?b ... GROUP BY ?b",
175
		"max"=>"SELECT (MAX(?a) AS ?n) ?b ... GROUP BY ?b",
176
		"sample"=>"SELECT (SAMPLE(?a) AS ?n) ?b ... GROUP BY ?b",
177
		"load"=>"LOAD <...>",
178
	); 
179
180
	var $caps_cache;
181
	var $caps_anysubject;
182
	function capabilityCache( $filename, $dba_type='db4' )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
183
	{
184
		$this->caps_cache = dba_open($filename, "c", $dba_type );
185
	}
186
	function capabilityCodes()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
187
	{
188
		return array_keys( $this->caps_desc );
189
	}
190
	function capabilityDescription($code)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
191
	{
192
		return $this->caps_desc[$code];
193
	}
194
195
	# return true if the endpoint supports a capability
196
	# nb. returns false if connecion isn't authoriased to use the feature, eg LOAD
197
	function supports( $code ) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
198
	{
199
		if( isset( $this->caps[$code] ) ) { return $this->caps[$code]; }
200
		$was_cached = false;
201
		if( isset( $this->caps_cache ) )
202
		{
203
			$CACHE_TIMEOUT_SECONDS = 7*24*60*60;
204
			$db_key = $this->endpoint.";".$code;
205
			$db_val = dba_fetch( $db_key, $this->caps_cache );
206
			if( $db_val !== false )
207
			{
208
				list( $result, $when ) = preg_split( '/;/', $db_val );
209
				if( $when + $CACHE_TIMEOUT_SECONDS > time() )
210
				{
211
					return $result;
212
				}
213
				$was_cached = true;
214
			}
215
		}
216
		$r = null;
0 ignored issues
show
Unused Code introduced by
$r is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
217
218
		if( $code == "select" ) { $r = $this->test_select(); }
219
		elseif( $code == "constant_as" ) { $r = $this->test_constant_as(); }
220
		elseif( $code == "math_as" ) { $r = $this->test_math_as(); }
221
		elseif( $code == "count" ) { $r = $this->test_count(); }
222
		elseif( $code == "max" ) { $r = $this->test_max(); }
223
		elseif( $code == "load" ) { $r = $this->test_load(); }
224
		elseif( $code == "sample" ) { $r = $this->test_sample(); }
225
		else { print "<p>Unknown capability code: '$code'</p>"; return false; }
226
		$this->caps[$code] = $r;
227
		if( isset( $this->caps_cache ) )
228
		{
229
			$db_key = $this->endpoint.";".$code;
230
			$db_val = $r.";".time();
231
			if( $was_cached )
232
			{
233
				dba_replace( $db_key, $db_val, $this->caps_cache );
234
			}
235
			else
236
			{
237
				dba_insert( $db_key, $db_val, $this->caps_cache );
238
			}
239
		}
240
		return $r;
241
	}
242
243
	function anySubject()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
244
	{
245
		if( !isset( $this->caps_anysubject ) )
246
		{
247
			$results = $this->query( 
248
			  "SELECT * WHERE { ?s ?p ?o } LIMIT 1" );
249
			if( sizeof($results)) 
250
			{
251
				$row = $results->fetch_array();
252
				$this->caps_anysubject = $row["s"];
253
			}
254
		}
255
		return $this->caps_anysubject;
256
	}
257
258
	# return true if the endpoint supports SELECT 
259
	function test_select() 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
260
	{
261
		$output = $this->dispatchQuery( 
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
262
		  "SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 1" );
263
		return !isset( $this->errno );
264
	}
265
266
	# return true if the endpoint supports AS
267
	function test_math_as() 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
268
	{
269
		$output = $this->dispatchQuery( 
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
270
		  "SELECT (1+2 AS ?bar) WHERE { ?s ?p ?o } LIMIT 1" );
271
		return !isset( $this->errno );
272
	}
273
274
	# return true if the endpoint supports AS
275
	function test_constant_as() 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
276
	{
277
		$output = $this->dispatchQuery( 
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
278
		  "SELECT (\"foo\" AS ?bar) WHERE { ?s ?p ?o } LIMIT 1" );
279
		return !isset( $this->errno );
280
	}
281
282
	# return true if the endpoint supports SELECT (COUNT(?x) as ?n) ... GROUP BY 
283
	function test_count() 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
284
	{
285
		# assumes at least one rdf:type predicate
286
		$s = $this->anySubject();
287
		if( !isset($s) ) { return false; }
288
		$output = $this->dispatchQuery( 
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
289
		  "SELECT (COUNT(?p) AS ?n) ?o WHERE { <$s> ?p ?o } GROUP BY ?o" );
290
		return !isset( $this->errno );
291
	}
292
293
	function test_max() 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
294
	{
295
		$s = $this->anySubject();
296
		if( !isset($s) ) { return false; }
297
		$output = $this->dispatchQuery( 
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
298
		  "SELECT (MAX(?p) AS ?max) ?o WHERE { <$s> ?p ?o } GROUP BY ?o" );
299
		return !isset( $this->errno );
300
	}
301
302
	function test_sample() 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
303
	{
304
		$s = $this->anySubject();
305
		if( !isset($s) ) { return false; }
306
		$output = $this->dispatchQuery( 
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
307
		  "SELECT (SAMPLE(?p) AS ?sam) ?o WHERE { <$s> ?p ?o } GROUP BY ?o" );
308
		return !isset( $this->errno );
309
	}
310
311
	function test_load() 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
312
	{
313
		$output = $this->dispatchQuery( 
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
314
		  "LOAD <http://graphite.ecs.soton.ac.uk/sparqllib/examples/loadtest.rdf>" );
315
		return !isset( $this->errno );
316
	}
317
318
319
}
320
321
class sparql_result
322
{
323
	var $rows;
324
	var $fields;
325
	var $db;
326
	var $i = 0;
327
	function __construct( $db, $rows, $fields )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
328
	{
329
		$this->rows = $rows;
330
		$this->fields = $fields;
331
		$this->db = $db;
332
	}
333
334
	function fetch_array()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
335
	{
336
		if( !@$this->rows[$this->i] ) { return; }
337
		$r = array();
338
		foreach( $this->rows[$this->i++]  as $k=>$v )
339
		{
340
			$r[$k] = $v["value"];
341
			$r["$k.type"] = $v["type"];
342
			if( isset( $v["language"] ) )
343
			{
344
				$r["$k.language"] = $v["language"];
345
			}
346
			if( isset( $v["datatype"] ) )
347
			{
348
				$r["$k.datatype"] = $v["datatype"];
349
			}
350
		}
351
		return $r;
352
	}
353
354
	function fetch_all()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
355
	{
356
		$r = new sparql_results();
357
		$r->fields = $this->fields;
358
		foreach( $this->rows as $i=>$row )
359
		{
360
			$r []= $this->fetch_array();
361
		}
362
		return $r;
363
	}
364
365
	function num_rows()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
366
	{
367
		return sizeof( $this->rows );
368
	}
369
370
	function field_array()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
371
	{
372
		return $this->fields;
373
	}
374
375
	function field_name($i)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
376
	{
377
		return $this->fields[$i];
378
	}
379
}
380
381
382
# class xx_xml adapted code found at http://php.net/manual/en/function.xml-parse.php
383
# class is cc-by 
384
# hello at rootsy dot co dot uk / 24-May-2008 09:30
385
class xx_xml {
386
387
	// XML parser variables
388
	var $parser;
389
	var $name;
390
	var $attr;
391
	var $data  = array();
392
	var $stack = array();
393
	var $keys;
394
	var $path;
395
	var $looks_legit = false;
396
	var $error;
397
  
398
	// either you pass url atau contents.
399
	// Use 'url' or 'contents' for the parameter
400
	var $type;
401
402
	// function with the default parameter value
403
	function xx_xml($url='http://www.opocot.com', $type='url') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
404
		$this->type = $type;
405
		$this->url  = $url;
0 ignored issues
show
Bug introduced by
The property url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
406
		$this->parse();
407
	}
408
  
409
	function error() { return $this->error; }
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
410
411
	// parse XML data
412
	function parse()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
413
	{
414
		$this->rows = array();
0 ignored issues
show
Bug introduced by
The property rows does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
415
		$this->fields = array();
0 ignored issues
show
Bug introduced by
The property fields does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
416
		$data = '';
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
417
		$this->parser = xml_parser_create ("UTF-8");
418
		xml_set_object($this->parser, $this);
419
		xml_set_element_handler($this->parser, 'startXML', 'endXML');
420
		xml_set_character_data_handler($this->parser, 'charXML');
421
422
		xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
423
424
		if ($this->type == 'url') {
425
			// if use type = 'url' now we open the XML with fopen
426
		  
427
			if (!($fp = fopen($this->url, 'rb'))) {
428
				$this->error("Cannot open {$this->url}");
0 ignored issues
show
Unused Code introduced by
The call to xx_xml::error() has too many arguments starting with "Cannot open {$this->url}".

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to the method xx_xml::error() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
429
			}
430
431
			while (($data = fread($fp, 8192))) {
432
				if (!xml_parse($this->parser, $data, feof($fp))) {
433
					$this->error = sprintf('XML error at line %d column %d',
434
						xml_get_current_line_number($this->parser),
435
						xml_get_current_column_number($this->parser));
436
					return;
437
				}
438
			}
439
	 	} else if ($this->type == 'contents') {
440
	  	// Now we can pass the contents, maybe if you want
441
			// to use CURL, SOCK or other method.
442
			$lines = explode("\n",$this->url);
443
			foreach ($lines as $val) {
444
				$data = $val . "\n";
445
				if (!xml_parse($this->parser, $data)) {
446
					$this->error = $data."\n".sprintf('XML error at line %d column %d',
447
						xml_get_current_line_number($this->parser),
448
				 		xml_get_current_column_number($this->parser));
449
					return;
450
				}
451
			}
452
		}
453
		if( !$this->looks_legit ) 
454
		{
455
			$this->error = "Didn't even see a sparql element, is this really an endpoint?";
456
		}
457
	}
458
459
	function startXML($parser, $name, $attr)	
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
460
	{
461
		if( $name == "sparql" ) { $this->looks_legit = true; }
462
		if( $name == "result" )
463
		{
464
			$this->result = array();
0 ignored issues
show
Bug introduced by
The property result does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
465
		}
466
		if( $name == "binding" )
467
		{
468
			$this->part = $attr["name"];
0 ignored issues
show
Bug introduced by
The property part does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
469
		}
470
		if( $name == "uri" || $name == "bnode" )
471
		{
472
			$this->part_type = $name;
0 ignored issues
show
Bug introduced by
The property part_type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
473
			$this->chars = "";
0 ignored issues
show
Bug introduced by
The property chars does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
474
		}
475
		if( $name == "literal" )
476
		{
477
			$this->part_type = "literal";
478
			if( isset( $attr["datatype"] ) )
479
			{
480
				$this->part_datatype = $attr["datatype"];
0 ignored issues
show
Bug introduced by
The property part_datatype does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
481
			}
482
			if( isset( $attr["xml:lang"] ) )
483
			{
484
				$this->part_lang = $attr["xml:lang"];
0 ignored issues
show
Bug introduced by
The property part_lang does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
485
			}
486
			$this->chars = "";
487
		}
488
		if( $name == "variable" )
489
		{
490
			$this->fields[] = $attr["name"];
491
		}
492
	}
493
494
	function endXML($parser, $name)	{
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
495
		if( $name == "result" )
496
		{
497
			$this->rows[] = $this->result;
498
			$this->result = array();
499
		}
500
		if( $name == "uri" || $name == "bnode" || $name == "literal" )
501
		{
502
			$this->result[$this->part] = array( "type"=>$name, "value"=>$this->chars );
503
			if( isset( $this->part_lang ) )
504
			{
505
				$this->result[$this->part]["lang"] = $this->part_lang;
506
			}
507
			if( isset( $this->part_datatype ) )
508
			{
509
				$this->result[$this->part]["datatype"] = $this->part_datatype;
510
			}
511
			$this->part_datatype = null;
512
			$this->part_lang = null;
513
		}
514
	}
515
516
	function charXML($parser, $data)	{
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
517
		@$this->chars .= $data;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
518
	}
519
520
}
521
522
class sparql_results extends ArrayIterator
523
{
524
	var $fields;
525
	function fields() { return $this->fields; }
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
526
527
	function render_table()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
528
	{
529
		$html = "<table class='sparql-results'><tr>";
530
		foreach( $this->fields as $i=>$field )
531
		{
532
			$html .= "<th>?$field</th>";
533
		}
534
		$html .= "</tr>";
535
		foreach( $this as $row )
536
		{
537
			$html.="<tr>";
538
			foreach( $row as $cell )
539
			{
540
				$html .= "<td>".htmlspecialchars( $cell )."</td>";
541
			}
542
			$html.="</tr>";
543
		}
544
		$html.="</table>
545
<style>
546
table.sparql-results { border-collapse: collapse; }
547
table.sparql-results tr td { border: solid 1px #000 ; padding:4px;vertical-align:top}
548
table.sparql-results tr th { border: solid 1px #000 ; padding:4px;vertical-align:top ; background-color:#eee;}
549
</style>
550
";
551
		return $html;exit;
0 ignored issues
show
Unused Code introduced by
die; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
552
	}
553
554
}
555