| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WQS::SPARQL; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
469550
|
use strict; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
129
|
|
|
4
|
3
|
|
|
3
|
|
25
|
use warnings; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
188
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
1880
|
use Class::Utils qw(set_params); |
|
|
3
|
|
|
|
|
41105
|
|
|
|
3
|
|
|
|
|
2124
|
|
|
7
|
3
|
|
|
3
|
|
309
|
use Error::Pure qw(err); |
|
|
3
|
|
|
|
|
9
|
|
|
|
3
|
|
|
|
|
148
|
|
|
8
|
3
|
|
|
3
|
|
1841
|
use HTTP::Request; |
|
|
3
|
|
|
|
|
90537
|
|
|
|
3
|
|
|
|
|
141
|
|
|
9
|
3
|
|
|
3
|
|
2719
|
use JSON::XS; |
|
|
3
|
|
|
|
|
21674
|
|
|
|
3
|
|
|
|
|
242
|
|
|
10
|
3
|
|
|
3
|
|
2705
|
use LWP::UserAgent; |
|
|
3
|
|
|
|
|
136793
|
|
|
|
3
|
|
|
|
|
171
|
|
|
11
|
3
|
|
|
3
|
|
40
|
use URI; |
|
|
3
|
|
|
|
|
13
|
|
|
|
3
|
|
|
|
|
98
|
|
|
12
|
3
|
|
|
3
|
|
1634
|
use URI::QueryParam; |
|
|
3
|
|
|
|
|
520
|
|
|
|
3
|
|
|
|
|
131
|
|
|
13
|
3
|
|
|
3
|
|
1656
|
use Unicode::UTF8 qw(encode_utf8); |
|
|
3
|
|
|
|
|
2387
|
|
|
|
3
|
|
|
|
|
2329
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = 0.02; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Constructor. |
|
18
|
|
|
|
|
|
|
sub new { |
|
19
|
4
|
|
|
4
|
1
|
263396
|
my ($class, @params) = @_; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Create object. |
|
22
|
4
|
|
|
|
|
11
|
my $self = bless {}, $class; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# User agent. |
|
25
|
4
|
|
|
|
|
40
|
$self->{'agent'} = __PACKAGE__." ($VERSION)"; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# LWP::UserAgent object. |
|
28
|
4
|
|
|
|
|
10
|
$self->{'lwp_user_agent'} = undef; |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Query site. |
|
31
|
4
|
|
|
|
|
10
|
$self->{'query_site'} = 'query.wikidata.org'; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# SPARQL endpoint. |
|
34
|
4
|
|
|
|
|
11
|
$self->{'sparql_endpoint'} = '/bigdata/namespace/wdq/sparql'; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Verbose mode. |
|
37
|
4
|
|
|
|
|
9
|
$self->{'verbose'} = 0; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Process parameters. |
|
40
|
4
|
|
|
|
|
21
|
set_params($self, @params); |
|
41
|
|
|
|
|
|
|
|
|
42
|
2
|
100
|
|
|
|
29
|
if (! defined $self->{'lwp_user_agent'}) { |
|
43
|
|
|
|
|
|
|
$self->{'lwp_user_agent'} = LWP::UserAgent->new( |
|
44
|
1
|
|
|
|
|
16
|
'agent' => $self->{'agent'}, |
|
45
|
|
|
|
|
|
|
); |
|
46
|
|
|
|
|
|
|
} else { |
|
47
|
1
|
50
|
|
|
|
12
|
if (! $self->{'lwp_user_agent'}->isa('LWP::UserAgent')) { |
|
48
|
1
|
|
|
|
|
6
|
err "Parameter 'lwp_user_agent' must be a 'LWP::UserAgent' instance."; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Full URL of api. |
|
53
|
1
|
|
|
|
|
4455
|
$self->{'_api_uri'} = 'https://'.$self->{'query_site'}.$self->{'sparql_endpoint'}; |
|
54
|
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
14
|
return $self; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub query { |
|
59
|
0
|
|
|
0
|
1
|
|
my ($self, $query) = @_; |
|
60
|
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
if ($self->{'verbose'}) { |
|
62
|
0
|
|
|
|
|
|
print encode_utf8($query)."\n"; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my $uri = URI->new($self->{'_api_uri'}); |
|
66
|
0
|
|
|
|
|
|
$uri->query_param_append('format' => 'json'); |
|
67
|
0
|
|
|
|
|
|
$uri->query_param_append('query' => $query); |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
return $self->_http_get_json($uri->as_string); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub query_count { |
|
73
|
0
|
|
|
0
|
1
|
|
my ($self, $query) = @_; |
|
74
|
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my $ret_hr = $self->query($query); |
|
76
|
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
return $ret_hr->{'results'}->{'bindings'}->[0]->{'count'}->{'value'}; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub _http_get_json { |
|
81
|
0
|
|
|
0
|
|
|
my ($self, $uri) = @_; |
|
82
|
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
my $res = $self->{'lwp_user_agent'}->get($uri); |
|
84
|
0
|
0
|
|
|
|
|
if ($res->is_success) { |
|
85
|
0
|
|
|
|
|
|
my $content = decode_json($res->decoded_content); |
|
86
|
0
|
0
|
|
|
|
|
return wantarray ? ($content, $res->headers) : $content; |
|
87
|
|
|
|
|
|
|
} else { |
|
88
|
0
|
|
|
|
|
|
err 'Cannot get.', |
|
89
|
|
|
|
|
|
|
'Error', $res->status_line, |
|
90
|
|
|
|
|
|
|
'URI', $uri; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__ |