The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

wdq - command line access to Wikidata Query Service

USAGE

Access Wikidata Query Service via command line. In query mode (default), a SPARQL query is read from STDIN or option --query. Default namespaces are and SELECT clause are added if missing. In lookup mode, Wikidata entity ids, URLs, or Wikimedia project URLs are read from STDIN or as command line arguments to look up label and description.

EXAMPLES

  # get all parts of the solar system
  wdq -q '?c wdt:P361 wd:Q544'
  wdq -q '{ ?c wdt:P361 wd:Q544 }'                # equivalent
  wdq -q 'SELECT * WHERE { ?c wdt:P361 wd:Q544 }' # equivalent

  # look up label and description
  wdq Q1
  wdq lookup Q1             # equivalent
  echo Q1 | wdq lookup      # equivalent

OPTIONS

--query|-q QUERY

Query or query file (- for STDIN as default)

--format|-f FORMAT|TEMPLATE

Output format or string template. Call wdq help formats for details.

--export EXPORTER

Use a Catmandu exporter as output format.

--no-header|-H

Exclude header in CSV output or other exporter.

--limit INTEGER

Add or override a LIMIT clause to limitate the number of results. Single-digit options such as -1 can also be used to also set a limit.

--ids|-i

Abbreviate Wikidata identifier URIs as strings (except output format xml).

--language|-g

Language to query labels and descriptions in. Set to the locale by default. This option is currentl only used on lookup mode.

--ignore

Ignore empty results instead of issuing warning and exit code.

--color|-C

By default output is colored if writing to a terminal. Disable this with --no-color or force color with --color or -C.

--api URL

SPARQL endpoint. Default value: https://query.wikidata.org/bigdata/namespace/wdq/sparql

--no-mediawiki|-m

Don't query MediaWiki API to map URLs to Wikidata items.

--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

-N

Don't execute any queries. Same as --no-mediawiki --no-execute.

--help|-h|-?

Show usage help

--ontology

Show information about the Wikidata Ontology

--no-default-prefixes

Don't add default namespace prefixes to the SPARQL query

--man

Show detailled manual

--version|-V

Show version if this script

OUTPUT

Output can be controlled with options --format, --export, --header/--no-header, and --color/--no-color.

Formats

Option --format (or -f) sets an output format or string template:

simple (default)

Flat JSON without language tags (default)

ldjson

Line delimited Flat JSON

csv

SPARQL Query Results CSV Format. Suppress header with option --no-header or -H. Use Catmandu CSV exporter for more options

tsv

SPARQL Query Results TSV Format

xml

SPARQL Query Results XML Format

json

SPARQL Query Results JSON Format

pretty

Default string template to print label, id, and description. Also sets option --ids unless disabled

...

String template. Call wdq help pretty for details

Pretty

Option --format can be set to a string template with bracket expressions (for instance {id} is replaced by the value of variable id). The variable name can be preceded by a color code and a length:

  {n~foo} : highlight value of foo like a name
  {v~foo} : highlight value of foo like a value
  {i~foo} : highlight value of foo like an identifier
  {t~foo} : highlight value of foo like a title
  {e~foo} : highlight value of foo like an error

  {4foo}  : cut value of foo to 4 characters or less
  {>4foo} : cut/fill value of foo to 4 characters, right-aligned
  {<4foo} : cut/fill value of foo to 4 characters, left-aligned

  {v~<8foo} : set both, color and length

Export

Option --export sets a Catmandu exporter to create output with. Given the corresponding exporter modules installed, one can write results as YAML, Excel (XLS), and Markdown table (Table) among other formats:

  wdq --export YAML                               # short form
  wdq --format ldjson | catmandu convert to YAML  # equivalent

Use Catmandu config file (catmandu.yml) to further configure export. See also tools such as jq and miller for processing results.

WIKIDATA ONTOLOGY

  Entity (item/property)
   wd:Q_ <-- owl:sameAs --> wd:Q_
         --> rdfs:label, skos:altLabel, schema:description "_"@_
         --> schema:dateModified, schema:version
         --> wdt:P_ "_", URI, _:blank
         --> p:P_ Statement

  Item
   wd:Q_ <-- schema:about <http://_.wikipedia.org/wiki/_>
                          --> schema:inLanguage, wikibase:badge

  Property
   wd:P_ --> wikibase:propertyType PropertyType
         --> wkibase:directClaim        wdt:P_
         --> wikibase:claim             p:P_
         --> wikibase:statementProperty ps:P_
         --> wikibase:statementValue    psv:P_
         --> wikibase:qualifier         pq:P_
         --> wikibase:qualifierValue    pqv:P_
         --> wikibase:reference         pr:P_
         --> wikibase:referenceValue    prv:P_
         --> wikibase:novalue           wdno:P_

  PropertyType
   wikibase: String, Url, WikibaseItem, WikibaseProperty, CommonsMedia,
             Monolingualtext, GlobeCoordinate, Quantity, Time


  Statement
   wds:_ --> wikibase:rank Rank
         --> a wdno:P_
         --> ps:P_ "_", URI, _:blank
         --> psv:P_ Value
         --> pq:P_ "_", URI, _:blank
         --> pqv:P_ Value
         --> prov:wasDerivedFrom Reference

  Reference
   wdref:_ --> pr:P_ "_", URI
           --> prv:P_ Value

  Rank
   wikibase: NormalRank, PreferredRank, DeprecatedRank, BestRank

  Value (GlobecoordinateValue/QuantityValue/TimeValue)
   wdv:_ --> wikibase: geoLatitude, geoLongitude, geoPrecision, geoGlobe URI
         --> wikibase: timeValue, timePrecision, timeTimezone, timeCalendarModel
         --> wikibase: quantityAmount, quantityUpperBound, quantityLowerBound,
                       quantityUnit URI

COPYRIGHT AND LICENSE

Copyright by Jakob Voss voss@gbv.de

Based on a PHP script by Marius Hoch hoo@online.de at https://github.com/mariushoch/asparagus.

Licensed under GPL 2.0+