survos / wikidata
用于处理 Wikidata API 的 PHP 客户端。
Requires
- php: ^8.2
- illuminate/collections: ^10.0|^11.0
- symfony/http-client: ^6.4||^7.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.0
- rector/rector: ^1.1
This package is auto-updated.
Last update: 2024-08-25 20:11:08 UTC
README
Wikidata
Wikidata 提供了一个 API,用于搜索和检索来自 wikidata.org 的数据。
Fork of https://github.com/freearhey/wikidata @todo: consider https://github.com/freearhey/wikidata/compare/master...JamesFrost:wikidata:master
安装
composer require survos/wikidata
用法
首先,我们需要创建一个 Wikidata
类的实例并将其保存到某个变量中,例如这样
require_once('vendor/autoload.php'); use Wikidata\Wikidata; $wikidata = new Wikidata();
然后,我们可以使用可用方法之一来访问 Wikidata 数据库。
可用方法
search()
search()
方法为您提供了一个按标签查找 Wikidata 实体的方式。
$results = $wikidata->search($query, $lang, $limit);
参数
$query
: 要搜索的术语(必需)$lang
: 指定结果语言(默认:'en')$limit
: 设置自定义限制(默认:10)
示例
$results = $wikidata->search('car', 'fr', 5); /* Collection { #items: array:5 [ 0 => SearchResult { id: "Q1759802" lang: "fr" label: "autocar" wiki_url: "https://fr.wikipedia.org/wiki/autocar" description: "transport routier pouvant accueillir plusieurs voyageurs pour de longues distances" aliases: array:1 [ 0 => "car" ] } 1 => SearchResult { id: "Q224743" lang: "fr" label: "Car" wiki_url: "https://fr.wikipedia.org/wiki/Car" description: "page d'homonymie d'un projet Wikimédia" aliases: [] } ... ] } */
search()
方法始终返回带有结果的 Illuminate\Support\Collection
类。这意味着您可以使用 Laravel 集合中所有可用的 方法。
searchBy()
searchBy
帮助您通过实体的属性值来查找 Wikidata 实体。
$results = $wikidata->searchBy($propId, $entityId, $lang, $limit);
参数
$propId
: 要搜索的属性的 ID(必需)$entityId
: 实体的 ID(必需)$lang
: 指定结果语言(默认:'en')$limit
: 设置自定义限制(默认:10)
示例
// List of people who born in city Pomona, US $results = $wikidata->searchBy('P19', 'Q486868'); /* Collection { #items: array:10 [ 0 => SearchResult { id: "Q92638" lang: "en" label: "Robert Tarjan" wiki_url: "https://en.wikipedia.org/wiki/Robert_Tarjan" description: "American computer scientist" aliases: array:2 [ 0 => "Robert E. Tarjan" 1 => "Robert Endre Tarjan" ] } 1 => SearchResult { id: "Q184805" lang: "en" label: "Tom Waits" wiki_url: "https://en.wikipedia.org/wiki/Tom_Waits" description: "American singer-songwriter and actor" aliases: [] } ... ] } */
searchBy()
方法始终返回带有结果的 Illuminate\Support\Collection
类。这意味着您可以使用 Laravel 集合中所有可用的 方法。
get()
get()
通过指定的 ID 返回 Wikidata 实体。
$entity = $wikidata->get($entityId, $lang);
参数
$entityId
: 实体的 ID(必需)$lang
: 指定结果语言(默认:'en')
示例
// Get all data about Steve Jobs $entity = $wikidata->get('Q19837'); /* Entity { id: "Q19837" lang: "en" label: "Steve Jobs" wiki_url: "https://en.wikipedia.org/wiki/Steve_Jobs" aliases: array:2 [ 0 => "Steven Jobs", 1 => "Steven Paul Jobs" ] description: "American entrepreneur and co-founder of Apple Inc." properties: Collection { ... } } */ // List of all properties as an array $properties = $entity->properties->toArray(); /* [ "P1006" => Property { id: "P1006" label: "NTA ID" values: Collection { #items: array:6 [ 0 => Value { id: "Q5593916" label: "Grammy Trustees Award" qualifiers: Collection { #items: array:1 [ 0 => Qualifier { id: "P585" label: "point in time" value: "2012-01-01T00:00:00Z" } ] } }, ... ] } }, ... ] */
升级指南
从 3.1._ 升级到 3.2._
新版本的主要更改发生在属性值的存储方式上。现在每个属性可以有多个值。在这方面,Property
对象中的 value
属性被替换为 values
属性。
此外,值现在以 Value
对象的形式呈现,而不是字符串。这使得我们不仅能够保存每个值的字符串表示,还能够保存其 qualifiers
列表。有关 qualifiers
的详细信息,请参阅 这里。
测试
vendor/bin/phpunit
贡献
如果您发现错误或想为代码或文档做出贡献,您可以提交一个 问题 或一个 pull request。
许可
Wikidata 在 MIT 许可证 下授权。