File Coverage

blib/lib/Wikibase/Datatype/Struct/Property.pm
Criterion Covered Total %
statement 95 95 100.0
branch 34 34 100.0
condition 4 5 80.0
subroutine 11 11 100.0
pod 2 2 100.0
total 146 147 99.3


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