Skip to content

Commit 8fb6a75

Browse files
committed
Ensure non-rdf:nil list node references are stored across graphs.
1 parent 8ca3291 commit 8fb6a75

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

jsonld.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,8 +2809,7 @@ protected function _normalize($dataset, $options) {
28092809
protected function _fromRDF($dataset, $options) {
28102810
$default_graph = new stdClass();
28112811
$graph_map = (object)array('@default' => $default_graph);
2812-
// TODO: seems like usages could be replaced by this single map
2813-
$node_references = (object)array();
2812+
$referenced_once = (object)array();
28142813

28152814
foreach($dataset as $name => $graph) {
28162815
if(!property_exists($graph_map, $name)) {
@@ -2848,17 +2847,25 @@ protected function _fromRDF($dataset, $options) {
28482847
// object may be an RDF list/partial list node but we can't know
28492848
// easily until all triples are read
28502849
if($object_is_id) {
2851-
self::addValue(
2852-
$node_references, $o->value, $node->{'@id'}, array(
2853-
'propertyIsArray' => true));
2854-
$object = $node_map->{$o->value};
2855-
if(!property_exists($object, 'usages')) {
2856-
$object->usages = array();
2850+
if($o->value === self::RDF_NIL) {
2851+
$object = $node_map->{$o->value};
2852+
if(!property_exists($object, 'usages')) {
2853+
$object->usages = array();
2854+
}
2855+
$object->usages[] = (object)array(
2856+
'node' => $node,
2857+
'property' => $p,
2858+
'value' => $value);
2859+
} else if(property_exists($referenced_once, $o->value)) {
2860+
// object referenced more than once
2861+
$referenced_once->{$o->value} = false;
2862+
} else {
2863+
// track single reference
2864+
$referenced_once->{$o->value} = (object)array(
2865+
'node' => $node,
2866+
'property' => $p,
2867+
'value' => $value);
28572868
}
2858-
$object->usages[] = (object)array(
2859-
'node' => $node,
2860-
'property' => $p,
2861-
'value' => $value);
28622869
}
28632870
}
28642871
}
@@ -2880,31 +2887,30 @@ protected function _fromRDF($dataset, $options) {
28802887
$list_nodes = array();
28812888

28822889
// ensure node is a well-formed list node; it must:
2883-
// 1. Be referenced only once and used only once in a list.
2890+
// 1. Be referenced only once.
28842891
// 2. Have an array for rdf:first that has 1 item.
28852892
// 3. Have an array for rdf:rest that has 1 item.
2886-
// 4. Have no keys other than: @id, usages, rdf:first, rdf:rest, and,
2893+
// 4. Have no keys other than: @id, rdf:first, rdf:rest, and,
28872894
// optionally, @type where the value is rdf:List.
28882895
$node_key_count = count(array_keys((array)$node));
28892896
while($property === self::RDF_REST &&
2890-
property_exists($node_references, $node->{'@id'}) &&
2891-
count($node_references->{$node->{'@id'}}) === 1 &&
2892-
count($node->usages) === 1 &&
2897+
property_exists($referenced_once, $node->{'@id'}) &&
2898+
is_object($referenced_once->{$node->{'@id'}}) &&
28932899
property_exists($node, self::RDF_FIRST) &&
28942900
property_exists($node, self::RDF_REST) &&
28952901
is_array($node->{self::RDF_FIRST}) &&
28962902
is_array($node->{self::RDF_REST}) &&
28972903
count($node->{self::RDF_FIRST}) === 1 &&
28982904
count($node->{self::RDF_REST}) === 1 &&
2899-
($node_key_count === 4 || ($node_key_count === 5 &&
2905+
($node_key_count === 3 || ($node_key_count === 4 &&
29002906
property_exists($node, '@type') && is_array($node->{'@type'}) &&
29012907
count($node->{'@type'}) === 1 &&
29022908
$node->{'@type'}[0] === self::RDF_LIST))) {
29032909
$list[] = $node->{self::RDF_FIRST}[0];
29042910
$list_nodes[] = $node->{'@id'};
29052911

29062912
// get next node, moving backwards through list
2907-
$usage = $node->usages[0];
2913+
$usage = $referenced_once->{$node->{'@id'}};
29082914
$node = $usage->node;
29092915
$property = $usage->property;
29102916
$head = $usage->value;
@@ -2938,6 +2944,8 @@ protected function _fromRDF($dataset, $options) {
29382944
unset($graph_object->{$list_node});
29392945
}
29402946
}
2947+
2948+
unset($nil->usages);
29412949
}
29422950

29432951
$result = array();
@@ -2952,14 +2960,12 @@ protected function _fromRDF($dataset, $options) {
29522960
sort($subjects_);
29532961
foreach($subjects_ as $subject_) {
29542962
$node_ = $graph_object->{$subject_};
2955-
unset($node_->usages);
29562963
// only add full subjects to top-level
29572964
if(!self::_isSubjectReference($node_)) {
29582965
$node->{'@graph'}[] = $node_;
29592966
}
29602967
}
29612968
}
2962-
unset($node->usages);
29632969
// only add full subjects to top-level
29642970
if(!self::_isSubjectReference($node)) {
29652971
$result[] = $node;

0 commit comments

Comments
 (0)