line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Wikibase::Datatype::Struct::Sense; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
1022929
|
use base qw(Exporter); |
|
8
|
|
|
|
|
56
|
|
|
8
|
|
|
|
|
781
|
|
4
|
8
|
|
|
8
|
|
51
|
use strict; |
|
8
|
|
|
|
|
19
|
|
|
8
|
|
|
|
|
149
|
|
5
|
8
|
|
|
8
|
|
38
|
use warnings; |
|
8
|
|
|
|
|
26
|
|
|
8
|
|
|
|
|
252
|
|
6
|
|
|
|
|
|
|
|
7
|
8
|
|
|
8
|
|
1356
|
use Error::Pure qw(err); |
|
8
|
|
|
|
|
34323
|
|
|
8
|
|
|
|
|
259
|
|
8
|
8
|
|
|
8
|
|
190
|
use Readonly; |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
326
|
|
9
|
8
|
|
|
8
|
|
3452
|
use Wikibase::Datatype::Sense; |
|
8
|
|
|
|
|
19769
|
|
|
8
|
|
|
|
|
235
|
|
10
|
8
|
|
|
8
|
|
1778
|
use Wikibase::Datatype::Struct::Language; |
|
8
|
|
|
|
|
28
|
|
|
8
|
|
|
|
|
356
|
|
11
|
8
|
|
|
8
|
|
2049
|
use Wikibase::Datatype::Struct::Statement; |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
3745
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = 0.11; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub obj2struct { |
18
|
5
|
|
|
5
|
1
|
12702
|
my ($obj, $base_uri) = @_; |
19
|
|
|
|
|
|
|
|
20
|
5
|
100
|
|
|
|
19
|
if (! defined $obj) { |
21
|
1
|
|
|
|
|
4
|
err "Object doesn't exist."; |
22
|
|
|
|
|
|
|
} |
23
|
4
|
100
|
|
|
|
29
|
if (! $obj->isa('Wikibase::Datatype::Sense')) { |
24
|
1
|
|
|
|
|
7
|
err "Object isn't 'Wikibase::Datatype::Sense'."; |
25
|
|
|
|
|
|
|
} |
26
|
3
|
100
|
|
|
|
13
|
if (! defined $base_uri) { |
27
|
1
|
|
|
|
|
5
|
err 'Base URI is required.'; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
2
|
|
|
|
|
12
|
my $struct_hr = { |
31
|
|
|
|
|
|
|
'id' => $obj->id, |
32
|
|
|
|
|
|
|
}; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# Glosses. |
35
|
2
|
|
|
|
|
21
|
foreach my $glosse (@{$obj->glosses}) { |
|
2
|
|
|
|
|
7
|
|
36
|
4
|
|
|
|
|
36
|
$struct_hr->{'glosses'}->{$glosse->language} |
37
|
|
|
|
|
|
|
= Wikibase::Datatype::Struct::Language::obj2struct($glosse); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Statements. |
41
|
2
|
|
|
|
|
15
|
foreach my $statement (@{$obj->statements}) { |
|
2
|
|
|
|
|
9
|
|
42
|
4
|
|
100
|
|
|
36
|
$struct_hr->{'claims'}->{$statement->snak->property} //= []; |
43
|
4
|
|
|
|
|
72
|
push @{$struct_hr->{'claims'}->{$statement->snak->property}}, |
|
4
|
|
|
|
|
15
|
|
44
|
|
|
|
|
|
|
Wikibase::Datatype::Struct::Statement::obj2struct($statement, $base_uri); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
|
|
63
|
return $struct_hr; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub struct2obj { |
51
|
2
|
|
|
2
|
1
|
133
|
my $struct_hr = shift; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Glosses. |
54
|
2
|
|
|
|
|
6
|
my $glosses_ar; |
55
|
2
|
|
|
|
|
6
|
foreach my $lang (keys %{$struct_hr->{'glosses'}}) { |
|
2
|
|
|
|
|
13
|
|
56
|
4
|
|
|
|
|
8
|
push @{$glosses_ar}, Wikibase::Datatype::Struct::Language::struct2obj($struct_hr->{'glosses'}->{$lang}); |
|
4
|
|
|
|
|
17
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Statements. |
60
|
2
|
|
|
|
|
13
|
my $statements_ar = []; |
61
|
2
|
|
|
|
|
14
|
foreach my $property (keys %{$struct_hr->{'claims'}}) { |
|
2
|
|
|
|
|
26
|
|
62
|
3
|
|
|
|
|
8
|
foreach my $claim_hr (@{$struct_hr->{'claims'}->{$property}}) { |
|
3
|
|
|
|
|
10
|
|
63
|
3
|
|
|
|
|
6
|
push @{$statements_ar}, Wikibase::Datatype::Struct::Statement::struct2obj( |
|
3
|
|
|
|
|
13
|
|
64
|
|
|
|
|
|
|
$claim_hr, |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $obj = Wikibase::Datatype::Sense->new( |
70
|
|
|
|
|
|
|
'glosses' => $glosses_ar, |
71
|
2
|
|
|
|
|
74
|
'id' => $struct_hr->{'id'}, |
72
|
|
|
|
|
|
|
'statements' => $statements_ar, |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
2
|
|
|
|
|
331
|
return $obj; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
__END__ |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=pod |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=encoding utf8 |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 NAME |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Wikibase::Datatype::Struct::Sense - Wikibase sense structure serialization. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 SYNOPSIS |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Sense qw(obj2struct struct2obj); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, $base_uri); |
95
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 DESCRIPTION |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
This conversion is between objects defined in Wikibase::Datatype and structures |
100
|
|
|
|
|
|
|
serialized via JSON to MediaWiki. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 SUBROUTINES |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 C<obj2struct> |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, $base_uri); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Convert Wikibase::Datatype::Sense instance to structure. |
109
|
|
|
|
|
|
|
C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/). |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Returns reference to hash with structure. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 C<struct2obj> |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Convert structure of sense to object. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Returns Wikibase::Datatype::Sense instance. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 ERRORS |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
obj2struct(): |
124
|
|
|
|
|
|
|
Base URI is required. |
125
|
|
|
|
|
|
|
Object doesn't exist. |
126
|
|
|
|
|
|
|
Object isn't 'Wikibase::Datatype::Sense'. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 EXAMPLE1 |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=for comment filename=obj2struct_sense.pl |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
use strict; |
133
|
|
|
|
|
|
|
use warnings; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
use Data::Printer; |
136
|
|
|
|
|
|
|
use Wikibase::Datatype::Sense; |
137
|
|
|
|
|
|
|
use Wikibase::Datatype::Snak; |
138
|
|
|
|
|
|
|
use Wikibase::Datatype::Statement; |
139
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Sense qw(obj2struct); |
140
|
|
|
|
|
|
|
use Wikibase::Datatype::Value::Item; |
141
|
|
|
|
|
|
|
use Wikibase::Datatype::Value::Monolingual; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Statement. |
144
|
|
|
|
|
|
|
my $statement = Wikibase::Datatype::Statement->new( |
145
|
|
|
|
|
|
|
# instance of (P31) human (Q5) |
146
|
|
|
|
|
|
|
'snak' => Wikibase::Datatype::Snak->new( |
147
|
|
|
|
|
|
|
'datatype' => 'wikibase-item', |
148
|
|
|
|
|
|
|
'datavalue' => Wikibase::Datatype::Value::Item->new( |
149
|
|
|
|
|
|
|
'value' => 'Q5', |
150
|
|
|
|
|
|
|
), |
151
|
|
|
|
|
|
|
'property' => 'P31', |
152
|
|
|
|
|
|
|
), |
153
|
|
|
|
|
|
|
); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# Object. |
156
|
|
|
|
|
|
|
my $obj = Wikibase::Datatype::Sense->new( |
157
|
|
|
|
|
|
|
'glosses' => [ |
158
|
|
|
|
|
|
|
Wikibase::Datatype::Value::Monolingual->new( |
159
|
|
|
|
|
|
|
'language' => 'en', |
160
|
|
|
|
|
|
|
'value' => 'Glosse en', |
161
|
|
|
|
|
|
|
), |
162
|
|
|
|
|
|
|
Wikibase::Datatype::Value::Monolingual->new( |
163
|
|
|
|
|
|
|
'language' => 'cs', |
164
|
|
|
|
|
|
|
'value' => 'Glosse cs', |
165
|
|
|
|
|
|
|
), |
166
|
|
|
|
|
|
|
], |
167
|
|
|
|
|
|
|
'id' => 'ID', |
168
|
|
|
|
|
|
|
'statements' => [ |
169
|
|
|
|
|
|
|
$statement, |
170
|
|
|
|
|
|
|
], |
171
|
|
|
|
|
|
|
); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# Get structure. |
174
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/'); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Dump to output. |
177
|
|
|
|
|
|
|
p $struct_hr; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# Output: |
180
|
|
|
|
|
|
|
# \ { |
181
|
|
|
|
|
|
|
# glosses { |
182
|
|
|
|
|
|
|
# cs { |
183
|
|
|
|
|
|
|
# language "cs", |
184
|
|
|
|
|
|
|
# value "Glosse cs" |
185
|
|
|
|
|
|
|
# }, |
186
|
|
|
|
|
|
|
# en { |
187
|
|
|
|
|
|
|
# language "en", |
188
|
|
|
|
|
|
|
# value "Glosse en" |
189
|
|
|
|
|
|
|
# } |
190
|
|
|
|
|
|
|
# }, |
191
|
|
|
|
|
|
|
# id "ID", |
192
|
|
|
|
|
|
|
# claims { |
193
|
|
|
|
|
|
|
# P31 [ |
194
|
|
|
|
|
|
|
# [0] { |
195
|
|
|
|
|
|
|
# mainsnak { |
196
|
|
|
|
|
|
|
# datatype "wikibase-item", |
197
|
|
|
|
|
|
|
# datavalue { |
198
|
|
|
|
|
|
|
# type "wikibase-entityid", |
199
|
|
|
|
|
|
|
# value { |
200
|
|
|
|
|
|
|
# entity-type "item", |
201
|
|
|
|
|
|
|
# id "Q5", |
202
|
|
|
|
|
|
|
# numeric-id 5 |
203
|
|
|
|
|
|
|
# } |
204
|
|
|
|
|
|
|
# }, |
205
|
|
|
|
|
|
|
# property "P31", |
206
|
|
|
|
|
|
|
# snaktype "value" |
207
|
|
|
|
|
|
|
# }, |
208
|
|
|
|
|
|
|
# rank "normal", |
209
|
|
|
|
|
|
|
# type "statement" |
210
|
|
|
|
|
|
|
# } |
211
|
|
|
|
|
|
|
# ] |
212
|
|
|
|
|
|
|
# } |
213
|
|
|
|
|
|
|
# } |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 EXAMPLE2 |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=for comment filename=struct2obj_sense.pl |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
use strict; |
220
|
|
|
|
|
|
|
use warnings; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
use Data::Printer; |
223
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Sense qw(struct2obj); |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
# Item structure. |
226
|
|
|
|
|
|
|
my $struct_hr = { |
227
|
|
|
|
|
|
|
'glosses' => { |
228
|
|
|
|
|
|
|
'cs' => { |
229
|
|
|
|
|
|
|
'language' => 'cs', |
230
|
|
|
|
|
|
|
'value' => 'Glosse cs', |
231
|
|
|
|
|
|
|
}, |
232
|
|
|
|
|
|
|
'en' => { |
233
|
|
|
|
|
|
|
'language' => 'en', |
234
|
|
|
|
|
|
|
'value' => 'Glosse en', |
235
|
|
|
|
|
|
|
}, |
236
|
|
|
|
|
|
|
}, |
237
|
|
|
|
|
|
|
'id' => 'ID', |
238
|
|
|
|
|
|
|
'claims' => { |
239
|
|
|
|
|
|
|
'P31' => [{ |
240
|
|
|
|
|
|
|
'mainsnak' => { |
241
|
|
|
|
|
|
|
'datatype' => 'wikibase-item', |
242
|
|
|
|
|
|
|
'datavalue' => { |
243
|
|
|
|
|
|
|
'type' => 'wikibase-entityid', |
244
|
|
|
|
|
|
|
'value' => { |
245
|
|
|
|
|
|
|
'entity-type' => 'item', |
246
|
|
|
|
|
|
|
'id' => 'Q5', |
247
|
|
|
|
|
|
|
'numeric-id' => 5, |
248
|
|
|
|
|
|
|
}, |
249
|
|
|
|
|
|
|
}, |
250
|
|
|
|
|
|
|
'property' => 'P31', |
251
|
|
|
|
|
|
|
'snaktype' => 'value', |
252
|
|
|
|
|
|
|
}, |
253
|
|
|
|
|
|
|
'rank' => 'normal', |
254
|
|
|
|
|
|
|
'type' => 'statement', |
255
|
|
|
|
|
|
|
}], |
256
|
|
|
|
|
|
|
}, |
257
|
|
|
|
|
|
|
}; |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
# Get object. |
260
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
# Dump object. |
263
|
|
|
|
|
|
|
p $obj; |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
# Output: |
266
|
|
|
|
|
|
|
# Wikibase::Datatype::Sense { |
267
|
|
|
|
|
|
|
# Parents Mo::Object |
268
|
|
|
|
|
|
|
# public methods (7) : BUILD, can (UNIVERSAL), DOES (UNIVERSAL), check_array_object (Mo::utils), check_number_of_items (Mo::utils), isa (UNIVERSAL), VERSION (UNIVERSAL) |
269
|
|
|
|
|
|
|
# private methods (1) : __ANON__ (Mo) |
270
|
|
|
|
|
|
|
# internals: { |
271
|
|
|
|
|
|
|
# glosses [ |
272
|
|
|
|
|
|
|
# [0] Wikibase::Datatype::Value::Monolingual, |
273
|
|
|
|
|
|
|
# [1] Wikibase::Datatype::Value::Monolingual |
274
|
|
|
|
|
|
|
# ], |
275
|
|
|
|
|
|
|
# id "ID", |
276
|
|
|
|
|
|
|
# statements [ |
277
|
|
|
|
|
|
|
# [0] Wikibase::Datatype::Statement |
278
|
|
|
|
|
|
|
# ] |
279
|
|
|
|
|
|
|
# } |
280
|
|
|
|
|
|
|
# } |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
L<Error::Pure>, |
285
|
|
|
|
|
|
|
L<Exporter>, |
286
|
|
|
|
|
|
|
L<Readonly>, |
287
|
|
|
|
|
|
|
L<Wikibase::Datatype::Sense>, |
288
|
|
|
|
|
|
|
L<Wikibase::Datatype::Struct::Language>, |
289
|
|
|
|
|
|
|
L<Wikibase::Datatype::Struct::Statement>. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head1 SEE ALSO |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=over |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Struct> |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
Wikibase structure serialization. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Sense> |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
Wikibase sense datatype. |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=back |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head1 REPOSITORY |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct> |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=head1 AUTHOR |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
L<http://skim.cz> |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
© 2020-2023 Michal Josef Špaček |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
BSD 2-Clause License |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=head1 VERSION |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
0.11 |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
=cut |