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              
2             use base qw(Exporter);
3 4     4   399726 use strict;
  4         35  
  4         396  
4 4     4   20 use warnings;
  4         7  
  4         56  
5 4     4   14  
  4         8  
  4         160  
6             use Error::Pure qw(err);
7 4     4   932 use Readonly;
  4         17096  
  4         67  
8 4     4   130 use Wikibase::Datatype::Mediainfo;
  4         7  
  4         107  
9 4     4   1156 use Wikibase::Datatype::Struct::Language;
  4         12027  
  4         97  
10 4     4   1233 use Wikibase::Datatype::Struct::MediainfoStatement;
  4         10  
  4         183  
11 4     4   1299  
  4         10  
  4         1968  
12             Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj);
13              
14             our $VERSION = 0.09;
15              
16             my ($obj, $base_uri) = @_;
17              
18 7     7 1 10457 if (! defined $obj) {
19             err "Object doesn't exist.";
20 7 100       15 }
21 1         4 if (! $obj->isa('Wikibase::Datatype::Mediainfo')) {
22             err "Object isn't 'Wikibase::Datatype::Mediainfo'.";
23 6 100       21 }
24 1         4 if (! defined $base_uri) {
25             err 'Base URI is required.';
26 5 100       11 }
27 1         3  
28             my $struct_hr = {
29             'type' => 'mediainfo',
30 4         8 };
31              
32             # Claims.
33             foreach my $statement (@{$obj->statements}) {
34             $struct_hr->{'statements'}->{$statement->snak->property} //= [];
35 4         6 push @{$struct_hr->{'statements'}->{$statement->snak->property}},
  4         6  
36 4   100     23 Wikibase::Datatype::Struct::MediainfoStatement::obj2struct($statement, $base_uri);
37 4         40 }
  4         8  
38              
39             # Descriptions.
40             $struct_hr->{'descriptions'} = {};
41             foreach my $desc (@{$obj->descriptions}) {
42 4         19 $struct_hr->{'descriptions'}->{$desc->language}
43 4         5 = Wikibase::Datatype::Struct::Language::obj2struct($desc);
  4         7  
44 0         0 }
45              
46             # Id.
47             if (defined $obj->id) {
48             $struct_hr->{'id'} = $obj->id;
49 4 100       26 }
50 1         6  
51             # Labels.
52             foreach my $label (@{$obj->labels}) {
53             $struct_hr->{'labels'}->{$label->language}
54 4         19 = Wikibase::Datatype::Struct::Language::obj2struct($label);
  4         7  
55 2         12 }
56            
57             # Last revision id.
58             if (defined $obj->lastrevid) {
59             $struct_hr->{'lastrevid'} = $obj->lastrevid;
60 4 100       23 }
61 1         6  
62             # Modified date.
63             if (defined $obj->modified) {
64             $struct_hr->{'modified'} = $obj->modified;
65 4 100       19 }
66 1         5  
67             # Namespace.
68             if (defined $obj->ns) {
69             $struct_hr->{'ns'} = $obj->ns;
70 4 100       19 }
71 3         24  
72             # Page ID.
73             if (defined $obj->page_id) {
74             $struct_hr->{'pageid'} = $obj->page_id;
75 4 100       25 }
76 1         6  
77             # Title.
78             if (defined $obj->title) {
79             $struct_hr->{'title'} = $obj->title;
80 4 100       19 }
81 1         6  
82             return $struct_hr;
83             }
84 4         30  
85             my $struct_hr = shift;
86              
87             if (! exists $struct_hr->{'type'} || $struct_hr->{'type'} ne 'mediainfo') {
88 5     5 1 7568 err "Structure isn't for 'mediainfo' type.";
89             }
90 5 100 100     22  
91 2         6 # Descriptions.
92             my $descriptions_ar = [];
93             foreach my $lang (keys %{$struct_hr->{'descriptions'}}) {
94             push @{$descriptions_ar}, Wikibase::Datatype::Struct::Language::struct2obj(
95 3         7 $struct_hr->{'descriptions'}->{$lang},
96 3         4 );
  3         9  
97 0         0 }
98 0         0  
99             # Labels.
100             my $labels_ar = [];
101             foreach my $lang (keys %{$struct_hr->{'labels'}}) {
102             push @{$labels_ar}, Wikibase::Datatype::Struct::Language::struct2obj(
103 3         6 $struct_hr->{'labels'}->{$lang},
104 3         4 );
  3         7  
105 2         8 }
106 2         4  
107             # Statements.
108             my $statements_ar = [];
109             foreach my $property (keys %{$struct_hr->{'statements'}}) {
110             foreach my $claim_hr (@{$struct_hr->{'statements'}->{$property}}) {
111 3         5 push @{$statements_ar}, Wikibase::Datatype::Struct::MediainfoStatement::struct2obj(
112 3         3 $claim_hr,
  3         7  
113 0         0 );
  0         0  
114 0         0 }
  0         0  
115             }
116              
117             my $obj = Wikibase::Datatype::Mediainfo->new(
118             'descriptions' => $descriptions_ar,
119             defined $struct_hr->{'id'} ? ('id' => $struct_hr->{'id'}) : (),
120             'labels' => $labels_ar,
121             defined $struct_hr->{'lastrevid'} ? ('lastrevid' => $struct_hr->{'lastrevid'}) : (),
122             defined $struct_hr->{'modified'} ? ('modified' => $struct_hr->{'modified'}) : (),
123             defined $struct_hr->{'ns'} ? ('ns' => $struct_hr->{'ns'}) : (),
124             defined $struct_hr->{'pageid'} ? ('page_id' => $struct_hr->{'pageid'}) : (),
125             'statements' => $statements_ar,
126             defined $struct_hr->{'title'} ? ('title' => $struct_hr->{'title'}) : (),
127             );
128              
129 3 100       28 return $obj;
    100          
    100          
    100          
    100          
    100          
