App-wdq

 view release on metacpan or  search on metacpan

script/wdq  view on Meta::CPAN

my $SITELINK_PATTERN = qr{^
    https?://
    (?<base>
      ( [^.]+. ( wikipedia | wiktionary | wikibooks | wikiquote |
                wikisource | wikinews | wikiversity | wikivoyage )
      | ( commons | species )\.wikimedia
    )\.org)
    /wiki/
    (?<title>.+)
}x;

sub pod_text {
    require Pod::Usage;
    my $text;
    open my $out, '>', \$text;
    Pod::Usage::pod2usage(
        -exit    => 'NOEXIT',
        -output  => $out,
        -verbose => 99,
        indent   => 2,
        @_
    );
    $text =~ s/^(.[^\s].+:)$/cBold($1)/mge;               # headers
    $text =~ s/("[^"\n]+")/cValue($1)/mge;                # strings
    $text =~ s/(<[^->\n][^>\n]+>)/cIdentifier($1)/mge;    # URLs
    $text;
}

# help mode
if ( $OPT{help} ) {
    @ARGV = qw(help);
}
elsif ( $OPT{version} ) {
    @ARGV = qw(help version);
}
elsif ( $OPT{namespaces} ) {
    warning("--namespaces is deprecated. Use 'wdq help prefixes' instead!");
    @ARGV = qw(help prefixes);
}
elsif ( $OPT{ontology} ) {
    warning("--ontology is deprecated. Use 'wdq help ontology' instead!");
    @ARGV = qw(help ontology);
}

if ( @ARGV and $ARGV[0] eq 'help' ) {
    shift @ARGV;
    my $topic = lc( shift @ARGV );

    my $help;
    if ( $topic =~ /^ver(sion)?$/ ) {
        $help = cBold('wdq') . " $VERSION\n";
    }
    elsif ( $topic =~ /^pref?(ix(es)?)?$/ ) {
        $help = join( "\n",
            map { sprintf "%8s: %s", $_, cIdentifier( $NAMESPACES{$_} ); }
            sort keys %NAMESPACES )
          . "\n";
        $help =~ s/^([^:]+):/cName($1).":"/mge;
    }
    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;

script/wdq  view on Meta::CPAN

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

=item C<csv>

SPARQL Query Results CSV Format. Suppress header with option
C<--no-header>/C<-H>.  Use Catmandu CSV exporter for more options

=item C<tsv>

SPARQL Query Results TSV Format

=item C<xml>

SPARQL Query Results XML Format

=item C<json>

SPARQL Query Results JSON Format

=item C<...>

String template.  Call C<wdq help pretty> for details

=back

=head2 Pretty

Option C<--format> can be set to a string template with bracket expressions
with optional template parameters (for instance C<{id|pre= (|post=)}>).

=over

=item style

Highlight C<n> name, C<v> value, C<i> identifier, C<t> title, or C<e> error

=item length

Abbreviate long values



( run in 0.569 second using v1.01-cache-2.11-cpan-ceb78f64989 )