Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

wikibase-sdk

maxlath37.8kMIT10.2.3TypeScript support: included

utils functions to query a Wikibase instance and simplify its results

wikidata, wikibase, sdk, api, wdq, sparql, wbk

readme

wikibase-sdk

JS utils functions to query a Wikibase instance and simplify its results

This package was primarily developed as wikidata-sdk but has now being generalized to support any Wikibase instance, wikidata.org among others, and was thus renamed wikibase-sdk.

This project received a Wikimedia Project Grant.

wikibase           wikidata

License npm version Required Node.js version npm downloads code style type definitions

Summary

Changelog

See CHANGELOG.md for version info

Dependencies

NodeJS >= v12.0.0 or not too outdated web browsers (see Object.fromEntries browser compatibility table) For older JS runtimes, you can use ES5 bundles from wikibase-sdk <= v8.

Install

as an ES module

Install via npm to be able to use the import the module.

npm install wikibase-sdk

Then in your javascript:

import { WBK } from 'wikibase-sdk'
const wbk = WBK({
  instance: 'https://my-wikibase-instan.se',
  sparqlEndpoint: 'https://query.my-wikibase-instan.se/sparql' // Required to use `sparqlQuery` and `getReverseClaims` functions, optional otherwise
})

The wdk object of previous versions of this documentation - from the time this module was bound to wikidata.org only - thus corresponds to the following:

import { WBK } from 'wikibase-sdk'

const wdk = WBK({
  instance: 'https://www.wikidata.org',
  sparqlEndpoint: 'https://query.wikidata.org/sparql'
})

For convenience, and for the sake of retro-compatibility, that same wdk object can be obtain directly from the wikibase-sdk/wikidata.org package:

import wdk from 'wikibase-sdk/wikidata.org'

By default wikibase-sdk assumes that your Wikibase instance has $wgScriptPath set to /w, but if that's not the case, you can set it by passing a wgScriptPath parameter:

import { WBK } from 'wikibase-sdk'
const wbk = WBK({
  instance: 'https://my-wikibase-instan.se',
  wgScriptPath: '/some_custom_script_path'
})

as an CommonJS module

Importing with CommonJS require is not supported anymore in version >= v9.0.0, but can still be done by installing an older version:

npm install wikibase-sdk@v8

See the corresponding version documentation

download pre-bundled files

Pre-bundled files is not supported anymore in version >= v9.0.0, but can still be done by pre-bundled files from older versions:

wget https://raw.githubusercontent.com/maxlath/wikibase-sdk/v8.1.1/dist/wikibase-sdk.js
wget https://raw.githubusercontent.com/maxlath/wikibase-sdk/v8.1.1/dist/wikidata-sdk.js
wget https://raw.githubusercontent.com/maxlath/wikibase-sdk/v8.1.1/dist/wikibase-sdk.min.js
wget https://raw.githubusercontent.com/maxlath/wikibase-sdk/v8.1.1/dist/wikidata-sdk.min.js

See the corresponding version documentation

Features

Wikibase API

A set of functions to make read queries to a Wikibase instance API (see Wikidata API documentation). For write operations, see wikibase-edit.

Wikibase Query

There are additional functions for Wikibase instances that have a SPARQL Query Service (such as Wikidata Query for wikidata.org). SPARQL can be a weird thing at first, but the Wikidata team and community really puts lots of efforts to make things easy with a super rich Wikidata Query Help page, an awesome tool to test you queries and visualize the result, and lots of examples!

General helpers

Contributing

Context

This library had for primary purpose to serve the needs of the inventaire project but extending its capabilities to other needs it totally possible: feel welcome to post your suggestions as issues or pull requests!

