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:
1 2 3 4 5 |
use Queryr\EntityStore\EntityStoreFactory; $factory = new EntityStoreFactory( $dbalConnection, new EntityStoreConfig( /* optional config */ ) ); |
$dbalConnection is a Connection object from Doctrine DBAL.
For writing values, you will need either ItemStore or PropertyStore.
1 2 |
$itemStore = $factory->newItemStore(); $propertyStore = $factory->newPropertyStore(); |
The main write methods are “store document” and “remove document by id”.
1 2 |
$itemStore->storeItemRow( $itemRow ); $itemStore->deleteItemById( $itemId ); |
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:
1 |
$q42 = $itemStore->getItemRowByNumericItemId( 42 ); |
Property data type lookup:
1 2 |
$lookup = $factory->newPropertyTypeLookup(); $propertyType = $lookup->getTypeOfProperty( $propertyId ); |
Get cheaply retrievable info on the first 100 items:
1 |
$itemInfoList = $itemStore->getItemInfo( 100, 0 ); |
Restrict the result to items of type “book”, assuming 424242 is the numeric id of “book”:
1 |
$itemInfoList = $itemStore->getItemInfo( 100, 0, 424242 ); |
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:
1 2 3 4 5 |
use Queryr\TermStore\TermStoreFactory; $factory = new TermStoreFactory( $dbalConnection, new TermStoreConfig( /* optional config */ ) ); |
Writing to the store:
1 2 3 4 |
$writer = $factory->newTermStoreWriter(); $writer->storeEntityFingerprint( $entityId, $fingerprint ); $writer->dropTermsForId( $entityId ); |
Lookup up an EntityId based on terms:
1 2 3 4 5 |
$idLookup = $factory->newEntityIdLookup(); $idLookup->getItemIdByLabel( $languageCode, $labelText ); $idLookup->getItemIdByText( $languageCode, $termText ); $idLookup->getIdByLabel( $languageCode, $labelText ); |
(See the EntityIdLookup interface for all methods and their documentation.)
Lookup label based on EntityId and language:
1 2 |
$labelLookup = $factory->newLabelLookup(); $labelLookup->getLabelByIdAndLanguage( $entityId, $languageCode ); |
(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”