Module:Wikidata and Module:Wikidata/sandbox: Difference between pages
(Difference between pages)
imported>Graham87 m update comment too |
imported>Graham87 line up with edit to fix link in main module |
||
| Line 362: | Line 362: | ||
local function findClaims(entity, property) | local function findClaims(entity, property) | ||
if not property or not entity or not entity | if not property or not entity or not mw.wikibase.getBestStatements(entity, property)[1] then return end | ||
if mw.ustring.match(property, "^P%d+$") then | if mw.ustring.match(property, "^P%d+$") then | ||
-- if the property is given by an id (P..) access the claim list by this id | -- if the property is given by an id (P..) access the claim list by this id | ||
return entity | return mw.wikibase.getBestStatements(entity, property) | ||
else | else | ||
property = mw.wikibase.resolvePropertyId(property) | property = mw.wikibase.resolvePropertyId(property) | ||
if not property then return end | if not property then return end | ||
return entity | return mw.wikibase.getBestStatements(entity, property) | ||
end | end | ||
end | end | ||
| Line 402: | Line 402: | ||
else | else | ||
-- otherwise return the main snak | -- otherwise return the main snak | ||
return claim.mainsnak | return claim[1].mainsnak | ||
end | end | ||
end | end | ||
| Line 439: | Line 439: | ||
local function parseInput(frame) | local function parseInput(frame) | ||
local qid = frame.args.qid | local qid = frame.args.qid or mw.wikibase.getEntityIdForCurrentPage() or nil | ||
if qid and (#qid == 0) then qid = nil end | if qid and (#qid == 0) then qid = nil end | ||
local propertyID = mw.text.trim(frame.args[1] or "") | local propertyID = mw.text.trim(frame.args[1] or "") | ||
| Line 446: | Line 446: | ||
return false, input_parm, nil, nil | return false, input_parm, nil, nil | ||
end | end | ||
local claims | local claims | ||
if | if qid and mw.wikibase.entityExists(qid) and mw.wikibase.getBestStatements(qid, propertyID)[1] then | ||
claims = | claims = mw.wikibase.getBestStatements(qid, propertyID) | ||
if not claims then | if not claims then | ||
return false, "", nil, nil | return false, "", nil, nil | ||
| Line 456: | Line 455: | ||
return false, "", nil, nil | return false, "", nil, nil | ||
end | end | ||
return true | return true, claims | ||
end | end | ||
local function isType(claims, type) | local function isType(claims, type) | ||
return claims | return claims and claims.mainsnak.snaktype == "value" and claims.mainsnak.datavalue.type == type | ||
end | end | ||
local function getValue( | local function getValue(claims, delim, labelHook) | ||
if labelHook == nil then | if labelHook == nil then | ||
labelHook = function (qnumber) | labelHook = function (qnumber) | ||
| Line 467: | Line 466: | ||
end | end | ||
end | end | ||
if isType(claims, "wikibase-entityid") then | local out = {} | ||
if claims[1] then | |||
local i = 1 | |||
while claims[i] ~= nil do | |||
if isType(claims[i], "wikibase-entityid") then | |||
local qnumber = "Q" .. claims[i].mainsnak.datavalue.value["numeric-id"] | |||
local sitelink = mw.wikibase.getSitelink(qnumber) | |||
out[#out + 1] = "[[" .. | local label = labelHook(qnumber) or mw.wikibase.getLabel(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 | |||
else | else | ||
out[#out + 1] = | out[#out + 1] = mw.wikibase.renderSnak(claims[i].mainsnak) | ||
end | end | ||
i = i + 1 | |||
end | end | ||
end | end | ||
return table.concat(out, delim) | |||
end | end | ||
| Line 522: | Line 524: | ||
delim = delimdefault | delim = delimdefault | ||
end | end | ||
local go | local go, claims = parseInput(frame) | ||
if not go then | if not go then | ||
return | return claims | ||
end | end | ||
return getValue( | return getValue(claims, delim) | ||
end | end | ||
-- Same as above, but uses the short name property for label if available. | -- Same as above, but uses the short name property for label if available. | ||
p.getValueShortName = function(frame) | p.getValueShortName = function(frame) | ||
local go | local go, claims = parseInput(frame) | ||
if not go then | if not go then | ||
return | return claims | ||
end | end | ||
-- if wiki-linked value output as link if possible | -- if wiki-linked value output as link if possible | ||
local function labelHook (qnumber) | local function labelHook (qnumber) | ||
local label | local label | ||
local | local i = 1 | ||
if mw.wikibase.entityExists(qnumber) then | |||
if | if mw.wikibase.getBestStatements(qnumber, "P1813")[1] then | ||
while mw.wikibase.getBestStatements(qnumber, "P1813")[i] ~= nil do | |||
if | if mw.wikibase.getBestStatements(qnumber, "P1813")[i].mainsnak.datavalue.value.language == "en" then | ||
label = | label = mw.wikibase.getBestStatements(qnumber, "P1813")[i].mainsnak.datavalue.value.text | ||
end | end | ||
i = i + 1 | |||
end | end | ||
end | end | ||
| Line 552: | Line 554: | ||
return label | return label | ||
end | end | ||
return getValue( | return getValue(claims, ", ", labelHook); | ||
end | end | ||
| Line 565: | Line 567: | ||
local input_parm = mw.text.trim(frame.args[3] or "") | local input_parm = mw.text.trim(frame.args[3] or "") | ||
if input_parm == "FETCH_WIKIDATA" then | if input_parm == "FETCH_WIKIDATA" then | ||
local claims | local claims | ||
if | if frame.args[1] and mw.wikibase.entityExists(itemID) and mw.wikibase.getBestStatements(itemID, propertyID)[1] then | ||
claims = | claims = mw.wikibase.getBestStatements(itemID, propertyID) | ||
end | end | ||
if claims then | if claims then | ||
return getValue( | return getValue(claims, ", ") | ||
else | else | ||
return "" | return "" | ||
| Line 584: | Line 585: | ||
local input_parm = mw.text.trim(frame.args[3] or "") | local input_parm = mw.text.trim(frame.args[3] or "") | ||
if input_parm == "FETCH_WIKIDATA" then | if input_parm == "FETCH_WIKIDATA" then | ||
local | local entityid = mw.wikibase.getEntityIdForCurrentPage() | ||
if | if mw.wikibase.entityExists(entityid) ~= nil and mw.wikibase.getBestStatements(entityid, propertyID)[1] ~= nil then | ||
local out = {} | local out = {} | ||
local i = 1 | |||
for k2, v2 in pairs( | while mw.wikibase.getBestStatements(entityid, propertyID)[i] ~= nil do | ||
for k2, v2 in pairs(mw.wikibase.getBestStatements(entityid, propertyID)[i].qualifiers[qualifierID]) do | |||
if v2.snaktype == 'value' then | if v2.snaktype == 'value' then | ||
out[#out + 1] = outputHook(v2); | out[#out + 1] = outputHook(v2); | ||
end | end | ||
end | end | ||
i = i + 1 | |||
end | end | ||
return table.concat(out, ", "), true | return table.concat(out, ", "), true | ||
| Line 616: | Line 619: | ||
-- This is used to get a value like 'male' (for property p21) which won't be linked and numbers without the thousand separators | -- 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) | p.getRawValue = function(frame) | ||
local go | local go, claims = parseInput(frame) | ||
if not go then | if not go then | ||
return | return claims | ||
end | end | ||
local | local result = {} | ||
local | local i = 1 | ||
-- if number type: remove thousand separators, bounds and units | while claims[i] ~= nil do | ||
-- if number type: remove thousand separators, bounds and units | |||
if isType(claims[i], "quantity") then | |||
result[#result +1] = mw.ustring.gsub(mw.wikibase.renderSnak(claims[i].mainsnak), "(%d),(%d)", "%1%2") | |||
result[#result +1] = mw.ustring.gsub(mw.wikibase.renderSnak(claims[i].mainsnak), "(%d)±.*", "%1") | |||
else | |||
result[#result + 1] = mw.wikibase.renderSnak(claims[i].mainsnak) | |||
end | |||
i = i + 1 | |||
end | end | ||
return result | return table.concat(result, ', ') | ||
end | end | ||
-- This is used to get the unit name for the numeric value returned by getRawValue | -- This is used to get the unit name for the numeric value returned by getRawValue | ||
p.getUnits = function(frame) | p.getUnits = function(frame) | ||
local go | local go, claims = parseInput(frame) | ||
if not go then | if not go then | ||
return | return claims | ||
end | end | ||
local | local result = {} | ||
local | local i = 1 | ||
if isType(claims, "quantity") then | while claims[i] ~= nil do | ||
local i = 1 | |||
if isType(claims[i], "quantity") then | |||
result[#result +1] = mw.ustring.sub(mw.renderSnak(claims[i].mainsnak), mw.ustring.find(result, " ")+1, -1) | |||
else | |||
result[#result + 1] = mw.renderSnak(claims[i].mainsnak) | |||
end | |||
i = i + 1 | |||
end | end | ||
return result | return result | ||
| Line 646: | Line 660: | ||
-- This is used to get the unit's QID to use with the numeric value returned by getRawValue | -- This is used to get the unit's QID to use with the numeric value returned by getRawValue | ||
p.getUnitID = function(frame) | p.getUnitID = function(frame) | ||
local go | local go, claims = parseInput(frame) | ||
if not go then | if not go then | ||
return | return claims | ||
end | end | ||
local result | local result | ||
if isType(claims, "quantity") then | if isType(claims[1], "quantity") then | ||
-- get the url for the unit entry on Wikidata: | -- get the url for the unit entry on Wikidata: | ||
result = claims[1].mainsnak.datavalue.value.unit | result = claims[1].mainsnak.datavalue.value.unit | ||
| Line 683: | Line 696: | ||
local date_format = mw.text.trim(frame.args[3] or i18n["datetime"]["default-format"]) | 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 date_addon = mw.text.trim(frame.args[4] or i18n["datetime"]["default-addon"]) | ||
local go | local go, claims = parseInput(frame) | ||
if not go then | if not go then | ||
return | return claims | ||
end | end | ||
local out = {} | local out = {} | ||
local i = 1 | |||
if | while claims[i] ~= nil do | ||
local timestamp = | if claims[i].mainsnak.datavalue.type == 'time' then | ||
local dateprecision = | local timestamp = claims[i].mainsnak.datavalue.value.time | ||
local dateprecision = claims[i].mainsnak.datavalue.value.precision | |||
-- A year can be stored like this: "+1872-00-00T00:00:00Z", | -- A year can be stored like this: "+1872-00-00T00:00:00Z", | ||
-- which is processed here as if it were the day before "+1872-01-01T00:00:00Z", | -- which is processed here as if it were the day before "+1872-01-01T00:00:00Z", | ||
| Line 699: | Line 712: | ||
timestamp = timestamp:gsub("%-00%-00T", "-01-01T") | timestamp = timestamp:gsub("%-00%-00T", "-01-01T") | ||
out[#out + 1] = parseDateFull(timestamp, dateprecision, date_format, date_addon) | out[#out + 1] = parseDateFull(timestamp, dateprecision, date_format, date_addon) | ||
i = i + 1 | |||
end | end | ||
end | end | ||
| Line 722: | Line 736: | ||
local sep = mw.text.trim(frame.args[3] or " ") | local sep = mw.text.trim(frame.args[3] or " ") | ||
local imgsize = mw.text.trim(frame.args[4] or "frameless") | local imgsize = mw.text.trim(frame.args[4] or "frameless") | ||
local go | local go, claims = parseInput(frame) | ||
if not go then | if not go then | ||
return | return claims | ||
end | end | ||
if (claims[1] and claims[1].mainsnak.datatype == "commonsMedia") then | if (claims[1] and claims[1].mainsnak.datatype == "commonsMedia") then | ||
local out = {} | local out = {} | ||
local i = 1 | |||
local filename = | while claims[i] ~= nil do | ||
local filename = claims[i].mainsnak.datavalue.value | |||
out[#out + 1] = "[[File:" .. filename .. "|" .. imgsize .. "]]" | out[#out + 1] = "[[File:" .. filename .. "|" .. imgsize .. "]]" | ||
i = i + 1 | |||
end | end | ||
return table.concat(out, sep) | return table.concat(out, sep) | ||
| Line 744: | Line 759: | ||
-- formatPropertyValues returns a table with the P1323 values concatenated with ", " so we have to split them out into a table in order to construct the return string | -- formatPropertyValues returns a table with the P1323 values concatenated with ", " so we have to split them out into a table in order to construct the return string | ||
p.getTAValue = function(frame) | p.getTAValue = function(frame) | ||
local | local entid = mw.wikibase.getEntityIdForCurrentPage() | ||
local props = | local props = mw.wikibase.getEntity(entid):formatPropertyValues('P1323') | ||
local out = {} | local out = {} | ||
local t = {} | local t = {} | ||
| Line 798: | Line 813: | ||
local input_parm = mw.text.trim(frame.args[1] or "") | local input_parm = mw.text.trim(frame.args[1] or "") | ||
if input_parm == "FETCH_WIKIDATA" then | if input_parm == "FETCH_WIKIDATA" then | ||
local imglbl | local imglbl | ||
if | if id and mw.wikibase.entityExists(id) and mw.wikibase.getBestStatements(id, "P18")[1] and | ||
mw.wikibase.getBestStatements(id, "P18")[1].mainsnak.datavalue and | |||
mw.wikibase.getBestStatements(id, "P18")[1].rank == "preferred" then -- in getBestStatements if there is an preferred value, it is ordered first. | |||
imglbl = mw.wikibase.getBestStatements(id, "P18")[1].mainsnak.datavalue.value | |||
elseif id and mw.wikibase.entityExists(id) and mw.wikibase.getBestStatements(id, "P18")[1] and | |||
mw.wikibase.getBestStatements(id, "P18")[1].mainsnak.datavalue then -- only normal and preferred rank values outputted in beststatements | |||
imglbl = mw.wikibase.getBestStatements(id, "P18")[1].mainsnak.datavalue.value | |||
end | end | ||
return imglbl | return imglbl | ||
else | else | ||
| Line 843: | Line 834: | ||
p.getPropertyIDs = function(frame) | p.getPropertyIDs = function(frame) | ||
local go | local go, propclaims = parseInput(frame) | ||
if not go then | if not go then | ||
return | return propclaims | ||
end | end | ||
-- if wiki-linked value collect the QID in a table | -- if wiki-linked value collect the QID in a table | ||
if (propclaims[1] and propclaims[1].mainsnak.snaktype == "value" and propclaims[1].mainsnak.datavalue.type == "wikibase-entityid") then | if (propclaims[1] and propclaims[1].mainsnak.snaktype == "value" and propclaims[1].mainsnak.datavalue.type == "wikibase-entityid") then | ||
local out = {} | local out = {} | ||
local i = 1 | |||
out[#out + 1] = "Q" .. | while claims[i] ~= nil do | ||
out[#out + 1] = "Q" .. claims[i].mainsnak.datavalue.value["numeric-id"] | |||
i = i + 1 | |||
end | end | ||
return table.concat(out, ", ") | return table.concat(out, ", ") | ||
| Line 868: | Line 860: | ||
function p.claim(frame) | function p.claim(frame) | ||
local property = frame.args[1] or "" | local property = frame.args[1] or "" | ||
local id = frame.args["id"] | local id = frame.args["id"] or mw.wikibase.getEntityIdForCurrentPage() | ||
local qualifierId = frame.args["qualifier"] | local qualifierId = frame.args["qualifier"] | ||
local parameter = frame.args["parameter"] | local parameter = frame.args["parameter"] | ||
| Line 877: | Line 869: | ||
if default then showerrors = nil end | if default then showerrors = nil end | ||
-- | -- check wikidata entity | ||
if not mw.wikibase.entityExists(id) then | |||
if showerrors then return printError("entity-not-found") else return default end | if showerrors then return printError("entity-not-found") else return default end | ||
end | end | ||
-- fetch the first claim of satisfying the given property | -- fetch the first claim of satisfying the given property | ||
local claims = | local claims = mw.wikibase.getBestStatements(id, property) or nil | ||
if not claims or not claims[1] then | if not claims or not claims[1] then | ||
if showerrors then return printError("property-not-found") else return default end | if showerrors then return printError("property-not-found") else return default end | ||
end | end | ||
-- | -- no need to sort, bestStatements orders preferred rank first | ||
local result | local result | ||
local error | local error | ||
local i = 1 | |||
if list then | if list then | ||
local value | local value | ||
| Line 909: | Line 890: | ||
result = {} | result = {} | ||
for idx in pairs(claims) do | for idx in pairs(claims) do | ||
local claim = claims[ | local claim = claims[i] | ||
value, error = getValueOfClaim(claim, qualifierId, parameter) | value, error = getValueOfClaim(claim, qualifierId, parameter) | ||
if not value and showerrors then value = error end | if not value and showerrors then value = error end | ||
if value and references then value = value .. getReferences(frame, claim) end | if value and references then value = value .. getReferences(frame, claim) end | ||
result[#result + 1] = value | result[#result + 1] = value | ||
i = i + 1 | |||
end | end | ||
result = table.concat(result, list) | result = table.concat(result, list) | ||
else | else | ||
-- return first element | -- return first element | ||
local claim = claims[ | local claim = claims[i] | ||
result, error = getValueOfClaim(claim, qualifierId, parameter) | result, error = getValueOfClaim(claim, qualifierId, parameter) | ||
if result and references then result = result .. getReferences(frame, claim) end | if result and references then result = result .. getReferences(frame, claim) end | ||
| Line 936: | Line 918: | ||
end | end | ||
local data = mw.wikibase.getEntity(id) | local data = mw.wikibase.getEntity(id) | ||
if not | if not id or not mw.wikibase.entityExists(id) then | ||
return nil | return nil | ||
end | end | ||
| Line 966: | Line 948: | ||
if qid == "" then qid = nil end | if qid == "" then qid = nil end | ||
local f = mw.text.trim( frame.args[1] or "") | local f = mw.text.trim( frame.args[1] or "") | ||
if not quid or not mw.wikibase.entityExists(qid) then | |||
return | return | ||
end | end | ||
local link = | local link = mw.wikibase.getSitelink( f ) | ||
if not link then | if not link then | ||
return | return | ||