line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Wikibase::Datatype::Struct::Value::Item; |
2
|
|
|
|
|
|
|
|
3
|
46
|
|
|
46
|
|
370180
|
use base qw(Exporter); |
|
46
|
|
|
|
|
150
|
|
|
46
|
|
|
|
|
5389
|
|
4
|
46
|
|
|
46
|
|
314
|
use strict; |
|
46
|
|
|
|
|
109
|
|
|
46
|
|
|
|
|
1104
|
|
5
|
46
|
|
|
46
|
|
248
|
use warnings; |
|
46
|
|
|
|
|
135
|
|
|
46
|
|
|
|
|
1678
|
|
6
|
|
|
|
|
|
|
|
7
|
46
|
|
|
46
|
|
1848
|
use Error::Pure qw(err); |
|
46
|
|
|
|
|
27364
|
|
|
46
|
|
|
|
|
2097
|
|
8
|
46
|
|
|
46
|
|
504
|
use Readonly; |
|
46
|
|
|
|
|
122
|
|
|
46
|
|
|
|
|
2044
|
|
9
|
46
|
|
|
46
|
|
21209
|
use Wikibase::Datatype::Value::Item; |
|
46
|
|
|
|
|
40577
|
|
|
46
|
|
|
|
|
14590
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = 0.08; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub obj2struct { |
16
|
18
|
|
|
18
|
1
|
19912
|
my $obj = shift; |
17
|
|
|
|
|
|
|
|
18
|
18
|
100
|
|
|
|
57
|
if (! defined $obj) { |
19
|
1
|
|
|
|
|
5
|
err "Object doesn't exist."; |
20
|
|
|
|
|
|
|
} |
21
|
17
|
100
|
|
|
|
78
|
if (! $obj->isa('Wikibase::Datatype::Value::Item')) { |
22
|
1
|
|
|
|
|
5
|
err "Object isn't 'Wikibase::Datatype::Value::Item'."; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
16
|
|
|
|
|
72
|
my $numeric_id = $obj->value; |
26
|
16
|
|
|
|
|
217
|
$numeric_id =~ s/^Q//ms; |
27
|
16
|
|
|
|
|
68
|
my $struct_hr = { |
28
|
|
|
|
|
|
|
'value' => { |
29
|
|
|
|
|
|
|
'entity-type' => $obj->type, |
30
|
|
|
|
|
|
|
'id' => $obj->value, |
31
|
|
|
|
|
|
|
'numeric-id' => $numeric_id, |
32
|
|
|
|
|
|
|
}, |
33
|
|
|
|
|
|
|
'type' => 'wikibase-entityid', |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
|
36
|
16
|
|
|
|
|
197
|
return $struct_hr; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub struct2obj { |
40
|
18
|
|
|
18
|
1
|
25927
|
my $struct_hr = shift; |
41
|
|
|
|
|
|
|
|
42
|
18
|
100
|
100
|
|
|
266
|
if (! exists $struct_hr->{'type'} |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
43
|
|
|
|
|
|
|
|| ! defined $struct_hr->{'type'} |
44
|
|
|
|
|
|
|
|| $struct_hr->{'type'} ne 'wikibase-entityid' |
45
|
|
|
|
|
|
|
|| ! exists $struct_hr->{'value'} |
46
|
|
|
|
|
|
|
|| ! exists $struct_hr->{'value'}->{'entity-type'} |
47
|
|
|
|
|
|
|
|| ! defined $struct_hr->{'value'}->{'entity-type'} |
48
|
|
|
|
|
|
|
|| $struct_hr->{'value'}->{'entity-type'} ne 'item') { |
49
|
|
|
|
|
|
|
|
50
|
7
|
|
|
|
|
22
|
err "Structure isn't for 'item' datatype."; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $obj = Wikibase::Datatype::Value::Item->new( |
54
|
11
|
|
|
|
|
89
|
'value' => $struct_hr->{'value'}->{'id'}, |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
11
|
|
|
|
|
1054
|
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::Value::Item - Wikibase item structure serialization. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Value::Item qw(obj2struct struct2obj); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj); |
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); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Convert Wikibase::Datatype::Value::Item instance to structure. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Returns reference to hash with structure. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 C<struct2obj> |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Convert structure of item to object. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Returns Wikibase::Datatype::Value::Item instance. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 ERRORS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
obj2struct(): |
105
|
|
|
|
|
|
|
Object doesn't exist. |
106
|
|
|
|
|
|
|
Object isn't 'Wikibase::Datatype::Value::Item'. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
struct2obj(): |
109
|
|
|
|
|
|
|
Structure isn't for 'item' datatype. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 EXAMPLE1 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
use strict; |
114
|
|
|
|
|
|
|
use warnings; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
use Data::Printer; |
117
|
|
|
|
|
|
|
use Wikibase::Datatype::Value::Item; |
118
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Value::Item qw(obj2struct); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Object. |
121
|
|
|
|
|
|
|
my $obj = Wikibase::Datatype::Value::Item->new( |
122
|
|
|
|
|
|
|
'value' => 'Q123', |
123
|
|
|
|
|
|
|
); |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Get structure. |
126
|
|
|
|
|
|
|
my $struct_hr = obj2struct($obj); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Dump to output. |
129
|
|
|
|
|
|
|
p $struct_hr; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Output: |
132
|
|
|
|
|
|
|
# \ { |
133
|
|
|
|
|
|
|
# type "wikibase-entityid", |
134
|
|
|
|
|
|
|
# value { |
135
|
|
|
|
|
|
|
# entity-type "item", |
136
|
|
|
|
|
|
|
# id "Q123", |
137
|
|
|
|
|
|
|
# numeric-id 123 |
138
|
|
|
|
|
|
|
# } |
139
|
|
|
|
|
|
|
# } |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 EXAMPLE2 |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
use strict; |
144
|
|
|
|
|
|
|
use warnings; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
use Wikibase::Datatype::Struct::Value::Item qw(struct2obj); |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# Item structure. |
149
|
|
|
|
|
|
|
my $struct_hr = { |
150
|
|
|
|
|
|
|
'type' => 'wikibase-entityid', |
151
|
|
|
|
|
|
|
'value' => { |
152
|
|
|
|
|
|
|
'entity-type' => 'item', |
153
|
|
|
|
|
|
|
'id' => 'Q123', |
154
|
|
|
|
|
|
|
'numberic-id' => 123, |
155
|
|
|
|
|
|
|
}, |
156
|
|
|
|
|
|
|
}; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Get object. |
159
|
|
|
|
|
|
|
my $obj = struct2obj($struct_hr); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# Get value. |
162
|
|
|
|
|
|
|
my $value = $obj->value; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# Get type. |
165
|
|
|
|
|
|
|
my $type = $obj->type; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# Print out. |
168
|
|
|
|
|
|
|
print "Type: $type\n"; |
169
|
|
|
|
|
|
|
print "Value: $value\n"; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# Output: |
172
|
|
|
|
|
|
|
# Type: item |
173
|
|
|
|
|
|
|
# Value: Q123 |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
L<Error::Pure>, |
178
|
|
|
|
|
|
|
L<Exporter>, |
179
|
|
|
|
|
|
|
L<Readonly>, |
180
|
|
|
|
|
|
|
L<Wikibase::Datatype::Value::Item>. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 SEE ALSO |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=over |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Struct> |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Wikibase structure serialization. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item L<Wikibase::Datatype::Value::Item> |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Wikibase item value datatype. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=back |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 REPOSITORY |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct> |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 AUTHOR |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
L<http://skim.cz> |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
© Michal Josef Špaček 2020-2021 |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
BSD 2-Clause License |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head1 VERSION |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
0.08 |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |