Module:Wd: Difference between revisions
Jump to navigation
Jump to search
>Thayts No edit summary |
>Thayts (Added methods propertyWithUnit, qualifierWithUnit and propertyWithQualifier) |
||
Line 7: | Line 7: | ||
local bestRank = true | local bestRank = true | ||
local foundRank = 3 | local foundRank = 3 | ||
local maxRank, minRank | |||
local | local linked = false | ||
local propertyWithQualifier = false | |||
local withUnit = false | |||
local function unknownDatatypeError(type) | local function unknownDatatypeError(type) | ||
Line 14: | Line 18: | ||
end | end | ||
local function getValue(snak) | local function convertUnit(unit) | ||
local i, j | |||
if unit == "" or unit == "1" then | |||
return nil | |||
end | |||
if unit:match('^http[s]?://') then | |||
i, j = unit:find("Q") | |||
if i then | |||
local itemID = unit:sub(i) | |||
if itemID == "Q11229" then -- percentage | |||
return "%" | |||
else | |||
local label = mw.wikibase.label(itemID) | |||
if label ~= "" then | |||
if linked then | |||
local linkTarget = mw.wikibase.sitelink(itemID) | |||
if linkTarget then | |||
return " " .. "[[" .. linkTarget .. "|" .. (label or linkTarget) .. "]]" | |||
end | |||
end | |||
return " " .. label | |||
else | |||
return " " .. itemID | |||
end | |||
end | |||
else | |||
return " " .. unit | |||
end | |||
end | |||
return " " .. unit | |||
end | |||
local function getValue(snak, withUnit) | |||
withUnit = withUnit or false | |||
if snak.snaktype ~= 'value' then return nil end | if snak.snaktype ~= 'value' then return nil end | ||
Line 27: | Line 73: | ||
elseif snak.datavalue.type == 'quantity' then | elseif snak.datavalue.type == 'quantity' then | ||
-- strip + signs from front | -- strip + signs from front | ||
local value = mw.ustring.gsub(snak.datavalue.value['amount'], "\+(.+)", "%1") | |||
if withUnit then | |||
local unit = convertUnit(snak.datavalue.value['unit']) | |||
if unit then | |||
value = value .. unit | |||
end | |||
end | |||
return value | |||
elseif snak.datavalue.type == 'wikibase-entityid' then | elseif snak.datavalue.type == 'wikibase-entityid' then | ||
return mw.wikibase.label("Q" .. snak.datavalue.value['numeric-id']) | return mw.wikibase.label("Q" .. snak.datavalue.value['numeric-id']) | ||
Line 33: | Line 86: | ||
return unknownDatatypeError(snak.datavalue.type) | return unknownDatatypeError(snak.datavalue.type) | ||
end | end | ||
end | |||
local function getValueWithUnit(snak) | |||
return getValue(snak, true) | |||
end | end | ||
Line 162: | Line 219: | ||
p.property = function(frame) | p.property = function(frame) | ||
local entity, propertyID, claims, rankPos | local entity, propertyID, claims, rankPos | ||
local nextArg = mw.text.trim(frame.args[1] or "") | local nextArg = mw.text.trim(frame.args[1] or "") | ||
local nextIndex = 2 | local nextIndex = 2 | ||
Line 203: | Line 259: | ||
end | end | ||
else | else | ||
local value = getValue(v.mainsnak) | local value | ||
if withUnit then | |||
value = getValueWithUnit(v.mainsnak) | |||
else | |||
value = getValue(v.mainsnak) | |||
end | |||
if value then appendOutput(value, rankPos) end | if value then appendOutput(value, rankPos) end | ||
end | end | ||
Line 215: | Line 276: | ||
end | end | ||
p.propertyWithUnit = function(frame) | |||
withUnit = true | |||
return p.property(frame) | |||
end | |||
p.qualifier = function(frame) | p.qualifier = function(frame) | ||
local entity, propertyID, propertyValue, qualifierID, claims, rankPos | local entity, propertyID, propertyValue, qualifierID, claims, rankPos, outValue, outQualifier | ||
local nextArg = mw.text.trim(frame.args[1] or "") | local nextArg = mw.text.trim(frame.args[1] or "") | ||
local nextIndex = 2 | local nextIndex = 2 | ||
Line 265: | Line 330: | ||
for k2, v2 in pairs(v.qualifiers[qualifierID]) do | for k2, v2 in pairs(v.qualifiers[qualifierID]) do | ||
if v2.snaktype == 'value' then | if v2.snaktype == 'value' then | ||
outValue = "" | |||
outQualifier = "" | |||
if linked and v2.datavalue.type == 'wikibase-entityid' then | if linked and v2.datavalue.type == 'wikibase-entityid' then | ||
local itemID = "Q" .. v2.datavalue.value['numeric-id'] | local itemID = "Q" .. v2.datavalue.value['numeric-id'] | ||
Line 271: | Line 339: | ||
if linkTarget then | if linkTarget then | ||
outQualifier = "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" | |||
elseif linkName then | elseif linkName then | ||
outQualifier = linkName | |||
end | end | ||
else | else | ||
local value = getValue(v2) | local value | ||
if value then | if withUnit then | ||
value = getValueWithUnit(v2) | |||
else | |||
value = getValue(v2) | |||
end | |||
if value then outQualifier = value end | |||
end | end | ||
if propertyWithQualifier then | |||
outQualifier = " <span style=\"font-size:smaller\">(" .. outQualifier .. ")</span>" | |||
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 | |||
outValue = outValue .. "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" | |||
elseif linkName then | |||
outValue = outValue .. linkName | |||
end | |||
else | |||
local value | |||
if withUnit then | |||
value = getValueWithUnit(v.mainsnak) | |||
else | |||
value = getValue(v.mainsnak) | |||
end | |||
if value then outValue = outValue .. value end | |||
end | |||
end | |||
outValue = outValue .. outQualifier | |||
appendOutput(outValue, rankPos) | |||
end | end | ||
end | end | ||
Line 288: | Line 388: | ||
return "" | return "" | ||
end | end | ||
end | |||
p.qualifierWithUnit = function(frame) | |||
withUnit = true | |||
return p.qualifier(frame) | |||
end | |||
p.propertyWithQualifier = function(frame) | |||
propertyWithQualifier = true | |||
withUnit = true | |||
return p.qualifier(frame) | |||
end | end | ||
Revision as of 09:29, 28 August 2016
Documentation for this module may be created at Module:Wd/doc
local p = {} local outPreferred = {} local outNormal = {} local outDeprecated = {} local bestRank = true local foundRank = 3 local maxRank, minRank local linked = false local propertyWithQualifier = false local withUnit = false local function unknownDatatypeError(type) return "<strong class=\"error\">Unknown or unsupported datatype '" .. type .. "'</strong>" end local function convertUnit(unit) local i, j if unit == "" or unit == "1" then return nil end if unit:match('^http[s]?://') then i, j = unit:find("Q") if i then local itemID = unit:sub(i) if itemID == "Q11229" then -- percentage return "%" else local label = mw.wikibase.label(itemID) if label ~= "" then if linked then local linkTarget = mw.wikibase.sitelink(itemID) if linkTarget then return " " .. "[[" .. linkTarget .. "|" .. (label or linkTarget) .. "]]" end end return " " .. label else return " " .. itemID end end else return " " .. unit end end return " " .. unit end local function getValue(snak, withUnit) withUnit = withUnit or false 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 local value = mw.ustring.gsub(snak.datavalue.value['amount'], "\+(.+)", "%1") if withUnit then local unit = convertUnit(snak.datavalue.value['unit']) if unit then value = value .. unit end end return value 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 getValueWithUnit(snak) return getValue(snak, true) 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 local function setRankBoundaries(rank) local rankPos if (rank == "best") then return -- use defaults else bestRank = false end if (rank == "all") then maxRank = 1 minRank = 3 return end if (rank:sub(1,9) == "preferred") then rankPos = 1 elseif (rank:sub(1,6) == "normal") then rankPos = 2 elseif (rank:sub(1,10) == "deprecated") then rankPos = 3 end if (rank:sub(-1) == "+") then maxRank = 1 minRank = rankPos elseif (rank:sub(-1) == "-") then maxRank = rankPos minRank = 3 else maxRank = rankPos minRank = rankPos end end local function convertRank(rank) if (rank == "preferred") then return 1 elseif (rank == "normal") then return 2 elseif (rank == "deprecated") then return 3 else return 4 -- default (in its literal sense) end end local function rankMatches(rankPos) if bestRank then if foundRank > rankPos then foundRank = rankPos -- found a better rank, reset worse rank outputs if foundRank == 1 then outNormal = {} outDeprecated = {} elseif foundRank == 2 then outDeprecated = {} end end return foundRank >= rankPos -- == would also work here else return (maxRank <= rankPos and rankPos <= minRank) end end local function appendOutput(value, rankPos) if rankPos == 1 then outPreferred[#outPreferred + 1] = value elseif rankPos == 2 then outNormal[#outNormal + 1] = value elseif rankPos == 3 then outDeprecated[#outDeprecated + 1] = value end end local function out() local out = "" outPreferred = table.concat(outPreferred, ", ") outNormal = table.concat(outNormal, ", ") outDeprecated = table.concat(outDeprecated, ", ") if outPreferred ~= "" then out = outPreferred end if outNormal ~= "" then if out ~= "" then out = out .. "; " end out = out .. outNormal end if outDeprecated ~= "" then if out ~= "" then out = out .. "; " end out = out .. outDeprecated end return out end p.property = function(frame) local entity, propertyID, claims, rankPos local nextArg = mw.text.trim(frame.args[1] or "") local nextIndex = 2 if nextArg == "best" or nextArg == "all" or nextArg:match('^preferred[+-]?$') or nextArg:match('^normal[+-]?$') or nextArg:match('^deprecated[+-]?$') then setRankBoundaries(nextArg) nextArg = mw.text.trim(frame.args[nextIndex] or "") nextIndex = nextIndex + 1 end 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 for k, v in pairs(claims) do rankPos = convertRank(v.rank) if rankMatches(rankPos) then 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 appendOutput("[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]", rankPos) elseif linkName then appendOutput(linkName, rankPos) end else local value if withUnit then value = getValueWithUnit(v.mainsnak) else value = getValue(v.mainsnak) end if value then appendOutput(value, rankPos) end end end end end return out() else return "" end end p.propertyWithUnit = function(frame) withUnit = true return p.property(frame) end p.qualifier = function(frame) local entity, propertyID, propertyValue, qualifierID, claims, rankPos, outValue, outQualifier local nextArg = mw.text.trim(frame.args[1] or "") local nextIndex = 2 if nextArg == "best" or nextArg == "all" or nextArg:match('^preferred[+-]?$') or nextArg:match('^normal[+-]?$') or nextArg:match('^deprecated[+-]?$') then setRankBoundaries(nextArg) nextArg = mw.text.trim(frame.args[nextIndex] or "") nextIndex = nextIndex + 1 end 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 qualifierID = nextArg nextArg = mw.text.trim(frame.args[nextIndex] or "") nextIndex = nextIndex + 1 if nextArg == "" then propertyValue = nil else propertyValue = qualifierID qualifierID = nextArg end if entity and entity.claims then claims = entity.claims[propertyID] end if claims then for k, v in pairs(claims) do rankPos = convertRank(v.rank) if (not propertyValue or (v.mainsnak.snaktype == 'value' and snakEqualsValue(v.mainsnak, propertyValue))) and v.qualifiers[qualifierID] then if rankMatches(rankPos) then for k2, v2 in pairs(v.qualifiers[qualifierID]) do if v2.snaktype == 'value' then outValue = "" outQualifier = "" 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 outQualifier = "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" elseif linkName then outQualifier = linkName end else local value if withUnit then value = getValueWithUnit(v2) else value = getValue(v2) end if value then outQualifier = value end end if propertyWithQualifier then outQualifier = " <span style=\"font-size:smaller\">(" .. outQualifier .. ")</span>" 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 outValue = outValue .. "[[" .. linkTarget .. "|" .. (linkName or linkTarget) .. "]]" elseif linkName then outValue = outValue .. linkName end else local value if withUnit then value = getValueWithUnit(v.mainsnak) else value = getValue(v.mainsnak) end if value then outValue = outValue .. value end end end outValue = outValue .. outQualifier appendOutput(outValue, rankPos) end end end end end return out() else return "" end end p.qualifierWithUnit = function(frame) withUnit = true return p.qualifier(frame) end p.propertyWithQualifier = function(frame) propertyWithQualifier = true withUnit = true return p.qualifier(frame) 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