Skip to content
 
 

Latest commit

 

History

218 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Wikidata-CLI

The Command-line interface interface to Wikidata

wikidata

NPM NPM

License Node JavaScript Style Guide

Summary


Dependencies

  • NodeJs (recommended way to install it: use the awesome NVM)

Installation

npm install -g wikidata-cli

Installing globally allows to make the command wd accessible from your shell $PATH


Commands

Read operations

wd label

Working with Wikidata, we often end up with obscure ids. We can always look-up those ids labels on the website but that means loading pages and pages, when a small API call and parsing could return just what we need: a label

wd label Q1103345
# => The Cluetrain Manifesto

Options:

  • -c, --clipboard: copy the result to clipboard
  • -l, --lang: specify the label's language

By default, the result uses your environment local language (process.env.LANG), but you can pass a 2-letters language code as second argument

wd label Q1103345 -l de
# => Cluetrain-Manifest
wd label Q123 -l zh
# => 9月

wd claims

A quick way to access the claims of an entity

# all Q2001's claims
wd claims Q2001
# or just his place of birth
wd claims Q2001 P19

Options:

  • -v, --verbose: make results more verbose
  • -l, --lang: specify the properties labels' language
wd claims Q2001 -l es
wd claims Q2001 P19 --lang ru

Options when passing both an id an property:

  • -c, --clipboard: copy the result to clipboard
  • -j, --json: format the result as JSON

wd coord

A command to output the geographic coordinates (latitude and longitude) of an entity

wd coord Q2112
# => 52.016666666667 8.5166666666667

wd data

A quick way to access an entity's raw JSON data

wd data Q1496

This simply outputs the result of https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=Q1496, parsed to keep only what is relevant to the requested entity (here Q1496). The output is valid json, so it lets you the possibility to pipe it to a JSON parser such as jsondepth:

wd data Q1496 | jd labels.pt
# => { language: 'pt', value: 'Fernão de Magalhães' }

wd id

This one is kind of the inverse of wd label: pass it the title of a Wikipedia article and it will return the corresponding Wikidata id

wd id Cantabria
# => Q3946
wd id New Delhi
# => Q987

Options:

  • -v, --verbose: make results more verbose
  • -c, --clipboard: copy the result to clipboard
  • -l, --lang: specify from which language the title comes By default, it will look at the Wikipedia corresponding to your environment local language (process.env.LANG), but you can specify another language by passing a 2-letters language code
wd id -l fr science politique
# => Q36442

You can also pass it full Wikipedia urls and let it deduce the language from there

wd id https://en.wikipedia.org/wiki/Friedrich_Nietzsche
# => Q9358

wd props

A command to access the list of all Wikidata properties in a given language (by default the environment local language)

Get the list of all Wikidata properties in your environment local language
wd props

Outputs a JSON object of the kind:

[...]
  "P2897": "identifiant Eldoblaje Movie",
  "P2898": null,
  "P2899": "âge minimal",
[...]

NB: properties without a label in the requested language are set to null, as you can see above for P2898 in French

This is especially convenient when you're looking for a property:

# look for a property dealing with images
wd props |grep image

Outputs:

