| 1 | package Catmandu::Importer::RDFscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/; |
| 2 | |
| 3 | use openscip-perl cpan Catmandu-RDF 0.32 open/ ':std', ':encoding(utf8)'; |
| 4 | use namespace::cleanscip-perl cpan Catmandu-RDF 0.32 namespace/clean/; |
| 5 | use Catmandu::Sanescip-perl cpan Catmandu-RDF 0.32 Catmandu/Sane/; |
| 6 | use Mooscip-perl cpan Catmandu-RDF 0.32 Moo/; |
| 7 | use RDF::Trine::Parserscip-perl cpan Catmandu-RDF 0.32 RDF/Trine/Parser/; |
| 8 | use RDF::Trine::Modelscip-perl cpan Catmandu-RDF 0.32 RDF/Trine/Model/; |
| 9 | use RDF::Trine::Store::SPARQLscip-perl cpan Catmandu-RDF 0.32 RDF/Trine/Store/SPARQL/; |
| 10 | use RDF::Trine::Store::LDFscip-perl cpan Catmandu-RDF 0.32 RDF/Trine/Store/LDF/; |
| 11 | use RDF::Trine::Storescip-perl cpan Catmandu-RDF 0.32 RDF/Trine/Store/; |
| 12 | use RDF::Queryscip-perl cpan Catmandu-RDF 0.32 RDF/Query/; |
| 13 | use RDF::LDFscip-perl cpan Catmandu-RDF 0.32 RDF/LDF/; |
| 14 | use RDF::aREFscip-perl cpan Catmandu-RDF 0.32 RDF/aREF/; |
| 15 | use RDF::aREF::Encoderscip-perl cpan Catmandu-RDF 0.32 RDF/aREF/Encoder/; |
| 16 | use RDF::NSscip-perl cpan Catmandu-RDF 0.32 RDF/NS/; |
| 17 | use IO::Pipescip-perl cpan Catmandu-RDF 0.32 IO/Pipe/; |
| 18 | use JSONscip-perl cpan Catmandu-RDF 0.32 JSON/; |
| 19 | use LWP::UserAgent::CHICachingscip-perl cpan Catmandu-RDF 0.32 LWP/UserAgent/CHICaching/; |
| 20 | |
| 21 | our $VERSIONscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$VERSION. = '0.32'; |
| 22 | |
| 23 | withscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/with(). 'Catmandu::RDF'; |
| 24 | withscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/with(). 'Catmandu::Importer'; |
| 25 | |
| 26 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). url => ( |
| 27 | is => 'ro' |
| 28 | ); |
| 29 | |
| 30 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). base => ( |
| 31 | is => 'ro', |
| 32 | lazy => 1, |
| 33 | builder => sub { |
| 34 | definedscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/defined(). $_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[0]->filescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/file(). ? "file://".$_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[0]->filescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/file(). : "http://example.org/"; |
| 35 | } |
| 36 | ); |
| 37 | |
| 38 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). encoder => ( |
| 39 | is => 'ro', |
| 40 | lazy => 1, |
| 41 | builder => sub { |
| 42 | my $ns = $_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[0]->nsscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/ns().; |
| 43 | RDF::aREF::Encoder->newscip-perl cpan Catmandu-RDF 0.32 RDF/aREF/Encoder/new().( |
| 44 | ns => (($ns // 1) ? $ns : { }), |
| 45 | subject_map => !$_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[0]->predicate_mapscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/predicate_map()., |
| 46 | ); |
| 47 | } |
| 48 | ); |
| 49 | |
| 50 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). sparql => ( |
| 51 | is => 'ro', |
| 52 | lazy => 1, |
| 53 | trigger => sub { |
| 54 | my ($sparql, $ns) = ($_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[1], $_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[0]->nsscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/ns().); |
| 55 | $sparql = do { local (@ARGVscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@ARGV`.,$/scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`$/`.) = $sparql; <> } if $sparql =~ /^\S+$/ && -r $sparql; |
| 56 | my %prefix; |
| 57 | # guess requires prefixes (don't override existing). Don't mind false positives |
| 58 | $prefix{$_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.} = 1 for ($sparql =~ /\s([a-z][a-z0-0_-]*):/mig); |
| 59 | deletescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/delete(). $prefix{$_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.} for ($sparql =~ /PREFIX\s+([^:]+):/mg); |
| 60 | $_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[0]->{sparql} = joinscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/join(). "\n", (mapscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/map(). { $ns->SPARQLscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/SPARQL().($_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.) } keysscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/keys(). %prefix), $sparql; |
| 61 | } |
| 62 | ); |
| 63 | |
| 64 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). sparql_result => ( |
| 65 | is => 'ro', |
| 66 | default => sub { 'simple' } |
| 67 | ); |
| 68 | |
| 69 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). predicate_map => ( |
| 70 | is => 'ro', |
| 71 | ); |
| 72 | |
| 73 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). triples => ( |
| 74 | is => 'ro', |
| 75 | ); |
| 76 | |
| 77 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). cache => ( |
| 78 | is => 'ro', |
| 79 | default => sub { 0 } |
| 80 | ); |
| 81 | |
| 82 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). cache_options => ( |
| 83 | is => 'ro', |
| 84 | default => sub { +{ |
| 85 | driver => 'Memory', |
| 86 | global => 1 , |
| 87 | max_size => 1024*1024 |
| 88 | } } |
| 89 | ); |
| 90 | |
| 91 | hasscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/has(). speed => ( |
| 92 | is => 'ro', |
| 93 | ); |
| 94 | |
| 95 | sub BUILDscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/BUILD(). { |
| 96 | my ($self) = @_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.; |
| 97 | |
| 98 | if ($self->cachescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/cache().) { |
| 99 | my $options = $self->cache_optionsscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/cache_options(). // {}; |
| 100 | my $cache = CHI->newscip-perl cpan Catmandu-RDF 0.32 CHI/new().( %$options ); |
| 101 | my $ua = LWP::UserAgent::CHICaching->newscip-perl cpan Catmandu-RDF 0.32 LWP/UserAgent/CHICaching/new().(cache => $cache); |
| 102 | RDF::Trine->default_useragentscip-perl cpan Catmandu-RDF 0.32 RDF/Trine/default_useragent().($ua); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | sub generatorscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/generator(). { |
| 107 | my ($self) = @_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.; |
| 108 | |
| 109 | if ($self->sparqlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/sparql().) { |
| 110 | return $self->sparql_generatorscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/sparql_generator().; |
| 111 | } else { |
| 112 | return $self->rdf_generatorscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/rdf_generator().; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | sub sparql_generatorscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/sparql_generator(). { |
| 117 | my ($self) = @_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.; |
| 118 | |
| 119 | warnscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/warn(). "--triples not active for sparql queries" if ($self->triplesscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/triples().); |
| 120 | warnscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/warn(). "--predicate_map not active for sparql queries" if ($self->predicate_mapscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/predicate_map().); |
| 121 | |
| 122 | my $encoder = RDF::aREF::Encoder->newscip-perl cpan Catmandu-RDF 0.32 RDF/aREF/Encoder/new().( ns => {} ); # never return qnames |
| 123 | |
| 124 | sub { |
| 125 | state $stream = $self->_sparql_streamscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/_sparql_stream().; |
| 126 | if (definedscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/defined().($stream) && definedscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/defined().(my $row = $stream->nextscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/next().)) { |
| 127 | if (refscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/ref(). $row eq 'RDF::Query::VariableBindings' || refscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/ref(). $row eq 'RDF::Trine::VariableBindings') { |
| 128 | my $ref = {}; |
| 129 | for (keysscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/keys(). %$row) { |
| 130 | my $val = $row->{$_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.}; |
| 131 | $ref->{$_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.} = $self->sparql_resultscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/sparql_result(). eq 'aref' |
| 132 | ? $encoder->objectscip-perl cpan Catmandu-RDF 0.32 RDF/aREF/Encoder/object().($val) : do { # TODO: clean up |
| 133 | if ( $val->is_resourcescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/is_resource(). ) { |
| 134 | $val->uri_valuescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/uri_value().; |
| 135 | } elsif ( $val->is_literalscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/is_literal().) { |
| 136 | $val->literal_valuescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/literal_value().; |
| 137 | } else { |
| 138 | $val->as_stringscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/as_string(). |
| 139 | } |
| 140 | }; |
| 141 | } |
| 142 | return $ref; |
| 143 | } else { |
| 144 | diescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/die(). "Expected a RDF::Query::VariableBindings or RDF::Trine::VariableBindings but got a " . refscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/ref().($row); |
| 145 | } |
| 146 | } else { |
| 147 | return ($stream = undef); |
| 148 | } |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | sub rdf_generatorscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/rdf_generator(). { |
| 153 | my ($self) = @_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.; |
| 154 | sub { |
| 155 | state $stream = $self->_hashref_streamscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/_hashref_stream().; |
| 156 | return unless $stream; |
| 157 | |
| 158 | my $aref = { }; |
| 159 | |
| 160 | if ($self->triplesscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/triples().) { |
| 161 | if (my $hashref = $stream->()scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`->()`().) { |
| 162 | $self->encoderscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/encoder().->add_hashrefscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/add_hashref().($hashref, $aref); |
| 163 | } |
| 164 | else { |
| 165 | return ($stream = undef); |
| 166 | } |
| 167 | } |
| 168 | else { |
| 169 | # TODO: include namespace mappings if requested |
| 170 | while (my $hashref = $stream->()scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`->()`().) { |
| 171 | $self->encoderscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/encoder().->add_hashrefscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/add_hashref().( |
| 172 | $hashref, |
| 173 | $aref |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | if ($self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url().) { |
| 178 | $aref->{_url} = $self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url().; |
| 179 | } |
| 180 | |
| 181 | $stream = undef; |
| 182 | } |
| 183 | |
| 184 | if ($self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url().) { |
| 185 | # RDF::Trine::Parser parses data from URL to UTF-8 |
| 186 | # but we want internal character sequences |
| 187 | _utf8_decodescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/_utf8_decode().($aref); |
| 188 | } |
| 189 | |
| 190 | return $aref; |
| 191 | }; |
| 192 | } |
| 193 | |
| 194 | sub _utf8_decodescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/_utf8_decode(). { |
| 195 | if (refscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/ref(). $_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[0] eq 'HASH') { |
| 196 | # FIXME: UTF-8 in property values |
| 197 | foreach (valuesscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/values(). %{$_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[0]}) { |
| 198 | refscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/ref().($_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.) ? _utf8_decodescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/_utf8_decode().($_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.) : utf8::decodescip-perl cpan Catmandu-RDF 0.32 utf8/decode().($_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.); |
| 199 | } |
| 200 | } else { |
| 201 | foreach (@{$_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.[0]}) { |
| 202 | refscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/ref().($_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.) ? _utf8_decodescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/_utf8_decode().($_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.) : utf8::decodescip-perl cpan Catmandu-RDF 0.32 utf8/decode().($_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/$_.); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | sub _sparql_streamscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/_sparql_stream(). { |
| 208 | my ($self) = @_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.; |
| 209 | |
| 210 | diescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/die(). "need an url" unless $self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url().; |
| 211 | |
| 212 | $self->logscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/log().->infoscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/info().("parsing: " . $self->sparqlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/sparql().); |
| 213 | |
| 214 | my $store; |
| 215 | |
| 216 | # Check if this server is an LDF server |
| 217 | my $ldf_client = RDF::LDF->newscip-perl cpan Catmandu-RDF 0.32 RDF/LDF/new().(url => $self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url().); |
| 218 | |
| 219 | if ($ldf_client->is_fragment_serverscip-perl cpan Catmandu-RDF 0.32 RDF/LDF/is_fragment_server().) { |
| 220 | $store = RDF::Trine::Store->new_with_configscip-perl cpan Catmandu-RDF 0.32 RDF/Trine/Store/new_with_config().({ |
| 221 | storetype => 'LDF', |
| 222 | url => $self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url(). |
| 223 | }); |
| 224 | } |
| 225 | else { |
| 226 | $store = RDF::Trine::Store->new_with_configscip-perl cpan Catmandu-RDF 0.32 RDF/Trine/Store/new_with_config().({ |
| 227 | storetype => 'SPARQL', |
| 228 | url => $self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url(). |
| 229 | }); |
| 230 | } |
| 231 | |
| 232 | unless ($store) { |
| 233 | $self->logscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/log().->errorscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/error().("failed to connect to " . $self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url().); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | my $model = RDF::Trine::Model->newscip-perl cpan Catmandu-RDF 0.32 RDF/Trine/Model/new().($store); |
| 238 | |
| 239 | my $rdf_query = RDF::Query->newscip-perl cpan Catmandu-RDF 0.32 RDF/Query/new().($self->sparqlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/sparql().); |
| 240 | |
| 241 | unless ($rdf_query) { |
| 242 | $self->logscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/log().->errorscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/error().("failed to parse " . $self->sparqlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/sparql().); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | my $iterator = $rdf_query->executescip-perl cpan Catmandu-RDF 0.32 RDF/Query/execute().($model); |
| 247 | |
| 248 | unless ($iterator) { |
| 249 | $self->logscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/log().->errorscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/error().("failed to execute " . $self->sparqlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/sparql(). . " at " . $self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url().); |
| 250 | return; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | sub _hashref_streamscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/_hashref_stream(). { |
| 255 | my ($self) = @_scip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/`@_`.; |
| 256 | |
| 257 | # Create a pipe stream to convert a callback handler into an iterator |
| 258 | my $pipe = IO::Pipe->newscip-perl cpan Catmandu-RDF 0.32 IO/Pipe/new().(); |
| 259 | |
| 260 | if (my $pid = forkscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/fork().()) { |
| 261 | # parent |
| 262 | $pipe->readerscip-perl cpan Catmandu-RDF 0.32 IO/Pipe/reader().(); |
| 263 | |
| 264 | binmodescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/binmode().($pipe,':encoding(UTF-8)'); |
| 265 | |
| 266 | return sub { |
| 267 | state $line = <$pipe>; |
| 268 | |
| 269 | return decode_jsonscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/decode_json().($line) if definedscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/defined().($line); |
| 270 | |
| 271 | waitpidscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/waitpid().($pid,0); |
| 272 | |
| 273 | return undef; |
| 274 | }; |
| 275 | } |
| 276 | else { |
| 277 | # child |
| 278 | $pipe->writerscip-perl cpan Catmandu-RDF 0.32 IO/Pipe/writer().(); |
| 279 | |
| 280 | binmodescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/binmode().($pipe,':encoding(UTF-8)'); |
| 281 | |
| 282 | my $parser = $self->typescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/type(). |
| 283 | ? RDF::Trine::Parser->newscip-perl cpan Catmandu-RDF 0.32 RDF/Trine/Parser/new().( $self->typescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/type(). ) : 'RDF::Trine::Parser'; |
| 284 | |
| 285 | my $handler = sub { |
| 286 | my $triple = shiftscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/shift().; |
| 287 | state $start = timescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/time().; |
| 288 | state $count = 0; |
| 289 | |
| 290 | my $subject = $triple->subjectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/subject().->is_blankscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/is_blank(). ? |
| 291 | '_:' . $triple->subjectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/subject().->blank_identifierscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/blank_identifier(). : |
| 292 | $triple->subjectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/subject().->uri_valuescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/uri_value().; |
| 293 | my $predicate = $triple->predicatescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/predicate().->is_blankscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/is_blank(). ? |
| 294 | '_:' . $triple->predicatescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/predicate().->blank_identifierscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/blank_identifier(). : |
| 295 | $triple->predicatescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/predicate().->valuescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/value().; |
| 296 | my $value = $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->is_literalscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/is_literal(). ? |
| 297 | $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->literal_valuescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/literal_value(). : |
| 298 | $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->is_blankscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/is_blank(). ? |
| 299 | '_:' . $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->blank_identifierscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/blank_identifier(). : |
| 300 | $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->uri_valuescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/uri_value().; |
| 301 | my $type = lcscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/lc(). $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->typescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/type().; |
| 302 | $type = 'bnode' if $type eq 'blank'; |
| 303 | my $lang = $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->is_literalscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/is_literal(). ? $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->literal_value_languagescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/literal_value_language(). : undef; |
| 304 | my $datatype = $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->is_literalscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/is_literal(). ? $triple->objectscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/object().->literal_datatypescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/literal_datatype(). : undef; |
| 305 | |
| 306 | # Create the RDF::Trine type RDF/JSON RDF::aREF can parse |
| 307 | my $hashref = {}; |
| 308 | |
| 309 | $hashref->{$subject}->{$predicate}->[0]->{type} = $type; |
| 310 | $hashref->{$subject}->{$predicate}->[0]->{datatype} = $datatype if $datatype; |
| 311 | $hashref->{$subject}->{$predicate}->[0]->{lang} = $lang if $lang; |
| 312 | $hashref->{$subject}->{$predicate}->[0]->{value} = $value; |
| 313 | |
| 314 | printscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/print(). $pipe encode_jsonscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/encode_json().($hashref) , "\n"; |
| 315 | |
| 316 | $count++; |
| 317 | |
| 318 | if ($self->speedscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/speed(). && ($count % 100 == 0) && (my $elapsed = timescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/time(). - $start) ) { |
| 319 | printf STDERR "triples %9d (%d/sec)\n" , $count , $count/$elapsed; |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | if ($self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url().) { |
| 324 | $parser->parse_urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/parse_url().( $self->urlscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/url()., $handler); |
| 325 | } |
| 326 | else { |
| 327 | my $from_scalar = (refscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/ref(). $self->filescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/file(). // '') eq 'SCALAR'; |
| 328 | |
| 329 | if (!$self->typescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/type(). and $self->filescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/file(). and !$from_scalar) { |
| 330 | $parser = $parser->guess_parser_by_filenamescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/guess_parser_by_filename().($self->filescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/file().)->newscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/new().; |
| 331 | } |
| 332 | |
| 333 | if ($from_scalar) { |
| 334 | $parsescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/parse().r->parse( $self->basescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/base()., ${$self->filescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/file().}, $handler ); |
| 335 | } |
| 336 | else { |
| 337 | $parser->parse_filescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/parse_file().( $self->basescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/base()., $self->filescip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/file(). // $self->fhscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/fh()., $handler ); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | exitscip-perl cpan Catmandu-RDF 0.32 Catmandu/Importer/RDF/exit().(0); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | |
| 346 | 1; |
| 347 | |
| 348 | __END__ |
| 349 | |
| 350 | =head1 NAME |
| 351 | |
| 352 | Catmandu::Importer::RDF - parse RDF data |
| 353 | |
| 354 | =head1 SYNOPSIS |
| 355 | |
| 356 | Command line client C<catmandu>: |
| 357 | |
| 358 | catmandu convert RDF --url http://d-nb.info/gnd/4151473-7 to YAML |
| 359 | |
| 360 | catmandu convert RDF --file rdfdump.ttl to JSON |
| 361 | |
| 362 | # Parse the input into on JSON document per triplet. This is the |
| 363 | # most memory efficient (and fastest) way to parse RDF input. |
| 364 | catmandu convert RDF --triples 1 --file rdfdump.ttl to JSON |
| 365 | |
| 366 | # Transform back into NTriples (conversions to and from triples is the |
| 367 | # most efficient way to process RDF) |
| 368 | catmandu convert RDF --triples 1 --file rdfdump.ttl to RDF --type NTriples |
| 369 | |
| 370 | # Query a SPARQL endpoint |
| 371 | catmandu convert RDF --url http://dbpedia.org/sparql |
| 372 | --sparql "SELECT ?film WHERE { ?film dct:subject <http://dbpedia.org/resource/Category:French_films> }" |
| 373 | |
| 374 | catmandu convert RDF --url http://example.org/sparql --sparql query.rq |
| 375 | |
| 376 | # Query a Linked Data Fragment endpoint |
| 377 | catmandu convert RDF --url http://fragments.dbpedia.org/2014/en |
| 378 | --sparql "SELECT ?film WHERE { ?film dct:subject <http://dbpedia.org/resource/Category:French_films> }" |
| 379 | |
| 380 | In Perl code: |
| 381 | |
| 382 | use Catmandu::Importer::RDF; |
| 383 | my $url = "http://dx.doi.org/10.2474/trol.7.147"; |
| 384 | my $rdf = Catmandu::Importer::RDF->new( url => $url )->first; |
| 385 | |
| 386 | =head1 DESCRIPTION |
| 387 | |
| 388 | This L<Catmandu::Importer> can be use to import RDF data from URLs, files or |
| 389 | input streams, SPARQL endpoints, and Linked Data Fragment endpoints. |
| 390 | |
| 391 | By default an RDF graph is imported as single item in aREF format (see |
| 392 | L<RDF::aREF>). |
| 393 | |
| 394 | =head1 CONFIGURATION |
| 395 | |
| 396 | =over |
| 397 | |
| 398 | =item url |
| 399 | |
| 400 | URL to retrieve RDF from. |
| 401 | |
| 402 | =item type |
| 403 | |
| 404 | RDF serialization type (e.g. C<ttl> for RDF/Turtle). |
| 405 | |
| 406 | =item base |
| 407 | |
| 408 | Base URL. By default derived from the URL or file name. |
| 409 | |
| 410 | =item ns |
| 411 | |
| 412 | Use default namespace prefixes as provided by L<RDF::NS> to abbreviate |
| 413 | predicate and datatype URIs. Set to C<0> to disable abbreviating URIs. |
| 414 | Set to a specific date to get stable namespace prefix mappings. |
| 415 | |
| 416 | =item triples |
| 417 | |
| 418 | Import each RDF triple as one aREF subject map (default) or predicate map |
| 419 | (option C<predicate_map>), if enabled. This is the most efficient way to |
| 420 | process large input files. All the processing can be streamed. |
| 421 | |
| 422 | =item predicate_map |
| 423 | |
| 424 | Import RDF as aREF predicate map, if possible. |
| 425 | |
| 426 | =item file |
| 427 | |
| 428 | =item fh |
| 429 | |
| 430 | =item encoding |
| 431 | |
| 432 | =item fix |
| 433 | |
| 434 | Default configuration options of L<Catmandu::Importer>. |
| 435 | |
| 436 | =item sparql |
| 437 | |
| 438 | The SPARQL query to be executed on the URL endpoint (currectly only SELECT is |
| 439 | supported). The query can be supplied as string or as filename. The importer |
| 440 | tries to automatically add missing PREFIX statements from the default namespace |
| 441 | prefixes. |
| 442 | |
| 443 | =item sparql_result |
| 444 | |
| 445 | Encoding of SPARQL result values. With C<aref>, query results are encoded in |
| 446 | aREF format, with URIs in C<E<lt>> and C<E<gt>> (no qNames) and literal nodes |
| 447 | appended by C<@> and optional language code. By default (value C<simple>), all |
| 448 | RDF nodes are simplfied to their literal form. |
| 449 | |
| 450 | =item cache |
| 451 | |
| 452 | Set to a true value to cache repeated URL responses in a L<CHI> based backend. |
| 453 | |
| 454 | =item cache_options |
| 455 | |
| 456 | Provide the L<CHI> based options for caching result sets. By default a memory store of |
| 457 | 1MB size is used. This is equal to: |
| 458 | |
| 459 | Catamandu::Importer::RDF->new( ..., |
| 460 | cache => 1, |
| 461 | cache_options => { |
| 462 | driver => 'Memory', |
| 463 | global => 1, |
| 464 | max_size => 1024*1024 |
| 465 | }); |
| 466 | |
| 467 | =item speed |
| 468 | |
| 469 | If set to a true value, then write RDF file processing speed on the STDERR as |
| 470 | number of triples parsed per second. |
| 471 | |
| 472 | =back |
| 473 | |
| 474 | =head1 METHODS |
| 475 | |
| 476 | See L<Catmandu::Importer>. |
| 477 | |
| 478 | =head1 SEE ALSO |
| 479 | |
| 480 | L<RDF::Trine::Store>, L<RDF::Trine::Parser> |
| 481 | |
| 482 | =encoding utf8 |
| 483 | |
| 484 | =cut |