Module:Wikidata: Difference between revisions
simplify Qid; capitalise first word returned by getRawValue |
Jackmcbarn (talk | contribs) add methods for qualifiers |
||
Line 15: | Line 15: | ||
else |
else |
||
out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]<abbr title='Article is not yet available in this wiki'>[*]</abbr>" |
out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]<abbr title='Article is not yet available in this wiki'>[*]</abbr>" |
||
end |
|||
end |
|||
end |
|||
return table.concat(out, ", ") |
|||
else |
|||
return "" |
|||
end |
|||
else |
|||
return input_parm |
|||
end |
|||
end |
|||
p.getQualifierValue = function(frame) |
|||
local propertyID = mw.text.trim(frame.args[1] or "") |
|||
local qualifierID = mw.text.trim(frame.args[2] or "") |
|||
local input_parm = mw.text.trim(frame.args[3] or "") |
|||
if input_parm == "FETCH_WIKIDATA" then |
|||
local entity = mw.wikibase.getEntity() |
|||
if entity.claims[propertyID] ~= nil then |
|||
local out = {} |
|||
for k, v in pairs(entity.claims[propertyID]) do |
|||
for k2, v2 in pairs(v.qualifiers[qualifierID]) do |
|||
if v2.snaktype == 'value' then |
|||
if (mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"])) then |
|||
out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"]) .. "]]" |
|||
else |
|||
out[#out + 1] = "[[:d:Q" .. v2.datavalue.value["numeric-id"] .. "|" .. mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"]) .. "]]<abbr title='Article is not yet available in this wiki'>[*]</abbr>" |
|||
end |
|||
end |
end |
||
end |
end |
||
Line 38: | Line 66: | ||
if v.mainsnak.snaktype == 'value' then |
if v.mainsnak.snaktype == 'value' then |
||
out[#out + 1] = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) |
out[#out + 1] = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) |
||
end |
|||
end |
|||
local ret = table.concat(out, ", ") |
|||
return string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2) |
|||
else |
|||
return "" |
|||
end |
|||
else |
|||
return input_parm |
|||
end |
|||
end |
|||
p.getRawQualifierValue = function(frame) |
|||
local propertyID = mw.text.trim(frame.args[1] or "") |
|||
local qualifierID = mw.text.trim(frame.args[2] or "") |
|||
local input_parm = mw.text.trim(frame.args[3] or "") |
|||
if input_parm == "FETCH_WIKIDATA" then |
|||
local entity = mw.wikibase.getEntity() |
|||
if entity.claims[propertyID] ~= nil then |
|||
local out = {} |
|||
for k, v in pairs(entity.claims[propertyID]) do |
|||
for k2, v2 in pairs(v.qualifiers[qualifierID]) do |
|||
if v2.snaktype == 'value' then |
|||
out[#out + 1] = mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"]) |
|||
end |
|||
end |
end |
||
end |
end |
||
Line 78: | Line 131: | ||
else |
else |
||
out[#out + 1] = os.date("%B %e, %Y", os.time(dt)) |
out[#out + 1] = os.date("%B %e, %Y", os.time(dt)) |
||
end |
|||
end |
|||
end |
|||
return table.concat(out, ", ") |
|||
else |
|||
return "" |
|||
end |
|||
else |
|||
return input_parm |
|||
end |
|||
end |
|||
p.getQualifierDateValue = function(frame) |
|||
local propertyID = mw.text.trim(frame.args[1] or "") |
|||
local qualifierID = mw.text.trim(frame.args[2] or "") |
|||
local input_parm = mw.text.trim(frame.args[3] or "") |
|||
local date_format = mw.text.trim(frame.args[4] or "dmy") |
|||
if input_parm == "FETCH_WIKIDATA" then |
|||
local entity = mw.wikibase.getEntity() |
|||
if entity.claims[propertyID] ~= nil then |
|||
local out = {} |
|||
local dt = {} |
|||
for k, v in pairs(entity.claims[propertyID]) do |
|||
for k2, v2 in pairs(v.qualifiers[qualifierID]) do |
|||
if v2.snaktype == 'value' then |
|||
local d = v2.datavalue.value.time |
|||
if #d > 26 then |
|||
dt.year = string.sub(d, 9, 12) |
|||
dt.month = string.sub(d, 14, 15) |
|||
dt.day = string.sub(d, 17, 18) |
|||
else |
|||
dt.year = string.sub(d, 9, 10) |
|||
dt.month = string.sub(d, 12, 13) |
|||
dt.day = string.sub(d, 15, 16) |
|||
end |
|||
if date_format == "dmy" then |
|||
out[#out + 1] = os.date("%e %B %Y", os.time(dt)) |
|||
else |
|||
out[#out + 1] = os.date("%B %e, %Y", os.time(dt)) |
|||
end |
|||
end |
end |
||
end |
end |
Revision as of 01:49, 11 September 2014
Module:Wikidata is deprecated. Please use Module:WikidataIB or Module:Wd instead. |
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This Lua module is used on approximately 134,000 pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
Methods
The module "Wikidata" contains the following methods, that allow the calling script to retrieve the value for any property from Wikidata by supplying the property ID as the first parameter:
getValue
: Returns wiki-linked values, if applicable. All other values will be output similar to{{#property:}}
, except that if values with preferred rank exist, then only they are returned. UnlikeformatStatements
from w:ru:Модуль:Wikidata,getValue
does not yet pick up any references (see w:en:Module talk:Wikidata/Archive 1#Why the references from Wikidata get dropped?).getRawValue
: Returns non-linked property values and numbers with the thousand separator. It also returns the plain numeric value of a property which is a quantity. All other values will be output similar to{{#property:}}
, including normal values, even if preferred values exist.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".
Wikidata qualifier values (if present) can be retrieved by the following methods:
getQualifierValue
: returns only wiki-linked values, if applicable.getRawQualifierValue
: returns non-linked valuesgetQualifierDateValue
: returns formatted dates
If the property is not defined in Wikidata for the article that invokes this code, then an empty string ("") is returned.
Other methods:
pageId
: returns the Wikidata id (Q…) of the current page or nothing if the page is not connected to Wikidata. To get the Wikidata id of another page, use {{#invoke:ResolveEntityId|page}}.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.
- Call as
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.
Arbitrary Access
As of 16 September 2015, it is now possible to fetch data from other articles by using their QID. The following call:
{{#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. For example:
{{#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 Richard Burton (Q151973)) 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.
Parameters
- 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.
Usage
Example: spouse (P26)
{{#invoke:Wikidata|getValue|P26|}}
= returns nothing, so suppresses the display of spouse in an infobox{{#invoke:Wikidata|getValue|P26|FETCH_WIKIDATA}}
= returns the linked value(s) of property P26 (spouse) stored in wikidata for the corresponding article (e.g. for article Bill Clinton, it returns Hillary Clinton){{#invoke:Wikidata|getValue|P26|[[Hillary Rodham Clinton]]}}
= returns Hillary Rodham Clinton, allowing an infobox to use a local value rather than the value stored in Wikidata.
Example in Infobox template
Inside an infobox definition, it may be called like this:
| data55 = {{#invoke:Wikidata|getValue|P26|{{{spouse|FETCH_WIKIDATA}}} }}
which causes the infobox to:
- not display spouse if the infobox parameter
|spouse
is set to be blank (as"| spouse ="
) - display the linked value(s) from Wikidata if the infobox parameter
|spouse
is not supplied - display the local infobox parameter
|spouse
if it is supplied (e.g."|spouse = Hillary Rodham Clinton"
)
Optionally, it could be called as:
| data55 = {{#invoke:Wikidata|getValue|P26|{{{spouse|}}} }}
which causes the infobox to:
- not display spouse if the infobox parameter
|spouse
is set to be blank (as"| spouse ="
} - not display spouse if the infobox parameter
|spouse
is not supplied - display the local infobox parameter
|spouse
if it is supplied (e.g."|spouse = Hillary Rodham Clinton"
) - display the linked value(s) from Wikidata if the infobox parameter is locally set to FETCH_WIKIDATA
See Module:WikidataIB for a modification that allows fields, on a per article basis, to be blacklisted so that they never display. It also allows the editor to specify, on a per article basis, which fields may be automatically fetched from Wikidata when local parameter is supplied; the default is none, allowing an infobox to be modified to accept Wikidata without any change in the articles using the infobox until the functionality is enabled in the article.
Example:birth place
This works in just the same way as the calls above:
{{#invoke:Wikidata|getValue|P19|}}
= returns nothing, so suppresses the display of birth place in an infobox{{#invoke:Wikidata|getValue|P19|FETCH_WIKIDATA}}
= returns the linked value(s) of property P19 (place of birth) stored in wikidata for the corresponding article (e.g. for article Bill Clinton, it returns Hope, Arkansas){{#invoke:Wikidata|getValue|P19|[[Hope, Arkansas|Hope]]}}
= returns Hope, allowing an infobox to use a local value rather than the value stored in Wikidata.
Example:gender
We don't want the returned value linked, so use:
- -
{{#invoke:Wikidata|getRawValue|P21|FETCH_WIKIDATA}}
Example:date of birth
If we want the date of birth in dmy format, we use:
- -
{{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|dmy}}
If we want the date of birth in mdy format, we use:
- -
{{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|mdy}}
If we want a year of birth, we use:
- -
{{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y}}
If we want a year of birth that may be BC, but should read "BCE", we use:
- -
{{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y|BCE}}
Example: Linking to Wikidata item
Use the following code to just retrieve the Q-ID:
- -
{{#invoke:Wikidata|pageId}}
Linking to Wikidata used the usual Wiki markup:
- -
[[d:{{#invoke:Wikidata|pageId}}|Name of Link]]
Example: Linking to another wiki page
Use code like this to link to another wiki. English Wikivoyage in the example:
- -
[[voy:{{#invoke:Wikidata|getSiteLink|enwikivoyage}}|Name of Link]]
Testing
Testing spouse
Copy and paste the following into any article and preview it (please don't save!):
* - {{#invoke:Wikidata|getValue|P26|}}
* - {{#invoke:Wikidata|getValue|P26|FETCH_WIKIDATA}}
* - {{#invoke:Wikidata|getValue|P26|[[Hillary Rodham Clinton]]}}
In Bill Clinton you should get:
In Barack Obama you should get:
In Richard Burton you should get:
- -
- - Elizabeth Taylor, Sally Burton, Sybil Christopher, Suzy Miller, Elizabeth Taylor
- - Hillary Rodham Clinton
In Franz Kafka you should get:
- -
- -
- - Hillary Rodham Clinton
Testing birthplace
Copy and paste the following into any article and preview it (please don't save!):
* - {{#invoke:Wikidata|getValue|P19|}}
* - {{#invoke:Wikidata|getValue|P19|FETCH_WIKIDATA}}
* - {{#invoke:Wikidata|getValue|P19|[[Newport]]}}
Try William Ellery and check that the Wikidata call correctly disambiguates.
Testing getValue, getRawValue and getDateValue
Copy and paste the following into any article and preview it (please don't save!):
* - {{#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}}
This should return the Wikidata values for birthplace, spouse, citizenship, religion, gender, date of birth (twice) and year of birth, if they exist. The dob is first in dmy format and then in mdy.
Testing dates BC/BCE
Copy and paste the following into a short section of article such as Horace #See also and preview it (please don't save!):
* - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y}}
* - {{#invoke:Wikidata|getDateValue|P569|FETCH_WIKIDATA|y|BCE}}
This should return 65 BC and 65 BCE, respectively.
See also
- Module:WikidataIB, for handling Wikidata in infoboxes
local p = {}
-- This is used to get a normal wiki-linked value, or a comma separated list of them if multiple values exist
p.getValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntity()
if entity.claims[propertyID] ~= nil then
local out = {}
for k, v in pairs(entity.claims[propertyID]) do
if v.mainsnak.snaktype == 'value' then
if (mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"])) then
out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]"
else
out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]<abbr title='Article is not yet available in this wiki'>[*]</abbr>"
end
end
end
return table.concat(out, ", ")
else
return ""
end
else
return input_parm
end
end
p.getQualifierValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local qualifierID = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[3] or "")
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntity()
if entity.claims[propertyID] ~= nil then
local out = {}
for k, v in pairs(entity.claims[propertyID]) do
for k2, v2 in pairs(v.qualifiers[qualifierID]) do
if v2.snaktype == 'value' then
if (mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"])) then
out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"]) .. "]]"
else
out[#out + 1] = "[[:d:Q" .. v2.datavalue.value["numeric-id"] .. "|" .. mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"]) .. "]]<abbr title='Article is not yet available in this wiki'>[*]</abbr>"
end
end
end
end
return table.concat(out, ", ")
else
return ""
end
else
return input_parm
end
end
-- This is used to get a value like 'male' (for property p21) which won't be linked
p.getRawValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntity()
if entity.claims[propertyID] ~= nil then
local out = {}
for k, v in pairs(entity.claims[propertyID]) do
if v.mainsnak.snaktype == 'value' then
out[#out + 1] = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
end
end
local ret = table.concat(out, ", ")
return string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2)
else
return ""
end
else
return input_parm
end
end
p.getRawQualifierValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local qualifierID = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[3] or "")
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntity()
if entity.claims[propertyID] ~= nil then
local out = {}
for k, v in pairs(entity.claims[propertyID]) do
for k2, v2 in pairs(v.qualifiers[qualifierID]) do
if v2.snaktype == 'value' then
out[#out + 1] = mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"])
end
end
end
local ret = table.concat(out, ", ")
return string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2)
else
return ""
end
else
return input_parm
end
end
-- This is used to get a date value for date_of_birth (p569), etc. which won't be linked -- consolidate by testing if entity.claims[propertyID].mainsnak.datavalue.type is "time"
-- Dates are stored as 28 characters if the year >99 -- e.g. +00000002014-01-01T00:00:00Z for 2014
-- Dates are stored as 26 characters if the year =<99 -- e.g. +000000050-01-01T00:00:00Z for 50 CE
p.getDateValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local date_format = mw.text.trim(frame.args[3] or "dmy")
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntity()
if entity.claims[propertyID] ~= nil then
local out = {}
local dt = {}
for k, v in pairs(entity.claims[propertyID]) do
if v.mainsnak.snaktype == 'value' then
local d = v.mainsnak.datavalue.value.time
if #d > 26 then
dt.year = string.sub(d, 9, 12)
dt.month = string.sub(d, 14, 15)
dt.day = string.sub(d, 17, 18)
else
dt.year = string.sub(d, 9, 10)
dt.month = string.sub(d, 12, 13)
dt.day = string.sub(d, 15, 16)
end
if date_format == "dmy" then
out[#out + 1] = os.date("%e %B %Y", os.time(dt))
else
out[#out + 1] = os.date("%B %e, %Y", os.time(dt))
end
end
end
return table.concat(out, ", ")
else
return ""
end
else
return input_parm
end
end
p.getQualifierDateValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local qualifierID = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[3] or "")
local date_format = mw.text.trim(frame.args[4] or "dmy")
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntity()
if entity.claims[propertyID] ~= nil then
local out = {}
local dt = {}
for k, v in pairs(entity.claims[propertyID]) do
for k2, v2 in pairs(v.qualifiers[qualifierID]) do
if v2.snaktype == 'value' then
local d = v2.datavalue.value.time
if #d > 26 then
dt.year = string.sub(d, 9, 12)
dt.month = string.sub(d, 14, 15)
dt.day = string.sub(d, 17, 18)
else
dt.year = string.sub(d, 9, 10)
dt.month = string.sub(d, 12, 13)
dt.day = string.sub(d, 15, 16)
end
if date_format == "dmy" then
out[#out + 1] = os.date("%e %B %Y", os.time(dt))
else
out[#out + 1] = os.date("%B %e, %Y", os.time(dt))
end
end
end
end
return table.concat(out, ", ")
else
return ""
end
else
return input_parm
end
end
return p