跳转到内容

模組:Wikidata/doc

维基百科,自由的百科全书

这是本页的一个历史版本,由YFdyh000留言 | 贡献2022年5月30日 (一) 19:22 信息框模板中使用:​ 修饰语句)编辑。这可能和当前版本存在着巨大的差异。

这是Module:Wikidata的文档页面

方法

“Wikidata”模块包含下列方法,将属性ID作为首个参数提供,可从维基数据中检索任何属性的值:

  • getValue:以wiki内链形式返回值(如果适用)。其他值的输出类似{{#property:}},但如果值有首选等级则值返回有首选等级的值。与w:ru:Модуль:WikidataformatStatements不同,getValue不获取任何参考资料(另见w:en:Module talk:Wikidata/Archive 1#Why the references from Wikidata get dropped?)。
  • getRawValue:返回的属性值没有内链形式,数字使用千位分隔符。将数量属性的数值返回为纯数字。其他值的输出类似{{#property:}},包括一般的值,即使存在首选的值。
  • getDateValue: Special method to return formatted dates. The default date format is [day month year]; an optional third parameter can be added for other date formats: for [month day, year], add "mdy"; for [month year], add "my"; for year only, add "y". A further optional parameter, which must be fourth, such as "BCE", may be added to override the default "BC" that is suffixed for dates BC.
  • getImages: Returns all of the images of a particular property, e.g. image (P18), Gene Atlas Image (P692), etc. Parameters are | propertyID | value / FETCH_WIKIDATA / nil | separator (default=space) | size (default=220px). The returned text will be in the format [[File:Filename | size]] for each image with a selectable size and separator (which may be html). If a property is supplied that is not of type "commonsMedia", it will return empty text.
  • getUnits This takes a propertyID which is a quantity as the first parameter, and returns the name of the units that the property is using on the current page. It returns empty if the property is not a quantity type or if the value is not set. The second parameter may be used to override fetching from Wikidata, if it is anything other than "FETCH_WIKIDATA".
  • getUnitID This takes a propertyID which is a quantity as the first parameter, and returns the QID of the units that the property is using on the current page. It returns empty if the property is not a quantity type or if the value is not set. The second parameter may be used to override fetching from Wikidata, if it is anything other than "FETCH_WIKIDATA".

可以通过以下方法检索维基数据的限定符值(如果存在):

  • getQualifierValue:返回有wiki链接(内链)的值,如果适用。
  • getRawQualifierValue:返回非内链格式的值
  • getQualifierDateValue:返回格式化后的日期

如果调用此代码的条目在维基数据中没有定义属性,则返回一个空字符串("")。

其他方法:

  • pageId:返回当前页面的维基数据ID(QID),如果未链接维基数据则什么也不返回。使用{{#invoke:ResolveEntityId|page}}获取其他页面的QID。
  • getTAValue: gets the TA98 (Terminologia Anatomica first edition 1998) values for property P1323. It takes no parameters and constructs its output as a list, with each value linked to an external resource. This is an exemplar for writing calls that need to return external links.
  • ViewSomething: gets anything in the structured data, including labels, descriptions, references and interwiki links. See [1]
  • getSiteLink: gets name of a page in given in argument wiki (like "enwikiquote" for English Wikiquote, "arwiki" for the Arabic Wikipedia).
  • Dump: {{#invoke:Wikidata|Dump|claims}} spies the structured data. It uses the same arguments as ViewSomething. Try this with preview only to see results. That helps you a lot in developing Lua scripts that access the data. If used without arguments, it dumps everything including labels, descriptions, references and interwiki links. There is the wrapper template, see {{Dump}}.
  • getImageLegend: returns an image legend (image is property P18; image legend is property P2096).
    Call as {{#invoke:Wikidata |getImageLegend | <PARAMETER> | lang=<ISO-639code> |id=<QID>}}
    Returns PARAMETER, unless it is equal to "FETCH_WIKIDATA", from Item QID (expensive call). If QID is omitted or blank, the current article is used (not an expensive call). If lang is omitted, it uses the local wiki language, otherwise it uses the provided ISO-639 language code.
    The label is returned from the first image with 'preferred' rank; or from the first image with 'normal' rank if no image has preferred rank.
  • getValueShortName: returns the same data as getValue, but utilizes the property short name as the label, if available. This allows for piped links to use a shorter label where preferred. If short name is not set on the item, the normal label is used.

访问其他QID

2015年9月16日起,可以通过其他条目的QID获取其他条目的数据。 调用如下:

  • {{#invoke:Wikidata|getValueFromID|<QID>|<Property>|FETCH_WIKIDATA}}

will do the same as getValue, but takes an extra parameter, which is the QID of the Wikidata item that you want to get the property value from. 例如:

  • {{#invoke:Wikidata|getValueFromID|{{Get QID|Richard Burton (actor) }}|P26|FETCH_WIKIDATA}}

will fetch a list of the linked values for 'spouse' (P26) from redirect page title Richard Burton (actor) (corresponding to Wikidata label (无项目)) from anywhere in the English Wikipedia.

This means that testing environments may be set up in user space, but remember that these calls are classed as expensive, so please use them as sparingly as possible.

参数

  • For the generalized case (getValue), two unnamed parameters are supplied. The first is the ID of the property that is to be retrieved (e.g. P19 for birthplace or P26 for spouse). The second may be null, "FETCH_WIKIDATA", or any other string, which becomes the returned value.
  • For the generalized unlinked case (getRawValue), two unnamed parameters are supplied. The first is the ID of the property that is to be retrieved (e.g. P21 for gender). The second may be null, "FETCH_WIKIDATA", or any other string, which becomes the returned value.
  • For the generalized date case (getDateValue), three unnamed parameters are supplied. The first is the ID of the property that is to be retrieved (e.g. P569 for date of birth). The second may be null, "FETCH_WIKIDATA", or any other string, which becomes the returned value. The third is the format that the date should be returned in, either dmy, mdy, my, or y.

Please note that lower-case parameters are no longer supported by the wikibase call: p123, so please check that upper-case, like P123, is used if problems should arise.

用法

例子:配偶 (P26)

  • {{#invoke:Wikidata|getValue|P26|}} = 不返回任何内容
  • {{#invoke:Wikidata|getValue|P26|FETCH_WIKIDATA}} = 返回内链形式的维基数据中存储的P26(配偶)属性
  • {{#invoke:Wikidata|getValue|P26|[[Hillary Rodham Clinton]]}} = 返回Hillary Rodham Clinton,即允许信息框使用本地自定义的值代替维基数据中的值。

信息框模板中使用

信息框的定义中可以这样调用:

  • | data55 = {{#invoke:Wikidata|getValue|P26|{{{spouse|FETCH_WIKIDATA}}} }}

使信息框变为:

  1. 信息框参数|spouse设为空(即"| spouse =")时,不显示spouse
  2. 未提供信息框参数|spouse时,根据维基数据内容显示内链形式的一个或多个值
  3. 本地提供信息框参数时,显示本地提供的参数|spouse。例如"|spouse = Hillary Rodham Clinton"

另一种方式:

  • | data55 = {{#invoke:Wikidata|getValue|P26|{{{spouse|}}} }}

使信息框变为:

  1. 信息框参数|spouse设为空(即"| spouse =")时,不显示spouse
  2. 未提供信息框参数|spouse时,不显示spouse
  3. 本地提供信息框参数时,显示本地提供的参数|spouse。例如"|spouse = Hillary Rodham Clinton"
  4. 本地将信息框参数设为FETCH_WIKIDATA时,根据维基数据内容显示内链形式的一个或多个值

另见Module:WikidataIB版本,支持在每个条目上用本地参数定义:不显示的参数(黑名单);仅从维基数据自动获取哪些参数(白名单)。用它可以修改信息框而不对使用信息框的条目内容和效果产生任何修改,直至在条目中明确启用相应参数。

例子:出生地

方式如上所述:

  • {{#invoke:Wikidata|getValue|P19|}} = 不返回任何内容
  • {{#invoke:Wikidata|getValue|P19|FETCH_WIKIDATA}} = 返回内链形式的维基数据中存储的P19(出生地)属性,指向对应条目
  • {{#invoke:Wikidata|getValue|P19|[[Hope, Arkansas|Hope]]}} = 返回Hope,即允许信息框使用本地自定义的值代替维基数据中的值。

例子:性别

不希望返回内链形式的性别,因此使用:

  • - {{#invoke:Wikidata|getRawValue|P21|FETCH_WIKIDATA}}

例子:出生日期

使用dmy格式的出生日期则使用:

  • - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|dmy}}

使用mdy格式的出生日期则使用:

  • - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|mdy}}

仅获取出生年份:

  • - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y}}

获取出生年份(BC)但希望显示为BCE:

  • - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y|BCE}}

例子:链接到一个维基数据项

使用下列代码检索QID:

  • - {{#invoke:Wikidata|pageId}}

用wiki语法的内链链接到维基数据:

  • - [[d:{{#invoke:Wikidata|pageId}}|Name of Link]]

例子:链接到另一个wiki页面

链接到另一个wiki站点,此例为英文维基导游:

  • - [[voy:{{#invoke:Wikidata|getSiteLink|enwikivoyage}}|Name of Link]]


测试

配偶

复制并粘贴下列代码到任何条目并预览(请勿保存!):

 * - {{#invoke:Wikidata|getValue|P26|}}
 * - {{#invoke:Wikidata|getValue|P26|FETCH_WIKIDATA}}
 * - {{#invoke:Wikidata|getValue|P26|[[希拉里·克林顿]]}}

比尔·克林顿中可以看到:

但在贝拉克·奥巴马中:

理查德·伯顿中:


弗朗茨·卡夫卡中则是:

出生地

复制并粘贴下列代码到任何条目并预览(请勿保存!):

 * - {{#invoke:Wikidata|getValue|P19|}}
 * - {{#invoke:Wikidata|getValue|P19|FETCH_WIKIDATA}}
 * - {{#invoke:Wikidata|getValue|P19|[[纽波特]]}}

威廉·埃勒里中尝试,看到维基数据如何正确的消歧义。

测试getValue、getRawValue及getDateValue

复制并粘贴下列代码到任何条目并预览(请勿保存!):

 * - {{#invoke:Wikidata|getValue|P19|FETCH_WIKIDATA}}
 * - {{#invoke:Wikidata|getValue|P26|FETCH_WIKIDATA}}
 * - {{#invoke:Wikidata|getValue|P27|FETCH_WIKIDATA}}
 * - {{#invoke:Wikidata|getValue|P140|FETCH_WIKIDATA}}
 * - {{#invoke:Wikidata|getRawValue|P21|FETCH_WIKIDATA}}
 * - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|dmy}}
 * - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|mdy}}
 * - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y}}

如果存在,这分别会返回出生地、配偶、国籍、宗教、性别、出生日期(两种格式)、出生年份。

另见