line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Wikibase::Datatype::Struct::Snak; |
2
|
|
|
|
|
|
|
|
3
|
28
|
|
|
28
|
|
367962
|
use base qw(Exporter); |
|
28
|
|
|
|
|
99
|
|
|
28
|
|
|
|
|
3367
|
|
4
|
28
|
|
|
28
|
|
195
|
use strict; |
|
28
|
|
|
|
|
60
|
|
|
28
|
|
|
|
|
748
|
|
5
|
28
|
|
|
28
|
|
142
|
use warnings; |
|
28
|
|
|
|
|
60
|
|
|
28
|
|
|
|
|
911
|
|
6
|
|
|
|
|
|
|
|
7
|
28
|
|
|
28
|
|
1710
|
use Error::Pure qw(err); |
|
28
|
|
|
|
|
24417
|
|
|
28
|
|
|
|
|
1245
|
|
8
|
28
|
|
|
28
|
|
335
|
use Readonly; |
|
28
|
|
|
|
|
61
|
|
|
28
|
|
|
|
|
1296
|
|
9
|
28
|
|
|
28
|
|
11201
|
use Wikibase::Datatype::Snak; |
|
28
|
|
|
|
|
119390
|
|
|
28
|
|
|
|
|
906
|
|
10
|
28
|
|
|
28
|
|
14191
|
use Wikibase::Datatype::Struct::Value; |
|
28
|
|
|
|
|
94
|
|
|
28
|
|
|
|
|
8004
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = 0.08; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub obj2struct { |
17
|
35
|
|
|
35
|
1
|
7421
|
my ($obj, $base_uri) = @_; |
18
|
|
|
|
|
|
|
|
19
|
35
|
100
|
|
|
|
135
|
if (! defined $obj) { |
20
|
1
|
|
|
|
|
4
|
err "Object doesn't exist."; |
21
|
|
|
|
|
|
|
} |
22
|
34
|
100
|
|
|
|
132
|
if (! $obj->isa('Wikibase::Datatype::Snak')) { |
23
|
1
|
|
|
|
|
6
|
err "Object isn't 'Wikibase::Datatype::Snak'."; |
24
|
|
|
|
|
|
|
} |
25
|
33
|
100
|
|
|
|
80
|
if (! defined $base_uri) { |
26
|
1
|
|
|
|
|
4
|
err 'Base URI is required.'; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
32
|
100
|
|
|
|
109
|
my $struct_hr = { |
30
|
|
|
|
|
|
|
defined $obj->datavalue |
31
|
|
|
|
|
|
|
? ('datavalue' => Wikibase::Datatype::Struct::Value::obj2struct($obj->datavalue, $base_uri)) |
32
|
|
|
|
|
|
|
: (), |
33
|
|
|
|
|
|
|
'datatype' => $obj->datatype, |
34
|
|
|
|
|
|
|
'property' => $obj->property, |
35
|
|
|
|
|
|
|
'snaktype' => $obj->snaktype, |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
|
38
|
32
|
|
|
|
|
712
|
return $struct_hr; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub struct2obj { |
42
|
27
|
|
|
27
|
1
|
5249
|
my $struct_hr = shift; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Data value isn't required for snaktype 'novalue'. |
45
|
27
|
|
|
|
|
54
|
my $datavalue; |
46
|
27
|
100
|
|
|
|
77
|
if (exists $struct_hr->{'datavalue'}) { |
47
|
24
|
|
|
|
|
80
|
$datavalue = Wikibase::Datatype::Struct::Value::struct2obj($struct_hr->{'datavalue'}); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $obj = Wikibase::Datatype::Snak->new( |
51
|
|
|
|
|
|
|
'datavalue' => $datavalue, |
52
|
|
|
|
|
|
|
'datatype' => $struct_hr->{'datatype'}, |
53
|
|
|
|
|
|
|
'property' => $struct_hr->{'property'}, |
54
|
27
|
|
|
|
|
143
|
'snaktype' => $struct_hr->{'snaktype'}, |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
24
|
|
|
|
|
4850
|
return $obj; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
__END__ |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=pod |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=encoding utf8 |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 NAME |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Wikibase::Datatype::Struct::Snak - Wikibase snak structure serialization. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Snak qw(obj2struct struct2obj); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, $base_uri); |
77
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 DESCRIPTION |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
This conversion is between objects defined in Wikibase::Datatype and structures |
82
|
|
|
|
|
|
|
serialized via JSON to MediaWiki. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 SUBROUTINES |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 C<obj2struct> |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, $base_uri); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Convert Wikibase::Datatype::Snak instance to structure. |
91
|
|
|
|
|
|
|
C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/). |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Returns reference to hash with structure. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 C<struct2obj> |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Convert structure of snak to object. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Returns Wikibase::Datatype::Snak instance. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 ERRORS |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
obj2struct(): |
106
|
|
|
|
|
|
|
Base URI is required. |
107
|
|
|
|
|
|
|
Object doesn't exist. |
108
|
|
|
|
|
|
|
Object isn't 'Wikibase::Datatype::Snak'. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 EXAMPLE1 |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
use strict; |
113
|
|
|
|
|
|
|
use warnings; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
use Data::Printer; |
116
|
|
|
|
|
|
|
use Wikibase::Datatype::Snak; |
117
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Snak qw(obj2struct); |
118
|
|
|
|
|
|
|
use Wikibase::Datatype::Value::Item; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Object. |
121
|
|
|
|
|
|
|
# instance of (P31) human (Q5) |
122
|
|
|
|
|
|
|
my $obj = Wikibase::Datatype::Snak->new( |
123
|
|
|
|
|
|
|
'datatype' => 'wikibase-item', |
124
|
|
|
|
|
|
|
'datavalue' => Wikibase::Datatype::Value::Item->new( |
125
|
|
|
|
|
|
|
'value' => 'Q5', |
126
|
|
|
|
|
|
|
), |
127
|
|
|
|
|
|
|
'property' => 'P31', |
128
|
|
|
|
|
|
|
); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Get structure. |
131
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/'); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# Dump to output. |
134
|
|
|
|
|
|
|
p $struct_hr; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Output: |
137
|
|
|
|
|
|
|
# \ { |
138
|
|
|
|
|
|
|
# datatype "wikibase-item", |
139
|
|
|
|
|
|
|
# datavalue { |
140
|
|
|
|
|
|
|
# type "wikibase-entityid", |
141
|
|
|
|
|
|
|
# value { |
142
|
|
|
|
|
|
|
# entity-type "item", |
143
|
|
|
|
|
|
|
# id "Q5", |
144
|
|
|
|
|
|
|
# numeric-id 5 |
145
|
|
|
|
|
|
|
# } |
146
|
|
|
|
|
|
|
# }, |
147
|
|
|
|
|
|
|
# property "P31", |
148
|
|
|
|
|
|
|
# snaktype "value" |
149
|
|
|
|
|
|
|
# } |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 EXAMPLE2 |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
use strict; |
154
|
|
|
|
|
|
|
use warnings; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Snak qw(struct2obj); |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Item structure. |
159
|
|
|
|
|
|
|
my $struct_hr = { |
160
|
|
|
|
|
|
|
'datatype' => 'wikibase-item', |
161
|
|
|
|
|
|
|
'datavalue' => { |
162
|
|
|
|
|
|
|
'type' => 'wikibase-entityid', |
163
|
|
|
|
|
|
|
'value' => { |
164
|
|
|
|
|
|
|
'entity-type' => 'item', |
165
|
|
|
|
|
|
|
'id' => 'Q5', |
166
|
|
|
|
|
|
|
'numeric-id' => 5, |
167
|
|
|
|
|
|
|
}, |
168
|
|
|
|
|
|
|
}, |
169
|
|
|
|
|
|
|
'property' => 'P31', |
170
|
|
|
|
|
|
|
'snaktype' => 'value', |
171
|
|
|
|
|
|
|
}; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# Get object. |
174
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Get value. |
177
|
|
|
|
|
|
|
my $datavalue = $obj->datavalue->value; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# Get datatype. |
180
|
|
|
|
|
|
|
my $datatype = $obj->datatype; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# Get property. |
183
|
|
|
|
|
|
|
my $property = $obj->property; |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# Print out. |
186
|
|
|
|
|
|
|
print "Property: $property\n"; |
187
|
|
|
|
|
|
|
print "Type: $datatype\n"; |
188
|
|
|
|
|
|
|
print "Value: $datavalue\n"; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# Output: |
191
|
|
|
|
|
|
|
# Property: P31 |
192
|
|
|
|
|
|
|
# Type: wikibase-item |
193
|
|
|
|
|
|
|
# Value: Q5 |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
L<Error::Pure>, |
198
|
|
|
|
|
|
|
L<Exporter>, |
199
|
|
|
|
|
|
|
L<Readonly>, |
200
|
|
|
|
|
|
|
L<Wikibase::Datatype::Snak>, |
201
|
|
|
|
|
|
|
L<Wikibase::Datatype::Struct::Value>. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 SEE ALSO |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=over |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Struct> |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Wikibase structure serialization. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Snak> |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Wikibase snak datatype. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=back |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 REPOSITORY |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct> |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 AUTHOR |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
L<http://skim.cz> |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
© Michal Josef Špaček 2020-2021 |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
BSD 2-Clause License |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head1 VERSION |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
0.08 |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=cut |