EntityStore and TermStore for Wikibase/Wikidata

I’m happy to announce the public release of two new PHP libraries that provide services around Wikibase, the software behind Wikidata. They are called QueryR EntityStore and QueryR TermStore.

Both these libraries provide persistence and lookup services for specific Wikibase data. These services are build on top of Doctrine DBAL, so support various databases, including in-memory SQLite.

QueryR EntityStore

This component has two database tables: one for storing items and one for storing blobs. Each has a blob field, plus some indexed fields to allow finding entities by various criteria. These tables are an internal implementation detail that is not exposed, and are just listed here to give you an idea of what can be done with the library.

All services are constructed via the EntityStoreFactory class:

$dbalConnection is a Connection object from Doctrine DBAL.

For writing values, you will need either ItemStore or PropertyStore.

The main write methods are “store document” and “remove document by id”.

Note that $itemRow is of type ItemRow, which is defined by this component. ItemRow represents all values in a row of the items table. It does not require having a fully instantiated Wikibase DataModel EntityDocument object, you just need the JSON.

Next to ItemRow there also is ItemInfo, which is identical, apart for not having the JSON. (Internally these share code via the package private trait ItemRowInfo.)

Here are some examples of how entities can be looked up. To get a full list, look at the services you can construct via the store, and their interfaces.

Fetching an Item by id:

Property data type lookup:

Get cheaply retrievable info on the first 100 items:

Restrict the result to items of type “book”, assuming 424242 is the numeric id of “book”:

QueryR TermStore

This component also has two database tables: one for labels and one for aliases. Again, these tables are an internal implementation detail that is not exposed, and are just listed here to give you an idea of what can be done with the library.

All services are constructed via the TermStoreFactory class:

Writing to the store:

Lookup up an EntityId based on terms:

(See the EntityIdLookup interface for all methods and their documentation.)

Lookup label based on EntityId and language:

(See the LabelLookup interface for documentation.)

Installation

QueryR EntityStore is available on Packagist as queryr/entity-store, and QueryR TermStore is available as queryr/term-store. They both support PHP 5.5 and later, including PHP 7 and HHVM. For more detailed instructions, including things such as release notes and how to run the tests, see their respective readme files.

Author

To avoid potential confusion, I’d like to explicitly state that Wikimedia Deutchland, my current employer, was not in any way involved in the development of these libraries. They have been written by me as a personal project.

Contributions in the form of pull requests or issue submission are welcome.

1 thought on “EntityStore and TermStore for Wikibase/Wikidata”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.