line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Wikibase::Datatype::Struct::Snak; |
2
|
|
|
|
|
|
|
|
3
|
32
|
|
|
32
|
|
1046700
|
use base qw(Exporter); |
|
32
|
|
|
|
|
109
|
|
|
32
|
|
|
|
|
3103
|
|
4
|
32
|
|
|
32
|
|
220
|
use strict; |
|
32
|
|
|
|
|
103
|
|
|
32
|
|
|
|
|
694
|
|
5
|
32
|
|
|
32
|
|
180
|
use warnings; |
|
32
|
|
|
|
|
74
|
|
|
32
|
|
|
|
|
928
|
|
6
|
|
|
|
|
|
|
|
7
|
32
|
|
|
32
|
|
1538
|
use Error::Pure qw(err); |
|
32
|
|
|
|
|
24219
|
|
|
32
|
|
|
|
|
1360
|
|
8
|
32
|
|
|
32
|
|
382
|
use Readonly; |
|
32
|
|
|
|
|
71
|
|
|
32
|
|
|
|
|
1413
|
|
9
|
32
|
|
|
32
|
|
11407
|
use Wikibase::Datatype::Snak; |
|
32
|
|
|
|
|
4434532
|
|
|
32
|
|
|
|
|
1090
|
|
10
|
32
|
|
|
32
|
|
14874
|
use Wikibase::Datatype::Struct::Value; |
|
32
|
|
|
|
|
97
|
|
|
32
|
|
|
|
|
9104
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = 0.11; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub obj2struct { |
17
|
43
|
|
|
43
|
1
|
7268
|
my ($obj, $base_uri) = @_; |
18
|
|
|
|
|
|
|
|
19
|
43
|
100
|
|
|
|
117
|
if (! defined $obj) { |
20
|
1
|
|
|
|
|
4
|
err "Object doesn't exist."; |
21
|
|
|
|
|
|
|
} |
22
|
42
|
100
|
|
|
|
167
|
if (! $obj->isa('Wikibase::Datatype::Snak')) { |
23
|
1
|
|
|
|
|
6
|
err "Object isn't 'Wikibase::Datatype::Snak'."; |
24
|
|
|
|
|
|
|
} |
25
|
41
|
100
|
|
|
|
99
|
if (! defined $base_uri) { |
26
|
1
|
|
|
|
|
3
|
err 'Base URI is required.'; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
40
|
100
|
|
|
|
118
|
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
|
40
|
|
|
|
|
894
|
return $struct_hr; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub struct2obj { |
42
|
35
|
|
|
35
|
1
|
7721
|
my $struct_hr = shift; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Data value isn't required for snaktype 'novalue'. |
45
|
35
|
|
|
|
|
67
|
my $datavalue; |
46
|
35
|
100
|
|
|
|
113
|
if (exists $struct_hr->{'datavalue'}) { |
47
|
32
|
|
|
|
|
120
|
$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
|
35
|
|
|
|
|
205
|
'snaktype' => $struct_hr->{'snaktype'}, |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
32
|
|
|
|
|
7489
|
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
|
|
|
|
|
|
|
=for comment filename=obj2struct_snak.pl |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
use strict; |
115
|
|
|
|
|
|
|
use warnings; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
use Data::Printer; |
118
|
|
|
|
|
|
|
use Wikibase::Datatype::Snak; |
119
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Snak qw(obj2struct); |
120
|
|
|
|
|
|
|
use Wikibase::Datatype::Value::Item; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Object. |
123
|
|
|
|
|
|
|
# instance of (P31) human (Q5) |
124
|
|
|
|
|
|
|
my $obj = Wikibase::Datatype::Snak->new( |
125
|
|
|
|
|
|
|
'datatype' => 'wikibase-item', |
126
|
|
|
|
|
|
|
'datavalue' => Wikibase::Datatype::Value::Item->new( |
127
|
|
|
|
|
|
|
'value' => 'Q5', |
128
|
|
|
|
|
|
|
), |
129
|
|
|
|
|
|
|
'property' => 'P31', |
130
|
|
|
|
|
|
|
); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# Get structure. |
133
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/'); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# Dump to output. |
136
|
|
|
|
|
|
|
p $struct_hr; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# Output: |
139
|
|
|
|
|
|
|
# \ { |
140
|
|
|
|
|
|
|
# datatype "wikibase-item", |
141
|
|
|
|
|
|
|
# datavalue { |
142
|
|
|
|
|
|
|
# type "wikibase-entityid", |
143
|
|
|
|
|
|
|
# value { |
144
|
|
|
|
|
|
|
# entity-type "item", |
145
|
|
|
|
|
|
|
# id "Q5", |
146
|
|
|
|
|
|
|
# numeric-id 5 |
147
|
|
|
|
|
|
|
# } |
148
|
|
|
|
|
|
|
# }, |
149
|
|
|
|
|
|
|
# property "P31", |
150
|
|
|
|
|
|
|
# snaktype "value" |
151
|
|
|
|
|
|
|
# } |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 EXAMPLE2 |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=for comment filename=struct2obj_snak.pl |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
use strict; |
158
|
|
|
|
|
|
|
use warnings; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Snak qw(struct2obj); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# Item structure. |
163
|
|
|
|
|
|
|
my $struct_hr = { |
164
|
|
|
|
|
|
|
'datatype' => 'wikibase-item', |
165
|
|
|
|
|
|
|
'datavalue' => { |
166
|
|
|
|
|
|
|
'type' => 'wikibase-entityid', |
167
|
|
|
|
|
|
|
'value' => { |
168
|
|
|
|
|
|
|
'entity-type' => 'item', |
169
|
|
|
|
|
|
|
'id' => 'Q5', |
170
|
|
|
|
|
|
|
'numeric-id' => 5, |
171
|
|
|
|
|
|
|
}, |
172
|
|
|
|
|
|
|
}, |
173
|
|
|
|
|
|
|
'property' => 'P31', |
174
|
|
|
|
|
|
|
'snaktype' => 'value', |
175
|
|
|
|
|
|
|
}; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# Get object. |
178
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# Get value. |
181
|
|
|
|
|
|
|
my $datavalue = $obj->datavalue->value; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# Get datatype. |
184
|
|
|
|
|
|
|
my $datatype = $obj->datatype; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# Get property. |
187
|
|
|
|
|
|
|
my $property = $obj->property; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# Print out. |
190
|
|
|
|
|
|
|
print "Property: $property\n"; |
191
|
|
|
|
|
|
|
print "Type: $datatype\n"; |
192
|
|
|
|
|
|
|
print "Value: $datavalue\n"; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# Output: |
195
|
|
|
|
|
|
|
# Property: P31 |
196
|
|
|
|
|
|
|
# Type: wikibase-item |
197
|
|
|
|
|
|
|
# Value: Q5 |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
L<Error::Pure>, |
202
|
|
|
|
|
|
|
L<Exporter>, |
203
|
|
|
|
|
|
|
L<Readonly>, |
204
|
|
|
|
|
|
|
L<Wikibase::Datatype::Snak>, |
205
|
|
|
|
|
|
|
L<Wikibase::Datatype::Struct::Value>. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 SEE ALSO |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=over |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Struct> |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Wikibase structure serialization. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Snak> |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Wikibase snak datatype. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=back |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 REPOSITORY |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 AUTHOR |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
L<http://skim.cz> |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
© 2020-2023 Michal Josef Špaček |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
BSD 2-Clause License |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head1 VERSION |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
0.11 |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=cut |