Skip to content

Commit

Permalink
ported PR easyrdf#404 (#35)
Browse files Browse the repository at this point in the history
Author: @nevali

Title: Prevent RdfNamespace::splitUri() from generating prefix:#fragment
qnames

Quote from original PR:

> it's possible to coax EasyRdf\RdfNamespace::splitUri() into generating
a qname in the form prefix:#fragment, which is not valid, for example by
defining a namespace as https://www.example.com/foo/ and then referring
to https://www.example.com/foo/#bar; this fix prevents that
  • Loading branch information
k00ni authored Aug 29, 2023
1 parent ecc453d commit 899d102
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/RdfNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ public static function splitUri($uri, $createNamespace = false)

$local_part = substr($uri, \strlen($long));

if (str_contains($local_part, '/')) {
// we can't have '/' in local part
// we can't have '/' or '#' in local part
if (str_contains($local_part, '/') || str_contains($local_part, '#')) {
continue;
}

Expand Down Expand Up @@ -394,7 +394,7 @@ public static function prefixOfUri($uri)
* @param string $uri The full URI (eg 'http://xmlns.com/foaf/0.1/name')
* @param bool $createNamespace If true, a new namespace will be created
*
* @return string|void The shortened URI (eg 'foaf:name') or null
* @return string|void The shortened URI (eg 'foaf:name') or void
*/
public static function shorten($uri, $createNamespace = false)
{
Expand Down
12 changes: 12 additions & 0 deletions tests/EasyRdf/RdfNamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,16 @@ public function testShortNamespace()
RdfNamespace::shorten('http://example.org/bar/baz')
);
}

/**
* URIs with fragments can only be shortened if the '#' character
* is part of the prefix. `prefix:[...]#fragment` is not a valid result
*/
public function testNoShortFragment()
{
RdfNamespace::set('ex', 'http://example.org/');

$this->assertNull(RdfNamespace::shorten('http://example.org/foo#bar'));
$this->assertNull(RdfNamespace::shorten('http://example.org/#quack'));
}
}
17 changes: 15 additions & 2 deletions tests/EasyRdf/Serialiser/TurtleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,26 @@ public function testSerialiseUnknownDatatype()
public function testSerialiseShortenableResource()
{
RdfNamespace::set('example', 'http://example.com/');
$joe = $this->graph->resource('http://example.com/joe#me');
$joe = $this->graph->resource('http://example.com/joe');
$joe->add('rdf:type', 'foaf:Person');

$turtle = $this->serialiser->serialise($this->graph, 'turtle');
$this->assertSame(
"@prefix example: <http://example.com/> .\n\n".
"example:joe#me a \"foaf:Person\" .\n",
"example:joe a \"foaf:Person\" .\n",
$turtle
);
}

public function testSerialiseUnshortenableResource()
{
RdfNamespace::set('example', 'http://example.com/');
$joe = $this->graph->resource('http://example.com/joe#me');
$joe->add('rdf:type', 'foaf:Person');

$turtle = $this->serialiser->serialise($this->graph, 'turtle');
$this->assertSame(
"<http://example.com/joe#me> a \"foaf:Person\" .\n",
$turtle
);
}
Expand Down

0 comments on commit 899d102

Please sign in to comment.