File Coverage

blib/lib/Wikibase/Datatype/Struct/Statement.pm
Criterion Covered Total %
statement 45 46 97.8
branch 13 14 92.8
condition n/a
subroutine 11 11 100.0
pod 2 2 100.0
total 71 73 97.2


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Statement;
2              
3 24     24   1037682 use base qw(Exporter);
  24         101  
  24         2402  
4 24     24   187 use strict;
  24         79  
  24         492  
5 24     24   120 use warnings;
  24         64  
  24         719  
6              
7 24     24   1494 use Error::Pure qw(err);
  24         34336  
  24         838  
8 24     24   300 use Readonly;
  24         73  
  24         851  
9 24     24   8655 use Wikibase::Datatype::Statement;
  24         92047  
  24         942  
10 24     24   11268 use Wikibase::Datatype::Struct::Reference;
  24         72  
  24         1185  
11 24     24   10841 use Wikibase::Datatype::Struct::Snak;
  24         79  
  24         1218  
12 24     24   253 use Wikibase::Datatype::Struct::Utils qw(obj_array_ref2struct struct2snaks_array_ref);
  24         63  
  24         9560  
13              
14             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
15              
16             our $VERSION = 0.11;
17              
18             sub obj2struct {
19 20     20 1 16382 my ($obj, $base_uri) = @_;
20              
21 20 100       64 if (! defined $obj) {
22 1         4 err "Object doesn't exist.";
23             }
24 19 100       85 if (! $obj->isa('Wikibase::Datatype::Statement')) {
25 1         6 err "Object isn't 'Wikibase::Datatype::Statement'.";
26             }
27 18 50       45 if (! defined $base_uri) {
28 0         0 err 'Base URI is required.';
29             }
30              
31             my $struct_hr = {
32             defined $obj->id ? ('id' => $obj->id) : (),
33             'mainsnak' => Wikibase::Datatype::Struct::Snak::obj2struct($obj->snak, $base_uri),
34 18         74 @{$obj->property_snaks} ? (
35 2         26 %{obj_array_ref2struct($obj->property_snaks, 'qualifiers', $base_uri)},
36             ) : (),
37             'rank' => $obj->rank,
38 18         415 @{$obj->references} ? (
39             'references' => [
40 4         45 map { Wikibase::Datatype::Struct::Reference::obj2struct($_, $base_uri); }
41 18 100       68 @{$obj->references},
  4 100       68  
    100          
42             ],
43             ) : (),
44             'type' => 'statement',
45             };
46              
47 18         209 return $struct_hr;
48             }
49              
50             sub struct2obj {
51 10     10 1 211 my $struct_hr = shift;
52              
53             my $obj = Wikibase::Datatype::Statement->new(
54             exists $struct_hr->{'id'} ? ('id' => $struct_hr->{'id'}) : (),
55             'property_snaks' => struct2snaks_array_ref($struct_hr, 'qualifiers'),
56             'snak' => Wikibase::Datatype::Struct::Snak::struct2obj($struct_hr->{'mainsnak'}),
57             'references' => [
58 4         26 map { Wikibase::Datatype::Struct::Reference::struct2obj($_) }
59 10         76 @{$struct_hr->{'references'}}
60             ],
61 10 100       111 'rank' => $struct_hr->{'rank'},
62             );
63              
64 10         1446 return $obj;
65             }
66              
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding utf8
74              
75             =head1 NAME
76              
77             Wikibase::Datatype::Struct::Statement - Wikibase statement structure serialization.
78              
79             =head1 SYNOPSIS
80              
81             use Wikibase::Datatype::Struct::Statement qw(obj2struct struct2obj);
82              
83             my $struct_hr = obj2struct($obj, $base_uri);
84             my $obj = struct2obj($struct_hr);
85              
86             =head1 DESCRIPTION
87              
88             This conversion is between objects defined in Wikibase::Datatype and structures
89             serialized via JSON to MediaWiki.
90              
91             =head1 SUBROUTINES
92              
93             =head2 C<obj2struct>
94              
95             my $struct_hr = obj2struct($obj, $base_uri);
96              
97             Convert Wikibase::Datatype::Statement instance to structure.
98             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
99              
100             Returns reference to hash with structure.
101              
102             =head2 C<struct2obj>
103              
104             my $obj = struct2obj($struct_hr);
105              
106             Convert structure of statement to object.
107              
108             Returns Wikibase::Datatype::Statement instance.
109              
110             =head1 ERRORS
111              
112             obj2struct():
113             Base URI is required.
114             Object doesn't exist.
115             Object isn't 'Wikibase::Datatype::Statement'.
116              
117             =head1 EXAMPLE1
118              
119             =for comment filename=obj2struct_statement.pl
120              
121             use strict;
122             use warnings;
123              
124             use Data::Printer;
125             use Wikibase::Datatype::Reference;
126             use Wikibase::Datatype::Snak;
127             use Wikibase::Datatype::Statement;
128             use Wikibase::Datatype::Struct::Statement qw(obj2struct);
129             use Wikibase::Datatype::Value::Item;
130             use Wikibase::Datatype::Value::String;
131             use Wikibase::Datatype::Value::Time;
132              
133             # Object.
134             my $obj = Wikibase::Datatype::Statement->new(
135             'id' => 'Q123$00C04D2A-49AF-40C2-9930-C551916887E8',
136              
137             # instance of (P31) human (Q5)
138             'snak' => Wikibase::Datatype::Snak->new(
139             'datatype' => 'wikibase-item',
140             'datavalue' => Wikibase::Datatype::Value::Item->new(
141             'value' => 'Q5',
142             ),
143             'property' => 'P31',
144             ),
145             'property_snaks' => [
146             # of (P642) alien (Q474741)
147             Wikibase::Datatype::Snak->new(
148             'datatype' => 'wikibase-item',
149             'datavalue' => Wikibase::Datatype::Value::Item->new(
150             'value' => 'Q474741',
151             ),
152             'property' => 'P642',
153             ),
154             ],
155             'references' => [
156             Wikibase::Datatype::Reference->new(
157             'snaks' => [
158             # stated in (P248) Virtual International Authority File (Q53919)
159             Wikibase::Datatype::Snak->new(
160             'datatype' => 'wikibase-item',
161             'datavalue' => Wikibase::Datatype::Value::Item->new(
162             'value' => 'Q53919',
163             ),
164             'property' => 'P248',
165             ),
166              
167             # VIAF ID (P214) 113230702
168             Wikibase::Datatype::Snak->new(
169             'datatype' => 'external-id',
170             'datavalue' => Wikibase::Datatype::Value::String->new(
171             'value' => '113230702',
172             ),
173             'property' => 'P214',
174             ),
175              
176             # retrieved (P813) 7 December 2013
177             Wikibase::Datatype::Snak->new(
178             'datatype' => 'time',
179             'datavalue' => Wikibase::Datatype::Value::Time->new(
180             'value' => '+2013-12-07T00:00:00Z',
181             ),
182             'property' => 'P813',
183             ),
184             ],
185             ),
186             ],
187             );
188              
189             # Get structure.
190             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
191              
192             # Dump to output.
193             p $struct_hr;
194              
195             # Output:
196             # \ {
197             # id "Q123$00C04D2A-49AF-40C2-9930-C551916887E8",
198             # mainsnak {
199             # datatype "wikibase-item",
200             # datavalue {
201             # type "wikibase-entityid",
202             # value {
203             # entity-type "item",
204             # id "Q5",
205             # numeric-id 5
206             # }
207             # },
208             # property "P31",
209             # snaktype "value"
210             # },
211             # qualifiers {
212             # P642 [
213             # [0] {
214             # datatype "wikibase-item",
215             # datavalue {
216             # type "wikibase-entityid",
217             # value {
218             # entity-type "item",
219             # id "Q474741",
220             # numeric-id 474741
221             # }
222             # },
223             # property "P642",
224             # snaktype "value"
225             # }
226             # ]
227             # },
228             # qualifiers-order [
229             # [0] "P642"
230             # ],
231             # rank "normal",
232             # references [
233             # [0] {
234             # snaks {
235             # P214 [
236             # [0] {
237             # datatype "external-id",
238             # datavalue {
239             # type "string",
240             # value 113230702
241             # },
242             # property "P214",
243             # snaktype "value"
244             # }
245             # ],
246             # P248 [
247             # [0] {
248             # datatype "wikibase-item",
249             # datavalue {
250             # type "wikibase-entityid",
251             # value {
252             # entity-type "item",
253             # id "Q53919",
254             # numeric-id 53919
255             # }
256             # },
257             # property "P248",
258             # snaktype "value"
259             # }
260             # ],
261             # P813 [
262             # [0] {
263             # datatype "time",
264             # datavalue {
265             # type "time",
266             # value {
267             # after 0,
268             # before 0,
269             # calendarmodel "http://test.wikidata.org/entity/Q1985727",
270             # precision 11,
271             # time "+2013-12-07T00:00:00Z",
272             # timezone 0
273             # }
274             # },
275             # property "P813",
276             # snaktype "value"
277             # }
278             # ]
279             # },
280             # snaks-order [
281             # [0] "P248",
282             # [1] "P214",
283             # [2] "P813"
284             # ]
285             # }
286             # ],
287             # type "statement"
288             # }
289              
290             =head1 EXAMPLE2
291              
292             =for comment filename=struct2obj_statement.pl
293              
294             use strict;
295             use warnings;
296              
297             use Wikibase::Datatype::Struct::Statement qw(struct2obj);
298              
299             # Item structure.
300             my $struct_hr = {
301             'id' => 'Q123$00C04D2A-49AF-40C2-9930-C551916887E8',
302             'mainsnak' => {
303             'datatype' => 'wikibase-item',
304             'datavalue' => {
305             'type' => 'wikibase-entityid',
306             'value' => {
307             'entity-type' => 'item',
308             'id' => 'Q5',
309             'numeric-id' => 5,
310             },
311             },
312             'property' => 'P31',
313             'snaktype' => 'value',
314             },
315             'qualifiers' => {
316             'P642' => [{
317             'datatype' => 'wikibase-item',
318             'datavalue' => {
319             'type' => 'wikibase-entityid',
320             'value' => {
321             'entity-type' => 'item',
322             'id' => 'Q474741',
323             'numeric-id' => 474741,
324             },
325             },
326             'property' => 'P642',
327             'snaktype' => 'value',
328             }],
329             },
330             'qualifiers-order' => [
331             'P642',
332             ],
333             'rank' => 'normal',
334             'references' => [{
335             'snaks' => {
336             'P214' => [{
337             'datatype' => 'external-id',
338             'datavalue' => {
339             'type' => 'string',
340             'value' => '113230702',
341             },
342             'property' => 'P214',
343             'snaktype' => 'value',
344             }],
345             'P248' => [{
346             'datatype' => 'wikibase-item',
347             'datavalue' => {
348             'type' => 'wikibase-entityid',
349             'value' => {
350             'entity-type' => 'item',
351             'id' => 'Q53919',
352             'numeric-id' => 53919,
353             },
354             },
355             'property' => 'P248',
356             'snaktype' => 'value',
357             }],
358             'P813' => [{
359             'datatype' => 'time',
360             'datavalue' => {
361             'type' => 'time',
362             'value' => {
363             'after' => 0,
364             'before' => 0,
365             'calendarmodel' => 'http://test.wikidata.org/entity/Q1985727',
366             'precision' => 11,
367             'time' => '+2013-12-07T00:00:00Z',
368             'timezone' => 0,
369             },
370             },
371             'property' => 'P813',
372             'snaktype' => 'value',
373             }],
374             },
375             'snaks-order' => [
376             'P248',
377             'P214',
378             'P813',
379             ],
380             }],
381             'type' => 'statement',
382             };
383              
384             # Get object.
385             my $obj = struct2obj($struct_hr);
386              
387             # Print out.
388             print 'Id: '.$obj->id."\n";
389             print 'Claim: '.$obj->snak->property.' -> '.$obj->snak->datavalue->value."\n";
390             print "Qualifiers:\n";
391             foreach my $property_snak (@{$obj->property_snaks}) {
392             print "\t".$property_snak->property.' -> '.
393             $property_snak->datavalue->value."\n";
394             }
395             print "References:\n";
396             foreach my $reference (@{$obj->references}) {
397             print "\tReference:\n";
398             foreach my $reference_snak (@{$reference->snaks}) {
399             print "\t\t".$reference_snak->property.' -> '.
400             $reference_snak->datavalue->value."\n";
401             }
402             }
403             print 'Rank: '.$obj->rank."\n";
404              
405             # Output:
406             # Id: Q123$00C04D2A-49AF-40C2-9930-C551916887E8
407             # Claim: P31 -> Q5
408             # Qualifiers:
409             # P642 -> Q474741
410             # References:
411             # Reference:
412             # P248 -> Q53919
413             # P214 -> 113230702
414             # P813 -> +2013-12-07T00:00:00Z
415             # Rank: normal
416              
417             =head1 DEPENDENCIES
418              
419             L<Error::Pure>,
420             L<Exporter>,
421             L<Readonly>,
422             L<Wikibase::Datatype::Statement>,
423             L<Wikibase::Datatype::Struct::Reference>,
424             L<Wikibase::Datatype::Struct::Snak>,
425             L<Wikibase::Datatype::Struct::Utils>.
426              
427             =head1 SEE ALSO
428              
429             =over
430              
431             =item L<Wikibase::Datatype::Struct>
432              
433             Wikibase structure serialization.
434              
435             =item L<Wikibase::Datatype::Statement>
436              
437             Wikibase statement datatype.
438              
439             =back
440              
441             =head1 REPOSITORY
442              
443             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
444              
445             =head1 AUTHOR
446              
447             Michal Josef Špaček L<mailto:skim@cpan.org>
448              
449             L<http://skim.cz>
450              
451             =head1 LICENSE AND COPYRIGHT
452              
453             © 2020-2023 Michal Josef Špaček
454              
455             BSD 2-Clause License
456              
457             =head1 VERSION
458              
459             0.11
460              
461             =cut