File Coverage

blib/lib/Wikibase/Datatype/Item.pm
Criterion Covered Total %
statement 43 43 100.0
branch 4 4 100.0
condition n/a
subroutine 8 8 100.0
pod 0 1 0.0
total 55 56 98.2


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