130             }
131              
132 3         342 1;
133              
134              
135             =pod
136              
137             =encoding utf8
138              
139             =head1 NAME
140              
141             Wikibase::Datatype::Struct::Mediainfo - Wikibase mediainfo structure serialization.
142              
143             =head1 SYNOPSIS
144              
145             use Wikibase::Datatype::Struct::Mediainfo qw(obj2struct struct2obj);
146              
147             my $struct_hr = obj2struct($obj, $base_uri);
148             my $obj = struct2obj($struct_hr);
149              
150             =head1 DESCRIPTION
151              
152             This conversion is between objects defined in Wikibase::Datatype and structures
153             serialized via JSON to MediaWiki.
154              
155             =head1 SUBROUTINES
156              
157             =head2 C<obj2struct>
158              
159             my $struct_hr = obj2struct($obj, $base_uri);
160              
161             Convert Wikibase::Datatype::Mediainfo instance to structure.
162             C<$base_uri> is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
163              
164             Returns reference to hash with structure.
165              
166             =head2 C<struct2obj>
167              
168             my $obj = struct2obj($struct_hr);
169              
170             Convert structure of mediainfo to object.
171              
172             Returns Wikibase::Datatype::Mediainfo instance.
173              
174             =head1 ERRORS
175              
176             obj2struct():
177             Base URI is required.
178             Object doesn't exist.
179             Object isn't 'Wikibase::Datatype::Mediainfo'.
180              
181             struct2obj():
182             Structure isn't for 'mediainfo' type.
183              
184             =head1 EXAMPLE1
185              
186             use strict;
187             use warnings;
188              
189             use Data::Printer;
190             use Wikibase::Datatype::Mediainfo;
191             use Wikibase::Datatype::MediainfoSnak;
192             use Wikibase::Datatype::MediainfoStatement;
193             use Wikibase::Datatype::Struct::Mediainfo qw(obj2struct);
194             use Wikibase::Datatype::Value::Item;
195             use Wikibase::Datatype::Value::Monolingual;
196              
197             # Object.
198             my $statement1 = Wikibase::Datatype::MediainfoStatement->new(
199             # instance of (P31) human (Q5)
200             'snak' => Wikibase::Datatype::MediainfoSnak->new(
201             'datavalue' => Wikibase::Datatype::Value::Item->new(
202             'value' => 'Q5',
203             ),
204             'property' => 'P31',
205             ),
206             'property_snaks' => [
207             # of (P642) alien (Q474741)
208             Wikibase::Datatype::MediainfoSnak->new(
209             'datavalue' => Wikibase::Datatype::Value::Item->new(
210             'value' => 'Q474741',
211             ),
212             'property' => 'P642',
213             ),
214             ],
215             );
216             my $statement2 = Wikibase::Datatype::MediainfoStatement->new(
217             # sex or gender (P21) male (Q6581097)
218             'snak' => Wikibase::Datatype::MediainfoSnak->new(
219             'datavalue' => Wikibase::Datatype::Value::Item->new(
220             'value' => 'Q6581097',
221             ),
222             'property' => 'P21',
223             ),
224             );
225              
226             # Main item.
227             my $obj = Wikibase::Datatype::Mediainfo->new(
228             'id' => 'Q42',
229             'labels' => [
230             Wikibase::Datatype::Value::Monolingual->new(
231             'language' => 'cs',
232             'value' => 'Douglas Adams',
233             ),
234             Wikibase::Datatype::Value::Monolingual->new(
235             'language' => 'en',
236             'value' => 'Douglas Adams',
237             ),
238             ],
239             'page_id' => 123,
240             'statements' => [
241             $statement1,
242             $statement2,
243             ],
244             'title' => 'Q42',
245             );
246              
247             # Get structure.
248             my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
249              
250             # Dump to output.
251             p $struct_hr;
252              
253             # Output:
254             # TODO
255              
256             =head1 EXAMPLE2
257              
258             use strict;
259             use warnings;
260              
261             use Data::Printer;
262             use Wikibase::Datatype::Struct::Mediainfo qw(struct2obj);
263              
264             # Item structure.
265             my $struct_hr = {
266             # TODO
267             };
268              
269             # Get object.
270             my $obj = struct2obj($struct_hr);
271              
272             # Print out.
273             p $obj;
274              
275             # Output:
276             # TODO
277              
278             =head1 DEPENDENCIES
279              
280             L<Error::Pure>,
281             L<Exporter>,
282             L<Readonly>,
283             L<Wikibase::Datatype::Mediainfo>,
284             L<Wikibase::Datatype::Struct::Language>,
285             L<Wikibase::Datatype::Struct::MediainfoStatement>.
286              
287             =head1 SEE ALSO
288              
289             =over
290              
291             =item L<Wikibase::Datatype::Struct>
292              
293             Wikibase structure serialization.
294              
295             =item L<Wikibase::Datatype::Mediainfo>
296              
297             Wikibase mediainfo datatype.
298              
299             =back
300              
301             =head1 REPOSITORY
302              
303             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct>
304              
305             =head1 AUTHOR
306              
307             Michal Josef Špaček L<mailto:skim@cpan.org>
308              
309             L<http://skim.cz>
310              
311             =head1 LICENSE AND COPYRIGHT
312              
313             © 2020-2022 Michal Josef Špaček
314              
315             BSD 2-Clause License
316              
317             =head1 VERSION
318              
319             0.09
320              
321             =cut