Design constraints

  • wikibase-sdk should stay "small" and dependency-free, so that a web application can include it in its bundle without paying a too high cost for it. A consequence is that the lib generates URLs where other libs would integrate doing the request and parsing it's response. But that actually feels quite right to do this way: simply generating the URLs let's users free to handle requests as they like (with callbacks, promises, async/await, custom request agent, whatever!)
  • Therefore, it should focus on providing basic, general helper functions most application working with a Wikibase instance would need.
  • Write operations should go into wikibase-edit as it involves working with Wikibase credentials/tokens.
  • General command-line interface tools should go to wikibase-cli, very specific ones — wikibase-dump-filter and alikes — should get their own modules.

See Also

You may also like

inventaire banner

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

License

MIT

changelog

CHANGELOG

versions follow SemVer

10.2.0 - 2024-10-06

10.1.0 - 2024-08-04

10.0.0 - 2024-03-14

BREAKING CHANGES:

  • remove the minimize option from simplifySparqlResults: that behavior is now handled by minimizeSimplifiedSparqlResults

9.2.0 - 2023-07-17

9.1.0 - 2023-04-18

  • export the wikimediaLanguageCodes array

9.0.0 - 2023-02-04

BREAKING CHANGES:

  • wikibase-sdk now uses ES modules: to keep using CommonJS, you will need to use wikibase-sdk <= v9
  • Some functions that were accepting arguments as several arguments were changed to accept arguments as an object only. The concerned functions are:
    • getReverseClaims: getReverseClaims('P50', 'Q535') should be replaced by getReverseClaims({ properties: 'P50', values: 'Q535' })
    • getRevisions: getRevisions('Q3548931', { limit: 10 }) should be replaced by getRevisions({ ids: 'Q3548931', { limit: 10 })
  • Functions that were accepting arguments either as several arguments or as an object now only accept arguments as an object. The concerned functions are:
  • simplify.claims, simplify.propertyClaims, and simplify.claim legacy interfaces were dropped: options are now expected in a single option object (rather than as several arguments)
  • Functions previously avaialble on wbk.parsers.wb and wbk.parsers.wd — namely entities and pagesTitles — are now directly on wbk.parsers

Other changes:

  • The source code was converted to TypeScript to recover types declarations, and keep them maintained in the future. This change was taken as an opportunity to do a bit of cleanup in the function interfaces.
  • The convenience module wikidata-sdk has been deprecated. The wdk object can now be imported from wikibase-sdk/wikidata.org: import wdk from 'wikibase-sdk/wikidata.org'

8.1.0 - 2022-10-31

8.0.0 - 2022-05-03

BREAKING CHANGE: use URLSearchParams in NodeJS, which only is available in NodeJS >= v10.0.0

7.15.0 - 2021-04-10

7.14.0 - 2021-10-18

7.13.0 - 2021-06-01

  • Added wgScriptPath to the initialization parameters

7.12.0 - 2021-05-01

7.11.0 - 2021-04-10

7.10.0 - 2021-04-10

7.9.0 - 2021-01-29

7.8.0 - 2020-10-07

  • Added function aliases:
    • simplify.snak = simplify.claim
    • simplify.propertySnaks = simplify.propertyClaims
    • simplify.snaks = simplify.claims

7.7.0 - 2020-07-15

7.6.0 - 2020-06-09

7.5.0 - 2020-06-08

7.4.0 - 2020-04-17

7.3.0 - 2020-04-08

  • getRevisions: added support for rvprops, rvuser, rvexcludeuser, and rvtag parameters

7.2.0 - 2019-12-06

  • Make getSitelinkData accept full article URLs and return { lang, project, key, title, url }

7.1.0 - 2019-09-08

  • Expose parsed instance URLs: wbk.instance

7.0.0 - 2019-05-31

Breaking Changes:

  • The module initialization was restructured to support Wikibase instances other than wikidata.org. As a consequence, while require('wikibase-sdk') still produces the same object as before, require('wikibase-sdk') returns a function that takes a config object, which then returns the equivalent of the wdk object but customized for the desired Wikibase instance (see import documentation).
  • Dropping bower support as it has been deprecated for a while now: old versions of wikibase-sdk should still be available though
  • Renamed:
    • wikidataTimeToEpochTime => wikibaseTimeToEpochTime
    • wikidataTimeToISOString => wikibaseTimeToISOString
    • wikidataTimeToSimpleDay => wikibaseTimeToSimpleDay
  • Removed legacy aliases:
    • simplifyClaim (use simplify.claim)
    • simplifyPropertyClaims (use simplify.propertyClaims)
    • simplifyClaims (use simplify.claims)
    • simplifySparqlResults (use simplify.sparqlResults)
    • getWikidataIdsFromWikipediaTitles (use getEntitiesFromSitelinks)
    • getWikidataIdsFromSitelinks (use getEntitiesFromSitelinks)

6.7.0 - 2019-05-26

6.6.0 - 2019-05-17

6.5.0 - 2019-05-17

6.4.0 - 2019-04-16

searchEntities: allow to set a value to the continue parameter

6.3.0 - 2019-04-11

  • simplify.claims: Add support for musical-notation datatype

6.2.0 - 2019-04-03

6.1.0 - 2019-03-28

6.0.0 - 2019-03-17

BREAKING CHANGE

  • swapping simplify.qualifier and simplify.qualifiers that were mistakenly inverted (fixed #47, ed0e7a4)
  • Make simplifySparqlResults consistently outputs an array of objects, unless the option minimize is set to true, in which case direct, requests with a single variable will return an array of those variables values ((ddba9e2)). The easy migration from v5.x.x is to replace simplifySparqlResults(results) by simplifySparqlResults(results, { minimize: true }) (This doesn't affect requests with several variables SELECTed)

5.16.0 - 2019-03-13

5.15.0 - 2018-07-21

5.14.0 - 2018-07-14

5.13.0 - 2018-07-06

5.12.0 - 2018-06-27

5.11.0 - 2018-05-19

5.10.0 - 2018-05-14

5.9.0 - 2018-02-26

5.8.0 - 2018-01-27

5.7.0 - 2017-12-20

5.6.0 - 2017-12-14

5.5.0 - 2017-12-04

5.4.0 - 2017-11-30

5.3.0 - 2017-11-09

5.2.0 - 2017-06-10

5.1.0 - 2017-03-31

5.0.0 - 2017-03-28

Breaking Changes:

  • (00e3c81) Fixed naming issues:
    • renamed isWikidataId => isEntityId
    • renamed isWikidataEntityId => isItemId
    • renamed isWikidataPropertyId => isPropertyId
  • (df4301a) Removed helpers that were only needed to work with the now-offline WDQ service that is, all helpers dealing with entities ids in there numeric id form: normalizeId, normalizeIds, isNumericId, getNumericId (those last removal were reverted by (2b5020e))
  • (a5ae25f) Removed normalizeWikidataTime alias: use one of wikidataTimeToDateObject, wikidataTimeToEpochTime, wikidataTimeToISOString instead
  • (e2158f9) simplifyClaims functions now use wikidataTimeToISOString as time normalizer instead of wikidataTimeToEpochTime

4.3.0 - 2017-03-28

4.2.0 - 2017-01-02

4.1.0 - 2016-11-08

4.0.0 - 2016-09-09

  • Completly removed uses and references to wdq:
    • BREAKING CHANGE: getReverseClaims now returns a query to the SPARQL endpoint. Its result should thus be parsed with simplifySparqlResults instead of wdk.parse.entities. This parser was thus removed.

3.2.0 - 2016-06-06

3.1.0 - 2016-06-02

3.0.0 - 2016-05-13

  • Extracting "bin" executables (qlabel, wikiqid) to there own modules: wikidata-cli. Bumping the major version as it could be considered a breaking change (despite the fact that those executables weren't really part of wikibase-sdk and that they were added very recently)

2.6.0 - 2016-04-28

2.5.0 - 2016-04-18

  • Added support for the uselang parameter in entities search

2.4.0 - 2016-04-15

2.3.0 - 2016-02-17