API.pm

filter: Statementstmt 48.5% 50 / 103 Branchbran 10.7% 3 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 12 / 18 Podpod 100% 4 / 4 totaltotal 43.6% 69 / 158 CC 16 SCAR 26.3
CC
16
Cov
40%
get_item
42.0
_init
20.0
_obj2json
20.0
CRAP 13.9
↑ Summary API/Resolve.pm »
1 package Wikibase::API;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use JSON::XS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use MediaWiki::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Unicode::UTF8 qw(decode_utf8 encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Wikibase::Datatype::Struct::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Wikibase::Datatype::Struct::Lexeme;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Wikibase::Datatype::Struct::Mediainfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Wikibase::Datatype::Struct::Property;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 our $VERSION = 0.07;
17  
18 # Constructor.
19 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
20 4 my ($class, @params) = @_;
21  
22 # Create object.
23 4 my $self = bless {}, $class;
24  
25 # Login name.
26 4 $self->{'login_name'} = undef;
27  
28 # Login password.
29 4 $self->{'login_password'} = undef;
30  
31 # MediaWiki::API object.
32 4 $self->{'mediawiki_api'} = MediaWiki::API->new;
33  
34 # MediaWiki site.
35 4 $self->{'mediawiki_site'} = 'test.wikidata.org';
36  
37 # Process parameters.
38 4 set_params($self, @params);
39  
40 3 $self->{'_mediawiki_base_uri'} = 'https://'.$self->{'mediawiki_site'};
41 # XXX Entity URI has http instead of https.
42 3 ▶ if ($self->{'mediawiki_site'} eq 'test.wikidata.org') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'mediawiki_site'} eq 'test.wikidata.org') { } 3 0 3
43 # XXX test.wikidata.org has same entity url as Wikidata.
44 3 $self->{'_mediawiki_entity_uri'} = 'http://www.wikidata.org/entity/';
45 } else {
46 0 $self->{'_mediawiki_entity_uri'} = 'http://'.$self->{'mediawiki_site'}.'/entity/';
47 }
48  
49 3 ▶ if (ref $self->{'mediawiki_api'} ne 'MediaWiki::API') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"mediawiki_api"} ne "MediaWiki::API") 2 1 3
50 2 err "Parameter 'mediawiki_api' must be a 'MediaWiki::API' instance."
51 }
52 $self->{'mediawiki_api'}->{'config'}->{'api_url'}
53 1 = $self->{'_mediawiki_base_uri'}.'/w/api.php';
54  
55 1 $self->{'_init'} = 0;
56  
57 1 return $self;
58 }
59  
60 0 ▶sub create_item {
Subroutine create_item: not called CC=2 SCAR=17.9
Pod documented
61 0 my ($self, $item_obj, $summary) = @_;
62  
63 0 $self->_init;
64  
65 0 my ($type, $data) = $self->_obj2json($item_obj);
66 my $res = $self->{'mediawiki_api'}->api({
67 'action' => 'wbeditentity',
68 'new' => $type,
69 'data' => $data,
70 defined $summary ? (
71 'summary' => $summary,
72 ) : (),
73 0 ▶ 'token' => $self->{'_csrftoken'},
Branch: 1 branch
BranchTrueFalseTotal
defined $summary ? : 0 0 0
74 });
75 0 $self->_mediawiki_api_error($res, 'Cannot create item.');
76  
77 0 return $res;
78 }
79  
80 0 ▶sub get_item {
Subroutine get_item: not called CC=6 SCAR=37.4
Pod documented
81 0 my ($self, $id, $opts_hr) = @_;
82  
83 0 $self->_init;
84  
85 0 my $struct_hr = $self->get_item_raw($id, $opts_hr);
86 0 ▶ if (! exists $struct_hr->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $struct_hr->{"type"}) 0 0 0
87 0 return;
88 }
89  
90 # XXX Rewrite to Wikibase::Datatype::Struct
91 0 my $item_obj;
92 0 ▶ if ($struct_hr->{'type'} eq 'item') {
Branch: 4 branches
BranchTrueFalseTotal
if ($struct_hr->{'type'} eq 'item') { } 0 0 0
elsif ($struct_hr->{'type'} eq 'mediainfo') { } 0 0 0
elsif ($struct_hr->{'type'} eq 'lexeme') { } 0 0 0
elsif ($struct_hr->{'type'} eq 'property') { } 0 0 0
93 0 $item_obj = Wikibase::Datatype::Struct::Item::struct2obj($struct_hr);
94 } elsif ($struct_hr->{'type'} eq 'mediainfo') {
95 0 $item_obj = Wikibase::Datatype::Struct::Mediainfo::struct2obj($struct_hr);
96 } elsif ($struct_hr->{'type'} eq 'lexeme') {
97 0 $item_obj = Wikibase::Datatype::Struct::Lexeme::struct2obj($struct_hr);
98 } elsif ($struct_hr->{'type'} eq 'property') {
99 0 $item_obj = Wikibase::Datatype::Struct::Property::struct2obj($struct_hr);
100 } else {
101 err 'Unsupported type.',
102 0 'Type', $struct_hr->{'type'},
103 ;
104 }
105  
106 0 return $item_obj;
107 }
108  
109 0 ▶sub get_item_raw {
Subroutine get_item_raw: not called CC=1 SCAR=6.9
Pod documented
110 0 my ($self, $id, $opts_hr) = @_;
111  
112 0 $self->_init;
113  
114 # TODO $opts_hr - Muzu vyfiltrovat jenom claims napr.
115  
116 0 my $res = $self->{'mediawiki_api'}->api({
117 'action' => 'wbgetentities',
118 'format' => 'json',
119 'ids' => $id,
120 });
121 0 $self->_mediawiki_api_error($res, 'Cannot get item.');
122  
123 0 my $struct_hr = $res->{'entities'}->{$id};
124  
125 0 return $struct_hr;
126 }
127  
128 0 ▶sub _init {
Subroutine _init: not called CC=4 SCAR=30.0
129 0 my $self = shift;
130  
131 0 ▶ if ($self->{'_init'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_init"}) 0 0 0
132 0 return;
133 }
134  
135 # Login.
136 0 ▶ if (defined $self->{'login_name'} && defined $self->{'login_password'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"login_name"} and defined $self->{"login_password"}) 0 0 0
Condition0%0/3
defined $self->{"login_name"} and defined $self->{"login_password"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"login_name"} and defined $self->{"login_password"}
defined $self->{"login_name"} defined $self->{"login_password"}
Truth table0%0/3
A and B
A= defined $self->{"login_name"}B= defined $self->{"login_password"}
ABresult
0X0
100
111
137 my $login_ret = $self->{'mediawiki_api'}->login({
138 'lgname' => $self->{'login_name'},
139 0 'lgpassword' => $self->{'login_password'},
140 });
141 0 $self->_mediawiki_api_error($login_ret, 'Cannot login.');
142 }
143  
144 # Token.
145 0 my $token_hr = $self->{'mediawiki_api'}->api({
146 'action' => 'query',
147 'meta' => 'tokens',
148 });
149 0 $self->_mediawiki_api_error($token_hr, 'Cannot get token.');
150 0 $self->{'_csrftoken'} = $token_hr->{'query'}->{'tokens'}->{'csrftoken'};
151  
152 # Initialized.
153 0 $self->{'_init'} = 1;
154  
155 0 return;
156 }
157  
158 0 ▶sub _obj2json {
Subroutine _obj2json: not called CC=4 SCAR=30.0
159 0 my ($self, $item_obj) = @_;
160  
161 0 my $type;
162 my $struct_hr;
163 0 ▶ if (! defined $item_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $item_obj) { } 0 0 0
164 0 return '{}';
165 } else {
166 0 ▶ if ($item_obj->isa('Wikibase::Datatype::Item')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item_obj->isa('Wikibase::Datatype::Item')) { } 0 0 0
elsif ($item_obj->isa('Wikibase::Datatype::Lexeme')) { } 0 0 0
167 0 $type = 'item';
168 $struct_hr = Wikibase::Datatype::Struct::Item::obj2struct($item_obj,
169 0 $self->{'_mediawiki_entity_uri'});
170 } elsif ($item_obj->isa('Wikibase::Datatype::Lexeme')) {
171 0 $type = 'lexeme';
172 $struct_hr = Wikibase::Datatype::Struct::Lexeme::obj2struct($item_obj,
173 0 $self->{'_mediawiki_entity_uri'});
174 } else {
175 0 err 'Bad data. Not supported object.';
176 }
177 }
178  
179 0 my $json = decode_utf8(JSON::XS->new->utf8->encode($struct_hr));
180  
181 0 return ($type, $json);
182 }
183  
184 0 ▶sub _mediawiki_api_error {
Subroutine _mediawiki_api_error: not called CC=2 SCAR=17.9
185 0 my ($self, $res, $message) = @_;
186  
187 0 ▶ if (! defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
188 err $message,
189 'Error code' => $self->{'mediawiki_api'}->{'error'}->{'code'},
190 0 'Error details' => encode_utf8($self->{'mediawiki_api'}->{'error'}->{'details'}),
191 ;
192 }
193  
194 0 return;
195 }
196  
197 6 1;
198  
199 __END__
↑ Summary API/Resolve.pm »