Skip to content

Commit b9970bf

Browse files
authored
Merge pull request #3 from byjg/1.0.8
Some Adjustments
2 parents 9023edc + 6af6365 commit b9970bf

File tree

6 files changed

+154
-74
lines changed

6 files changed

+154
-74
lines changed

.run/PHPUnit.run.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="PHPUnit" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
3+
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" scope="XML" use_alternative_configuration_file="true" />
4+
<method v="2" />
5+
</configuration>
6+
</component>

.travis.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
language: php
2+
3+
dist: bionic
4+
25
php:
3-
- "7.2"
4-
- "7.1"
5-
- "7.0"
6-
- "5.6"
6+
- "7.4"
7+
- "7.3"
78

89
install:
910
- composer install

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# PHP SPARQL Lib
2-
[![Build Status](https://travis-ci.org/byjg/PHP-SPARQL-Lib.svg?branch=master)](https://travis-ci.org/byjg/PHP-SPARQL-Lib)
2+
[![Build Status](https://travis-ci.com/byjg/PHP-SPARQL-Lib.svg?branch=master)](https://travis-ci.com/byjg/PHP-SPARQL-Lib)
33
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/41d16c0b-fdd6-4c00-8b46-9197b9489659/mini.png)](https://insight.sensiolabs.com/projects/41d16c0b-fdd6-4c00-8b46-9197b9489659)
44

55
Copyright 2010,2011,2012 Christopher Gutteridge & University of Southampton
@@ -26,7 +26,19 @@ print "<table class='example_table'>";
2626
print "<tr>";
2727
foreach( $fields as $field )
2828
{
29-
print "<th>$field</th>";
29+
print "<th>$field</th>";
30+
}
31+
```
32+
33+
## Simplified call
34+
35+
```php
36+
$results = new \SparQL\Connection::get( "http://rdf.ecs.soton.ac.uk/sparql/" )
37+
->withNamespace( "foaf","http://xmlns.com/foaf/0.1/" )
38+
->fetch("SELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5");
39+
40+
foreach ($results as $item) {
41+
print "<th>" . $item["person"]
3042
}
3143
```
3244

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
}
1313
],
1414
"require": {
15-
"byjg/webrequest": "1.0.*"
15+
"byjg/webrequest": "2.0.*",
16+
"ext-curl": "*"
1617
},
1718
"require-dev": {
1819
"phpunit/phpunit": "5.7.*|7.4.*"

src/Connection.php

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace SparQL;
44

5-
use ByJG\Util\CurlException;
6-
use ByJG\Util\WebRequest;
5+
use ByJG\Util\Exception\CurlException;
6+
use ByJG\Util\HttpClient;
7+
use ByJG\Util\Psr7\Request;
8+
use ByJG\Util\Uri;
79

810
class Connection
911
{
@@ -13,6 +15,8 @@ class Connection
1315
protected $namespace = array();
1416
protected $params = null;
1517
protected $endpoint;
18+
protected $timeout = null;
19+
protected $validateSsl = true;
1620

1721
# capabilities are either true, false or null if not yet tested.
1822

@@ -21,11 +25,6 @@ public function __construct($endpoint)
2125
$this->endpoint = $endpoint;
2226
}
2327

24-
public function ns($short, $long)
25-
{
26-
$this->namespace[$short] = $long;
27-
}
28-
2928
/**
3029
* @param null $params
3130
* @return null
@@ -46,32 +45,30 @@ public function cgiParams($params = null)
4645

4746
/**
4847
* @param string $query
49-
* @param int|null $timeout
5048
* @return Result
5149
* @throws \SparQL\ConnectionException
5250
* @throws \SparQL\Exception
5351
*/
54-
public function query($query, $timeout = null)
52+
public function query($query)
5553
{
5654
$prefixes = "";
5755
foreach ($this->namespace as $k => $v) {
5856
$prefixes .= "PREFIX $k: <$v>\n";
5957
}
60-
$output = $this->dispatchQuery($prefixes . $query, $timeout);
58+
$output = $this->dispatchQuery($prefixes . $query);
6159

6260
$parser = new ParseXml($output);
6361

6462
return new Result($parser->rows, $parser->fields);
6563
}
6664

6765
/**
68-
* @param int $timeout
6966
* @return bool
7067
*/
71-
public function alive($timeout = 3000)
68+
public function alive()
7269
{
7370
try {
74-
$this->query("SELECT * WHERE { ?s ?p ?o } LIMIT 1", $timeout);
71+
$this->query("SELECT * WHERE { ?s ?p ?o } LIMIT 1");
7572
return true;
7673
} catch (\Exception $ex) {
7774
return false;
@@ -81,12 +78,11 @@ public function alive($timeout = 3000)
8178
/**
8279
*
8380
* @param string $sparql
84-
* @param int $timeout Timeout in mileseconds
8581
* @return string
8682
* @throws ConnectionException
8783
* @throws \SparQL\Exception
8884
*/
89-
public function dispatchQuery($sparql, $timeout = null)
85+
public function dispatchQuery($sparql)
9086
{
9187
$url = $this->endpoint . "?query=" . urlencode($sparql);
9288
if ($this->params !== null) {
@@ -100,25 +96,31 @@ public function dispatchQuery($sparql, $timeout = null)
10096
;
10197
}
10298

103-
$webRequest = new WebRequest($url);
104-
105-
if (!empty($timeout)) {
106-
$webRequest->setCurlOption(CURLOPT_TIMEOUT_MS, $timeout);
99+
$request = Request::getInstance(Uri::getInstanceFromString($url));
100+
$client = HttpClient::getInstance();
101+
102+
if (!empty($this->timeout)) {
103+
$client->withCurlOption(CURLOPT_TIMEOUT_MS, $this->timeout * 1000);
104+
}
105+
106+
if ($this->validateSsl) {
107+
$client->withNoSSLVerification();
107108
}
108109

109110
try {
110-
$output = $webRequest->get();
111+
$response = $client->sendRequest($request);
112+
$output = $response->getBody();
111113
} catch (CurlException $ex) {
112114
throw new ConnectionException($ex->getMessage());
113115
}
114116

115117
if ($output === '') {
116118
throw new ConnectionException('URL returned no data', -1);
117119
}
118-
if ($webRequest->getLastStatus() != 200) {
120+
if ($response->getStatusCode() != 200) {
119121
throw new Exception(
120-
'Bad response, ' . $webRequest->getLastStatus() . ': ' . $output,
121-
$webRequest->getLastStatus()
122+
'Bad response, ' . $response->getStatusCode() . ': ' . $output,
123+
$response->getStatusCode()
122124
);
123125
}
124126

@@ -127,26 +129,68 @@ public function dispatchQuery($sparql, $timeout = null)
127129

128130
/**
129131
* @param string $endpoint
130-
* @param string $sparql
131-
* @param array $namespace
132-
* @return Results
133-
* @throws \SparQL\ConnectionException
134-
* @throws \SparQL\Exception
132+
* @return Connection
133+
*/
134+
public static function get($endpoint)
135+
{
136+
return new Connection($endpoint);
137+
138+
}
139+
140+
/**
141+
* @param $prefix
142+
* @param $uri
143+
* @return $this
135144
*/
136-
public static function get($endpoint, $sparql, $namespace = null)
145+
public function withNamespace($prefix, $uri = "")
137146
{
138-
$connection = new Connection($endpoint);
139-
foreach ((array) $namespace as $key => $value) {
140-
$connection->ns($key, $value);
147+
if (is_array($prefix)) {
148+
foreach ($prefix as $key => $value) {
149+
return $this->withNamespace($key, $value);
150+
}
151+
} else {
152+
$this->namespace[$prefix] = $uri;
153+
}
154+
return $this;
155+
}
156+
157+
/**
158+
* @param $timeout
159+
* @return Connection
160+
*/
161+
public function withTimeout($timeout)
162+
{
163+
$this->timeout = $timeout;
164+
return $this;
165+
}
166+
167+
public function withNoSSLVerification()
168+
{
169+
$this->validateSsl = false;
170+
return $this;
171+
}
172+
173+
/**
174+
* @param $sparql
175+
* @param array $params
176+
* @return Results|null
177+
* @throws ConnectionException
178+
* @throws Exception
179+
*/
180+
public function fetch($sparql, $params = [])
181+
{
182+
foreach ($params as $key => $value) {
183+
$sparql = str_replace(":$key", "'$value'", $sparql);
141184
}
142185

143-
$result = $connection->query($sparql);
186+
$result = $this->query($sparql);
144187
if (!$result) {
145188
return null;
146189
}
147190

148191
return $result->fetchAll();
149192
}
193+
150194
####################################
151195
# Endpoint Capability Testing
152196
####################################

tests/SparQL/ConnectionTest.php

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
class ConnectionTest extends TestCase
88
{
99

10-
const SPARQL_URL = 'http://dbpedia.org/sparql';
10+
const SPARQL_URL = 'https://dbpedia.org/sparql';
1111

1212
protected static $SPARQL_NS = [
1313
'dbpedia-owl' => 'http://dbpedia.org/ontology/',
14-
'dbpprop' => 'http://dbpedia.org/property/'
14+
'dbpprop' => 'http://dbpedia.org/property/',
1515
];
1616

1717
/**
@@ -32,12 +32,16 @@ protected function tearDown()
3232

3333
}
3434

35+
/**
36+
* @throws ConnectionException
37+
* @throws Exception
38+
*/
3539
public function testQueryOk()
3640
{
37-
$connection = new Connection(self::SPARQL_URL);
38-
foreach (self::$SPARQL_NS as $key => $value) {
39-
$connection->ns($key, $value);
40-
}
41+
$connection = Connection::get(self::SPARQL_URL)
42+
->withNoSSLVerification()
43+
->withTimeout(10)
44+
->withNamespace(self::$SPARQL_NS);
4145

4246
$result = $connection->query("select distinct ?Concept where {[] a ?Concept} LIMIT 5");
4347

@@ -50,11 +54,12 @@ public function testQueryOk()
5054
*/
5155
public function testGetOk()
5256
{
53-
$result = Connection::get(
54-
self::SPARQL_URL,
55-
"select distinct ?Concept where {[] a ?Concept} LIMIT 5",
56-
self::$SPARQL_NS
57-
);
57+
$result = Connection::get(self::SPARQL_URL)
58+
->withNamespace(self::$SPARQL_NS)
59+
->withNoSSLVerification()
60+
->withTimeout(10)
61+
->fetch("select distinct ?Concept where {[] a ?Concept} LIMIT 5");
62+
5863
$this->assertEquals(5, count($result));
5964
}
6065

@@ -63,46 +68,57 @@ public function testGetOk()
6368
*/
6469
public function testWrongSparQLDataset()
6570
{
66-
Connection::get(
67-
"http://invaliddomain:9812/",
68-
"select distinct ?Concept where {[] a ?Concept} LIMIT 5",
69-
self::$SPARQL_NS
70-
);
71+
Connection::get("http://invaliddomain:9812/")
72+
->withNamespace(self::$SPARQL_NS)
73+
->withNoSSLVerification()
74+
->withTimeout(10)
75+
->fetch("select distinct ?Concept where {[] a ?Concept} LIMIT 5");
7176
}
7277

7378
/**
7479
* @expectedException \SparQL\Exception
80+
* @throws ConnectionException
7581
*/
7682
function test_wrongSparQLDataset2()
7783
{
78-
Connection::get(self::SPARQL_URL, "?Concept {[] a ?Concept} LIMIT 5"); // Without NS
84+
Connection::get(self::SPARQL_URL)
85+
->withNoSSLVerification()
86+
->withTimeout(10)
87+
->fetch( "?Concept {[] a ?Concept} LIMIT 5"); // Without NS
7988
}
8089

8190
function test_navigateSparQLDataset()
8291
{
83-
$result = Connection::get(
84-
self::SPARQL_URL,
85-
'SELECT ?name ?meaning
86-
WHERE
92+
$result = Connection::get(self::SPARQL_URL)
93+
->withNamespace(self::$SPARQL_NS)
94+
->withNoSSLVerification()
95+
->withTimeout(10)
96+
->fetch(
97+
'SELECT *
98+
WHERE
8799
{
88-
?s a dbpedia-owl:Name;
89-
dbpprop:name ?name;
90-
dbpprop:meaning ?meaning
91-
. FILTER (str(?name) = "Garrick")
100+
?athlete rdfs:label "Cristiano Ronaldo"@en ;
101+
dbo:birthPlace ?place .
102+
?place a dbo:City ;
103+
rdfs:label ?cityName .
104+
FILTER ( LANG ( ?cityName ) = :lang )
92105
}',
93-
self::$SPARQL_NS
94-
);
106+
[
107+
"lang" => "en"
108+
]
109+
);
95110

96-
$this->assertEquals(1, count($result));
111+
$this->assertEquals(count($result), 1);
97112

98-
99-
100-
$this->assertEquals($result[0]["name"], "Garrick");
101-
$this->assertEquals($result[0]["name.type"], "literal");
102-
$this->assertEquals($result[0]["meaning"], "\"spear king\"");
103-
$this->assertEquals($result[0]["meaning.type"], "literal");
113+
$this->assertEquals("http://dbpedia.org/resource/Cristiano_Ronaldo", $result[0]["athlete"]);
114+
$this->assertEquals("uri", $result[0]["athlete.type"]);
115+
$this->assertEquals("http://dbpedia.org/resource/Funchal", $result[0]["place"]);
116+
$this->assertEquals("uri", $result[0]["place.type"]);
104117
}
105118

119+
/**
120+
* @throws Exception
121+
*/
106122
function testSupports()
107123
{
108124
$connection = new Connection(self::SPARQL_URL);

0 commit comments

Comments
 (0)