{
  "P6": "head of government",
  "P10": "video",
  "P14": "graphic symbol of thoroughfare",
  "P15": "route map",
  "P16": "highway system",
  "P17": "country",
  "P18": "image",
[...]
Get the list of all Wikidata properties in another language

Option: -l, --lang: specify the properties labels language

wd props -l sv
# outputs the properties in Swedish

This command first tries to find the list in the props folder (created at wikidata-cli root), and request them to query.wikidata.org if missing.

This means that after a while, your local version will miss new and updated properties: this can be solved by using the --reset options

Option: -r, --reset: clear properties cache

Get the list of all Wikidata properties and their data types

Every property accepts values of a precise type, one of CommonsMedia, ExternalId, String, WikibaseItem, Time, GlobeCoordinate, Monolingualtext, Quantity, Url, WikibaseProperty, or Math

Option: -t, --type: include properties types

wd props --type

Outputs:

{
  "P6": { "type": "WikibaseItem", "label": "head of government" },
  "P10": { "type": "CommonsMedia", "label": "video" },
  "P14": { "type": "CommonsMedia", "label": "graphic symbol of thoroughfare" },
  "P15": { "type": "CommonsMedia", "label": "route map" },
  "P16": { "type": "WikibaseItem", "label": "highway system" },
  "P17": { "type": "WikibaseItem", "label": "country" },
  "P18": { "type": "CommonsMedia", "label": "image" },
[...]

wd sparql

A command to run a SPARQL query and get its JSON output

From this SPARQL query file: ./path/to/query.rq

SELECT ?work WHERE {
  ?work wdt:P50 wd:Q42 .
}

get its output from your terminal like so:

wd sparql ./path/to/query.rq > ./results.json

Options:

wd sparql ./path/to/query.rq > ./results.json
wd sparql -r ./path/to/query.rq > ./raw_sparql_results.json

wd query

A command to generate and run a simple SPARQL query, passing one or two of the elements that make a statement:

  • -s, --subject
  • -p, --property
  • -o, --object
# what is the entity id matching the twitter username (P2002) "timberners_lee"?
wd query --property P2002 --object timberners_lee
# which works have exoplanets (Q44559) for main subject (P921)?
wd query --property P921 --object Q44559 --labels
# or with the short options syntax
wd query -p P921 -o Q44559 -a

Other Options:

  • -r, --raw: output raw SPARQL results (instead of results simplified by wikidata-sdk simplifySparqlResults function
  • -a, --labels: requests results labels
  • -l, --lang <lang>: specify the labels' language
  • -t, --limit <num>: set the request results limit (defaults to 1000)
  • -v, --verbose: log the generated request

wd open

A command to open a pages on Wikidata in a browser from the command line (yep, you can be that lazy)

open entities and properties pages
wd open Q123
# opens https://wikidata.org/wiki/Q123 in your default browser

wd open P659
# opens https://www.wikidata.org/wiki/Property:P659

# also working with any string that matches /(Q|P)\d+/
wd open https://inventaire.io/entity/wd:Q33977
# opens https://wikidata.org/wiki/Q33977

Options:

  • -p, --wikipedia: open the Wikipedia article instead
wd open -p Q123
# opens https://fr.wikipedia.org/wiki/Septembre because my system language is French
  • -l, --lang: specify which Wikipedia edition should be targeted
wd open -p -l sv Q123
# opens https://sv.wikipedia.org/wiki/September instead
open a search page
wd open Dan Simmons
# opens https://www.wikidata.org/w/index.php?title=Special:Search&search=Dan%20Simmons

Write operations

Those command modify Wikidata so you will be asked your Wikidata username and password to use them. Those will be persisted in clear text in this module's folder: ./config.json. Alternatively, in the case writing to this module's folder would require special rights, the config file with your crendentials can be found in your home folder: ~/.config/wikidata-cli/config.json. This allows not having to re-enter crendentials everytimes, but it can problematic on a non-personal computer: in such a case, make sure to run wd config clear once you're done.

wd set-label

Set a label on an entity in a given language

wd set-label <entity> <language> <label>
# Alias:
wd sl <entity> <language> <label>

Example:

# Set the label 'Bac à sable bulgroz' to the Sandbox entity (Q4115189) in French
wd set-label Q4115189 fr "Bac à sable bulgroz"

wd add-claim

Add a claim to an entity

wd add-claim <entity> <property> <value>
# Alias:
wd ac <entity> <property> <value>

Example:

# Add the Twitter account (P2002) 'bulgroz' to the Sandbox (Q4115189) entity
wd add-claim Q4115189 P2002 bulgroz
# The same but using the command alias
wd ac Q4115189 P2002 bulgroz
# The same but passing a reference as 4th argument:
# Add the statement that the Sandbox (Q4115189) has for part (P527) the sand (Q34679)
wd ac Q4115189 P527 Q34679
with a reference

Simply add a 4th argument, either a reference URL (P854), or the id of the project it is imported from (P143)

# this will be interpreted as being imported from Wikipedia in Uyghur (Q60856)
wd ac Q4115189 P527 Q34679 Q60856

wd add-reference

Add a reference to an claim

wd add-reference <claim-guid> <URL or project entity id>
# Alias:
wd ar <claim-guid> <URL or project entity id>

Example:

# /!\ be ware of the '$' sign that might need escaping
wd add-reference "Q4115189\$E66DBC80-CCC1-4899-90D4-510C9922A04F" 'https://example.org/rise-and-box-of-the-holy-sand-box'
# or
wd add-reference 'Q4115189$E66DBC80-CCC1-4899-90D4-510C9922A04F' 'https://example.org/rise-and-box-of-the-holy-sand-box'

Config

Allows to persist options

wd config <key> [value]

read options

# output the current config and the help menu
wd config
# output the config value for the key 'clipboard'
wd config clipboard

write options

wd config clipboard true

options

clipboard

copy the result to clipboard, when the command offers this option (same as allows passing -c,--clipboard)

# Default: false
wd config clipboard true
json

format output as JSON when possible (same as allows passing -j,--json)

# Default: false
wd config json true
lang

set the prefered language (same as allows passing -l,--lang)

# Default: process.env.LANG.slice(0, 2)
wd config lang nl
verbose

set commands to verbose (same as allows passing -v,--verbose)

# Default: false
wd config verbose true

See Also

A javascript tool suite to query and work with Wikidata data, heavily used by wikidata-cli

Edit Wikidata from NodeJS, used in wikidata-cli for all write operations

A command-line tool to filter a Wikidata dump by claim

Tools to setup an ElasticSearch instance fed with subsets of Wikidata

A command-line tool to extract taxonomies from Wikidata


You may also like

inventaire banner

Do you know inventaire.io? It's a web app to share books with your friends, built on top of Wikidata! And its libre software too.

License

MIT

About

read and edit a Wikibase instance from the command line

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages