line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WQS::SPARQL; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
219651
|
use strict; |
|
3
|
|
|
|
|
25
|
|
|
3
|
|
|
|
|
84
|
|
4
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
78
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
1798
|
use Class::Utils qw(set_params); |
|
3
|
|
|
|
|
28188
|
|
|
3
|
|
|
|
|
57
|
|
7
|
3
|
|
|
3
|
|
185
|
use Error::Pure qw(err); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
100
|
|
8
|
3
|
|
|
3
|
|
1365
|
use HTTP::Request; |
|
3
|
|
|
|
|
68386
|
|
|
3
|
|
|
|
|
93
|
|
9
|
3
|
|
|
3
|
|
2031
|
use JSON::XS; |
|
3
|
|
|
|
|
14814
|
|
|
3
|
|
|
|
|
155
|
|
10
|
3
|
|
|
3
|
|
2061
|
use LWP::UserAgent; |
|
3
|
|
|
|
|
85449
|
|
|
3
|
|
|
|
|
116
|
|
11
|
3
|
|
|
3
|
|
20
|
use URI; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
67
|
|
12
|
3
|
|
|
3
|
|
1311
|
use URI::QueryParam; |
|
3
|
|
|
|
|
2327
|
|
|
3
|
|
|
|
|
1269
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = 0.01; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Constructor. |
17
|
|
|
|
|
|
|
sub new { |
18
|
2
|
|
|
2
|
1
|
737
|
my ($class, @params) = @_; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Create object. |
21
|
2
|
|
|
|
|
5
|
my $self = bless {}, $class; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# User agent. |
24
|
2
|
|
|
|
|
25
|
$self->{'agent'} = __PACKAGE__." ($VERSION)"; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# LWP::UserAgent object. |
27
|
2
|
|
|
|
|
5
|
$self->{'lwp_user_agent'} = undef; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Query site. |
30
|
2
|
|
|
|
|
5
|
$self->{'query_site'} = 'query.wikidata.org'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# SPARQL endpoint. |
33
|
2
|
|
|
|
|
28
|
$self->{'sparql_endpoint'} = '/bigdata/namespace/wdq/sparql'; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Process parameters. |
36
|
2
|
|
|
|
|
10
|
set_params($self, @params); |
37
|
|
|
|
|
|
|
|
38
|
2
|
100
|
|
|
|
30
|
if (! defined $self->{'lwp_user_agent'}) { |
39
|
|
|
|
|
|
|
$self->{'lwp_user_agent'} = LWP::UserAgent->new( |
40
|
1
|
|
|
|
|
15
|
'agent' => $self->{'agent'}, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
} else { |
43
|
1
|
50
|
|
|
|
9
|
if (! $self->{'lwp_user_agent'}->isa('LWP::UserAgent')) { |
44
|
1
|
|
|
|
|
4
|
err "Parameter 'lwp_user_agent' must be a 'LWP::UserAgent' instance."; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Full URL of api. |
49
|
1
|
|
|
|
|
2960
|
$self->{'_api_uri'} = 'https://'.$self->{'query_site'}.$self->{'sparql_endpoint'}; |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
4
|
return $self; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub query { |
55
|
0
|
|
|
0
|
1
|
|
my ($self, $query) = @_; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
my $uri = URI->new($self->{'_api_uri'}); |
58
|
0
|
|
|
|
|
|
$uri->query_param_append('format' => 'json'); |
59
|
0
|
|
|
|
|
|
$uri->query_param_append('query' => $query); |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
return $self->_http_get_json($uri->as_string); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub query_count { |
65
|
0
|
|
|
0
|
1
|
|
my ($self, $query) = @_; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
my $ret_hr = $self->query($query); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
return $ret_hr->{'results'}->{'bindings'}->[0]->{'count'}->{'value'}; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _http_get_json { |
73
|
0
|
|
|
0
|
|
|
my ($self, $uri) = @_; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my $res = $self->{'lwp_user_agent'}->get($uri); |
76
|
0
|
0
|
|
|
|
|
if ($res->is_success) { |
77
|
0
|
|
|
|
|
|
my $content = decode_json($res->decoded_content); |
78
|
0
|
0
|
|
|
|
|
return wantarray ? ($content, $res->headers) : $content; |
79
|
|
|
|
|
|
|
} else { |
80
|
0
|
|
|
|
|
|
err 'Cannot get.', |
81
|
|
|
|
|
|
|
'Error', $res->status_line, |
82
|
|
|
|
|
|
|
'URI', $uri; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
__END__ |