File Coverage

blib/lib/Wikibase/Datatype/Print/Item.pm
Criterion Covered Total %
statement 41 41 100.0
branch 5 8 62.5
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 57 60 95.0


line stmt bran cond sub pod time code
1             package Wikibase::Datatype::Print::Item;
2              
3 3     3   910081 use base qw(Exporter);
  3         39  
  3         292  
4 3     3   22 use strict;
  3         6  
  3         57  
5 3     3   15 use warnings;
  3         5  
  3         81  
6              
7 3     3   925 use Error::Pure qw(err);
  3         23073  
  3         83  
8 3     3   116 use Readonly;
  3         6  
  3         112  
9 3     3   1331 use Wikibase::Datatype::Print::Sitelink;
  3         10  
  3         121  
10 3     3   1359 use Wikibase::Datatype::Print::Statement;
  3         10  
  3         170  
11 3         150 use Wikibase::Datatype::Print::Utils qw(print_aliases print_descriptions
12 3     3   21 print_labels print_sitelinks print_statements);
  3         8  
13 3     3   20 use Wikibase::Datatype::Print::Value::Monolingual;
  3         7  
  3         857  
14              
15             Readonly::Array our @EXPORT_OK => qw(print);
16              
17             our $VERSION = 0.13;
18              
19             sub print {
20 2     2 1 21813 my ($obj, $opts_hr) = @_;
21              
22 2 50       9 if (! defined $opts_hr) {
23 2         5 $opts_hr = {};
24             }
25              
26 2 50       8 if (! exists $opts_hr->{'lang'}) {
27 2         6 $opts_hr->{'lang'} = 'en';
28             }
29              
30 2 100       13 if (! $obj->isa('Wikibase::Datatype::Item')) {
31 1         7 err "Object isn't 'Wikibase::Datatype::Item'.";
32             }
33              
34 1         2 my @ret;
35              
36             # Label.
37 1         8 push @ret, print_labels($obj, $opts_hr,
38             \&Wikibase::Datatype::Print::Value::Monolingual::print);
39              
40             # Description.
41 1         10 push @ret, print_descriptions($obj, $opts_hr,
42             \&Wikibase::Datatype::Print::Value::Monolingual::print);
43              
44             # Aliases.
45 1         6 push @ret, print_aliases($obj, $opts_hr,
46             \&Wikibase::Datatype::Print::Value::Monolingual::print);
47              
48             # Sitelinks.
49 1         7 push @ret, print_sitelinks($obj, $opts_hr,
50             \&Wikibase::Datatype::Print::Sitelink::print);
51              
52             # Statements.
53 1         6 push @ret, print_statements($obj, $opts_hr,
54             \&Wikibase::Datatype::Print::Statement::print);
55              
56 1 50       13 return wantarray ? @ret : (join "\n", @ret);
57             }
58              
59             1;
60              
61             __END__
62              
63             =pod
64              
65             =encoding utf8
66              
67             =head1 NAME
68              
69             Wikibase::Datatype::Print::Item - Wikibase item pretty print helpers.
70              
71             =head1 SYNOPSIS
72              
73             use Wikibase::Datatype::Print::Item qw(print);
74              
75             my $pretty_print_string = print($obj, $opts_hr);
76             my @pretty_print_lines = print($obj, $opts_hr);
77              
78             =head1 SUBROUTINES
79              
80             =head2 C<print>
81              
82             my $pretty_print_string = print($obj, $opts_hr);
83             my @pretty_print_lines = print($obj, $opts_hr);
84              
85             Construct pretty print output for L<Wikibase::Datatype::Item>
86             object.
87              
88             Returns string in scalar context.
89             Returns list of lines in array context.
90              
91             =head1 ERRORS
92              
93             print():
94             Object isn't 'Wikibase::Datatype::Item'.
95              
96             =head1 EXAMPLE
97              
98             =for comment filename=create_and_print_item.pl
99              
100             use strict;
101             use warnings;
102              
103             use Unicode::UTF8 qw(decode_utf8 encode_utf8);
104             use Wikibase::Datatype::Item;
105             use Wikibase::Datatype::Print::Item;
106             use Wikibase::Datatype::Reference;
107             use Wikibase::Datatype::Sitelink;
108             use Wikibase::Datatype::Snak;
109             use Wikibase::Datatype::Statement;
110             use Wikibase::Datatype::Value::Item;
111             use Wikibase::Datatype::Value::Monolingual;
112             use Wikibase::Datatype::Value::String;
113             use Wikibase::Datatype::Value::Time;
114              
115             # Statements.
116             my $statement1 = Wikibase::Datatype::Statement->new(
117             # instance of (P31) human (Q5)
118             'snak' => Wikibase::Datatype::Snak->new(
119             'datatype' => 'wikibase-item',
120             'datavalue' => Wikibase::Datatype::Value::Item->new(
121             'value' => 'Q5',
122             ),
123             'property' => 'P31',
124             ),
125             'property_snaks' => [
126             # of (P642) alien (Q474741)
127             Wikibase::Datatype::Snak->new(
128             'datatype' => 'wikibase-item',
129             'datavalue' => Wikibase::Datatype::Value::Item->new(
130             'value' => 'Q474741',
131             ),
132             'property' => 'P642',
133             ),
134             ],
135             'references' => [
136             Wikibase::Datatype::Reference->new(
137             'snaks' => [
138             # stated in (P248) Virtual International Authority File (Q53919)
139             Wikibase::Datatype::Snak->new(
140             'datatype' => 'wikibase-item',
141             'datavalue' => Wikibase::Datatype::Value::Item->new(
142             'value' => 'Q53919',
143             ),
144             'property' => 'P248',
145             ),
146              
147             # VIAF ID (P214) 113230702
148             Wikibase::Datatype::Snak->new(
149             'datatype' => 'external-id',
150             'datavalue' => Wikibase::Datatype::Value::String->new(
151             'value' => '113230702',
152             ),
153             'property' => 'P214',
154             ),
155              
156             # retrieved (P813) 7 December 2013
157             Wikibase::Datatype::Snak->new(
158             'datatype' => 'time',
159             'datavalue' => Wikibase::Datatype::Value::Time->new(
160             'value' => '+2013-12-07T00:00:00Z',
161             ),
162             'property' => 'P813',
163             ),
164             ],
165             ),
166             ],
167             );
168             my $statement2 = Wikibase::Datatype::Statement->new(
169             # sex or gender (P21) male (Q6581097)
170             'snak' => Wikibase::Datatype::Snak->new(
171             'datatype' => 'wikibase-item',
172             'datavalue' => Wikibase::Datatype::Value::Item->new(
173             'value' => 'Q6581097',
174             ),
175             'property' => 'P21',
176             ),
177             'references' => [
178             Wikibase::Datatype::Reference->new(
179             'snaks' => [
180             # stated in (P248) Virtual International Authority File (Q53919)
181             Wikibase::Datatype::Snak->new(
182             'datatype' => 'wikibase-item',
183             'datavalue' => Wikibase::Datatype::Value::Item->new(
184             'value' => 'Q53919',
185             ),
186             'property' => 'P248',
187             ),
188              
189             # VIAF ID (P214) 113230702
190             Wikibase::Datatype::Snak->new(
191             'datatype' => 'external-id',
192             'datavalue' => Wikibase::Datatype::Value::String->new(
193             'value' => '113230702',
194             ),
195             'property' => 'P214',
196             ),
197              
198             # retrieved (P813) 7 December 2013
199             Wikibase::Datatype::Snak->new(
200             'datatype' => 'time',
201             'datavalue' => Wikibase::Datatype::Value::Time->new(
202             'value' => '+2013-12-07T00:00:00Z',
203             ),
204             'property' => 'P813',
205             ),
206             ],
207             ),
208             ],
209             );
210              
211             # Main item.
212             my $obj = Wikibase::Datatype::Item->new(
213             'aliases' => [
214             Wikibase::Datatype::Value::Monolingual->new(
215             'language' => 'cs',
216             'value' => decode_utf8('Douglas Noël Adams'),
217             ),
218             Wikibase::Datatype::Value::Monolingual->new(
219             'language' => 'cs',
220             'value' => 'Douglas Noel Adams',
221             ),
222             Wikibase::Datatype::Value::Monolingual->new(
223             'language' => 'cs',
224             'value' => 'Douglas N. Adams',
225             ),
226             Wikibase::Datatype::Value::Monolingual->new(
227             'language' => 'en',
228             'value' => 'Douglas Noel Adams',
229             ),
230             Wikibase::Datatype::Value::Monolingual->new(
231             'language' => 'en',
232             'value' => decode_utf8('Douglas Noël Adams'),
233             ),
234             Wikibase::Datatype::Value::Monolingual->new(
235             'language' => 'en',
236             'value' => 'Douglas N. Adams',
237             ),
238             ],
239             'descriptions' => [
240             Wikibase::Datatype::Value::Monolingual->new(
241             'language' => 'cs',
242             'value' => decode_utf8('anglický spisovatel, humorista a dramatik'),
243             ),
244             Wikibase::Datatype::Value::Monolingual->new(
245             'language' => 'en',
246             'value' => 'English writer and humorist',
247             ),
248             ],
249             'id' => 'Q42',
250             'labels' => [
251             Wikibase::Datatype::Value::Monolingual->new(
252             'language' => 'cs',
253             'value' => 'Douglas Adams',
254             ),
255             Wikibase::Datatype::Value::Monolingual->new(
256             'language' => 'en',
257             'value' => 'Douglas Adams',
258             ),
259             ],
260             'page_id' => 123,
261             'sitelinks' => [
262             Wikibase::Datatype::Sitelink->new(
263             'site' => 'cswiki',
264             'title' => 'Douglas Adams',
265             ),
266             Wikibase::Datatype::Sitelink->new(
267             'site' => 'enwiki',
268             'title' => 'Douglas Adams',
269             ),
270             ],
271             'statements' => [
272             $statement1,
273             $statement2,
274             ],
275             'title' => 'Q42',
276             );
277              
278             # Print.
279             print encode_utf8(scalar Wikibase::Datatype::Print::Item::print($obj))."\n";
280              
281             # Output:
282             # Label: Douglas Adams (en)
283             # Description: English writer and humorist (en)
284             # Aliases:
285             # Douglas Noel Adams (en)
286             # Douglas Noël Adams (en)
287             # Douglas N. Adams (en)
288             # Sitelinks:
289             # Douglas Adams (cswiki)
290             # Douglas Adams (enwiki)
291             # Statements:
292             # P31: Q5 (normal)
293             # P642: Q474741
294             # References:
295             # {
296             # P248: Q53919
297             # P214: 113230702
298             # P813: 7 December 2013 (Q1985727)
299             # }
300             # P21: Q6581097 (normal)
301             # References:
302             # {
303             # P248: Q53919
304             # P214: 113230702
305             # P813: 7 December 2013 (Q1985727)
306             # }
307              
308             =head1 DEPENDENCIES
309              
310             L<Error::Pure>,
311             L<Exporter>,
312             L<Readonly>,
313             L<Wikibase::Datatype::Print::Sitelink>,
314             L<Wikibase::Datatype::Print::Statement>,
315             L<Wikibase::Datatype::Print::Utils>,
316             L<Wikibase::Datatype::Print::Value::Monolingual>.
317              
318             =head1 SEE ALSO
319              
320             =over
321              
322             =item L<Wikibase::Datatype::Item>
323              
324             Wikibase item datatype.
325              
326             =back
327              
328             =head1 REPOSITORY
329              
330             L<https://github.com/michal-josef-spacek/Wikibase-Datatype-Print>
331              
332             =head1 AUTHOR
333              
334             Michal Josef Špaček L<mailto:skim@cpan.org>
335              
336             L<http://skim.cz>
337              
338             =head1 LICENSE AND COPYRIGHT
339              
340             © 2020-2023 Michal Josef Špaček
341              
342             BSD 2-Clause License
343              
344             =head1 VERSION
345              
346             0.13
347              
348             =cut
349