From 36b4c60e58f91fe49c807a0f59e27141b544afc6 Mon Sep 17 00:00:00 2001 From: Simon Stier <52674635+simontaurus@users.noreply.github.com> Date: Sat, 4 May 2024 08:28:50 +0200 Subject: [PATCH 01/12] fix: set correct Accept header for CONSTRUCT queries Refs #47 --- lib/Sparql/Client.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/Sparql/Client.php b/lib/Sparql/Client.php index 5afe621a..6ccc7ff0 100644 --- a/lib/Sparql/Client.php +++ b/lib/Sparql/Client.php @@ -338,6 +338,13 @@ protected function executeQuery($processed_query, $type) 'application/sparql-results+json' => 1.0, 'application/sparql-results+xml' => 0.8, ]; + $sparql_graph_types = [ + 'application/ld+json' => 1.0, + 'application/rdf+xml' => 0.9, + 'text/turtle' => 0.8, + 'application/n-quads' => 0.7, + 'application/n-triples' => 0.7, + ]; if ('update' == $type) { // accept anything, as "response body of a […] update request is implementation defined" @@ -368,7 +375,7 @@ protected function executeQuery($processed_query, $type) $accept = Format::formatAcceptHeader($sparql_results_types); } elseif ('CONSTRUCT' === $query_verb || 'DESCRIBE' === $query_verb) { // only "graph" - $accept = Format::getHttpAcceptHeader(); + $accept = Format::formatAcceptHeader($sparql_graph_types); } else { // both $accept = Format::getHttpAcceptHeader($sparql_results_types); From 4a55396a227afc0a32aa17eb1a770b9704691144 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Thu, 9 May 2024 08:25:00 +0200 Subject: [PATCH 02/12] ClientTest.php: added test case to reproduce the error --- tests/EasyRdf/Sparql/ClientTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/EasyRdf/Sparql/ClientTest.php b/tests/EasyRdf/Sparql/ClientTest.php index 006e4ff9..6138a29b 100644 --- a/tests/EasyRdf/Sparql/ClientTest.php +++ b/tests/EasyRdf/Sparql/ClientTest.php @@ -606,4 +606,20 @@ private static function parseAcceptHeader($accept_str) return $types; } + + /** + * @see https://github.com/sweetrdf/easyrdf/pull/48 + */ + public function testIssue47CorrectHeaderForConstructQueries(): void + { + Http::setDefaultHttpClient(new HttpClient()); + + $endpointUrl = 'https://dbpedia.org/sparql'; + $dummyConstructQueryString = 'construct { ?s ?q ?r } where { ?s ?p ?o . ?o ?q ?r } limit 1'; + $endPoint = new Client($endpointUrl); + $result = $endPoint->query($dummyConstructQueryString); + + // its also an instance of Graph without the changes in this PR + $this->assertTrue($result instanceof Graph); + } } From 2eeec6d829d43e0a30037249c7ba8b5312fd684e Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Thu, 9 May 2024 08:26:06 +0200 Subject: [PATCH 03/12] fixed failing test in OpenGraphProtocolTest.php --- tests/ExampleTest/OpenGraphProtocolTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ExampleTest/OpenGraphProtocolTest.php b/tests/ExampleTest/OpenGraphProtocolTest.php index 6ff4bd1f..f06c58de 100644 --- a/tests/ExampleTest/OpenGraphProtocolTest.php +++ b/tests/ExampleTest/OpenGraphProtocolTest.php @@ -43,7 +43,7 @@ public function testRottenTomatoes() { $output = executeExample('open_graph_protocol.php'); $this->assertStringContainsString('
assertStringContainsString('
Title:
Ocean\'s Eleven
', $output); + $this->assertStringContainsString('
Title:
Ocean\'s Eleven', $output); $this->assertStringContainsString('
Description:
', $output); } } From d0e6aa4d11de5c13eb590bbb951c60359c9f76c0 Mon Sep 17 00:00:00 2001 From: Simon Stier <52674635+simontaurus@users.noreply.github.com> Date: Fri, 10 May 2024 10:51:20 +0200 Subject: [PATCH 04/12] add test with wikidata endpoint --- tests/EasyRdf/Sparql/ClientTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/EasyRdf/Sparql/ClientTest.php b/tests/EasyRdf/Sparql/ClientTest.php index 6138a29b..49f63fb8 100644 --- a/tests/EasyRdf/Sparql/ClientTest.php +++ b/tests/EasyRdf/Sparql/ClientTest.php @@ -621,5 +621,12 @@ public function testIssue47CorrectHeaderForConstructQueries(): void // its also an instance of Graph without the changes in this PR $this->assertTrue($result instanceof Graph); + + $endpointUrl = 'https://query.wikidata.org/sparql'; + $endPoint = new Client($endpointUrl); + $result = $endPoint->query($dummyConstructQueryString); + + // its an instance of Result without the changes in this PR + $this->assertTrue($result instanceof Graph); } } From f245c712b7bdc22013d93c071934979ea63e1c51 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 10 May 2024 16:14:07 +0200 Subject: [PATCH 05/12] GraphTest.php: added test to show same faulty behavior in Graph::load method --- tests/EasyRdf/GraphTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/EasyRdf/GraphTest.php b/tests/EasyRdf/GraphTest.php index 8a8dbc35..3e1ae4bc 100644 --- a/tests/EasyRdf/GraphTest.php +++ b/tests/EasyRdf/GraphTest.php @@ -392,6 +392,20 @@ public function testLoadRedirect() ); } + /** + * Setup a Graph instance using a real Client to load a CONSTRUCT result. + * + * @see https://github.com/sweetrdf/easyrdf/pull/48 + */ + public function testIssue47GraphLoadRdfFile(): void + { + Http::setDefaultHttpClient(new Client()); + $graph = new Graph(); + $url = 'https://query.wikidata.org/sparql?query=construct+%7B+%3Fs+%3Fq+%3Fr+%7D+where+%7B+%3Fs+%3Fp+%3Fo+.+%3Fo+%3Fq+%3Fr+%7D+limit+1'; + $tripleCount = $graph->load($url); + $this->assertTrue(0 < $tripleCount); + } + public function testNewAndLoad() { $this->client->addMockOnce('GET', 'http://www.example.com/', readFixture('foaf.json')); From fd7359ccc74610c1a5dc19297365e05a4ae27f96 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 10 May 2024 16:15:38 +0200 Subject: [PATCH 06/12] Graph.php: Graph::load uses correct Accept-headers from now on --- lib/Graph.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Graph.php b/lib/Graph.php index 9970f6f7..a173bf17 100644 --- a/lib/Graph.php +++ b/lib/Graph.php @@ -323,11 +323,19 @@ public function load($uri = null, $format = null) } } } else { + $acceptHeader = Format::formatAcceptHeader([ + 'application/ld+json' => 1.0, + 'application/rdf+xml' => 0.9, + 'text/turtle' => 0.8, + 'application/n-quads' => 0.7, + 'application/n-triples' => 0.7, + ]); + // Send a list of all the formats we can parse if ($client instanceof Client) { - $client->setHeaders('Accept', Format::getHttpAcceptHeader()); + $client->setHeaders('Accept', $acceptHeader); } else { - $client->setHeaders(['Accept' => Format::getHttpAcceptHeader()]); + $client->setHeaders(['Accept' => $acceptHeader]); } } From 77bb203421531fb3a2dc5d63007cc0a4210d34ed Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 10 May 2024 16:20:04 +0200 Subject: [PATCH 07/12] Update GraphTest.php --- tests/EasyRdf/GraphTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/EasyRdf/GraphTest.php b/tests/EasyRdf/GraphTest.php index 3e1ae4bc..8020a0f5 100644 --- a/tests/EasyRdf/GraphTest.php +++ b/tests/EasyRdf/GraphTest.php @@ -42,6 +42,7 @@ use EasyRdf\Format; use EasyRdf\Graph; use EasyRdf\Http; +use EasyRdf\Http\Client; use EasyRdf\Literal; use EasyRdf\ParsedUri; use EasyRdf\RdfNamespace; From c7ac4b3f83b298e5cdeaabeaa01b2a50f9cdd92f Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 10 May 2024 16:27:04 +0200 Subject: [PATCH 08/12] Update GraphTest.php --- tests/EasyRdf/GraphTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/EasyRdf/GraphTest.php b/tests/EasyRdf/GraphTest.php index 8020a0f5..32713972 100644 --- a/tests/EasyRdf/GraphTest.php +++ b/tests/EasyRdf/GraphTest.php @@ -274,7 +274,7 @@ public function testLoadGraphUri() public function testLoadWithContentType() { $checkRequest = function ($client) { - $this->assertStringContainsString(',application/json,', $client->getHeader('Accept')); + $this->assertStringContainsString('application/ld+json,', $client->getHeader('Accept')); return true; }; From f605c77f956c733e4326ac23c01ddf86a112192d Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Fri, 10 May 2024 16:29:25 +0200 Subject: [PATCH 09/12] ClientTest.php: refined new test for better readability --- tests/EasyRdf/Sparql/ClientTest.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/EasyRdf/Sparql/ClientTest.php b/tests/EasyRdf/Sparql/ClientTest.php index 49f63fb8..203b84d1 100644 --- a/tests/EasyRdf/Sparql/ClientTest.php +++ b/tests/EasyRdf/Sparql/ClientTest.php @@ -613,20 +613,22 @@ private static function parseAcceptHeader($accept_str) public function testIssue47CorrectHeaderForConstructQueries(): void { Http::setDefaultHttpClient(new HttpClient()); + $query = 'construct { ?s ?q ?r } where { ?s ?p ?o . ?o ?q ?r } limit 1'; + /* + * use DBpedia's SPARQL endpoint and check result + */ $endpointUrl = 'https://dbpedia.org/sparql'; - $dummyConstructQueryString = 'construct { ?s ?q ?r } where { ?s ?p ?o . ?o ?q ?r } limit 1'; $endPoint = new Client($endpointUrl); - $result = $endPoint->query($dummyConstructQueryString); - - // its also an instance of Graph without the changes in this PR - $this->assertTrue($result instanceof Graph); + $result = $endPoint->query($query); + $this->assertTrue($result instanceof Graph, '$result is an instance of '.get_class($result)); + /* + * use DBpedia's SPARQL endpoint and check result + */ $endpointUrl = 'https://query.wikidata.org/sparql'; $endPoint = new Client($endpointUrl); - $result = $endPoint->query($dummyConstructQueryString); - - // its an instance of Result without the changes in this PR - $this->assertTrue($result instanceof Graph); + $result = $endPoint->query($query); + $this->assertTrue($result instanceof Graph, '$result is an instance of '.get_class($result)); } } From b55fe3e826d2ee92899e923fe82c268bb364042f Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Mon, 13 May 2024 08:31:03 +0200 Subject: [PATCH 10/12] Update tests/EasyRdf/Sparql/ClientTest.php Co-authored-by: Ted Thibodeau Jr --- tests/EasyRdf/Sparql/ClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/EasyRdf/Sparql/ClientTest.php b/tests/EasyRdf/Sparql/ClientTest.php index 203b84d1..320d4f88 100644 --- a/tests/EasyRdf/Sparql/ClientTest.php +++ b/tests/EasyRdf/Sparql/ClientTest.php @@ -624,7 +624,7 @@ public function testIssue47CorrectHeaderForConstructQueries(): void $this->assertTrue($result instanceof Graph, '$result is an instance of '.get_class($result)); /* - * use DBpedia's SPARQL endpoint and check result + * use Wikidata's SPARQL endpoint to check result */ $endpointUrl = 'https://query.wikidata.org/sparql'; $endPoint = new Client($endpointUrl); From 3571182eb88403a70358108c85e7882b2e4231ad Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Mon, 13 May 2024 08:31:09 +0200 Subject: [PATCH 11/12] Update tests/EasyRdf/Sparql/ClientTest.php Co-authored-by: Ted Thibodeau Jr --- tests/EasyRdf/Sparql/ClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/EasyRdf/Sparql/ClientTest.php b/tests/EasyRdf/Sparql/ClientTest.php index 320d4f88..51149a91 100644 --- a/tests/EasyRdf/Sparql/ClientTest.php +++ b/tests/EasyRdf/Sparql/ClientTest.php @@ -616,7 +616,7 @@ public function testIssue47CorrectHeaderForConstructQueries(): void $query = 'construct { ?s ?q ?r } where { ?s ?p ?o . ?o ?q ?r } limit 1'; /* - * use DBpedia's SPARQL endpoint and check result + * use DBpedia's SPARQL endpoint to check result */ $endpointUrl = 'https://dbpedia.org/sparql'; $endPoint = new Client($endpointUrl); From 72b47cd727c852b49d68e1e39a94549bdc9e1798 Mon Sep 17 00:00:00 2001 From: Konrad Abicht Date: Mon, 13 May 2024 08:31:17 +0200 Subject: [PATCH 12/12] Update tests/EasyRdf/GraphTest.php Co-authored-by: Ted Thibodeau Jr --- tests/EasyRdf/GraphTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/EasyRdf/GraphTest.php b/tests/EasyRdf/GraphTest.php index 32713972..319e73b6 100644 --- a/tests/EasyRdf/GraphTest.php +++ b/tests/EasyRdf/GraphTest.php @@ -394,7 +394,7 @@ public function testLoadRedirect() } /** - * Setup a Graph instance using a real Client to load a CONSTRUCT result. + * Set up a Graph instance using a real Client to load a CONSTRUCT result. * * @see https://github.com/sweetrdf/easyrdf/pull/48 */