File Coverage

blib/lib/Wikibase/Datatype/Item.pm
Criterion Covered Total %
statement 40 40 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Item;
2              
3 20     20   1580158 use strict;
  20         185  
  20         567  
4 20     20   126 use warnings;
  20         46  
  20         549  
5              
6 20     20   9120 use Error::Pure qw(err);
  20         222102  
  20         368  
7 20     20   10205 use Mo qw(build default is);
  20         10442  
  20         113  
8 20     20   54273 use Mo::utils qw(check_array_object check_number check_number_of_items);
  20         28251  
  20         340  
9 20     20   10302 use Wikibase::Datatype::Value::Monolingual;
  20         77  
  20         7858  
10              
11             our $VERSION = 0.29;
12              
13             has aliases => (
14             default => [],
15             is => 'ro',
16             );
17              
18             has descriptions => (
19             default => [],
20             is => 'ro',
21             );
22              
23             has id => (
24             is => 'ro',
25             );
26              
27             has labels => (
28             default => [],
29             is => 'ro',
30             );
31              
32             has lastrevid => (
33             is => 'ro',
34             );
35              
36             has modified => (
37             is => 'ro',
38             );
39              
40             has ns => (
41             default => 0,
42             is => 'ro',
43             );
44              
45             has page_id => (
46             is => 'ro',
47             );
48              
49             has sitelinks => (
50             default => [],
51             is => 'ro',
52             );
53              
54             has statements => (
55             default => [],
56             is => 'ro',
57             );
58              
59             has title => (
60             is => 'ro',
61             );
62              
63             sub BUILD {
64 29     29 0 9912 my $self = shift;
65              
66             # Check aliases.
67 29         141 check_array_object($self, 'aliases', 'Wikibase::Datatype::Value::Monolingual',
68             'Alias');
69              
70             # Check descriptions.
71 28         590 check_array_object($self, 'descriptions', 'Wikibase::Datatype::Value::Monolingual',
72             'Description');
73 27         405 check_number_of_items($self, 'descriptions', 'language', 'Description', 'language');
74              
75             # Check labels.
76 26         897 check_array_object($self, 'labels', 'Wikibase::Datatype::Value::Monolingual',
77             'Label');
78 25         402 check_number_of_items($self, 'labels', 'language', 'Label', 'language');
79              
80             # If length of value is greater than 250, strip.
81 24         550 my @labels = @{$self->labels};
  24         85  
82 24         207 my $strip_label = 0;
83 24         117 for (my $i = 0; $i < @labels; $i++) {
84 5 100       79 if (length $labels[$i]->value > 250) {
85 1         11 my $new_label = substr $labels[$i]->value, 0, 250;
86 1         11 my $new_language = $labels[$i]->language;
87 1         7 $strip_label = 1;
88 1         4 $labels[$i] = Wikibase::Datatype::Value::Monolingual->new(
89             'language' => $new_language,
90             'value' => $new_label,
91             );
92             }
93             }
94 24 100       107 if ($strip_label) {
95 1         3 $self->{'labels'} = \@labels;
96             }
97              
98             # Check page id.
99 24         97 check_number($self, 'page_id');
100              
101             # Check sitelinks.
102 23         330 check_array_object($self, 'sitelinks', 'Wikibase::Datatype::Sitelink',
103             'Sitelink');
104 22         327 check_number_of_items($self, 'sitelinks', 'site', 'Sitelink', 'site');
105              
106             # Check statements.
107 21         547 check_array_object($self, 'statements', 'Wikibase::Datatype::Statement',
108             'Statement');
109              
110 20         320 return;
111             }
112              
113             1;
114              
115             __END__
116              
117             =pod
118              
119             =encoding utf8
120              
121             =head1 NAME
122              
123             Wikibase::Datatype::Item - Wikibase item datatype.
124              
125             =head1 SYNOPSIS
126              
127             use Wikibase::Datatype::Item;
128              
129             my $obj = Wikibase::Datatype::Item->new(%params);
130             my $aliases_ar = $obj->aliases;
131             my $descriptions_ar = $obj->descriptions;
132             my $id = $obj->id;
133             my $labels_ar = $obj->labels;
134             my $lastrevid = $obj->lastrevid;
135             my $modified = $obj->modified;
136             my $ns = $obj->ns;
137             my $page_id = $obj->page_id;
138             my $sitelinks_ar = $obj->sitelinks;
139             my $statements_ar = $obj->statements;
140             my $title = $obj->title;
141              
142             =head1 DESCRIPTION
143              
144             This datatype is item class for representing claim.
145              
146             =head1 METHODS
147              
148             =head2 C<new>
149              
150             my $obj = Wikibase::Datatype::Item->new(%params);
151              
152             Constructor.
153              
154             Returns instance of object.
155              
156             =over 8
157              
158             =item * C<aliases>
159              
160             Item aliases. Multiple per language.
161             Reference to array with Wikibase::Datatype::Value::Monolingual instances.
162             Parameter is optional.
163              
164             =item * C<descriptions>
165              
166             Item descriptions. One per language.
167             Reference to array with Wikibase::Datatype::Value::Monolingual instances.
168             Parameter is optional.
169              
170             =item * C<id>
171              
172             Id.
173             Parameter is optional.
174              
175             =item * C<labels>
176              
177             Item descriptions. One per language. Check length of string (250) and strip it
178             if it's longer.
179             Reference to array with Wikibase::Datatype::Value::Monolingual instances.
180             Parameter is optional.
181              
182             =item * C<lastrevid>
183              
184             Last revision ID.
185             Parameter is optional.
186              
187             =item * C<modified>
188              
189             Date of modification.
190             Parameter is optional.
191              
192             =item * C<ns>
193              
194             Namespace.
195             Default value is 0.
196              
197             =item * C<page_id>
198              
199             Page id. Numeric value.
200             Parameter is optional.
201              
202             =item * C<sitelinks>
203              
204             Item sitelinks. One per site.
205             Reference to array with Wikibase::Datatype::Sitelink instances.
206             Parameter is optional.
207              
208             =item * C<statements>
209              
210             Item statements.
211             Reference to array with Wikibase::Datatype::Statement instances.
212             Parameter is optional.
213              
214             =item * C<title>
215              
216             Item title.
217             Parameter is optional.
218              
219             =back
220              
221             =head2 C<aliases>
222              
223             my $aliases_ar = $obj->aliases;
224              
225             Get aliases.
226              
227             Returns reference to array with Wikibase::Datatype::Value::Monolingual instances.
228              
229             =head2 C<descriptions>
230              
231             my $descriptions_ar = $obj->descriptions;
232              
233             Get descriptions.
234              
235             Returns reference to array with Wikibase::Datatype::Value::Monolingual instances.
236              
237             =head2 C<id>
238              
239             my $id = $obj->id;
240              
241             Get id.
242              
243             Returns string.
244              
245             =head2 C<labels>
246              
247             my $labels_ar = $obj->labels;
248              
249             Get labels.
250              
251             Returns reference to array with Wikibase::Datatype::Value::Monolingual instances.
252              
253             =head2 C<lastrevid>
254              
255             my $lastrevid = $obj->lastrevid;
256              
257             Get last revision ID.
258              
259             Returns string.
260              
261             =head2 C<modified>
262              
263             my $modified = $obj->modified;
264              
265             Get date of modification.
266              
267             Returns string.
268              
269             =head2 C<ns>
270              
271             my $ns = $obj->ns;
272              
273             Get namespace.
274              
275             Returns number.
276              
277             =head2 C<page_id>
278              
279             my $page_id = $obj->page_id;
280              
281             Get page id.
282              
283             Returns number.
284              
285             =head2 C<sitelinks>
286              
287             my $sitelinks_ar = $obj->sitelinks;
288              
289             Get sitelinks.
290              
291             Returns reference to array with Wikibase::Datatype::Sitelink instances.
292              
293             =head2 C<statements>
294              
295             my $statements_ar = $obj->statements;
296              
297             Get statements.
298              
299             Returns reference to array with Wikibase::Datatype::Statement instances.
300              
301             =head2 C<title>
302              
303             my $title = $obj->title;
304              
305             Get title.
306              
307             Returns string.
308              
309             =head1 ERRORS
310              
311             new():
312             From Mo::utils::check_array_object():
313             Alias isn't 'Wikibase::Datatype::Value::Monolingual' object.
314             Description isn't 'Wikibase::Datatype::Value::Monolingual' object.
315             Label isn't 'Wikibase::Datatype::Value::Monolingual' object.
316             Parameter 'aliases' must be a array.
317             Parameter 'descriptions' must be a array.
318             Parameter 'labels' must be a array.
319             Parameter 'sitelinks' must be a array.
320             Parameter 'statements' must be a array.
321             Sitelink isn't 'Wikibase::Datatype::Sitelink' object.
322             Statement isn't 'Wikibase::Datatype::Statement' object.
323             From Mo::utils::check_page_id():
324             Parameter 'page_id' must a number.
325             From Mo::utils::check_number_of_items():
326             Sitelink for site '%s' has multiple values.
327             Description for language '%s' has multiple values.
328             Label for language '%s' has multiple values.
329              
330             =head1 EXAMPLE
331              
332             =for comment filename=create_and_print_item.pl
333              
334             use strict;
335             use warnings;
336              
337             use Unicode::UTF8 qw(decode_utf8 encode_utf8);
338             use Wikibase::Datatype::Item;
339             use Wikibase::Datatype::Reference;
340             use Wikibase::Datatype::Sitelink;
341             use Wikibase::Datatype::Snak;
342             use Wikibase::Datatype::Statement;
343             use Wikibase::Datatype::Value::Item;
344             use Wikibase::Datatype::Value::Monolingual;
345             use Wikibase::Datatype::Value::String;
346             use Wikibase::Datatype::Value::Time;
347              
348             # Statements.
349             my $statement1 = Wikibase::Datatype::Statement->new(
350             # instance of (P31) human (Q5)
351             'snak' => Wikibase::Datatype::Snak->new(
352             'datatype' => 'wikibase-item',
353             'datavalue' => Wikibase::Datatype::Value::Item->new(
354             'value' => 'Q5',
355             ),
356             'property' => 'P31',
357             ),
358             'property_snaks' => [
359             # of (P642) alien (Q474741)
360             Wikibase::Datatype::Snak->new(
361             'datatype' => 'wikibase-item',
362             'datavalue' => Wikibase::Datatype::Value::Item->new(
363             'value' => 'Q474741',
364             ),
365             'property' => 'P642',
366             ),
367             ],
368             'references' => [
369             Wikibase::Datatype::Reference->new(
370             'snaks' => [
371             # stated in (P248) Virtual International Authority File (Q53919)
372             Wikibase::Datatype::Snak->new(
373             'datatype' => 'wikibase-item',
374             'datavalue' => Wikibase::Datatype::Value::Item->new(
375             'value' => 'Q53919',
376             ),
377             'property' => 'P248',
378             ),
379              
380             # VIAF ID (P214) 113230702
381             Wikibase::Datatype::Snak->new(
382             'datatype' => 'external-id',
383             'datavalue' => Wikibase::Datatype::Value::String->new(
384             'value' => '113230702',
385             ),
386             'property' => 'P214',
387             ),
388              
389             # retrieved (P813) 7 December 2013
390             Wikibase::Datatype::Snak->new(
391             'datatype' => 'time',
392             'datavalue' => Wikibase::Datatype::Value::Time->new(
393             'value' => '+2013-12-07T00:00:00Z',
394             ),
395             'property' => 'P813',
396             ),
397             ],
398             ),
399             ],
400             );
401             my $statement2 = Wikibase::Datatype::Statement->new(
402             # sex or gender (P21) male (Q6581097)
403             'snak' => Wikibase::Datatype::Snak->new(
404             'datatype' => 'wikibase-item',
405             'datavalue' => Wikibase::Datatype::Value::Item->new(
406             'value' => 'Q6581097',
407             ),
408             'property' => 'P21',
409             ),
410             'references' => [
411             Wikibase::Datatype::Reference->new(
412             'snaks' => [
413             # stated in (P248) Virtual International Authority File (Q53919)
414             Wikibase::Datatype::Snak->new(
415             'datatype' => 'wikibase-item',
416             'datavalue' => Wikibase::Datatype::Value::Item->new(
417             'value' => 'Q53919',
418             ),
419             'property' => 'P248',
420             ),
421              
422             # VIAF ID (P214) 113230702
423             Wikibase::Datatype::Snak->new(
424             'datatype' => 'external-id',
425             'datavalue' => Wikibase::Datatype::Value::String->new(
426             'value' => '113230702',
427             ),
428             'property' => 'P214',
429             ),
430              
431             # retrieved (P813) 7 December 2013
432             Wikibase::Datatype::Snak->new(
433             'datatype' => 'time',
434             'datavalue' => Wikibase::Datatype::Value::Time->new(
435             'value' => '+2013-12-07T00:00:00Z',
436             ),
437             'property' => 'P813',
438             ),
439             ],
440             ),
441             ],
442             );
443              
444             # Main item.
445             my $obj = Wikibase::Datatype::Item->new(
446             'aliases' => [
447             Wikibase::Datatype::Value::Monolingual->new(
448             'language' => 'cs',
449             'value' => decode_utf8('Douglas Noël Adams'),
450             ),
451             Wikibase::Datatype::Value::Monolingual->new(
452             'language' => 'cs',
453             'value' => 'Douglas Noel Adams',
454             ),
455             Wikibase::Datatype::Value::Monolingual->new(
456             'language' => 'cs',
457             'value' => 'Douglas N. Adams',
458             ),
459             Wikibase::Datatype::Value::Monolingual->new(
460             'language' => 'en',
461             'value' => 'Douglas Noel Adams',
462             ),
463             Wikibase::Datatype::Value::Monolingual->new(
464             'language' => 'en',
465             'value' => decode_utf8('Douglas Noël Adams'),
466             ),
467             Wikibase::Datatype::Value::Monolingual->new(
468             'language' => 'en',
469             'value' => 'Douglas N. Adams',
470             ),
471             ],
472             'descriptions' => [
473             Wikibase::Datatype::Value::Monolingual->new(
474             'language' => 'cs',
475             'value' => decode_utf8('anglický spisovatel, humorista a dramatik'),
476             ),
477             Wikibase::Datatype::Value::Monolingual->new(
478             'language' => 'en',
479             'value' => 'English writer and humorist',
480             ),
481             ],
482             'id' => 'Q42',
483             'labels' => [
484             Wikibase::Datatype::Value::Monolingual->new(
485             'language' => 'cs',
486             'value' => 'Douglas Adams',
487             ),
488             Wikibase::Datatype::Value::Monolingual->new(
489             'language' => 'en',
490             'value' => 'Douglas Adams',
491             ),
492             ],
493             'page_id' => 123,
494             'sitelinks' => [
495             Wikibase::Datatype::Sitelink->new(
496             'site' => 'cswiki',
497             'title' => 'Douglas Adams',
498             ),
499             Wikibase::Datatype::Sitelink->new(
500             'site' => 'enwiki',
501             'title' => 'Douglas Adams',
502             ),
503             ],
504             'statements' => [
505             $statement1,
506             $statement2,
507             ],
508             'title' => 'Q42',
509             );
510              
511             # Print out.
512             print "Title: ".$obj->title."\n";
513             print 'Id: '.$obj->id."\n";
514             print 'Page id: '.$obj->page_id."\n";
515             print "Labels:\n";
516             foreach my $label (sort { $a->language cmp $b->language } @{$obj->labels}) {
517             print "\t".encode_utf8($label->value).' ('.$label->language.")\n";
518             }
519             print "Descriptions:\n";
520             foreach my $desc (sort { $a->language cmp $b->language } @{$obj->descriptions}) {
521             print "\t".encode_utf8($desc->value).' ('.$desc->language.")\n";
522             }
523             print "Aliases:\n";
524             foreach my $alias (sort { $a->language cmp $b->language } @{$obj->aliases}) {
525             print "\t".encode_utf8($alias->value).' ('.$alias->language.")\n";
526             }
527             print "Sitelinks:\n";
528             foreach my $sitelink (@{$obj->sitelinks}) {
529             print "\t".$sitelink->title.' ('.$sitelink->site.")\n";
530             }
531             print "Statements:\n";
532             foreach my $statement (@{$obj->statements}) {
533             print "\tStatement:\n";
534             print "\t\t".$statement->snak->property.' -> '.$statement->snak->datavalue->value."\n";
535             print "\t\tQualifers:\n";
536             foreach my $property_snak (@{$statement->property_snaks}) {
537             print "\t\t\t".$property_snak->property.' -> '.
538             $property_snak->datavalue->value."\n";
539             }
540             print "\t\tReferences:\n";
541             foreach my $reference (@{$statement->references}) {
542             print "\t\t\tReference:\n";
543             foreach my $reference_snak (@{$reference->snaks}) {
544             print "\t\t\t".$reference_snak->property.' -> '.
545             $reference_snak->datavalue->value."\n";
546             }
547             }
548             }
549              
550             # Output:
551             # Title: Q42
552             # Id: Q42
553             # Page id: 123
554             # Labels:
555             # Douglas Adams (cs)
556             # Douglas Adams (en)
557             # Descriptions:
558             # anglický spisovatel, humorista a dramatik (cs)
559             # English writer and humorist (en)
560             # Aliases:
561             # Douglas Noël Adams (cs)
562             # Douglas Noel Adams (cs)
563             # Douglas N. Adams (cs)
564             # Douglas Noel Adams (en)
565             # Douglas Noël Adams (en)
566             # Douglas N. Adams (en)
567             # Sitelinks:
568             # Douglas Adams (cswiki)
569             # Douglas Adams (enwiki)
570             # Statements:
571             # Statement:
572             # P31 -> Q5
573             # Qualifers:
574             # P642 -> Q474741
575             # References:
576             # Reference:
577             # P248 -> Q53919
578             # P214 -> 113230702
579             # P813 -> +2013-12-07T00:00:00Z
580             # Statement:
581             # P21 -> Q6581097
582             # Qualifers:
583             # References:
584             # Reference:
585             # P248 -> Q53919
586             # P214 -> 113230702
587             # P813 -> +2013-12-07T00:00:00Z
588              
589             =head1 DEPENDENCIES
590              
591             L<Error::Pure>,
592             L<Mo>,
593             L<Mo:utils>.
594              
595             =head1 SEE ALSO
596              
597             =over
598              
599             =item L<Wikibase::Datatype>
600              
601             Wikibase datatypes.
602              
603             =back
604              
605             =head1 REPOSITORY
606              
607             L<https://github.com/michal-josef-spacek/Wikibase-Datatype>
608              
609             =head1 AUTHOR
610              
611             Michal Josef Špaček L<mailto:skim@cpan.org>
612              
613             L<http://skim.cz>
614              
615             =head1 LICENSE AND COPYRIGHT
616              
617             © 2020-2023 Michal Josef Špaček
618              
619             BSD 2-Clause License
620              
621             =head1 VERSION
622              
623             0.29
624              
625             =cut