Content deleted Content added
Added option to get the short name of a property instead of the label, and an option to get a single value (best first match) |
|||
Line 1:
local p = {}
State.__index = State
-- allows for recursive calls
function State.new()
local
setmetatable(stt, State)
stt.outPreferred = {}
stt.outNormal = {}
stt.outDeprecated = {}
stt.bestRank = true
stt.foundRank = 3
stt.maxRank = nil
stt.minRank = nil
stt.linked = false
stt.propertyWithQualifier = false
stt.withUnit = false
stt.shortName = false
stt.singleValue = false
stt:setRankBoundaries("best")
return stt
end
return "<strong class=\"error\">Unknown or unsupported datatype '" .. type .. "'</strong>"
end
local i, j, itemID, label, target
Line 31 ⟶ 47:
itemID = unit:sub(i)
if itemID == "Q11229" then -- 'percentage'
return "%"
else
Line 37 ⟶ 53:
target = nil
if self.linked or label == nil then
target = mw.wikibase.sitelink(itemID)
end
if self.linked then
if target then
return " " .. "[[" .. target .. "|" .. (label or target) .. "]]"
Line 59 ⟶ 75:
end
return p.property({args={"single", itemID, "P1813"}}) -- 'short name'
end
function State:getValue(snak, appendUnit)
appendUnit = appendUnit or false
Line 74 ⟶ 94:
-- strip + signs from front
local value = mw.ustring.gsub(snak.datavalue.value['amount'], "\+(.+)", "%1")
if appendUnit then
local unit = self:convertUnit(snak.datavalue.value['unit'])
if unit then
value = value .. unit
end
end
return value
elseif snak.datavalue.type == 'wikibase-entityid' then
local value = ""
local target = nil
local itemID = "Q" .. snak.datavalue.value['numeric-id']
if self.shortName then
value = self:getShortName(itemID)
end
if value == "" then
value = mw.wikibase.label(itemID)
end
if self.linked or value == nil then
target = mw.wikibase.sitelink(itemID)
end
if self.linked then
if target then
value = "[[" .. target .. "|" .. (value or target) .. "]]"
Line 94 ⟶ 129:
value = (target or itemID)
end
return value
else
return self:unknownDatatypeError(snak.datavalue.type)
end
elseif snak.snaktype == 'somevalue' then
Line 107 ⟶ 143:
end
local temp, value
if snak.snaktype == 'value' and snak.datavalue.type == 'wikibase-entityid' then
return "Q" .. snak.datavalue.value['numeric-id']
else
temp = self.linked
self.linked = false
value = self:getValue(snak, false)
self.linked = temp
return value
end
end
if snak.snaktype == 'value' then
local snakValue = self:getRawValue(snak)
if snakValue and snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end
Line 144 ⟶ 180:
end
local rankPos
if (rank == "best") then
self.bestRank = true
self.foundRank = 3
return
else
self.bestRank = false
end
if (rank == "all") then
self.maxRank = 1
self.minRank = 3
return
end
Line 170 ⟶ 206:
if (rank:sub(-1) == "+") then
self.maxRank = 1
self.minRank = rankPos
elseif (rank:sub(-1) == "-") then
self.maxRank = rankPos
self.minRank = 3
else
self.maxRank = rankPos
self.minRank = rankPos
end
end
if (rank == "preferred") then
return 1
Line 193 ⟶ 229:
end
if self.bestRank then
if self.foundRank > rankPos then
self.foundRank = rankPos
-- found a better rank, reset worse rank outputs
if self.foundRank == 1 then
self.outNormal = {}
self.outDeprecated = {}
elseif self.foundRank == 2 then
self.outDeprecated = {}
end
end
return self.foundRank >= rankPos -- == would also work here
else
return (self.maxRank <= rankPos and rankPos <= self.minRank)
end
end
if rankPos == 1 then
self.outPreferred[#self.outPreferred + 1] = value
elseif rankPos == 2 then
self.outNormal[#self.outNormal + 1] = value
elseif rankPos == 3 then
self.outDeprecated[#self.outDeprecated + 1] = value
end
end
local out = ""
if self.outDeprecated[1] then
if self.singleValue then
else
out = table.concat(self.outDeprecated, ", ")
end
end
if self.outNormal
if
out =
else
if out ~= "" then
out = "; " .. out
end
out = table.concat(self.outNormal, ", ") .. out
end
end
if
if
out =
else
if out ~= "" then
out = "; " .. out
end
out = table.concat(self.outPreferred, ", ") .. out
end
end
Line 253 ⟶ 297:
end
if flag == "linked" then
self.linked = true
return true
elseif flag == "unit" then
self.withUnit = true
return true
elseif flag == "short" then
self.shortName = true
return true
elseif flag == "single" then
self.singleValue = true
return true
elseif flag == "best" or flag == "all" or flag:match('^preferred[+-]?$') or flag:match('^normal[+-]?$') or flag:match('^deprecated[+-]?$') then
self:setRankBoundaries(flag)
return true
else
Line 269 ⟶ 319:
p.property = function(frame)
local _ = State.new()
local entity, propertyID, claims, rankPos, value
local nextArg = mw.text.trim(frame.args[1] or "")
local nextIndex = 2
while _:processFlag(nextArg) do
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextIndex = nextIndex + 1
Line 291 ⟶ 341:
if claims then
for k, v in pairs(claims) do
rankPos = _:convertRank(v.rank)
if _:rankMatches(rankPos) then
value = _:getValue(v.mainsnak, _.withUnit)
if value then _:appendOutput(value, rankPos) end
end
end
return _:out()
else
return ""
Line 303 ⟶ 353:
end
p.qualifier = function(frame, _)
_ = _ or State.new()
local entity, propertyID, propertyValue, qualifierID, claims, qualifiers, rankPos, outValue, outInter, outQualifier
local nextArg = mw.text.trim(frame.args[1] or "")
local nextIndex = 2
while _:processFlag(nextArg) do
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextIndex = nextIndex + 1
Line 348 ⟶ 398:
if claims then
for k, v in pairs(claims) do
rankPos = _:convertRank(v.rank)
if propertyValue == nil or _:snakEqualsValue(v.mainsnak, propertyValue) then
if _:rankMatches(rankPos) then
outValue = nil
outInter = nil
outQualifier = {}
if _.propertyWithQualifier then
outValue = _:getValue(v.mainsnak, _.withUnit)
end
if v.qualifiers then qualifiers = v.qualifiers[qualifierID] end
if (not _.propertyWithQualifier or outValue) and qualifiers then
for k2, v2 in pairs(v.qualifiers[qualifierID]) do
outInter = _:getValue(v2, _.withUnit)
if outInter then
if not _.propertyWithQualifier then
_:appendOutput(outInter, rankPos)
else
outQualifier[#outQualifier + 1] = outInter
Line 373 ⟶ 423:
end
if outValue and _.propertyWithQualifier then
outQualifier = table.concat(outQualifier, ", ")
Line 381 ⟶ 431:
end
_:appendOutput(outValue, rankPos)
end
end
end
end
return _:out()
else
return ""
Line 393 ⟶ 443:
p.propertyWithQualifier = function(frame)
local _ = State.new()
_.withUnit = true
return p.qualifier(frame, _)
end
p.label = function(frame)
local
local label = ""
local target = ""
local nextArg = mw.text.trim(frame.args[1] or "")
local nextIndex = 2
while _:processFlag(nextArg) do
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextIndex = nextIndex + 1
Line 409 ⟶ 463:
if nextArg then
if nextArg:sub(1,1):upper() == "Q" then
if
label = _:getShortName(nextArg)
end
if label == "" then
label = mw.wikibase.label(nextArg)
end
if _.linked or label == nil then
target = mw.wikibase.sitelink(nextArg)
end
if _.linked and target then
end
else
label = mw.wikibase.label(nextArg)
end
|