File Coverage

blib/lib/Wikibase/Datatype/Property.pm
Criterion Covered Total %
statement 37 37 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 0 1 0.0
total 49 50 98.0


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