File Coverage

blib/lib/App/Wikidata/Print.pm
Criterion Covered Total %
statement 41 51 80.3
branch 1 8 12.5
condition 1 3 33.3
subroutine 11 11 100.0
pod 2 2 100.0
total 56 75 74.6


line stmt bran cond sub pod time code
1             package App::Wikidata::Print;
2              
3 4     4   117288 use strict;
  4         25  
  4         116  
4 4     4   20 use warnings;
  4         12  
  4         119  
5              
6 4     4   2028 use Class::Utils qw(set_params);
  4         59398  
  4         79  
7 4     4   11664 use Getopt::Std;
  4         249  
  4         311  
8 4     4   2053 use Unicode::UTF8 qw(encode_utf8);
  4         2267  
  4         250  
9 4     4   2281 use Wikibase::API;
  4         2971429  
  4         184  
10 4     4   2413 use Wikibase::Datatype::Print::Item;
  4         64292  
  4         229  
11 4     4   2153 use Wikibase::Datatype::Print::Lexeme;
  4         12303  
  4         180  
12 4     4   1989 use Wikibase::Datatype::Print::Property;
  4         2873  
  4         1862  
13              
14             our $VERSION = 0.02;
15              
16             # Constructor.
17             sub new {
18 2     2 1 1738 my ($class, @params) = @_;
19              
20             # Create object.
21 2         9 my $self = bless {}, $class;
22              
23             # Process parameters.
24 2         10 set_params($self, @params);
25              
26             # Object.
27 2         21 return $self;
28             }
29              
30             # Run.
31             sub run {
32 1     1 1 2 my $self = shift;
33              
34             # Process arguments.
35 1         10 $self->{'_opts'} = {
36             'h' => 0,
37             'l' => 'en',
38             'm' => 'www.wikidata.org',
39             };
40 1 50 33     5 if (! getopts('hl:m:', $self->{'_opts'}) || $self->{'_opts'}->{'h'}) {
41 1         132 print STDERR "Usage: $0 [-h] [-l lang] [-m mediawiki_site] [--version] wd_id\n";
42 1         25 print STDERR "\t-h\t\t\tPrint help.\n";
43 1         16 print STDERR "\t-l lang\t\t\tLanguage used (default is English = en).\n";
44 1         11 print STDERR "\t-m mediawiki_site\tMediaWiki site (default is www.wikidata.org).\n";
45 1         12 print STDERR "\t--version\t\tPrint version.\n";
46 1         13 print STDERR "\twd_id\t\t\tWikidata id (qid or pid or lid).\n";
47 1         5 return 1;
48             }
49 0           my $wd_id = $ARGV[0];
50              
51             my $api = Wikibase::API->new(
52 0           'mediawiki_site' => $self->{'_opts'}->{'m'},
53             );
54              
55 0           my $obj = $api->get_item($wd_id);
56              
57 0 0         if (ref $obj eq 'Wikibase::Datatype::Item') {
    0          
    0          
58             print encode_utf8(scalar Wikibase::Datatype::Print::Item::print($obj, {
59 0           'lang' => $self->{'_opts'}->{'l'},
60             })), "\n";
61             } elsif (ref $obj eq 'Wikibase::Datatype::Lexeme') {
62             print encode_utf8(scalar Wikibase::Datatype::Print::Lexeme::print($obj, {
63 0           'lang' => $self->{'_opts'}->{'l'},
64             })), "\n";
65             } elsif (ref $obj eq 'Wikibase::Datatype::Property') {
66             print encode_utf8(scalar Wikibase::Datatype::Print::Property::print($obj, {
67 0           'lang' => $self->{'_opts'}->{'l'},
68             })), "\n";
69             } else {
70 0           print STDERR "Unsupported Wikibase::Datatype object.";
71 0           return 1;
72             }
73              
74 0           return 0;
75             }
76              
77             1;
78              
79             __END__
80              
81             =pod
82              
83             =encoding utf8
84              
85             =head1 NAME
86              
87             App::Wikidata::Print - Base class for Wikidata command line tool wd-print.
88              
89             =head1 SYNOPSIS
90              
91             use App::Wikidata::Print;
92              
93             my $app = App::Wikidata::Print->new;
94             my $exit_code = $app->run;
95              
96             =head1 METHODS
97              
98             =head2 C<new>
99              
100             my $app = App::Wikidata::Print->new;
101              
102             Constructor.
103              
104             Returns instance of object.
105              
106             =head2 C<run>
107              
108             my $exit_code = $app->run;
109              
110             Run.
111              
112             Returns 1 for error, 0 for success.
113              
114             =head1 EXAMPLE
115              
116             =for comment filename=print_p2668_in_czech.pl
117              
118             use strict;
119             use warnings;
120              
121             use App::Wikidata::Print;
122              
123             # Arguments.
124             @ARGV = (
125             '-l cs',
126             'P2668',
127             );
128              
129             # Run.
130             exit App::Wikidata::Print->new->run;
131              
132             # Output like:
133             # Data type: wikibase-item
134             # Label: proměnlivost hodnot (cs)
135             # Description: pravděpodobnost, že se prohlášení s touto vlastností změní (cs)
136             # Statements:
137             # P2302: Q21503250 (normal)
138             # P2308: Q18616576
139             # P2309: Q21503252
140             # P2316: Q21502408
141             # P2302: Q21510865 (normal)
142             # P2308: Q23611439
143             # P2309: Q21503252
144             # P2302: Q52004125 (normal)
145             # P2305: Q29934218
146             # P2302: Q53869507 (normal)
147             # P5314: Q54828448
148             # P2302: Q21503247 (normal)
149             # P2306: P2302
150             # P2302: Q21510859 (normal)
151             # P2305: Q23611288
152             # P2305: Q24025284
153             # P2305: Q23611840
154             # P2305: Q23611587
155             # P2305: unknown value
156             # P2668: Q24025284 (normal)
157             # P3254: https://www.wikidata.org/wiki/Wikidata:Property_proposal/Archive/48#P2668 (normal)
158             # P31: Q19820110 (normal)
159             # P2271: P569 (normal)
160             # P2668: Q23611288
161             # P2271: P1082 (normal)
162             # P2668: Q23611587
163             # P2271: P39 (normal)
164             # P2668: Q23611840
165             # P2271: P3185 (normal)
166             # P2668: Q24025284
167             # P2271: P11021 (normal)
168             # P2668: unknown value
169             # P1629: Q23611439 (normal)
170             # P2559: use only instances of Q23611439 as values (en) (normal)
171             # P2559: nur Instanzen von Q23611439 als Werte verwenden (de) (normal)
172             # P2559: utiliser uniquement les instances de Q23611439 comme valeurs (fr) (normal)
173             # P2559: usar solo instancias del elemento Q23611439 (es) (normal)
174             # P2559: у якасьці значэньняў ужывайце толькі сутнасьці элемэнту Q23611439 (be-tarask) (normal)
175             # P2559: bruk kun forekomster av Q23611439 som verdier (nb) (normal)
176             # P2559: 请只将性质为Q23611439(维基数据属性更改频率)的项作为值 (zh-hans) (normal)
177             # P2559: gebruik alleen items van Q23611439 als waarden (nl) (normal)
178             # P2559: usar só instancias do elemento Q23611439 (gl) (normal)
179             # P2559: jako hodnoty používejte pouze instance Q23611439 (cs) (normal)
180             # P2559: usare solo istanze di Q23611439 come valori (it) (normal)
181             # P2559: utilitzeu només les instàncies de Q23611439 com a valors (ca) (normal)
182              
183             =head1 DEPENDENCIES
184              
185             L<Class::Utils>,
186             L<Getopt::Std>,
187             L<Unicode::UTF8>,
188             L<Wikibase::API>,
189             L<Wikibase::Datatype::Print::Item>,
190             L<Wikibase::Datatype::Print::Lexeme>,
191             L<Wikibase::Datatype::Print::Property>.
192              
193             =head1 REPOSITORY
194              
195             L<https://github.com/michal-josef-spacek/App-Wikidata-Print>
196              
197             =head1 AUTHOR
198              
199             Michal Josef Špaček L<mailto:skim@cpan.org>
200              
201             L<http://skim.cz>
202              
203             =head1 LICENSE AND COPYRIGHT
204              
205             © 2021-2023 Michal Josef Špaček
206              
207             BSD 2-Clause License
208              
209             =head1 VERSION
210              
211             0.02
212              
213             =cut