@@ -2809,8 +2809,7 @@ protected function _normalize($dataset, $options) {
2809
2809
protected function _fromRDF ($ dataset , $ options ) {
2810
2810
$ default_graph = new stdClass ();
2811
2811
$ 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 ();
2814
2813
2815
2814
foreach ($ dataset as $ name => $ graph ) {
2816
2815
if (!property_exists ($ graph_map , $ name )) {
@@ -2848,17 +2847,25 @@ protected function _fromRDF($dataset, $options) {
2848
2847
// object may be an RDF list/partial list node but we can't know
2849
2848
// easily until all triples are read
2850
2849
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 );
2857
2868
}
2858
- $ object ->usages [] = (object )array (
2859
- 'node ' => $ node ,
2860
- 'property ' => $ p ,
2861
- 'value ' => $ value );
2862
2869
}
2863
2870
}
2864
2871
}
@@ -2880,31 +2887,30 @@ protected function _fromRDF($dataset, $options) {
2880
2887
$ list_nodes = array ();
2881
2888
2882
2889
// 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.
2884
2891
// 2. Have an array for rdf:first that has 1 item.
2885
2892
// 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,
2887
2894
// optionally, @type where the value is rdf:List.
2888
2895
$ node_key_count = count (array_keys ((array )$ node ));
2889
2896
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 ' }}) &&
2893
2899
property_exists ($ node , self ::RDF_FIRST ) &&
2894
2900
property_exists ($ node , self ::RDF_REST ) &&
2895
2901
is_array ($ node ->{self ::RDF_FIRST }) &&
2896
2902
is_array ($ node ->{self ::RDF_REST }) &&
2897
2903
count ($ node ->{self ::RDF_FIRST }) === 1 &&
2898
2904
count ($ node ->{self ::RDF_REST }) === 1 &&
2899
- ($ node_key_count === 4 || ($ node_key_count === 5 &&
2905
+ ($ node_key_count === 3 || ($ node_key_count === 4 &&
2900
2906
property_exists ($ node , '@type ' ) && is_array ($ node ->{'@type ' }) &&
2901
2907
count ($ node ->{'@type ' }) === 1 &&
2902
2908
$ node ->{'@type ' }[0 ] === self ::RDF_LIST ))) {
2903
2909
$ list [] = $ node ->{self ::RDF_FIRST }[0 ];
2904
2910
$ list_nodes [] = $ node ->{'@id ' };
2905
2911
2906
2912
// get next node, moving backwards through list
2907
- $ usage = $ node ->usages [ 0 ] ;
2913
+ $ usage = $ referenced_once ->{ $ node ->{ ' @id ' }} ;
2908
2914
$ node = $ usage ->node ;
2909
2915
$ property = $ usage ->property ;
2910
2916
$ head = $ usage ->value ;
@@ -2938,6 +2944,8 @@ protected function _fromRDF($dataset, $options) {
2938
2944
unset($ graph_object ->{$ list_node });
2939
2945
}
2940
2946
}
2947
+
2948
+ unset($ nil ->usages );
2941
2949
}
2942
2950
2943
2951
$ result = array ();
@@ -2952,14 +2960,12 @@ protected function _fromRDF($dataset, $options) {
2952
2960
sort ($ subjects_ );
2953
2961
foreach ($ subjects_ as $ subject_ ) {
2954
2962
$ node_ = $ graph_object ->{$ subject_ };
2955
- unset($ node_ ->usages );
2956
2963
// only add full subjects to top-level
2957
2964
if (!self ::_isSubjectReference ($ node_ )) {
2958
2965
$ node ->{'@graph ' }[] = $ node_ ;
2959
2966
}
2960
2967
}
2961
2968
}
2962
- unset($ node ->usages );
2963
2969
// only add full subjects to top-level
2964
2970
if (!self ::_isSubjectReference ($ node )) {
2965
2971
$ result [] = $ node ;
0 commit comments