App-wdq
view release on metacpan or search on metacpan
elsif ( $topic =~ /^out(put)?$/ ) {
$help = pod_text( -sections => ['OUTPUT'] );
$help =~
s/( (name|value|identifier|title|error))/$COLORS{substr $2, 0,1}.$1."\e[0m"/mge
if $OPT{color};
}
elsif ( $topic eq 'pretty' ) {
$help = pod_text( -sections => ['OUTPUT/Pretty'] );
$help =~
s/( (name|value|identifier|title|error))/$COLORS{substr $2, 0,1}.$1."\e[0m"/mge
if $OPT{color};
}
elsif ( $topic =~ /^formats?$/ ) {
$help = pod_text( -sections => ['OUTPUT/Formats'] );
}
elsif ( $topic =~ /^modes?$/ ) {
$help = pod_text( -sections => ['MODES'] );
}
elsif ( $topic =~ /^exp?(ort)?$/ ) {
$help = pod_text( -sections => ['OUTPUT/Export'] );
}
elsif ( $topic =~ /^ont(ology)?$/ ) {
$help = pod_text( -sections => ['WIKIDATA ONTOLOGY'] );
$help =~ s/^.*Wikidata Ontology:.*\n//;
$help =~ s/^( | )//mg;
$help =~ s/^([a-z]+)/cBold($1)/mgei;
$help =~ s/ ([A-Z][A-Za-z]+)/" ".cBold($1)/mge;
$help =~ s/ ([a-z]+:([a-zA-Z_]+)?|[a-z][a-zA-Z]+)/" ".cName($1)/mge;
$help =~ s/(@[a-z_]+)/cIdentifier($1)/mge;
}
elsif ( $topic =~ /^opt(ions?)?$/ ) {
$help = pod_text( -sections => ['OPTIONS'] );
$help =~ s/^( *--?[^ ]+( [^ ].*)?)$/cName($1)/mge; # options
$help =~ s/^\n//gm; # remove empty lines
}
else {
$help = pod_text(
-msg => 'wdq' . ' [ '
. join( ' ] [ ',
cName('MODE'), cName('OPTIONS'),
cName('REQUEST') . ' | ' . cName('< REQUEST_FILE') )
. " ]\n",
-sections => ['SYNOPSIS'],
);
$help =~ s/\n\n -/\n -/gm;
$help =~ s/^ / /mg;
}
print $help;
exit;
}
if ( $OPT{man} ) {
my $module = $OPT{color} ? 'Pod::Text::Color' : 'Pod::Text';
# may fail if pure script installed by hand
eval "require $module; require App::wdq"; ## no critic
$module->new->parse_from_file( $INC{'App/wdq.pm'} // $0 );
exit;
}
# default SPARQL endpoint
$OPT{api} //= 'https://query.wikidata.org/bigdata/namespace/wdq/sparql';
# add default prefixes by default
$OPT{'default-prefixes'} //= 1;
# include header in output
$OPT{header} //= $OPT{H} ? 0 : 1;
# limit given as single digit option
foreach ( grep { $OPT{$_} } 1 .. 9 ) {
$OPT{limit} = $_ if !$OPT{limit} or $OPT{limit} > $_;
}
# validate language and set default value if missing
$OPT{language} //= do { my $l = $ENV{LANG} // 'en'; $l =~ s/_.*//; $l };
$OPT{language} = lc( $OPT{language} );
if ( grep { $_ !~ $LANGUAGE_PATTERN } split ",", $OPT{language} ) {
warning("invalid language(s): $OPT{language}");
exit 1;
}
# disable all requests
if ( $OPT{N} ) {
$OPT{'no-mediawiki'} = 1;
$OPT{'no-execute'} = 1;
}
my $MODE = !@ARGV ? 'query' : do {
my $arg = $ARGV[0];
$arg =~ s/^\s*|\s*$//g;
if ( $arg =~ /^(query|lookup|p?search)$/ ) {
lc( shift @ARGV );
}
elsif ( $arg =~ $ENTITY_PATTERN or $arg =~ $SITELINK_PATTERN ) {
'lookup';
}
else {
my $guess = () = $arg =~ /[a-z]+:[^\s]/gi;
$guess += () = $arg =~ /<[^>]+>/g;
$guess += () = $arg =~ /[?\$][^\s]/g;
if ( $guess > 2 ) {
warning("ignoring additional command line argument")
if $OPT{query} or @ARGV > 1;
'query';
}
else {
'search';
}
}
};
# default output format
if ( $OPT{format} =~ /{[^}]+}/ ) {
$OPT{pretty} = $OPT{format};
$OPT{format} = 'text';
}
else {
$OPT{format} =
lc( $OPT{format} // ( $MODE =~ /^p?search$/ ? 'text' : 'simple' ) );
if ( $OPT{format} eq 'text' ) {
=over
=item --query|-q QUERY
Query or query file (C<-> for STDIN as default)
=item --format|-f FORMAT|TEMPLATE
Output format or string template. Call C<wdq help formats> for details.
=item --export EXPORTER
Use a L<Catmandu> exporter as output format.
=item --no-header|-H
Exclude header in CSV output or other exporter.
=item --enumerate|-e
Enumerate results by adding a counter variable C<n>
=item --limit INTEGER
Add or override a LIMIT clause to limitate the number of results. Single-digit
options such as C<-1> can also be used to also set a limit.
=item --ids|-i
Abbreviate Wikidata identifier URIs as strings.
=item --language|-g
Language to query labels and descriptions in. Set to the locale by default.
This option is currentl only used on lookup mode.
=item --count|-c VARS
Prepend SPARQL QUERY to count distinct values
=item --label|-l VARS
=item --description|-d VARS
=item --text|-t VARS
Add label, description, or both. Adds C<label>/C<description> for variable C<id>
or C<xLabel>/C<xDescription> for any C<x>.
=item --ignore
Ignore empty results instead of issuing warning and exit code.
=item --color|-C
By default output is colored if writing to a terminal. Disable this with
C<--no-color>, C<--monochrome>, or C<-M>. Force color with C<--color> or C<-C>.
=item --api URL
SPARQL endpoint. Default value:
C<https://query.wikidata.org/bigdata/namespace/wdq/sparql>
=item --no-mediawiki|-m
Don't query MediaWiki API to map URLs to Wikidata items.
=item --no-execute|-n
Don't execute SPARQL queries but show them in expanded form. Useful to
validate and pretty-print queries. MediaWiki API requests may be
=item -N
Don't execute any queries. Same as C<--no-mediawiki --no-execute>.
=item --help|-h|-?
Show usage help
=item --ontology
Show information about the Wikidata Ontology
=item --no-default-prefixes
Don't add default namespace prefixes to the SPARQL query
=item --man
Show detailled manual
=item --version|-V
Show version if this script
=back
=head1 OUTPUT
Output can be controlled with options C<--format>/C<-f>, C<--export>,
C<--header>/C<--no-header>/C<-H>, and C<--color>/C<--no-color>/C<-C>.
=head2 Formats
Option C<--format>/C<-f> sets an output format or string template:
=over
=item C<simple> (default in query and lookup mode)
Flat JSON without language tags
=item C<text> (default in search mode)
Print C<label>, C<alias>, C<id> and C<description> or C<count> when counting.
Also sets option C<--ids>.
=item C<ldjson>
Line delimited Flat JSON
( run in 1.322 second using v1.01-cache-2.11-cpan-63c85eba8c4 )