line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
use base qw(Exporter); |
3
|
28
|
|
|
28
|
|
404883
|
use strict; |
|
28
|
|
|
|
|
75
|
|
|
28
|
|
|
|
|
2201
|
|
4
|
28
|
|
|
28
|
|
271
|
use warnings; |
|
28
|
|
|
|
|
48
|
|
|
28
|
|
|
|
|
432
|
|
5
|
28
|
|
|
28
|
|
107
|
|
|
28
|
|
|
|
|
41
|
|
|
28
|
|
|
|
|
608
|
|
6
|
|
|
|
|
|
|
use Error::Pure qw(err); |
7
|
28
|
|
|
28
|
|
1033
|
use Readonly; |
|
28
|
|
|
|
|
23880
|
|
|
28
|
|
|
|
|
835
|
|
8
|
28
|
|
|
28
|
|
248
|
use Wikibase::Datatype::Reference; |
|
28
|
|
|
|
|
51
|
|
|
28
|
|
|
|
|
734
|
|
9
|
28
|
|
|
28
|
|
8134
|
use Wikibase::Datatype::Struct::Utils qw(obj_array_ref2struct struct2snaks_array_ref); |
|
28
|
|
|
|
|
18262
|
|
|
28
|
|
|
|
|
650
|
|
10
|
28
|
|
|
28
|
|
8838
|
|
|
28
|
|
|
|
|
69
|
|
|
28
|
|
|
|
|
490
|
|
11
|
|
|
|
|
|
|
Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = 0.09; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my ($obj, $base_uri) = @_; |
16
|
|
|
|
|
|
|
|
17
|
9
|
|
|
9
|
1
|
7692
|
if (! defined $obj) { |
18
|
|
|
|
|
|
|
err "Object doesn't exist."; |
19
|
9
|
100
|
|
|
|
24
|
} |
20
|
1
|
|
|
|
|
3
|
if (! $obj->isa('Wikibase::Datatype::Reference')) { |
21
|
|
|
|
|
|
|
err "Object isn't 'Wikibase::Datatype::Reference'."; |
22
|
8
|
100
|
|
|
|
33
|
} |
23
|
1
|
|
|
|
|
5
|
if (! defined $base_uri) { |
24
|
|
|
|
|
|
|
err 'Base URI is required.'; |
25
|
7
|
100
|
|
|
|
15
|
} |
26
|
1
|
|
|
|
|
4
|
|
27
|
|
|
|
|
|
|
my $struct_hr = obj_array_ref2struct($obj->snaks, 'snaks', $base_uri); |
28
|
|
|
|
|
|
|
|
29
|
6
|
|
|
|
|
22
|
return $struct_hr; |
30
|
|
|
|
|
|
|
} |
31
|
6
|
|
|
|
|
25
|
|
32
|
|
|
|
|
|
|
my $struct_hr = shift; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $obj = Wikibase::Datatype::Reference->new( |
35
|
6
|
|
|
6
|
1
|
2201
|
'snaks' => struct2snaks_array_ref($struct_hr, 'snaks'), |
36
|
|
|
|
|
|
|
); |
37
|
6
|
|
|
|
|
20
|
|
38
|
|
|
|
|
|
|
return $obj; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
6
|
|
|
|
|
388
|
1; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=pod |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=encoding utf8 |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 NAME |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Wikibase::Datatype::Struct::Reference - Wikibase reference structure serialization. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 SYNOPSIS |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Reference qw(obj2struct struct2obj); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, $base_uri); |
57
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 DESCRIPTION |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
This conversion is between objects defined in Wikibase::Datatype and structures |
62
|
|
|
|
|
|
|
serialized via JSON to MediaWiki. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 SUBROUTINES |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 C<obj2struct> |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, $base_uri); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Convert Wikibase::Datatype::Reference instance to structure. |
71
|
|
|
|
|
|
|
C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/). |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Returns reference to hash with structure. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 C<struct2obj> |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Convert structure of reference to object. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Returns Wikibase::Datatype::Reference instance. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 ERRORS |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
obj2struct(): |
86
|
|
|
|
|
|
|
Base URI is required. |
87
|
|
|
|
|
|
|
Object doesn't exist. |
88
|
|
|
|
|
|
|
Object isn't 'Wikibase::Datatype::Reference'. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 EXAMPLE1 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
use strict; |
93
|
|
|
|
|
|
|
use warnings; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
use Data::Printer; |
96
|
|
|
|
|
|
|
use Wikibase::Datatype::Reference; |
97
|
|
|
|
|
|
|
use Wikibase::Datatype::Snak; |
98
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Reference qw(obj2struct); |
99
|
|
|
|
|
|
|
use Wikibase::Datatype::Value::Item; |
100
|
|
|
|
|
|
|
use Wikibase::Datatype::Value::String; |
101
|
|
|
|
|
|
|
use Wikibase::Datatype::Value::Time; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Object. |
104
|
|
|
|
|
|
|
# instance of (P31) human (Q5) |
105
|
|
|
|
|
|
|
my $obj = Wikibase::Datatype::Reference->new( |
106
|
|
|
|
|
|
|
'snaks' => [ |
107
|
|
|
|
|
|
|
# stated in (P248) Virtual International Authority File (Q53919) |
108
|
|
|
|
|
|
|
Wikibase::Datatype::Snak->new( |
109
|
|
|
|
|
|
|
'datatype' => 'wikibase-item', |
110
|
|
|
|
|
|
|
'datavalue' => Wikibase::Datatype::Value::Item->new( |
111
|
|
|
|
|
|
|
'value' => 'Q53919', |
112
|
|
|
|
|
|
|
), |
113
|
|
|
|
|
|
|
'property' => 'P248', |
114
|
|
|
|
|
|
|
), |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# VIAF ID (P214) 113230702 |
117
|
|
|
|
|
|
|
Wikibase::Datatype::Snak->new( |
118
|
|
|
|
|
|
|
'datatype' => 'external-id', |
119
|
|
|
|
|
|
|
'datavalue' => Wikibase::Datatype::Value::String->new( |
120
|
|
|
|
|
|
|
'value' => '113230702', |
121
|
|
|
|
|
|
|
), |
122
|
|
|
|
|
|
|
'property' => 'P214', |
123
|
|
|
|
|
|
|
), |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# retrieved (P813) 7 December 2013 |
126
|
|
|
|
|
|
|
Wikibase::Datatype::Snak->new( |
127
|
|
|
|
|
|
|
'datatype' => 'time', |
128
|
|
|
|
|
|
|
'datavalue' => Wikibase::Datatype::Value::Time->new( |
129
|
|
|
|
|
|
|
'value' => '+2013-12-07T00:00:00Z', |
130
|
|
|
|
|
|
|
), |
131
|
|
|
|
|
|
|
'property' => 'P813', |
132
|
|
|
|
|
|
|
), |
133
|
|
|
|
|
|
|
], |
134
|
|
|
|
|
|
|
); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Get structure. |
137
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/'); |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# Dump to output. |
140
|
|
|
|
|
|
|
p $struct_hr; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Output: |
143
|
|
|
|
|
|
|
# \ { |
144
|
|
|
|
|
|
|
# snaks { |
145
|
|
|
|
|
|
|
# P214 [ |
146
|
|
|
|
|
|
|
# [0] { |
147
|
|
|
|
|
|
|
# datatype "external-id", |
148
|
|
|
|
|
|
|
# datavalue { |
149
|
|
|
|
|
|
|
# type "string", |
150
|
|
|
|
|
|
|
# value 113230702 |
151
|
|
|
|
|
|
|
# }, |
152
|
|
|
|
|
|
|
# property "P214", |
153
|
|
|
|
|
|
|
# snaktype "value" |
154
|
|
|
|
|
|
|
# } |
155
|
|
|
|
|
|
|
# ], |
156
|
|
|
|
|
|
|
# P248 [ |
157
|
|
|
|
|
|
|
# [0] { |
158
|
|
|
|
|
|
|
# datatype "wikibase-item", |
159
|
|
|
|
|
|
|
# datavalue { |
160
|
|
|
|
|
|
|
# type "wikibase-entityid", |
161
|
|
|
|
|
|
|
# value { |
162
|
|
|
|
|
|
|
# entity-type "item", |
163
|
|
|
|
|
|
|
# id "Q53919", |
164
|
|
|
|
|
|
|
# numeric-id 53919 |
165
|
|
|
|
|
|
|
# } |
166
|
|
|
|
|
|
|
# }, |
167
|
|
|
|
|
|
|
# property "P248", |
168
|
|
|
|
|
|
|
# snaktype "value" |
169
|
|
|
|
|
|
|
# } |
170
|
|
|
|
|
|
|
# ], |
171
|
|
|
|
|
|
|
# P813 [ |
172
|
|
|
|
|
|
|
# [0] { |
173
|
|
|
|
|
|
|
# datatype "time", |
174
|
|
|
|
|
|
|
# datavalue { |
175
|
|
|
|
|
|
|
# type "time", |
176
|
|
|
|
|
|
|
# value { |
177
|
|
|
|
|
|
|
# after 0, |
178
|
|
|
|
|
|
|
# before 0, |
179
|
|
|
|
|
|
|
# calendarmodel "http://test.wikidata.org/entity/Q1985727", |
180
|
|
|
|
|
|
|
# precision 11, |
181
|
|
|
|
|
|
|
# time "+2013-12-07T00:00:00Z", |
182
|
|
|
|
|
|
|
# timezone 0 |
183
|
|
|
|
|
|
|
# } |
184
|
|
|
|
|
|
|
# }, |
185
|
|
|
|
|
|
|
# property "P813", |
186
|
|
|
|
|
|
|
# snaktype "value" |
187
|
|
|
|
|
|
|
# } |
188
|
|
|
|
|
|
|
# ] |
189
|
|
|
|
|
|
|
# }, |
190
|
|
|
|
|
|
|
# snaks-order [ |
191
|
|
|
|
|
|
|
# [0] "P248", |
192
|
|
|
|
|
|
|
# [1] "P214", |
193
|
|
|
|
|
|
|
# [2] "P813" |
194
|
|
|
|
|
|
|
# ] |
195
|
|
|
|
|
|
|
# } |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 EXAMPLE2 |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
use strict; |
200
|
|
|
|
|
|
|
use warnings; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Reference qw(struct2obj); |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# Item structure. |
205
|
|
|
|
|
|
|
my $struct_hr = { |
206
|
|
|
|
|
|
|
'snaks' => { |
207
|
|
|
|
|
|
|
'P214' => [{ |
208
|
|
|
|
|
|
|
'datatype' => 'external-id', |
209
|
|
|
|
|
|
|
'datavalue' => { |
210
|
|
|
|
|
|
|
'type' => 'string', |
211
|
|
|
|
|
|
|
'value' => '113230702', |
212
|
|
|
|
|
|
|
}, |
213
|
|
|
|
|
|
|
'property' => 'P214', |
214
|
|
|
|
|
|
|
'snaktype' => 'value', |
215
|
|
|
|
|
|
|
}], |
216
|
|
|
|
|
|
|
'P248' => [{ |
217
|
|
|
|
|
|
|
'datatype' => 'wikibase-item', |
218
|
|
|
|
|
|
|
'datavalue' => { |
219
|
|
|
|
|
|
|
'type' => 'wikibase-entityid', |
220
|
|
|
|
|
|
|
'value' => { |
221
|
|
|
|
|
|
|
'entity-type' => 'item', |
222
|
|
|
|
|
|
|
'id' => 'Q53919', |
223
|
|
|
|
|
|
|
'numeric-id' => 53919, |
224
|
|
|
|
|
|
|
}, |
225
|
|
|
|
|
|
|
}, |
226
|
|
|
|
|
|
|
'property' => 'P248', |
227
|
|
|
|
|
|
|
'snaktype' => 'value', |
228
|
|
|
|
|
|
|
}], |
229
|
|
|
|
|
|
|
'P813' => [{ |
230
|
|
|
|
|
|
|
'datatype' => 'time', |
231
|
|
|
|
|
|
|
'datavalue' => { |
232
|
|
|
|
|
|
|
'type' => 'time', |
233
|
|
|
|
|
|
|
'value' => { |
234
|
|
|
|
|
|
|
'after' => 0, |
235
|
|
|
|
|
|
|
'before' => 0, |
236
|
|
|
|
|
|
|
'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727', |
237
|
|
|
|
|
|
|
'precision' => 11, |
238
|
|
|
|
|
|
|
'time' => '+2013-12-07T00:00:00Z', |
239
|
|
|
|
|
|
|
'timezone' => 0, |
240
|
|
|
|
|
|
|
}, |
241
|
|
|
|
|
|
|
}, |
242
|
|
|
|
|
|
|
'property' => 'P813', |
243
|
|
|
|
|
|
|
'snaktype' => 'value', |
244
|
|
|
|
|
|
|
}], |
245
|
|
|
|
|
|
|
}, |
246
|
|
|
|
|
|
|
'snaks-order' => [ |
247
|
|
|
|
|
|
|
'P248', |
248
|
|
|
|
|
|
|
'P214', |
249
|
|
|
|
|
|
|
'P813', |
250
|
|
|
|
|
|
|
], |
251
|
|
|
|
|
|
|
}; |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
# Get object. |
254
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# Get value. |
257
|
|
|
|
|
|
|
my $snaks_ar = $obj->snaks; |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
# Print out number of snaks. |
260
|
|
|
|
|
|
|
print "Number of snaks: ".@{$snaks_ar}."\n"; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
# Output: |
263
|
|
|
|
|
|
|
# Number of snaks: 3 |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
L<Error::Pure>, |
268
|
|
|
|
|
|
|
L<Exporter>, |
269
|
|
|
|
|
|
|
L<Readonly>, |
270
|
|
|
|
|
|
|
L<Wikibase::Datatype::Reference>, |
271
|
|
|
|
|
|
|
L<Wikibase::Datatype::Struct::Utils>. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head1 SEE ALSO |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=over |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Struct> |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
Wikibase structure serialization. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Reference> |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
Wikibase reference datatype. |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=back |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=head1 REPOSITORY |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct> |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head1 AUTHOR |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
L<http://skim.cz> |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
© 2020-2022 Michal Josef Špaček |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
BSD 2-Clause License |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=head1 VERSION |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
0.09 |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=cut |