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