Module:WDfetch: Difference between revisions
Appearance
test Tag: Reverted |
Tag: Undo |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Function to fetch | -- Function to fetch the value of a specified Wikidata property from an entity. | ||
-- Requires both a property ID (e.g. P12081) and a Q‑ID (e.g. Q2934). | -- Requires both a property ID (e.g. P12081) and a Q‑ID (e.g. Q2934). | ||
function p.getvalue(frame) | function p.getvalue(frame) | ||
Line 17: | Line 16: | ||
local propertyId = args[1] or args.pid | local propertyId = args[1] or args.pid | ||
if not propertyId then | if not propertyId then | ||
return "Error: No property ID provided." | return "Error: No property ID provided." | ||
end | end | ||
Line 24: | Line 22: | ||
local qid = args.qid | local qid = args.qid | ||
if not qid then | if not qid then | ||
return "Error: No Qid provided." | return "Error: No Qid provided." | ||
end | end | ||
-- Retrieve the full entity object using the Lua interface. | -- Retrieve the full entity object using the Lua interface. | ||
local entity = mw.ext.UnlinkedWikibase.getEntity(qid) | local entity = mw.ext.UnlinkedWikibase.getEntity(qid) | ||
if not entity then | if not entity then | ||
return "Error: Entity not found for Qid " .. qid | return "Error: Entity not found for Qid " .. qid | ||
end | end | ||
Line 40: | Line 35: | ||
local propertyClaims = claims[propertyId] | local propertyClaims = claims[propertyId] | ||
if not propertyClaims or #propertyClaims == 0 then | if not propertyClaims or #propertyClaims == 0 then | ||
return nil | return nil | ||
end | end | ||
Line 47: | Line 41: | ||
local claim = propertyClaims[1] | local claim = propertyClaims[1] | ||
if not (claim and claim.mainsnak and claim.mainsnak.datavalue) then | if not (claim and claim.mainsnak and claim.mainsnak.datavalue) then | ||
return nil | return nil | ||
end | end | ||
local datavalue = claim.mainsnak.datavalue | |||
local value = datavalue.value | |||
-- If the value is a table, handle common cases. | |||
if type(value) == "table" then | |||
-- For linked entities, return the entity id. | |||
if value.id then | |||
value = value.id | |||
-- For monolingual text, return the text field. | |||
elseif value.text then | |||
value | value = value.text | ||
else | |||
value = tostring(value) | |||
end | |||
-- | |||
value = | |||
end | end | ||
-- Capitalize the first letter | -- Capitalize the first letter of the result if it's a string. | ||
if type(value) == "string" then | if type(value) == "string" then | ||
value = value:gsub("^%l", string.upper) | value = value:gsub("^%l", string.upper) | ||
end | end | ||
return value | return value | ||
end | end | ||
return p | return p |
Revision as of 12:31, 2 March 2025
Documentation for this module may be created at Module:WDfetch/doc
local p = {}
-- Function to fetch the value of a specified Wikidata property from an entity.
-- Requires both a property ID (e.g. P12081) and a Q‑ID (e.g. Q2934).
function p.getvalue(frame)
-- Collect arguments from the calling frame.
local args = {}
for k, v in pairs(frame:getParent().args) do
if v ~= "" then args[k] = v end
end
for k, v in pairs(frame.args) do
if v ~= "" then args[k] = v end
end
-- Get the property ID.
local propertyId = args[1] or args.pid
if not propertyId then
return "Error: No property ID provided."
end
-- Get the Q-ID.
local qid = args.qid
if not qid then
return "Error: No Qid provided."
end
-- Retrieve the full entity object using the Lua interface.
local entity = mw.ext.UnlinkedWikibase.getEntity(qid)
if not entity then
return "Error: Entity not found for Qid " .. qid
end
-- Extract claims from the entity.
local claims = entity.claims or {}
local propertyClaims = claims[propertyId]
if not propertyClaims or #propertyClaims == 0 then
return nil
end
-- Extract the first claim's value.
local claim = propertyClaims[1]
if not (claim and claim.mainsnak and claim.mainsnak.datavalue) then
return nil
end
local datavalue = claim.mainsnak.datavalue
local value = datavalue.value
-- If the value is a table, handle common cases.
if type(value) == "table" then
-- For linked entities, return the entity id.
if value.id then
value = value.id
-- For monolingual text, return the text field.
elseif value.text then
value = value.text
else
value = tostring(value)
end
end
-- Capitalize the first letter of the result if it's a string.
if type(value) == "string" then
value = value:gsub("^%l", string.upper)
end
return value
end
return p