Module:Wikidata: Difference between revisions

Content deleted Content added
Prune repetiotion of code for extracting a q number from an entity (in many places)
Prune duplication of "parse input from frame; get Wikidata entity object" code
Line 480:
end
 
local function parseInput(frame, qid)
 
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
return false, input_parm, nil, nil
end
local entity = mw.wikibase.getEntityObject(qid)
local claims
if entity and entity.claims then
claims = entity.claims[propertyID]
if not claims then
return false, "", nil, nil
end
else
return false, "", nil, nil
end
return true, entity, claims, propertyID
end
------------------------------------------------------------------------------
-- module global functions
Line 511 ⟶ 529:
-- This is used to get a 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 "")
local delimdefault = ", " -- **internationalise later**
local delim = frame.args.delimiter or ""
Line 521 ⟶ 537:
local qid = frame.args.qid
if qid and (#qid == 0) then qid = nil end
local go, errorOrentity, claims, propertyID = parseInput(frame, qid)
if input_parm == "FETCH_WIKIDATA" then
if not go then
local entity = mw.wikibase.getEntityObject(qid)
return errorOrentity
local claims
end
if entity and entity.claims then
local entity = errorOrentity
claims = entity.claims[propertyID]
-- if wiki-linked value output as link if possible
end
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
if claims then
local out = {}
-- if wiki-linked value output as link if possible
for k, v in pairs(claims) do
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
local qnumber = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
local out = {}
local sitelink = mw.wikibase.sitelink(qnumber)
for k, v in pairs(claims) do
local label = mw.wikibase.label(qnumber) or qnumber
local qnumber = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
localif sitelink = mw.wikibase.sitelink(qnumber)then
localout[#out label+ 1] = mw"[[" .wikibase. sitelink .. "|" .. label(qnumber) or.. qnumber"]]"
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
else
out[#out + 1] = "[[:d:" .. qnumber .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>"
end
end
return table.concat(out, delim)
else
out[#out + 1] = "[[:d:" .. qnumber .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>"
-- just return best values
return entity:formatPropertyValues(propertyID).value
end
else
return ""
end
return table.concat(out, delim)
else
-- just return input_parmbest values
return entity:formatPropertyValues(propertyID).value
end
end
Line 556 ⟶ 564:
-- Same as above, but uses the short name property for label if available.
p.getValueShortName = function(frame)
local go, errorOrentity, claims, propertyID = mw.text.trimparseInput(frame.args[1] or, ""nil)
if not go then
local input_parm = mw.text.trim(frame.args[2] or "")
return errorOrentity
if input_parm == "FETCH_WIKIDATA" then
end
local entity = mw.wikibase.getEntityObject()
local entity = errorOrentity
local claims
-- if wiki-linked value output as link if possible
if entity and entity.claims then
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
claims = entity.claims[propertyID]
local out = {}
end
iffor k, v in pairs(claims) thendo
local qnumber = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
-- if wiki-linked value output as link if possible
local sitelink = mw.wikibase.sitelink(qnumber)
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
local out = {}label
local claimEntity = mw.wikibase.getEntity(qnumber)
for k, v in pairs(claims) do
if claimEntity ~= nil then
local qnumber = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
if claimEntity.claims.P1813 then
local sitelink = mw.wikibase.sitelink(qnumber)
for k2, v2 in pairs(claimEntity.claims.P1813) do
local label
if v2.mainsnak.datavalue.value.language == "en" then
local claimEntity = mw.wikibase.getEntity(qnumber)
label = v2.mainsnak.datavalue.value.text
if claimEntity ~= nil then
if claimEntity.claims.P1813 then
for k2, v2 in pairs(claimEntity.claims.P1813) do
if v2.mainsnak.datavalue.value.language == "en" then
label = v2.mainsnak.datavalue.value.text
end
end
end
end
if label == nil or label == "" then label = mw.wikibase.label(qnumber) end
if label == nil then label = qnumber end
 
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
else
out[#out + 1] = "[[:d:" .. qnumber .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>"
end
end
end
return table.concat(out, ", ")
if label == nil or label == "" then label = mw.wikibase.label(qnumber) end
if label == nil then label = qnumber end
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
else
out[#out + 1] = "[[:d:" .. qnumber .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>"
-- just return best vakues
return entity:formatPropertyValues(propertyID).value
end
else
return ""
end
return table.concat(out, ", ")
else
-- just return input_parmbest vakues
return entity:formatPropertyValues(propertyID).value
end
end
Line 678 ⟶ 675:
-- This is used to get a value like 'male' (for property p21) which won't be linked and numbers without the thousand separators
p.getRawValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local qid = frame.args.qid
if qid and (#qid == 0) then qid = nil end
local go, errorOrentity, claims, propertyID = parseInput(frame, qid)
if input_parm == "FETCH_WIKIDATA" then
if not go then
local entity = mw.wikibase.getEntityObject(qid)
return errorOrentity
local claims
if entity and entity.claims then claims = entity.claims[propertyID] end
if claims then
local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
 
-- if number type: remove thousand separators, bounds and units
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") then
result = mw.ustring.gsub(result, "(%d),(%d)", "%1%2")
result = mw.ustring.gsub(result, "(%d)±.*", "%1")
end
return result
else
return ""
end
else
return input_parm
end
local entity = errorOrentity
local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
-- if number type: remove thousand separators, bounds and units
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") then
result = mw.ustring.gsub(result, "(%d),(%d)", "%1%2")
result = mw.ustring.gsub(result, "(%d)±.*", "%1")
end
return result
end
 
-- This is used to get the unit name for the numeric value returned by getRawValue
p.getUnits = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local qid = frame.args.qid
if qid and (#qid == 0) then qid = nil end
local go, errorOrentity, claims, propertyID = parseInput(frame, qid)
if input_parm == "FETCH_WIKIDATA" then
if not go then
local entity = mw.wikibase.getEntityObject(qid)
return errorOrentity
local claims
end
if entity and entity.claims then claims = entity.claims[propertyID] end
local entity = errorOrentity
if claims then
local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") then
result = mw.ustring.sub(result, mw.ustring.find(result, " ")+1, -1)
end
return result
else
return ""
end
else
return input_parm
end
return result
end
 
-- This is used to get the unit's QID to use with the numeric value returned by getRawValue
p.getUnitID = function(frame)
local propertyIDqid = mw.text.trim(frame.args[1] or "").qid
if qid and (#qid == 0) then qid = nil end
local input_parm = mw.text.trim(frame.args[2] or "")
local go, errorOrentity, claims = parseInput(frame, qid)
if input_parm == "FETCH_WIKIDATA" then
if not go then
local entity = mw.wikibase.getEntityObject()
return errorOrentity
local claims
end
if entity and entity.claims then claims = entity.claims[propertyID] end
local entity = errorOrentity
if claims then
local result
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") then
-- get the url for the unit entry on Wikidata:
result = claims[1].mainsnak.datavalue.value.unit
-- and just reurn the last bit from "Q" to the end (which is the QID):
result = mw.ustring.sub(result, mw.ustring.find(result, "Q"), -1)
end
return result
else
return ""
end
else
return input_parm
end
return result
end
 
Line 786 ⟶ 760:
-- So I'll just supply "Z" in the call to formatDate below:
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 i18n["datetime"]["default-format"])
local date_addon = mw.text.trim(frame.args[4] or i18n["datetime"]["default-addon"])
local go, errorOrentity, claims = parseInput(frame, nil)
if input_parm == "FETCH_WIKIDATA" then
if not go then
local entity = mw.wikibase.getEntityObject()
return errorOrentity
if entity.claims[propertyID] ~= nil then
end
local out = {}
local entity = errorOrentity
for k, v in pairs(entity.claims[propertyID]) do
local out = {}
if v.mainsnak.datavalue.type == 'time' then
for k, v in pairs(claims) do
local timestamp = v.mainsnak.datavalue.value.time
local dateprecision =if v.mainsnak.datavalue.value.precisiontype == 'time' then
local timestamp = v.mainsnak.datavalue.value.time
-- A year can be stored like this: "+1872-00-00T00:00:00Z",
local dateprecision = v.mainsnak.datavalue.value.precision
-- which is processed here as if it were the day before "+1872-01-01T00:00:00Z",
-- A year can be stored like this: "+1872-00-00T00:00:00Z",
-- and that's the last day of 1871, so the year is wrong.
-- which is processed here as if it were the day before "+1872-01-01T00:00:00Z",
-- So fix the month 0, day 0 timestamp to become 1 January instead:
-- and that's the last day of 1871, so the year is wrong.
timestamp = timestamp:gsub("%-00%-00T", "-01-01T")
-- So fix the month 0, day 0 timestamp to become 1 January instead:
out[#out + 1] = parseDateFull(timestamp, dateprecision, date_format, date_addon)
timestamp = timestamp:gsub("%-00%-00T", "-01-01T")
end
out[#out + 1] = parseDateFull(timestamp, dateprecision, date_format, date_addon)
end
return table.concat(out, ", ")
else
return ""
end
else
return input_parm
end
return table.concat(out, ", ")
end
 
p.getQualifierDateValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
Line 849 ⟶ 816:
-- If a property is chosen that is not of type "commonsMedia", it will return empty text.
p.getImages = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local sep = mw.text.trim(frame.args[3] or " ")
local imgsize = mw.text.trim(frame.args[4] or "frameless")
local qid = frame.args.qid
if input_parm == "FETCH_WIKIDATA" then
if qid and (#qid == 0) then qid = nil end
local entity = mw.wikibase.getEntityObject()
local go, errorOrentity, claims = parseInput(frame, qid)
if entitynot and entity.claimsgo then
return errorOrentity
claims = entity.claims[propertyID]
end
local entity = errorOrentity
if claims then
if (claims[1] and claims[1].mainsnak.datatype == "commonsMedia") then
local out = {}
for k, v in pairs(claims) do
local filename = v.mainsnak.datavalue.value
out[#out + 1] = "[[File:" .. filename .. "|" .. imgsize .. "]]"
end
return table.concat(out, sep)
else
return ""
end
else
return ""
end
return table.concat(out, sep)
else
return input_parm""
end
end
Line 982 ⟶ 941:
 
p.getPropertyIDs = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
-- can take a named parameter |qid which is the Wikidata ID for the article. This will not normally be used.
local qid = frame.args.qid
if qid and (#qid == 0) then qid = nil end
local go, errorOrentity, propclaims = parseInput(frame, qid)
if input_parm == "FETCH_WIKIDATA" then
if not go then
local entity = mw.wikibase.getEntityObject(qid)
return errorOrentity
local propclaims
end
if entity and entity.claims then
local entity = errorOrentity
propclaims = entity.claims[propertyID]
-- if wiki-linked value collect the QID in a table
end
if (propclaims[1] and propclaims[1].mainsnak.snaktype == "value" and propclaims[1].mainsnak.datavalue.type == "wikibase-entityid") then
if propclaims then
local out = {}
-- if wiki-linked value collect the QID in a table
for k, v in pairs(propclaims) do
if (propclaims[1] and propclaims[1].mainsnak.snaktype == "value" and propclaims[1].mainsnak.datavalue.type == "wikibase-entityid") then
out[#out + 1] = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
local out = {}
for k, v in pairs(propclaims) do
out[#out + 1] = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
end
return table.concat(out, ", ")
else
-- not a wikibase-entityid, so return empty
return ""
end
else
-- no claim, so return empty
return ""
end
return table.concat(out, ", ")
else
-- not a wikibase-entityid, so return empty
return input_parm
return ""
end
end