File Coverage

blib/lib/Wikibase/Datatype/Struct/Mediainfo.pm
Criterion Covered Total %
statement 72 79 91.1
branch 32 32 100.0
condition 5 5 100.0
subroutine 10 10 100.0
pod 2 2 100.0
total 121 128 94.5


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Struct::Mediainfo;
2              
3 4     4   1044976 use base qw(Exporter);
  4         49  
  4         421  
4 4     4   33 use strict;
  4         10  
  4         76  
5 4     4   18 use warnings;
  4         8  
  4         110  
6              
7 4     4   1526 use Error::Pure qw(err);
  4         23675  
  4         92  
8 4     4   166 use Readonly;
  4         10  
  4         137  
9 4     4   1720 use Wikibase::Datatype::Mediainfo;
  4         17385  
  4         132  
10 4     4   1802 use Wikibase::Datatype::Struct::Language;
  4         24  
  4         201  
11 4     4   1907 use Wikibase::Datatype::Struct::MediainfoStatement;
  4         20  
  4         2880  
12              
13             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
14              
15             our $VERSION = 0.11;
16              
17             sub obj2struct {
18 7     7 1 19906 my ($obj, $base_uri) = @_;
19              
20 7 100       26 if (! defined $obj) {
21 1         4 err "Object doesn't exist.";
22             }
23 6 100       31 if (! $obj->isa('Wikibase::Datatype::Mediainfo')) {
24 1         9 err "Object isn't 'Wikibase::Datatype::Mediainfo'.";
25             }
26 5 100       16 if (! defined $base_uri) {
27 1         7 err 'Base URI is required.';
28             }
29              
30 4         13 my $struct_hr = {
31             'type' => 'mediainfo',
32             };
33              
34             # Claims.
35 4         9 foreach my $statement (@{$obj->statements}) {
  4         11  
36 4   100     31 $struct_hr->{'statements'}->{$statement->snak->property} //= [];
37 4         73 push @{$struct_hr->{'statements'}->{$statement->snak->property}},
  4         12  
38             Wikibase::Datatype::Struct::MediainfoStatement::obj2struct($statement, $base_uri);
39             }
40              
41             # Descriptions.
42 4         32 $struct_hr->{'descriptions'} = {};
43 4         9 foreach my $desc (@{$obj->descriptions}) {
  4         12  
44 0         0 $struct_hr->{'descriptions'}->{$desc->language}
45             = Wikibase::Datatype::Struct::Language::obj2struct($desc);
46             }
47              
48             # Id.
49 4 100       37 if (defined $obj->id) {
50 1         18 $struct_hr->{'id'} = $obj->id;
51             }
52              
53             # Labels.
54 4         27 foreach my $label (@{$obj->labels}) {
  4         11  
55 2         26 $struct_hr->{'labels'}->{$label->language}
56             = Wikibase::Datatype::Struct::Language::obj2struct($label);
57             }
58            
59             # Last revision id.
60 4 100       33 if (defined $obj->lastrevid) {
61 1         16 $struct_hr->{'lastrevid'} = $obj->lastrevid;
62             }
63              
64             # Modified date.
65 4 100       28 if (defined $obj->modified) {
66 1         12 $struct_hr->{'modified'} = $obj->modified;
67             }
68              
69             # Namespace.
70 4 100       28 if (defined $obj->ns) {
71 3         35 $struct_hr->{'ns'} = $obj->ns;
72             }
73              
74             # Page ID.
75 4 100       41 if (defined $obj->page_id) {
76 1         9 $struct_hr->{'pageid'} = $obj->page_id;
77             }
78              
79             # Title.
80 4 100       30 if (defined $obj->title) {
81 1         10 $struct_hr->{'title'} = $obj->title;
82             }
83              
84 4         46 return $struct_hr;
85             }
86              
87             sub struct2obj {
88 5     5 1 13768 my $struct_hr = shift;
89              
90 5 100 100     32 if (! exists $struct_hr->{'type'} || $struct_hr->{'type'} ne 'mediainfo') {
91 2         8 err "Structure isn't for 'mediainfo' type.";
92             }
93              
94             # Descriptions.
95 3         10 my $descriptions_ar = [];
96 3         4 foreach my $lang (keys %{$struct_hr->{'descriptions'}}) {
  3         13  
97 0         0 push @{$descriptions_ar}, Wikibase::Datatype::Struct::Language::struct2obj(
98 0         0 $struct_hr->{'descriptions'}->{$lang},
99             );
100             }
101              
102             # Labels.
103 3         7 my $labels_ar = [];
104 3         5 foreach my $lang (keys %{$struct_hr->{'labels'}}) {
  3         13  
105 2         11 push @{$labels_ar}, Wikibase::Datatype::Struct::Language::struct2obj(
106 2         6 $struct_hr->{'labels'}->{$lang},
107             );
108             }
109              
110             # Statements.
111 3         7 my $statements_ar = [];
112 3         6 foreach my $property (keys %{$struct_hr->{'statements'}}) {
  3         11  
113 0         0 foreach my $claim_hr (@{$struct_hr->{'statements'}->{$property}}) {
  0         0  
114 0         0 push @{$statements_ar}, Wikibase::Datatype::Struct::MediainfoStatement::struct2obj(
  0         0  
115             $claim_hr,
116             );
117             }
118             }
119              
120             my $obj = Wikibase::Datatype::Mediainfo->new(
121             'descriptions' => $descriptions_ar,
122             defined $struct_hr->{'id'} ? ('id' => $struct_hr->{'id'}) : (),
123             'labels' => $labels_ar,
124             defined $struct_hr->{'lastrevid'} ? ('lastrevid' => $struct_hr->{'lastrevid'}) : (),
125             defined $struct_hr->{'modified'} ? ('modified' => $struct_hr->{'modified'}) : (),
126             defined $struct_hr->{'ns'} ? ('ns' => $struct_hr->{'ns'}) : (),
127             defined $struct_hr->{'pageid'} ? ('page_id' => $struct_hr->{'pageid'}) : (),
128             'statements' => $statements_ar,
129 3 100       45 defined $struct_hr->{'title'} ? ('title' => $struct_hr->{'title'}) : (),
    100          
    100          
    100          
    100          
    100          
130             );
131              
132 3         526 return $obj;
133             }
134              
135             1;
136              
137             __END__
138              
139             =pod
140              
141             =encoding utf8
142              
143             =head1 NAME
144              
145             Wikibase::Datatype::Struct::Mediainfo - Wikibase mediainfo structure serialization.
146              
147             =head1 SYNOPSIS
148              
149             use Wikibase::Datatype::Struct::Mediainfo qw(obj2struct struct2obj);
150              
151             my $struct_hr = obj2struct($obj, $base_uri);
152             my $obj = struct2obj($struct_hr);
153              
154             =head1 DESCRIPTION
155              
156             This conversion is between objects defined in Wikibase::Datatype and structures
157             serialized via JSON to MediaWiki.
158              
159             =head1 SUBROUTINES
160              
161             =head2 C<obj2struct>
162              
163             my $struct_hr = obj2struct($obj, $base_uri);
164              
165             Convert Wikibase::Datatype::Mediainfo instance to structure.
166             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
167              
168             Returns reference to hash with structure.
169              
170             =head2 C<struct2obj>
171              
172             my $obj = struct2obj($struct_hr);
173              
174             Convert structure of mediainfo to object.
175              
176             Returns Wikibase::Datatype::Mediainfo instance.
177              
178             =head1 ERRORS
179              
180             obj2struct():
181             Base URI is required.
182             Object doesn't exist.
183             Object isn't 'Wikibase::Datatype::Mediainfo'.
184              
185             struct2obj():
186             Structure isn't for 'mediainfo' type.
187              
188             =head1 EXAMPLE1
189              
190             =for comment filename=obj2struct_mediainfo.pl
191              
192             use strict;
193             use warnings;
194              
195             use Data::Printer;
196             use Wikibase::Datatype::Mediainfo;
197             use Wikibase::Datatype::MediainfoSnak;
198             use Wikibase::Datatype::MediainfoStatement;
199             use Wikibase::Datatype::Struct::Mediainfo qw(obj2struct);
200             use Wikibase::Datatype::Value::Item;
201             use Wikibase::Datatype::Value::Monolingual;
202              
203             # Object.
204             my $statement1 = Wikibase::Datatype::MediainfoStatement->new(
205             # instance of (P31) human (Q5)
206             'snak' => Wikibase::Datatype::MediainfoSnak->new(
207             'datavalue' => Wikibase::Datatype::Value::Item->new(
208             'value' => 'Q5',
209             ),
210             'property' => 'P31',
211             ),
212             'property_snaks' => [
213             # of (P642) alien (Q474741)
214             Wikibase::Datatype::MediainfoSnak->new(
215             'datavalue' => Wikibase::Datatype::Value::Item->new(
216             'value' => 'Q474741',
217             ),
218             'property' => 'P642',
219             ),
220             ],
221             );
222             my $statement2 = Wikibase::Datatype::MediainfoStatement->new(
223             # sex or gender (P21) male (Q6581097)
224             'snak' => Wikibase::Datatype::MediainfoSnak->new(
225             'datavalue' => Wikibase::Datatype::Value::Item->new(
226             'value' => 'Q6581097',
227             ),
228             'property' => 'P21',
229             ),
230             );
231              
232             # Main item.
233             my $obj = Wikibase::Datatype::Mediainfo->new(
234             'id' => 'Q42',
235             'labels' => [
236             Wikibase::Datatype::Value::Monolingual->new(
237             'language' => 'cs',
238             'value' => 'Douglas Adams',
239             ),
240             Wikibase::Datatype::Value::Monolingual->new(
241             'language' => 'en',
242             'value' => 'Douglas Adams',
243             ),
244             ],
245             'page_id' => 123,
246             'statements' => [
247             $statement1,
248             $statement2,
249             ],
250             'title' => 'Q42',
251             );
252              
253             # Get structure.
254             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
255              
256             # Dump to output.
257             p $struct_hr;
258              
259             # Output:
260             # {
261             # descriptions {},
262             # id "Q42",
263             # labels {
264             # cs {
265             # language "cs",
266             # value "Douglas Adams"
267             # },
268             # en {
269             # language "en",
270             # value "Douglas Adams"
271             # }
272             # },
273             # ns 6,
274             # pageid 123,
275             # statements {
276             # P21 [
277             # [0] {
278             # mainsnak {
279             # datavalue {
280             # type "wikibase-entityid",
281             # value {
282             # entity-type "item",
283             # id "Q6581097",
284             # numeric-id 6581097
285             # }
286             # },
287             # property "P21",
288             # snaktype "value"
289             # },
290             # rank "normal",
291             # type "statement"
292             # }
293             # ],
294             # P31 [
295             # [0] {
296             # mainsnak {
297             # datavalue {
298             # type "wikibase-entityid",
299             # value {
300             # entity-type "item",
301             # id "Q5",
302             # numeric-id 5
303             # }
304             # },
305             # property "P31",
306             # snaktype "value"
307             # },
308             # qualifiers {
309             # P642 [
310             # [0] {
311             # datavalue {
312             # type "wikibase-entityid",
313             # value {
314             # entity-type "item",
315             # id "Q474741",
316             # numeric-id 474741
317             # }
318             # },
319             # property "P642",
320             # snaktype "value"
321             # }
322             # ]
323             # },
324             # qualifiers-order [
325             # [0] "P642"
326             # ],
327             # rank "normal",
328             # type "statement"
329             # }
330             # ]
331             # },
332             # title "Q42",
333             # type "mediainfo"
334             # }
335              
336             =head1 EXAMPLE2
337              
338             =for comment filename=struct2obj_mediainfo.pl
339              
340             use strict;
341             use warnings;
342              
343             use Data::Printer;
344             use Wikibase::Datatype::Struct::Mediainfo qw(struct2obj);
345              
346             # Item structure.
347             my $struct_hr = {
348             'descriptions' => {},
349             'id' => 'Q42',
350             'labels' => {
351             'cs' => {
352             'language' => 'cs',
353             'value' => 'Douglas Adams',
354             },
355             'en' => {
356             'language' => 'en',
357             'value' => 'Douglas Adams',
358             },
359             },
360             'ns' => 6,
361             'pageid' => 123,
362             'statements' => {
363             'P21' => [{
364             'mainsnak' => {
365             'datavalue' => {
366             'type' => 'wikibase-entityid',
367             'value' => {
368             'entity-type' => 'item',
369             'id' => 'Q6581097',
370             'numeric-id' => 6581097,
371             },
372             },
373             'property' => 'P21',
374             'snaktype' => 'value',
375             },
376             'rank' => 'normal',
377             'type' => 'statement',
378             }],
379             'P31' => [{
380             'mainsnak' => {
381             'datavalue' => {
382             'type' => 'wikibase-entityid',
383             'value' => {
384             'entity-type' => 'item',
385             'id' => 'Q5',
386             'numeric-id' => 5,
387             },
388             },
389             'property' => 'P31',
390             'snaktype' => 'value',
391             },
392             'qualifiers' => {
393             'P642' => [{
394             'datavalue' => {
395             'type' => 'wikibase-entityid',
396             'value' => {
397             'entity-type' => 'item',
398             'id' => 'Q474741',
399             'numeric-id' => 474741,
400             }
401             },
402             'property' => 'P642',
403             'snaktype' => 'value',
404             }],
405             },
406             'qualifiers-order' => [
407             'P642',
408             ],
409             'rank' => 'normal',
410             'type' => 'statement',
411             }],
412             },
413             'title' => 'Q42',
414             'type' => 'mediainfo',
415             };
416              
417             # Get object.
418             my $obj = struct2obj($struct_hr);
419              
420             # Print out.
421             p $obj;
422              
423             # Output:
424             # Wikibase::Datatype::Mediainfo {
425             # parents: Mo::Object
426             # public methods (5):
427             # BUILD
428             # Error::Pure:
429             # err
430             # Mo::utils:
431             # check_array_object, check_number, check_number_of_items
432             # private methods (0)
433             # internals: {
434             # descriptions [],
435             # id "Q42",
436             # labels [
437             # [0] Wikibase::Datatype::Value::Monolingual,
438             # [1] Wikibase::Datatype::Value::Monolingual
439             # ],
440             # ns 6,
441             # page_id 123,
442             # statements [
443             # [0] Wikibase::Datatype::MediainfoStatement,
444             # [1] Wikibase::Datatype::MediainfoStatement
445             # ],
446             # title "Q42"
447             # }
448             # }
449              
450             =head1 DEPENDENCIES
451              
452             L<Error::Pure>,
453             L<Exporter>,
454             L<Readonly>,
455             L<Wikibase::Datatype::Mediainfo>,
456             L<Wikibase::Datatype::Struct::Language>,
457             L<Wikibase::Datatype::Struct::MediainfoStatement>.
458              
459             =head1 SEE ALSO
460              
461             =over
462              
463             =item L<Wikibase::Datatype::Struct>
464              
465             Wikibase structure serialization.
466              
467             =item L<Wikibase::Datatype::Mediainfo>
468              
469             Wikibase mediainfo datatype.
470              
471             =back
472              
473             =head1 REPOSITORY
474              
475             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
476              
477             =head1 AUTHOR
478              
479             Michal Josef Špaček L<mailto:skim@cpan.org>
480              
481             L<http://skim.cz>
482              
483             =head1 LICENSE AND COPYRIGHT
484              
485             © 2020-2023 Michal Josef Špaček
486              
487             BSD 2-Clause License
488              
489             =head1 VERSION
490              
491             0.11
492              
493             =cut