/usr/share/perl5/RDF/Query/Client.pm is in librdf-query-client-perl 0.112-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | package RDF::Query::Client;
use 5.010;
use strict;
use warnings;
use Carp 0 qw/carp/;
use LWP::UserAgent 0 qw//;
use RDF::Trine 0.133 qw//;
use Scalar::Util 0 qw/blessed/;
use URI::Escape 0 qw/uri_escape/;
BEGIN {
$RDF::Query::Client::AUTHORITY = 'cpan:TOBYINK';
$RDF::Query::Client::VERSION = '0.112';
}
use namespace::clean;
use authority::shared q<http://www.perlrdf.org/>;
sub new
{
my ($class, $query, $opts) = @_;
my $self = bless {
query => $query ,
useragent => ($opts->{UserAgent} // undef) ,
results => [] ,
error => undef ,
}, $class;
return $self;
}
sub execute
{
my ($self, $endpoint, $opts) = @_;
my $ua = $opts->{UserAgent} // $self->useragent;
my $request = $self->_prepare_request($endpoint, $opts);
my $response = $ua->request($request);
push @{ $self->{results} }, { response => $response };
my $iterator = $self->_create_iterator($response);
return unless defined $iterator;
$self->{results}[-1]{iterator} = $iterator;
if (wantarray)
{
return $iterator->get_all;
}
else
{
return $iterator;
}
}
our $LRDD;
sub discover_execute
{
my ($self, $resource_uri, $opts) = @_;
unless ($LRDD)
{
no warnings;
eval 'use HTTP::LRDD;';
eval
{
$LRDD = HTTP::LRDD->new('http://ontologi.es/sparql#endpoint', 'http://ontologi.es/sparql#fingerpoint')
if HTTP::LRDD->can('new');
};
}
unless (blessed($LRDD) and $LRDD->isa('HTTP::LRDD'))
{
$self->{error} = "Need HTTP::LRDD to use the discover_execute feature.";
return;
}
my $endpoint = $LRDD->discover($resource_uri)
or return;
return $self->execute($endpoint, $opts);
}
sub get
{
my $stream = execute(@_);
if (ref $stream)
{
if ($stream->is_bindings)
{
my $row = $stream->next;
return $stream->binding_values;
}
if ($stream->is_graph)
{
my $st = $stream->next;
return ($st->subject, $st->predicate, $st->object);
}
if ($stream->is_boolean)
{
my @rv;
push @rv, 1 if $stream->get_boolean;
return @rv;
}
}
return;
}
sub as_sparql
{
return (shift)->{query};
}
sub http_response
{
return (shift)->{results}[-1]{response};
}
sub error
{
return (shift)->{error};
}
sub prepare { carp "Method not implemented"; }
sub execute_plan { carp "Method not implemented"; }
sub execute_with_named_graphs { carp "Method not implemented"; }
sub aggregate { carp "Method not implemented"; }
sub pattern { carp "Method not implemented"; }
sub sse { carp "Method not implemented"; }
sub algebra_fixup { carp "Method not implemented"; }
sub add_function { carp "Method not implemented"; }
sub supported_extensions { carp "Method not implemented"; }
sub supported_functions { carp "Method not implemented"; }
sub add_computed_statement_generator { carp "Method not implemented"; }
sub get_computed_statement_generators { carp "Method not implemented"; }
sub net_filter_function { carp "Method not implemented"; }
sub add_hook_once { carp "Method not implemented"; }
sub add_hook { carp "Method not implemented"; }
sub parsed { carp "Method not implemented"; }
sub bridge { carp "Method not implemented"; }
sub log { carp "Method not implemented"; }
sub logger { carp "Method not implemented"; }
sub costmodel { carp "Method not implemented"; }
sub useragent
{
my ($self) = @_;
unless (defined $self->{useragent})
{
my $accept = join q{, },
'application/sparql-results+xml',
'application/sparql-results+json;q=0.9',
'application/rdf+xml',
'application/x-turtle',
'text/turtle';
my $agent = sprintf('%s/%s (%s) ',
__PACKAGE__,
__PACKAGE__->VERSION,
__PACKAGE__->AUTHORITY,
);
$self->{useragent} = LWP::UserAgent->new(
agent => $agent,
max_redirect => 2,
parse_head => 0,
protocols_allowed => [qw/http https/],
);
$self->{useragent}->default_header(Accept => $accept);
}
$self->{useragent};
}
sub _prepare_request
{
my ($self, $endpoint, $opts) = @_;
my $method = uc ($opts->{QueryMethod} // '');
if ($method !~ /^(get|post|patch)$/i)
{
$method = (length $self->{'query'} > 511) ? 'POST' : 'GET';
}
my $param = $opts->{QueryParameter} // 'query';
my $uri = '';
my $cnt = '';
if ($method eq 'GET')
{
$uri = $endpoint . ($endpoint =~ /\?/ ? '&' : '?');
$uri .= sprintf(
"%s=%s",
uri_escape($param),
uri_escape($self->{query})
);
if ($opts->{Parameters})
{
foreach my $field (keys %{$opts->{Parameters}})
{
$uri .= sprintf(
"&%s=%s",
uri_escape($field),
uri_escape($opts->{Parameters}->{$field}),
);
}
}
}
elsif ($method eq 'POST')
{
$uri = $endpoint;
$cnt = sprintf(
"%s=%s",
uri_escape($param),
uri_escape($self->{query})
);
if ($opts->{Parameters})
{
foreach my $field (keys %{$opts->{Parameters}})
{
$cnt .= sprintf(
"&%s=%s",
uri_escape($field),
uri_escape($opts->{Parameters}{$field}),
);
}
}
}
my $req = HTTP::Request->new($method => $uri);
my $type = $opts->{ContentType} // '';
if ($type =~ m{^application/sparql-query}i)
{
$req->content_type('application/sparql-query');
$req->content($self->{query});
}
elsif ($type =~ m{^application/sparql-update}i)
{
$req->content_type('application/sparql-update');
$req->content($self->{query});
}
else
{
$req->content_type('application/x-www-form-urlencoded');
$req->content($cnt);
}
$req->authorization_basic($opts->{AuthUsername}, $opts->{AuthPassword})
if defined $opts->{AuthUsername};
foreach my $k (keys %{$opts->{Headers}})
{
$req->header($k => $opts->{Headers}{$k});
}
return $req;
}
sub _create_iterator
{
my ($self, $response) = @_;
unless ($response->is_success)
{
$self->{error} = $response->message;
return;
}
if ($response->content_type =~ /sparql.results/)
{
local $@ = undef;
my $iterator = eval
{
if ($response->content_type =~ /json/)
{ RDF::Trine::Iterator->from_json($response->decoded_content); }
else
{ RDF::Trine::Iterator->from_string($response->decoded_content); }
};
return $iterator
if $iterator;
$self->{error} = $@;
return;
}
else
{
my $model;
eval
{
my $parser = RDF::Trine::Parser->parser_by_media_type($response->content_type);
my $tmp = RDF::Trine::Model->temporary_model;
$parser->parse_into_model($response->base, $response->decoded_content, $tmp);
$model = $tmp;
};
return $model->as_stream if defined $model;
$self->{error} = sprintf("Response of type '%s' could not be parsed.", $response->content_type);
return;
}
}
1;
__END__
=pod
=encoding utf8
=begin stopwords
'sparql'
application/sparql-query
application/sparql-update
application/x-www-form-urlencoded
rel
WebID
=end stopwords
=head1 NAME
RDF::Query::Client - get data from W3C SPARQL Protocol 1.0 servers
=head1 SYNOPSIS
use RDF::Query::Client;
my $query = RDF::Query::Client
->new('SELECT DISTINCT ?s WHERE { ?s ?p ?o . }');
my $iterator = $query->execute('http://example.com/sparql');
while (my $row = $iterator->next) {
print $row->{s}->as_string;
}
=head1 DESCRIPTION
=head2 Constructor
=over 4
=item C<< new ( $sparql, \%opts ) >>
Returns a new RDF::Query::Client object for the specified C<$sparql>.
The object's interface is designed to be roughly compatible with RDF::Query
objects, though RDF::Query is not required by this module.
Options include:
=over 4
=item B<UserAgent> - an LWP::UserAgent to handle HTTP requests.
=back
Unlike RDF::Query, where you get a choice of query language, the query
language for RDF::Query::Client is always 'sparql'. RDF::TrineShortcuts offers
a way to perform RDQL queries on remote SPARQL stores though (by transforming
RDQL to SPARQL).
=back
=head2 Public Methods
=over 4
=item C<< execute ( $endpoint, \%opts ) >>
C<$endpoint> is a URI object or string containing the endpoint
URI to be queried.
Options include:
=over 4
=item * B<UserAgent> - an LWP::UserAgent to handle HTTP requests.
=item * B<QueryMethod> - 'GET', 'POST', 'PATCH' or undef (automatic).
=item * B<QueryParameter> - defaults to 'query'.
=item * B<AuthUsername> - HTTP Basic authorization.
=item * B<AuthPassword> - HTTP Basic authorization.
=item * B<Headers> - additional headers to include (hashref).
=item * B<Parameters> - additional GET/POST fields to include (hashref).
=item * B<ContentType> - 'application/sparql-query',
'application/sparql-update' or 'application/x-www-form-urlencoded' (default).
=back
Returns undef on error; an RDF::Trine::Iterator if called in a
scalar context; an array obtained by calling C<get_all> on the
iterator if called in list context.
=item C<< discover_execute( $resource_uri, \%opts ) >>
Experimental feature. Discovers a SPARQL endpoint relevant to $resource_uri
and then calls C<< $query->execute >> against that. Uses an LRDD-like
method to discover the endpoint. If you're publishing data and want people
to be able to find your SPARQL endpoint automatically, the easiest way is to
include an Link header in HTTP responses:
Link: </my/endpoint>; rel="http://ontologi.es/sparql#endpoint"
Change the URL in the angled brackets, but not the URL in the rel string.
This feature requires the HTTP::LRDD package to be installed.
=item C<< get ( $endpoint, \%opts ) >>
Executes the query using the specified endpoint, and returns the first
matching row as a LIST of values. Takes the same arguments as C<execute>.
=item C<< as_sparql >>
Returns the query as a string in the SPARQL syntax.
=item C<< useragent >>
Returns the LWP::UserAgent object used for retrieving web content.
=item C<< http_response >>
Returns the last HTTP Response the client experienced.
=item C<< error >>
Returns the last error the client experienced.
=back
=head2 Security
The C<execute> and C<get> methods allow AuthUsername and
AuthPassword options to be passed to them for HTTP Basic authentication.
For more complicated authentication (Digest, OAuth, Windows, etc),
it is also possible to pass these methods a customised LWP::UserAgent.
If you have the Crypt::SSLeay package installed, requests to HTTPS
endpoints should work. It's possible to specify a client X.509
certificate (e.g. for WebID authentication) by setting particular
environment variables. See L<Crypt::SSLeay> documentation for details.
=head1 BUGS
Probably.
Please report any you find here:
L<https://rt.cpan.org/Dist/Display.html?Queue=RDF-Query-Client>.
=head1 SEE ALSO
=over 4
=item * L<RDF::Trine>, L<RDF::Trine::Iterator>
=item * L<RDF::Query>
=item * L<LWP::UserAgent>
=item * L<http://www.w3.org/TR/rdf-sparql-protocol/>
=item * L<http://www.w3.org/TR/rdf-sparql-query/>
=item * L<http://www.perlrdf.org/>
=back
=head1 AUTHOR
Toby Inkster, E<lt>tobyink@cpan.orgE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2009-2013 by Toby Inkster
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
=cut
|