Module:Wd: Difference between revisions
Jump to navigation
Jump to search
>Thayts (Added nice error message for unknown or unsupported datatypes) |
>Thayts (Added support for monolingualtext) |
||
Line 5: | Line 5: | ||
end | end | ||
local function | local function getValue(snak) | ||
if snak.snaktype ~= 'value' then return nil end | |||
if snak.datavalue.type == 'string' then | |||
return snak.datavalue.value | |||
elseif snak.datavalue.type == 'monolingualtext' then | |||
if snak.datavalue.value['language'] == "en" then | |||
if snak. | return snak.datavalue.value['text'] | ||
else | |||
return nil | |||
end | |||
elseif snak.datavalue.type == 'quantity' then | elseif snak.datavalue.type == 'quantity' then | ||
return | -- strip + signs from front | ||
elseif snak.datavalue.type == ' | return mw.ustring.gsub(snak.datavalue.value['amount'], "\+(.+)", "%1") | ||
return snak.datavalue.value | elseif snak.datavalue.type == 'wikibase-entityid' then | ||
return mw.wikibase.label("Q" .. snak.datavalue.value['numeric-id']) | |||
else | else | ||
return unknownDatatypeError(snak.datavalue.type) | return unknownDatatypeError(snak.datavalue.type) | ||
Line 26: | Line 26: | ||
end | end | ||
local function | local function getRawValue(snak) | ||
if snak.snaktype ~= 'value' then return | if snak.snaktype ~= 'value' then return nil end | ||
if snak.datavalue.type == 'wikibase-entityid' then | if snak.datavalue.type == 'wikibase-entityid' then | ||
return | return "Q" .. snak.datavalue.value['numeric-id'] | ||
else | else | ||
return | return getValue(snak) | ||
end | end | ||
end | end | ||
Line 43: | Line 39: | ||
local snakValue = getRawValue(snak) | local snakValue = getRawValue(snak) | ||
if snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end | if snakValue and snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end | ||
return snakValue == value | return snakValue == value | ||
Line 80: | Line 76: | ||
if linkTarget then | if linkTarget then | ||
out[#out + 1] = "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" | out[#out + 1] = "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" | ||
elseif linkName then | |||
out[#out + 1] = linkName | out[#out + 1] = linkName | ||
end | end | ||
else | else | ||
out[#out + 1] = | local value = getValue(v.mainsnak) | ||
if value then out[#out + 1] = value end | |||
end | end | ||
end | end | ||
Line 143: | Line 140: | ||
if linkTarget then | if linkTarget then | ||
out[#out + 1] = "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" | out[#out + 1] = "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" | ||
elseif linkName then | |||
out[#out + 1] = linkName | out[#out + 1] = linkName | ||
end | end | ||
else | else | ||
out[#out + 1] = | local value = getValue(v2) | ||
if value then out[#out + 1] = value end | |||
end | end | ||
end | end |
Revision as of 15:12, 21 August 2016
Documentation for this module may be created at Module:Wd/doc
local p = {} local function unknownDatatypeError(type) return "<strong class=\"error\">Unknown or unsupported datatype '" .. type .. "'</strong>" end local function getValue(snak) if snak.snaktype ~= 'value' then return nil end if snak.datavalue.type == 'string' then return snak.datavalue.value elseif snak.datavalue.type == 'monolingualtext' then if snak.datavalue.value['language'] == "en" then return snak.datavalue.value['text'] else return nil end elseif snak.datavalue.type == 'quantity' then -- strip + signs from front return mw.ustring.gsub(snak.datavalue.value['amount'], "\+(.+)", "%1") elseif snak.datavalue.type == 'wikibase-entityid' then return mw.wikibase.label("Q" .. snak.datavalue.value['numeric-id']) else return unknownDatatypeError(snak.datavalue.type) end end local function getRawValue(snak) if snak.snaktype ~= 'value' then return nil end if snak.datavalue.type == 'wikibase-entityid' then return "Q" .. snak.datavalue.value['numeric-id'] else return getValue(snak) end end local function snakEqualsValue(snak, value) local snakValue = getRawValue(snak) if snakValue and snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end return snakValue == value end p.property = function(frame) local entity, propertyID, claims local linked = false local nextArg = mw.text.trim(frame.args[1] or "") local nextIndex = 2 if nextArg == "linked" then linked = true nextArg = mw.text.trim(frame.args[nextIndex] or "") nextIndex = nextIndex + 1 end if nextArg:sub(1,1):upper() == "Q" then entity = mw.wikibase.getEntity(nextArg) propertyID = mw.text.trim(frame.args[nextIndex] or "") else entity = mw.wikibase.getEntity() propertyID = nextArg end if entity and entity.claims then claims = entity.claims[propertyID] end if claims then local out = {} for k, v in pairs(claims) do if v.mainsnak.snaktype == 'value' then if linked and v.mainsnak.datavalue.type == 'wikibase-entityid' then local itemID = "Q" .. v.mainsnak.datavalue.value['numeric-id'] local linkTarget = mw.wikibase.sitelink(itemID) local linkName = mw.wikibase.label(itemID) -- == getValue(v.mainsnak) if linkTarget then out[#out + 1] = "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" elseif linkName then out[#out + 1] = linkName end else local value = getValue(v.mainsnak) if value then out[#out + 1] = value end end end end return table.concat(out, ", ") else return "" end end p.qualifier = function(frame) local entity, propertyID, propertyValue, qualifierID, claims local linked = false local nextArg = mw.text.trim(frame.args[1] or "") local nextIndex = 2 if nextArg == "linked" then linked = true nextArg = mw.text.trim(frame.args[nextIndex] or "") nextIndex = nextIndex + 1 end if nextArg:sub(1,1):upper() == "Q" then entity = mw.wikibase.getEntity(nextArg) propertyID = mw.text.trim(frame.args[nextIndex] or "") nextIndex = nextIndex + 1 else entity = mw.wikibase.getEntity() propertyID = nextArg end nextArg = mw.text.trim(frame.args[nextIndex] or "") nextIndex = nextIndex + 1 if nextArg:sub(1,1):upper() == "P" then propertyValue = nil qualifierID = nextArg else -- Escaping: if the first character is '\' followed by 'P' or '\' (i.e. "\P" or "\\") then remove the '\' if nextArg:sub(1,2):upper() == "\\P" or nextArg:sub(1,2) == "\\\\" then nextArg = nextArg:sub(2) end propertyValue = nextArg qualifierID = mw.text.trim(frame.args[nextIndex] or "") nextIndex = nextIndex + 1 end if entity and entity.claims then claims = entity.claims[propertyID] end if claims then local out = {} for k, v in pairs(claims) do if (not propertyValue or (v.mainsnak.snaktype == 'value' and snakEqualsValue(v.mainsnak, propertyValue))) and v.qualifiers[qualifierID] then for k2, v2 in pairs(v.qualifiers[qualifierID]) do if v2.snaktype == 'value' then if linked and v2.datavalue.type == 'wikibase-entityid' then local itemID = "Q" .. v2.datavalue.value['numeric-id'] local linkTarget = mw.wikibase.sitelink(itemID) local linkName = mw.wikibase.label(itemID) -- == getValue(v2) if linkTarget then out[#out + 1] = "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" elseif linkName then out[#out + 1] = linkName end else local value = getValue(v2) if value then out[#out + 1] = value end end end end end end return table.concat(out, ", ") else return "" end end p.label = function(frame) local linked = false local nextArg = mw.text.trim(frame.args[1] or "") local nextIndex = 2 if nextArg == "linked" then linked = true nextArg = mw.text.trim(frame.args[nextIndex] or "") nextIndex = nextIndex + 1 end if nextArg then if linked and nextArg:sub(1,1):upper() == "Q" then local linkTarget = mw.wikibase.sitelink(nextArg) local linkName = mw.wikibase.label(nextArg) if linkTarget then return "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" else return linkName end else return mw.wikibase.label(nextArg) end else return mw.wikibase.label() end end return p