ppp/wikibase-entity-store

存储 Wikibase 实体的小型库

0.3.1 2016-07-08 11:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:37:27 UTC


README

Build Status Code Coverage Scrutinizer Code Quality Dependency Status

Packagist上:最新稳定版本 下载量

WikibaseEntityStore 是一个小型库,提供与 Wikibase 实体交互的统一接口。

目前有两个后端

  • 基于 API 的后端,部署简单但较慢
  • 基于 MongoDB 的后端,更快但需要维护 Wikibase 内容的本地副本

安装

使用以下方法之一

1 - 使用 composer 使用 master 分支安装库及其所有依赖项

composer require "ppp/wikibase-entity-store":dev-master"

2 - 创建一个 composer.json 文件,只定义对该包版本 1.0 的依赖,并在目录中运行 'composer install'

{
    "require": {
        "ppp/wikibase-entity-store": "~1.0"
    }
}

用法

功能

实体存储系统基于抽象类 EntityStore,它提供不同服务来操作实体。

这些服务包括

$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 )
	)
);

后端

API 后端

API 后端是使用起来最简单的一个。它使用 Wikibase 实例的 API 和 WikidataQuery(如果你使用此 EntityStore 作为 Wikidata 数据的后端,并希望有查询支持)。

配置文件如下所示

{
    "backend": "api",
    "api": {
        "url": "http://www.wikidata.org/w/api.php",
        "wikidataquery-url": "http://wdq.wmflabs.org/api"
    }
}

如果你想使用与 Wikidata 不同的 Wikibase 实例的存储,请将 http://www.wikidata.org/w/api.php 替换为你 WediaWiki API 的 URL。

wikidataquery-url 参数是可选的,如果你不希望使用 Wikidata 内容进行查询支持,可以取消设置。

无配置文件

$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' )
);

MongoDB 后端

MongoDB 后端使用 MongoDB 数据库。需要 doctrine/mongodb

配置文件如下所示

{
    "backend": "mongodb",
    "mongodb": {
        "server": SERVER,
        "database": DATABASE
    }
}

server 应该是一个 MongoDB 服务器连接字符串,而 database 是要使用的数据库的名称。

无配置文件

//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 );

您可以使用此脚本使用 Wikidata JSON 存档填充 MongoDB 数据库

 php entitystore import-json-dump MY_JSON_DUMP MY_CONFIGURATION_FILE

或使用此脚本使用增量 XML 存档

php entitystore import-incremental-xml-dump MY_XML_DUMP MY_CONFIGURATION_FILE

内存后端

基于实体文档数组的后端。适用于测试。

$store = new \Wikibase\EntityStore\InMemory\InMemoryEntityStore( array(
    new Item( new ItemId( 'Q42' ) )
) );

选项

不同的后端支持一组共享的选项。这些选项包括

  • string[] languages:允许筛选国际化值集合。默认值:null(即所有语言)。
  • bool languagefallback:在 languages 选项中定义的语言上应用语言回退系统。默认值:false

它们可以注入到配置中

{
  "options": {
    "languages": ["en", "fr"],
    "languagefallback": true
  }
}

它们也可以作为 EntityStore 构造函数的最后一个参数传递

$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
);

缓存支持

为了获得更好的性能,可以在 EntityStore 上添加一个缓存层

在配置文件中添加一个 cache 部分。

示例:带有两层缓存的配置。第一层是 PHP 数组,第二层是运行在 localhost:11211 上的 Memcached 实例。

{
    "cache": {
        "array": true,
        "memcached": {
            "host": "localhost",
            "port": 11211
        }
    }
}

无配置文件

$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 );

发布说明

0.3.1 (2016-01-11)

  • 添加对 WikibaseDataModel 5.0 和 6.0 以及 Number 0.7 的支持

0.3.0 (2016-01-11)

  • 更新到 WikibaseDataModel 4.0、WikibaseDataModelServices 3.2 和 MediaWikiApiBase 2.0
  • 更改 MongoDB 中标签索引的编码

0.1.0 (2015-04-21)

首次发布,具备以下特性

  • 从ID或术语检索实体
  • 导入Wikidata JSON完整存档和增量XML存档
  • 开始支持简单查询
  • API和MongoDB后端
  • 基本缓存系统