Module:Wd: Difference between revisions

From Random Island Wiki
Jump to navigation Jump to search
>Thayts
(Undid revision 736947340 by Thayts (talk))
>Thayts
(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: Line 1:
local p = {}
local p = {}


local outPreferred = {}
State = {}
local outNormal = {}
State.__index = State
local outDeprecated = {}


local bestRank = true
-- allows for recursive calls
local foundRank = 3
function State.new()
local maxRank, minRank
local stt = {}
 
setmetatable(stt, State)
local linked = false
 
stt.outPreferred = {}
local propertyWithQualifier = false
stt.outNormal = {}
local withUnit = false
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


local function unknownDatatypeError(type)
function State:unknownDatatypeError(type)
return "<strong class=\"error\">Unknown or unsupported datatype '" .. type .. "'</strong>"
return "<strong class=\"error\">Unknown or unsupported datatype '" .. type .. "'</strong>"
end
end


local function convertUnit(unit)
function State:convertUnit(unit)
local i, j, itemID, label, target
local i, j, itemID, label, target
Line 31: Line 47:
itemID = unit:sub(i)
itemID = unit:sub(i)
if itemID == "Q11229" then  -- percentage
if itemID == "Q11229" then  -- 'percentage'
return "%"
return "%"
else
else
Line 37: Line 53:
target = nil
target = nil
if linked or label == nil then
if self.linked or label == nil then
target = mw.wikibase.sitelink(itemID)
target = mw.wikibase.sitelink(itemID)
end
end
if linked then
if self.linked then
if target then
if target then
return " " .. "[[" .. target .. "|" .. (label or target) .. "]]"
return " " .. "[[" .. target .. "|" .. (label or target) .. "]]"
Line 59: Line 75:
end
end


local function getValue(snak, appendUnit)
function State:getShortName(itemID)
return p.property({args={"single", itemID, "P1813"}})  -- 'short name'
end
 
function State:getValue(snak, appendUnit)
appendUnit = appendUnit or false
appendUnit = appendUnit or false
Line 74: Line 94:
-- strip + signs from front
-- strip + signs from front
local value = mw.ustring.gsub(snak.datavalue.value['amount'], "\+(.+)", "%1")
local value = mw.ustring.gsub(snak.datavalue.value['amount'], "\+(.+)", "%1")
if appendUnit then
if appendUnit then
local unit = convertUnit(snak.datavalue.value['unit'])
local unit = self:convertUnit(snak.datavalue.value['unit'])
if unit then
if unit then
value = value .. unit
value = value .. unit
end
end
end
end
return value
return value
elseif snak.datavalue.type == 'wikibase-entityid' then
elseif snak.datavalue.type == 'wikibase-entityid' then
local value = ""
local target = nil
local itemID = "Q" .. snak.datavalue.value['numeric-id']
local itemID = "Q" .. snak.datavalue.value['numeric-id']
local value = mw.wikibase.label(itemID)
local target = mw.wikibase.sitelink(itemID)
if self.shortName then
if linked 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
if target then
value = "[[" .. target .. "|" .. (value or target) .. "]]"
value = "[[" .. target .. "|" .. (value or target) .. "]]"
Line 94: Line 129:
value = (target or itemID)
value = (target or itemID)
end
end
return value
return value
else
else
return unknownDatatypeError(snak.datavalue.type)
return self:unknownDatatypeError(snak.datavalue.type)
end
end
elseif snak.snaktype == 'somevalue' then
elseif snak.snaktype == 'somevalue' then
Line 107: Line 143:
end
end


local function getRawValue(snak)
function State:getRawValue(snak)
local temp, value
local temp, value
if snak.snaktype == 'value' and snak.datavalue.type == 'wikibase-entityid' then
if snak.snaktype == 'value' and snak.datavalue.type == 'wikibase-entityid' then
return "Q" .. snak.datavalue.value['numeric-id']
return "Q" .. snak.datavalue.value['numeric-id']
else
else
temp = linked
temp = self.linked
linked = false
self.linked = false
value = getValue(snak, false)
value = self:getValue(snak, false)
linked = temp
self.linked = temp
return value
return value
end
end
end
end


local function snakEqualsValue(snak, value)
function State:snakEqualsValue(snak, value)
if snak.snaktype == 'value' then
if snak.snaktype == 'value' then
local snakValue = getRawValue(snak)
local snakValue = self:getRawValue(snak)
if snakValue and snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end
if snakValue and snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end
Line 144: Line 180:
end
end


local function setRankBoundaries(rank)
function State:setRankBoundaries(rank)
local rankPos
local rankPos
if (rank == "best") then
if (rank == "best") then
bestRank = true
self.bestRank = true
foundRank = 3
self.foundRank = 3
return
return
else
else
bestRank = false
self.bestRank = false
end
end
if (rank == "all") then
if (rank == "all") then
maxRank = 1
self.maxRank = 1
minRank = 3
self.minRank = 3
return
return
end
end
Line 170: Line 206:
if (rank:sub(-1) == "+") then
if (rank:sub(-1) == "+") then
maxRank = 1
self.maxRank = 1
minRank = rankPos
self.minRank = rankPos
elseif (rank:sub(-1) == "-") then
elseif (rank:sub(-1) == "-") then
maxRank = rankPos
self.maxRank = rankPos
minRank = 3
self.minRank = 3
else
else
maxRank = rankPos
self.maxRank = rankPos
minRank = rankPos
self.minRank = rankPos
end
end
end
end


local function convertRank(rank)
function State:convertRank(rank)
if (rank == "preferred") then
if (rank == "preferred") then
return 1
return 1
Line 193: Line 229:
end
end


local function rankMatches(rankPos)
function State:rankMatches(rankPos)
if bestRank then
if self.bestRank then
if foundRank > rankPos then
if self.foundRank > rankPos then
foundRank = rankPos
self.foundRank = rankPos
-- found a better rank, reset worse rank outputs
-- found a better rank, reset worse rank outputs
if foundRank == 1 then
if self.foundRank == 1 then
outNormal = {}
self.outNormal = {}
outDeprecated = {}
self.outDeprecated = {}
elseif foundRank == 2 then
elseif self.foundRank == 2 then
outDeprecated = {}
self.outDeprecated = {}
end
end
end
end
return foundRank >= rankPos  -- == would also work here
return self.foundRank >= rankPos  -- == would also work here
else
else
return (maxRank <= rankPos and rankPos <= minRank)
return (self.maxRank <= rankPos and rankPos <= self.minRank)
end
end
end
end


local function appendOutput(value, rankPos)
function State:appendOutput(value, rankPos)
if rankPos == 1 then
if rankPos == 1 then
outPreferred[#outPreferred + 1] = value
self.outPreferred[#self.outPreferred + 1] = value
elseif rankPos == 2 then
elseif rankPos == 2 then
outNormal[#outNormal + 1] = value
self.outNormal[#self.outNormal + 1] = value
elseif rankPos == 3 then
elseif rankPos == 3 then
outDeprecated[#outDeprecated + 1] = value
self.outDeprecated[#self.outDeprecated + 1] = value
end
end
end
end


local function out()
function State:out()
local out = ""
local out = ""
outPreferred = table.concat(outPreferred, ", ")
if self.outDeprecated[1] then
outNormal = table.concat(outNormal, ", ")
if self.singleValue then
outDeprecated = table.concat(outDeprecated, ", ")
out = self.outDeprecated[1]
 
else
if outPreferred ~= "" then
out = table.concat(self.outDeprecated, ", ")
out = outPreferred
end
end
end
if outNormal ~= "" then
if self.outNormal[1] then
if out ~= "" then
if self.singleValue then
out = out .. "; "
out = self.outNormal[1]
else
if out ~= "" then
out = "; " .. out
end
out = table.concat(self.outNormal, ", ") .. out
end
end
out = out .. outNormal
end
end
if outDeprecated ~= "" then
if self.outPreferred[1] then
if out ~= "" then
if self.singleValue then
out = out .. "; "
out = self.outPreferred[1]
else
if out ~= "" then
out = "; " .. out
end
out = table.concat(self.outPreferred, ", ") .. out
end
end
out = out .. outDeprecated
end
end
Line 253: Line 297:
end
end


local function processFlag(flag)
function State:processFlag(flag)
if flag == "linked" then
if flag == "linked" then
linked = true
self.linked = true
return true
return true
elseif flag == "unit" then
elseif flag == "unit" then
withUnit = true
self.withUnit = true
return true
elseif flag == "short" then
self.shortName = true
return true
elseif flag == "single" then
self.singleValue = true
return true
return true
elseif flag == "best" or flag == "all" or flag:match('^preferred[+-]?$') or flag:match('^normal[+-]?$') or flag:match('^deprecated[+-]?$') then
elseif flag == "best" or flag == "all" or flag:match('^preferred[+-]?$') or flag:match('^normal[+-]?$') or flag:match('^deprecated[+-]?$') then
setRankBoundaries(flag)
self:setRankBoundaries(flag)
return true
return true
else
else
Line 269: Line 319:


p.property = function(frame)
p.property = function(frame)
local _ = State.new()
local entity, propertyID, claims, rankPos, value
local entity, propertyID, claims, rankPos, value
local nextArg = mw.text.trim(frame.args[1] or "")
local nextArg = mw.text.trim(frame.args[1] or "")
local nextIndex = 2
local nextIndex = 2
setRankBoundaries("best")
while _:processFlag(nextArg) do
while processFlag(nextArg) do
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextIndex = nextIndex + 1
nextIndex = nextIndex + 1
Line 291: Line 341:
if claims then
if claims then
for k, v in pairs(claims) do
for k, v in pairs(claims) do
rankPos = convertRank(v.rank)
rankPos = _:convertRank(v.rank)
if rankMatches(rankPos) then
if _:rankMatches(rankPos) then
value = getValue(v.mainsnak, withUnit)
value = _:getValue(v.mainsnak, _.withUnit)
if value then appendOutput(value, rankPos) end
if value then _:appendOutput(value, rankPos) end
end
end
end
end
return out()
return _:out()
else
else
return ""
return ""
Line 303: Line 353:
end
end


p.qualifier = function(frame)
p.qualifier = function(frame, _)
_ = _ or State.new()
local entity, propertyID, propertyValue, qualifierID, claims, qualifiers, rankPos, outValue, outInter, outQualifier
local entity, propertyID, propertyValue, qualifierID, claims, qualifiers, rankPos, outValue, outInter, 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
setRankBoundaries("best")
while _:processFlag(nextArg) do
while processFlag(nextArg) do
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextIndex = nextIndex + 1
nextIndex = nextIndex + 1
Line 348: Line 398:
if claims then
if claims then
for k, v in pairs(claims) do
for k, v in pairs(claims) do
rankPos = convertRank(v.rank)
rankPos = _:convertRank(v.rank)
if propertyValue == nil or snakEqualsValue(v.mainsnak, propertyValue) then
if propertyValue == nil or _:snakEqualsValue(v.mainsnak, propertyValue) then
if rankMatches(rankPos) then
if _:rankMatches(rankPos) then
outValue = nil
outValue = nil
outInter = nil
outInter = nil
outQualifier = {}
outQualifier = {}
if propertyWithQualifier then
if _.propertyWithQualifier then
outValue = getValue(v.mainsnak, withUnit)
outValue = _:getValue(v.mainsnak, _.withUnit)
end
end
if v.qualifiers then qualifiers = v.qualifiers[qualifierID] end
if v.qualifiers then qualifiers = v.qualifiers[qualifierID] end
if (not propertyWithQualifier or outValue) and qualifiers then
if (not _.propertyWithQualifier or outValue) and qualifiers then
for k2, v2 in pairs(v.qualifiers[qualifierID]) do
for k2, v2 in pairs(v.qualifiers[qualifierID]) do
outInter = getValue(v2, withUnit)
outInter = _:getValue(v2, _.withUnit)
if outInter then
if outInter then
if not propertyWithQualifier then
if not _.propertyWithQualifier then
appendOutput(outInter, rankPos)
_:appendOutput(outInter, rankPos)
else
else
outQualifier[#outQualifier + 1] = outInter
outQualifier[#outQualifier + 1] = outInter
Line 373: Line 423:
end
end
if outValue and propertyWithQualifier then
if outValue and _.propertyWithQualifier then
outQualifier = table.concat(outQualifier, ", ")
outQualifier = table.concat(outQualifier, ", ")
Line 381: Line 431:
end
end
appendOutput(outValue, rankPos)
_:appendOutput(outValue, rankPos)
end
end
end
end
end
end
end
end
return out()
return _:out()
else
else
return ""
return ""
Line 393: Line 443:


p.propertyWithQualifier = function(frame)
p.propertyWithQualifier = function(frame)
propertyWithQualifier = true
local _ = State.new()
withUnit = true
_.propertyWithQualifier = true
return p.qualifier(frame)
_.withUnit = true
return p.qualifier(frame, _)
end
end


p.label = function(frame)
p.label = function(frame)
local label, target
local _ = State.new()
local label = ""
local target = ""
local nextArg = mw.text.trim(frame.args[1] or "")
local nextArg = mw.text.trim(frame.args[1] or "")
local nextIndex = 2
local nextIndex = 2
while processFlag(nextArg) do
while _:processFlag(nextArg) do
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextIndex = nextIndex + 1
nextIndex = nextIndex + 1
Line 409: Line 463:
if nextArg then
if nextArg then
label = mw.wikibase.label(nextArg)
target = nil
if nextArg:sub(1,1):upper() == "Q" then
if nextArg:sub(1,1):upper() == "Q" then
if linked or label == nil then
if _.shortName then
label = _:getShortName(nextArg)
end
if label == "" then
label = mw.wikibase.label(nextArg)
end
if _.linked or label == nil then
target = mw.wikibase.sitelink(nextArg)
target = mw.wikibase.sitelink(nextArg)
end
end
if linked and target then
if _.linked and target then
return "[[" .. target .. "|" .. (label or target) .. "]]"
label = "[[" .. target .. "|" .. (label or target) .. "]]"
end
end
else
label = mw.wikibase.label(nextArg)
end
end

Revision as of 14:54, 31 August 2016

Documentation for this module may be created at Module:Wd/doc

local p = {}

State = {}
State.__index = State

-- allows for recursive calls
function State.new()
	local stt = {}
	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

function State:unknownDatatypeError(type)
	return "<strong class=\"error\">Unknown or unsupported datatype '" .. type .. "'</strong>"
end

function State:convertUnit(unit)
	local i, j, itemID, label, target
	
	if unit == "" or unit == "1" then
		return nil
	end
	
	if unit:match('^http[s]?://') then
		i, j = unit:find("Q")
		
		if i then
			itemID = unit:sub(i)
			
			if itemID == "Q11229" then  -- 'percentage'
				return "%"
			else
				label = mw.wikibase.label(itemID)
				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) .. "]]"
					end
					
					if not label then
						return " " .. "[[:d:" .. itemID .. "|" .. itemID .. "]]"
					end
				end
				
				return " " .. (label or target or itemID)
			end
		end
	end
	
	return " " .. unit
end

function State:getShortName(itemID)
	return p.property({args={"single", itemID, "P1813"}})  -- 'short name'
end

function State:getValue(snak, appendUnit)
	appendUnit = appendUnit or false
	
	if snak.snaktype == 'value' then
		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 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) .. "]]"
				elseif not value then
					value = "[[:d:" .. itemID .. "|" .. itemID .. "]]"
				end
			elseif not value then
				value = (target or itemID)
			end
			
			return value
		else
			return self:unknownDatatypeError(snak.datavalue.type)
		end
	elseif snak.snaktype == 'somevalue' then
		return "unknown"
	elseif snak.snaktype == 'novalue' then
		return "none"
	else
		return nil
	end
end

function State:getRawValue(snak)
	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

function State:snakEqualsValue(snak, value)
	if snak.snaktype == 'value' then
		local snakValue = self:getRawValue(snak)
		
		if snakValue and snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end
		
		return snakValue == value
	elseif snak.snaktype == 'somevalue' then
		if value == " " then  -- single space represents 'somevalue'
			return true
		else
			return false
		end
	elseif snak.snaktype == 'novalue' then
		if value == "" then  -- empty value represents 'novalue'
			return true
		else
			return false
		end
	else
		return false
	end
end

function State:setRankBoundaries(rank)
	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
	
	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
		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

function State: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

function State:rankMatches(rankPos)
	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

function State:appendOutput(value, rankPos)
	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

function State:out()
	local out = ""
	
	if self.outDeprecated[1] then
		if self.singleValue then
			out = self.outDeprecated[1]
		else
			out = table.concat(self.outDeprecated, ", ")
		end
	end
	
	if self.outNormal[1] then
		if self.singleValue then
			out = self.outNormal[1]
		else
			if out ~= "" then
				out = "; " .. out
			end
			
			out = table.concat(self.outNormal, ", ") .. out
		end
	end
	
	if self.outPreferred[1] then
		if self.singleValue then
			out = self.outPreferred[1]
		else
			if out ~= "" then
				out = "; " .. out
			end
			
			out = table.concat(self.outPreferred, ", ") .. out
		end
	end
	
	return out
end

function State:processFlag(flag)
	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
		return false
	end
end

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
	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
				value = _:getValue(v.mainsnak, _.withUnit)
				if value then _:appendOutput(value, rankPos) end
			end
		end
		return _:out()
	else
		return ""
	end
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
	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 = frame.args[nextIndex]
	nextIndex = nextIndex + 1
	
	qualifierID = nextArg
	
	nextArg = mw.text.trim(frame.args[nextIndex] or "")
	nextIndex = nextIndex + 1
	
	if nextArg == "" then
		propertyValue = nil
		qualifierID = mw.text.trim(qualifierID or "")
	else
		propertyValue = qualifierID  -- cannot be nil when reached; empty value represents 'novalue'
		if propertyValue ~= "" and mw.text.trim(propertyValue) == "" then
			propertyValue = " "  -- single space represents 'somevalue'
		else
			propertyValue = mw.text.trim(propertyValue)
		end
		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 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
								end
							end
						end
					end
					
					if outValue and _.propertyWithQualifier then
						outQualifier = table.concat(outQualifier, ", ")
						
						if outQualifier ~= "" then
							outQualifier = " <span style=\"font-size:smaller\">(" .. outQualifier .. ")</span>"
							outValue = outValue .. outQualifier
						end
						
						_:appendOutput(outValue, rankPos)
					end
				end
			end
		end
		return _:out()
	else
		return ""
	end
end

p.propertyWithQualifier = function(frame)
	local _ = State.new()
	_.propertyWithQualifier = true
	_.withUnit = true
	return p.qualifier(frame, _)
end

p.label = function(frame)
	local _ = State.new()
	
	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
	end
	
	if nextArg then
		if nextArg:sub(1,1):upper() == "Q" then
			if _.shortName then
				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
				label = "[[" .. target .. "|" .. (label or target) .. "]]"
			end
		else
			label = mw.wikibase.label(nextArg)
		end
		
		return (label or target)
	else
		return mw.wikibase.label()
	end
end

return p