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