App-wdq

 view release on metacpan or  search on metacpan

script/wdq  view on Meta::CPAN

#!/usr/bin/env perl
use v5.14;

use Getopt::Long;
use Scalar::Util qw(blessed);
use Encode qw(decode_utf8 encode_utf8);
use JSON;

our $VERSION = '0.4.4';

# Assume everything outside is UTF-8
binmode( STDIN,  ":encoding(UTF-8)" );
binmode( STDOUT, ":encoding(UTF-8)" );
binmode( STDERR, ":encoding(UTF-8)" );
@ARGV = map { decode_utf8($_) } @ARGV;

# get command line options
my %OPT;
Getopt::Long::Configure('bundling');
GetOptions(
    \%OPT,
    'help|h|?', 'version|V', 'man', 'ontology',
    'api=s',
    'format|f=s',
    'query|q=s',
    'ids|i!',
    'language|g=s',
    'label|l=s@',
    'description|d=s@',
    'text|t=s@',
    'enumerate|e!',
    'color|C!',
    'monochrome|M',
    'count|c=s',
    'header!', 'H!',
    'no-execute|n!',
    'no-mediawiki|m!',
    'N!',
    'ignore!',
    'limit=i', '1!', '2!', '3!', '4!', '5!', '6!', '7!', '8!', '9!',
    'default-prefixes!',
    'response=s',    # not documented, does not respect --limit
    'export=s',
    'force!',
) or exit 1;

# use color by default if output is terminal
$OPT{color} = 0 if $OPT{monochrome};
$OPT{color_stderr} = $OPT{color} // -t STDERR;    ## no critic
$OPT{color} //= -t STDOUT;                        ## no critic

my %COLORS = (
    t => "\e[1;39m",                              # title : bold
    v => "\e[0;32m",                              # value: green
    n => "\e[0;34m",                              # name: blue
    i => "\e[0;33m",                              # identifier: yellow
    e => "\e[1;31m",                              # error: bold red
);

sub cBold {
    $OPT{color} ? "$COLORS{t}$_[0]\e[0m" : $_[0];
}

sub cValue {
    $OPT{color} ? "$COLORS{v}$_[0]\e[0m" : $_[0];
}

sub cName {
    $OPT{color} ? "$COLORS{n}$_[0]\e[0m" : $_[0];
}

sub cIdentifier {
    $OPT{color} ? "$COLORS{i}$_[0]\e[0m" : $_[0];
}

sub warning {
    say STDERR $OPT{color_stderr} ? "$COLORS{e}$_[0]\e[0m" : $_[0];
}

my %NAMESPACES = (

    # standard ontologies
    rdf    => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    xsd    => 'http://www.w3.org/2001/XMLSchema#',
    rdfs   => 'http://www.w3.org/2000/01/rdf-schema#',
    owl    => 'http://www.w3.org/2002/07/owl#',
    skos   => 'http://www.w3.org/2004/02/skos/core#',
    schema => 'http://schema.org/',
    geo    => 'http://www.opengis.net/ont/geosparql#',
    prov   => 'http://www.w3.org/ns/prov#',

    # Wikibase ontology
    wikibase => 'http://wikiba.se/ontology#',
    wd       => 'http://www.wikidata.org/entity/',
    wdt      => 'http://www.wikidata.org/prop/direct/',
    wds      => 'http://www.wikidata.org/entity/statement/',
    p        => 'http://www.wikidata.org/prop/',
    wdref    => 'http://www.wikidata.org/reference/',
    wdv      => 'http://www.wikidata.org/value/',
    ps       => 'http://www.wikidata.org/prop/statement/',
    psv      => 'http://www.wikidata.org/prop/statement/value/',
    pq       => 'http://www.wikidata.org/prop/qualifier/',
    pqv      => 'http://www.wikidata.org/prop/qualifier/value/',
    pr       => 'http://www.wikidata.org/prop/reference/',
    prv      => 'http://www.wikidata.org/prop/reference/value/',
    wdno     => 'http://www.wikidata.org/prop/novalue/',

    # blazegraph SPARQL extensions
    hint => 'http://www.bigdata.com/queryHints#',
    bd   => 'http://www.bigdata.com/rdf#',
    bds  => 'http://www.bigdata.com/rdf/search#',
    fts  => 'http://www.bigdata.com/rdf/fts#',

    # not used in Wikidata Query Service
    wdata => 'http://www.wikidata.org/wiki/Special:EntityData/',
    cc    => 'http://creativecommons.org/ns#',
);



( run in 0.410 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )