line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::Wikidata; |
2
|
|
|
|
|
|
|
#ABSTRACT: Import from Wikidata |
3
|
|
|
|
|
|
|
our $VERSION = '0.06'; #VERSION |
4
|
1
|
|
|
1
|
|
1563
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
121177
|
|
|
1
|
|
|
|
|
7
|
|
5
|
1
|
|
|
1
|
|
289
|
use Moo; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
6
|
1
|
|
|
1
|
|
1398
|
use URI::Template; |
|
1
|
|
|
|
|
13132
|
|
|
1
|
|
|
|
|
1142
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
extends 'Catmandu::Importer::getJSON'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has api => ( |
11
|
|
|
|
|
|
|
is => 'ro', |
12
|
|
|
|
|
|
|
default => sub { 'http://www.wikidata.org/w/api.php' } |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has '+url' => ( |
16
|
|
|
|
|
|
|
is => 'ro', |
17
|
|
|
|
|
|
|
lazy => 1, |
18
|
|
|
|
|
|
|
builder => sub { |
19
|
0
|
|
|
0
|
|
|
URI::Template->new( |
20
|
|
|
|
|
|
|
$_[0]->api |
21
|
|
|
|
|
|
|
. '?action=wbgetentities&format=json{&ids}{&sites}{&titles}' |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has '+from' => ( |
27
|
|
|
|
|
|
|
is => 'ro', |
28
|
|
|
|
|
|
|
lazy => 1, |
29
|
|
|
|
|
|
|
builder => \&_build_from, |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has ids => ( |
33
|
|
|
|
|
|
|
is => 'ro', |
34
|
|
|
|
|
|
|
coerce => sub { [ split /[,| ]/, $_[0] ] } |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
has site => ( |
38
|
|
|
|
|
|
|
is => 'ro', |
39
|
|
|
|
|
|
|
default => sub { 'enwiki' }, |
40
|
|
|
|
|
|
|
trigger => sub { |
41
|
|
|
|
|
|
|
my ($self,$site) = @_; |
42
|
|
|
|
|
|
|
die "invalid site $site" if $site !~ /^[a-z]+([_-][a-z])*$/; |
43
|
|
|
|
|
|
|
$site =~ s/-/_/g; |
44
|
|
|
|
|
|
|
return $site; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
has title => ( |
49
|
|
|
|
|
|
|
is => 'ro', |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _build_from { |
53
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
my $vars; |
56
|
|
|
|
|
|
|
|
57
|
0
|
0
|
|
|
|
|
if ($self->ids) { |
|
|
0
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
my @ids = map { |
59
|
0
|
|
|
|
|
|
$_ =~ /^[QP][0-9]+$/i or die "invalid wikidata id $_\n"; |
60
|
0
|
|
|
|
|
|
uc($_); |
61
|
0
|
|
|
|
|
|
} @{$self->ids}; |
62
|
0
|
|
|
|
|
|
$vars = { ids => join('|', @ids) }; |
63
|
|
|
|
|
|
|
} elsif(defined $self->title) { |
64
|
0
|
|
|
|
|
|
my ($site, $title); |
65
|
0
|
0
|
|
|
|
|
if ($self->title =~ /^([a-z]+([_-][a-z])*):(.+)$/) { |
66
|
0
|
|
|
|
|
|
($site, $title) = ($1,$3); |
67
|
|
|
|
|
|
|
} else { |
68
|
0
|
|
|
|
|
|
($site, $title) = ($self->site,$self->title); |
69
|
|
|
|
|
|
|
} |
70
|
0
|
0
|
|
|
|
|
die "invalid site $site" if $site !~ /^[a-z]+([_-][a-z])*$/; |
71
|
0
|
|
|
|
|
|
$site =~ s/-/_/g; |
72
|
0
|
|
|
|
|
|
$vars = { sites => $site, titles => $title }; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
return ($vars ? $self->url->process($vars) : undef); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub request_hook { |
79
|
0
|
|
|
0
|
1
|
|
my ($self, $line) = @_; |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
if ($line =~ /^[PQ][0-9]+$/i) { |
|
|
0
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
return { ids => uc($line) }; |
83
|
|
|
|
|
|
|
} elsif ($line =~ /^([a-z]+([_-][a-z])*):(.+)$/) { |
84
|
0
|
|
|
|
|
|
my ($site, $title) = ($1,$3); |
85
|
0
|
|
|
|
|
|
$site =~ s/-/_/g; |
86
|
0
|
|
|
|
|
|
return { sites => $site, titles => $title }; |
87
|
|
|
|
|
|
|
} else { |
88
|
0
|
|
|
|
|
|
return { sites => $self->site, titles => $line }; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
return; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub response_hook { |
95
|
0
|
|
|
0
|
1
|
|
my ($self, $data) = @_; |
96
|
0
|
0
|
0
|
|
|
|
return unless ref $data and ref $data->{entities} eq 'HASH'; |
97
|
|
|
|
|
|
|
return [ |
98
|
0
|
0
|
|
|
|
|
map { |
99
|
0
|
|
|
|
|
|
$_->{missing} = 1 if exists $_->{missing}; |
100
|
0
|
|
|
|
|
|
$_; |
101
|
0
|
|
|
|
|
|
} grep { ref $_ eq 'HASH'; } |
102
|
0
|
|
|
|
|
|
values %{$data->{entities}} |
103
|
|
|
|
|
|
|
]; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__END__ |