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   380420 use base qw(Exporter);
  4         45  
  4         529  
4 4     4   94 use strict;
  4         10  
  4         105  
5 4     4   23 use warnings;
  4         6  
  4         127  
6              
7 4     4   1479 use Error::Pure qw(err);
  4         24323  
  4         122  
8 4     4   169 use Readonly;
  4         37  
  4         158  
9 4     4   1588 use Wikibase::Datatype::Mediainfo;
  4         15952  
  4         130  
10 4     4   1891 use Wikibase::Datatype::Struct::Language;
  4         11  
  4         187  
11 4     4   2010 use Wikibase::Datatype::Struct::MediainfoStatement;
  4         14  
  4         3014  
12              
13             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
14              
15             our $VERSION = 0.08;
16              
17             sub obj2struct {
18 7     7 1 17950 my ($obj, $base_uri) = @_;
19              
20 7 100       82 if (! defined $obj) {
21 1         5 err "Object doesn't exist.";
22             }
23 6 100       51 if (! $obj->isa('Wikibase::Datatype::Mediainfo')) {
24 1         10 err "Object isn't 'Wikibase::Datatype::Mediainfo'.";
25             }
26 5 100       20 if (! defined $base_uri) {
27 1         5 err 'Base URI is required.';
28             }
29              
30 4         18 my $struct_hr = {
31             'type' => 'mediainfo',
32             };
33              
34             # Claims.
35 4         9 foreach my $statement (@{$obj->statements}) {
  4         16  
36 4   100     40 $struct_hr->{'statements'}->{$statement->snak->property} //= [];
37 4         118 push @{$struct_hr->{'statements'}->{$statement->snak->property}},
  4         13  
38             Wikibase::Datatype::Struct::MediainfoStatement::obj2struct($statement, $base_uri);
39             }
40              
41             # Descriptions.
42 4         34 $struct_hr->{'descriptions'} = {};
43 4         12 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       51 if (defined $obj->id) {
50 1         9 $struct_hr->{'id'} = $obj->id;
51             }
52              
53             # Labels.
54 4         33 foreach my $label (@{$obj->labels}) {
  4         13  
55 2         21 $struct_hr->{'labels'}->{$label->language}
56             = Wikibase::Datatype::Struct::Language::obj2struct($label);
57             }
58            
59             # Last revision id.
60 4 100       45 if (defined $obj->lastrevid) {
61 1         8 $struct_hr->{'lastrevid'} = $obj->lastrevid;
62             }
63              
64             # Modified date.
65 4 100       35 if (defined $obj->modified) {
66 1         9 $struct_hr->{'modified'} = $obj->modified;
67             }
68              
69             # Namespace.
70 4 100       33 if (defined $obj->ns) {
71 3         34 $struct_hr->{'ns'} = $obj->ns;
72             }
73              
74             # Page ID.
75 4 100       40 if (defined $obj->page_id) {
76 1         8 $struct_hr->{'pageid'} = $obj->page_id;
77             }
78              
79             # Title.
80 4 100       35 if (defined $obj->title) {
81 1         8 $struct_hr->{'title'} = $obj->title;
82             }
83              
84 4         57 return $struct_hr;
85             }
86              
87             sub struct2obj {
88 5     5 1 11689 my $struct_hr = shift;
89              
90 5 100 100     34 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         8 my $descriptions_ar = [];
96 3         7 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         6 my $labels_ar = [];
104 3         6 foreach my $lang (keys %{$struct_hr->{'labels'}}) {
  3         10  
105 2         10 push @{$labels_ar}, Wikibase::Datatype::Struct::Language::struct2obj(
106 2         5 $struct_hr->{'labels'}->{$lang},
107             );
108             }
109              
110             # Statements.
111 3         7 my $statements_ar = [];
112 3         5 foreach my $property (keys %{$struct_hr->{'statements'}}) {
  3         8  
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       37 defined $struct_hr->{'title'} ? ('title' => $struct_hr->{'title'}) : (),
    100          
    100          
    100          
    100          
    100          
130             );
131              
132 3         455 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             use strict;
191             use warnings;
192              
193             use Data::Printer;
194             use Wikibase::Datatype::Mediainfo;
195             use Wikibase::Datatype::MediainfoSnak;
196             use Wikibase::Datatype::MediainfoStatement;
197             use Wikibase::Datatype::Struct::Mediainfo qw(obj2struct);
198             use Wikibase::Datatype::Value::Item;
199             use Wikibase::Datatype::Value::Monolingual;
200              
201             # Object.
202             my $statement1 = Wikibase::Datatype::MediainfoStatement->new(
203             # instance of (P31) human (Q5)
204             'snak' => Wikibase::Datatype::MediainfoSnak->new(
205             'datavalue' => Wikibase::Datatype::Value::Item->new(
206             'value' => 'Q5',
207             ),
208             'property' => 'P31',
209             ),
210             'property_snaks' => [
211             # of (P642) alien (Q474741)
212             Wikibase::Datatype::MediainfoSnak->new(
213             'datavalue' => Wikibase::Datatype::Value::Item->new(
214             'value' => 'Q474741',
215             ),
216             'property' => 'P642',
217             ),
218             ],
219             );
220             my $statement2 = Wikibase::Datatype::MediainfoStatement->new(
221             # sex or gender (P21) male (Q6581097)
222             'snak' => Wikibase::Datatype::MediainfoSnak->new(
223             'datavalue' => Wikibase::Datatype::Value::Item->new(
224             'value' => 'Q6581097',
225             ),
226             'property' => 'P21',
227             ),
228             );
229              
230             # Main item.
231             my $obj = Wikibase::Datatype::Mediainfo->new(
232             'id' => 'Q42',
233             'labels' => [
234             Wikibase::Datatype::Value::Monolingual->new(
235             'language' => 'cs',
236             'value' => 'Douglas Adams',
237             ),
238             Wikibase::Datatype::Value::Monolingual->new(
239             'language' => 'en',
240             'value' => 'Douglas Adams',
241             ),
242             ],
243             'page_id' => 123,
244             'statements' => [
245             $statement1,
246             $statement2,
247             ],
248             'title' => 'Q42',
249             );
250              
251             # Get structure.
252             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
253              
254             # Dump to output.
255             p $struct_hr;
256              
257             # Output:
258             # TODO
259              
260             =head1 EXAMPLE2
261              
262             use strict;
263             use warnings;
264              
265             use Data::Printer;
266             use Wikibase::Datatype::Struct::Mediainfo qw(struct2obj);
267              
268             # Item structure.
269             my $struct_hr = {
270             # TODO
271             };
272              
273             # Get object.
274             my $obj = struct2obj($struct_hr);
275              
276             # Print out.
277             p $obj;
278              
279             # Output:
280             # TODO
281              
282             =head1 DEPENDENCIES
283              
284             L<Error::Pure>,
285             L<Exporter>,
286             L<Readonly>,
287             L<Wikibase::Datatype::Mediainfo>,
288             L<Wikibase::Datatype::Struct::Language>,
289             L<Wikibase::Datatype::Struct::MediainfoStatement>.
290              
291             =head1 SEE ALSO
292              
293             =over
294              
295             =item L<Wikibase::Datatype::Struct>
296              
297             Wikibase structure serialization.
298              
299             =item L<Wikibase::Datatype::Mediainfo>
300              
301             Wikibase mediainfo datatype.
302              
303             =back
304              
305             =head1 REPOSITORY
306              
307             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
308              
309             =head1 AUTHOR
310              
311             Michal Josef Špaček L<mailto:skim@cpan.org>
312              
313             L<http://skim.cz>
314              
315             =head1 LICENSE AND COPYRIGHT
316              
317             © Michal Josef Špaček 2020-2021
318              
319             BSD 2-Clause License
320              
321             =head1 VERSION
322              
323             0.08
324              
325             =cut