This page is a translated version of the page Wikibase/API and the translation is 51% complete.
MediaWiki版本:
1.9

Wikibase API是什么?

Wikibase提供了一种将陈述转化为结构化数据的机制。 有关项目的详细说明请参阅wikidata.org。 参阅Wikidata:Glossary以获取更多关于Wikibase的信息。

 
一个声明

The Wikibase API allows querying, adding, removing and editing information on Wikidata or any other Wikibase instance.

它是通过一组对MediaWiki API模块的扩展提供的。 要使用Wikibase API,您至少要对MediaWiki 行动API有一定基础了解:行为、查询等,反过来说,以及诸如curtimestamprequestid之类的通用参数。

怎样使用Wikibase API?

Send requests to the API through HTTP, the same as with the MediaWiki Action API. See API:Tutorial#How to use it for information about how to use the MediaWiki Action API.

请求格式

API终端位于https://wikidata.org/w/api.php。

In the query string, add the action parameter, which tells the API which action to perform. For example, action=wbgetentities tells the API to get the data for Wikibase entities. For meta and property submodules, use action=query&meta=yourmodule and action=query&prop=modulename respectively.

有些参数几乎总是可用的。如果参数可以取多个值,那么这些参数以复数形式命名。这些参数包括:

  • id/idsIdentifies a specific entity or entities, typically an item. The plural form is used in wbgetentities. Lists of ids should be separated by the pipe character
  • site ∩ title/sites ∩ titlesIdentifies a single item or multiple items. The plural form is used in wbgetentities. Only one of sites and titles can have multiple values at the same time.
  • language/languagesThe language parameter filters labels and descriptions in get actions or identifies a specific language for a set of actions.
  • formatShould always be set to json (or jsonfm for debugging), or xml (or xmlfm for debugging). No other form is supported.
  • summary: 在系统生成的编辑摘要之后添加用户指定的编辑摘要。
  • tokenAn encrypted string the requester must pass for the request to be fulfilled.
  • baserevidAn id for the last known revision that must be passed so that the server can detect edit collisions.

一个简单的查询

GET请求

Get the item for page "Berlin" on English Wikipedia.

URL每部分的解释:

  • http://www.wikidata.org/w/api.php是主要终端。
  • action=wbgetentities告诉API获得Wikibase实体的数据。
  • sites=enwiki表示从英文维基百科获取数据。
  • titles=Berlin 表示获取数据页面的标题。
  • props=descriptions indicates properties to get from the entities, in this case the property is the descriptions of the entities.
  • languages=en means filter the results down to the English language.
  • format=json indicates JSON output, which is the recommended output format.

回应

{
    "entities": {
        "Q64": {
            "type": "item",
            "id": "Q64",
            "descriptions": {
                "en": {
                    "language": "en",
                    "value": "capital and largest city of Germany"
                }
            }
        }
    },
    "success": 1
}

The response of a request will usually contain:

  • A success key with a boolean cast as an integer if the request is successful. If the integer is zero, any additional values might be wrong.
  • An error key with an object of two, optionally three keys, code, info and *, if the request is unsuccessful.
  • Information about the action. The information is either passed on the top level or under item if it is one single item or items if it is several. If it is several items, each is found under a key with its own item id.

Empty objects are returned as JSON arrays and not as objects, while empty parameters remove the actual entry from the item.

API文档和Wikibase模块

Source-generated documentation for the MediaWiki Action API including Wikibase 'wb' API modules is available at wikidata.org/w/api.php

元模組

  • wikibase: ⧼Apihelp-query+wikibase-description⧽
  • wbcontentlanguages: 回傳有關在不同語境裡所接受內容語言 Wikibase 的資訊。

屬性模組

  • pageterms: ⧼Apihelp-query+pageterms-description⧽
  • wbentityusage: ⧼Apihelp-query+wbentityusage-description⧽

API模組

可能的错误

任何模块的可能的错误可以用action=paraminfo&modules=modulename找到。 The error format corresponds to that of the MediaWiki Action API. See API:Errors and warnings#Legacy format.

All error messages from the Wikibase modules should be internationalised (i18n) and localised (l10n). The user language for the logged-in user will be the default for error messages, but you can override this by adding uselang=languageCode to the URL query string.

错误类型 代码 信息
An internationalized error message that isn't localized. no-such-item <wikibase-api-no-such-item>
A correctly localized error message. no-such-item There are no such item to be found
A localized variant.(Norwegian) no-such-item Det finnes ingen slik item

补充资料

  • Many Wikimedia wikis run the Wikibase Client extension. This lets API clients on them to query the wikibase meta submodule to determine URLs for the full Wikibase repo, and the pageterms property submodule to get some Wikidata information about pages on the local wiki.
  • The calender models that can be used with Wikibase are the Gregorian calendar(d:Q1985727) and the Julian calendar(d:Q1985786).
  • The API uses revision ids for edit collision detection. If there is an edit collision, the requester must acquire a newer revision id to be able to continue. This typically involves requesting wbgetentities for the item in question, and then storing (and using) the revision from the entry.
  • Any MediaWiki installation can run Wikibase. On the Wikimedia cluster, most wikis do not run the full set of Wikibase extensions.

Wikimedia Commons has its own Wikibase for properties of the files on Commons.

  • Do not test the info value for a particular error, instead use the code value as this will remain independent of localisation.

参见