1. Go to this page and download the library: Download ppp/wikibase-entity-store library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ppp / wikibase-entity-store example snippets
$storeBuilder = new EntityStoreFromConfigurationBuilder();
$store = $storeBuilder->buildEntityStore( 'MY_CONFIG_FILE.json' ); //See backend section for examples of configuration file
//Retrieves the item Q1
try {
$item = $store->getItemLookup()->getItemForId( new ItemId( 'Q1' ) );
} catch( ItemNotFoundException $e ) {
//Item not found
}
//Retrieves the property P1
try {
$item = $store->getPropertyLookup()->getPropertyForId( new PropertyId( 'P1' ) );
} catch( PropertyNotFoundException $e ) {
//Property not found
}
//Retrieves the item Q1 as EntityDocument
try {
$item = $store->getEntityLookup()->getEntityDocumentForId( new ItemId( 'Q1' ) );
} catch( EntityNotFoundException $e ) {
//Property not found
}
//Retrieves the item Q1 and the property P1 as EntityDocuments
$entities = $store->getEntityLookup()->getEntityDocumentsForIds( array( new ItemId( 'Q1' ), new PropertyId( 'P1' ) ) );
//Retrieves the ids of the items that have as label or alias the term "Nyan Cat" in English (with a case insensitive compare)
$itemIds = $store->getItemIdForTermLookup()->getItemIdsForTerm( new Term( 'en', 'Nyan Cat' ) );
//Retrieves the ids of the properties that have as label or alias the term "foo" in French (with a case insensitive compare)
$propertyIds = $store->getPropertyIdForTermLookup()->getPropertyIdsForTerm( new Term( 'fr', 'Foo' ) );
//Do a query on items using the Ask query language: retrieves the first 10 items with P1: Q1
$itemIds = $store->getItemIdForQueryLookup()->getItemIdsForQuery(
new Query(
new SomeProperty(
new EntityIdValue( new PropertyId( 'P1' ) ),
new ValueDescription( new EntityIdValue( new ItemId( 'Q1' ) ) )
),
array(),
new QueryOptions( 10, 0 )
)
);
$store = new \Wikibase\EntityStore\Api\ApiEntityStore(
new \Mediawiki\Api\MediawikiApi( 'http://www.wikidata.org/w/api.php' ),
new \WikidataQueryApi\WikidataQueryApi( 'http://wdq.wmflabs.org/api' )
);
//Connect to MongoDB
$connection = new \Doctrine\MongoDB\Connection( MY_CONNECTION_STRING );
if( !$connection->connect() ) {
throw new RuntimeException( 'Fail to connect to the database' );
}
//Gets the database where entities are stored
$database = $connection
->selectDatabase( 'wikibase' );
$store = new \Wikibase\EntityStore\MongoDB\MongDBEntityStore( $database );
$store = new \Wikibase\EntityStore\InMemory\InMemoryEntityStore( array(
new Item( new ItemId( 'Q42' ) )
) );
$options = new \Wikibase\EntityStore\EntityStoreOptions( array(
EntityStore::OPTION_LANGUAGES => array( 'en', 'fr' ),
EntityStore::OPTION_LANGUAGE_FALLBACK => true
) );
$store = new \Wikibase\EntityStore\Api\ApiEntityStore(
new \Mediawiki\Api\MediawikiApi( 'http://www.wikidata.org/w/api.php' ),
null,
$options
);
$store = MY_ENTITY_STORE;
$cache = new \Doctrine\Common\Cache\ArrayCache(); //A very simple cache
$cacheLifeTime = 100000; //Life time of cache in seconds
$cachedStore = new \Wikibase\EntityStore\Cache\CachedEntityStore( $store, $cache, $cacheLifeTime );