Module:Wd: Difference between revisions

From Random Island Wiki
Jump to navigation Jump to search
>Thayts
No edit summary
>Thayts
(Added support for linked output)
Line 11: Line 11:
if snak.snaktype ~= 'value' then return "" end
if snak.snaktype ~= 'value' then return "" end
if snak.datavalue.type == "wikibase-entityid" then
if snak.datavalue.type == 'wikibase-entityid' then
return "Q" .. snak.datavalue.value["numeric-id"]
return "Q" .. snak.datavalue.value['numeric-id']
elseif snak.datavalue.type == "quantity" then
elseif snak.datavalue.type == 'quantity' then
return formatAmount(snak.datavalue.value["amount"])
return formatAmount(snak.datavalue.value['amount'])
else
else
return snak.datavalue.value
return snak.datavalue.value
Line 23: Line 23:
if snak.snaktype ~= 'value' then return "" end
if snak.snaktype ~= 'value' then return "" end
if snak.datavalue.type == "wikibase-entityid" then
if 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'])
elseif snak.datavalue.type == "quantity" then
elseif snak.datavalue.type == 'quantity' then
return formatAmount(snak.datavalue.value["amount"])
return formatAmount(snak.datavalue.value['amount'])
else
else
return snak.datavalue.value
return snak.datavalue.value
Line 35: Line 35:
local snakValue = getRawValue(snak)
local snakValue = getRawValue(snak)
if snak.datavalue.type == "wikibase-entityid" then value = value:upper() end
if snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end
return snakValue == value
return snakValue == value
Line 42: Line 42:
p.property = function(frame)
p.property = function(frame)
local entity, propertyID, claims
local entity, propertyID, claims
local firstArg = mw.text.trim(frame.args[1] or "")
local linked = false
local nextArg = mw.text.trim(frame.args[1] or "")
local nextIndex = 2
if firstArg:sub(1,1):upper() == "Q" then
if nextArg == "linked" then
entity = mw.wikibase.getEntity(firstArg)
linked = true
propertyID = mw.text.trim(frame.args[2] or "")
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
else
entity = mw.wikibase.getEntity()
entity = mw.wikibase.getEntity()
propertyID = firstArg
propertyID = nextArg
end
end
if entity and entity.claims then claims = entity.claims[propertyID] end
if entity and entity.claims then claims = entity.claims[propertyID] end
if claims then
if claims then
local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
local out = {}
for k, v in pairs(claims) do
-- if number type: remove thousand separators, bounds and units
if v.mainsnak.snaktype == 'value' then
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") then
if linked and v.mainsnak.datavalue.type == 'wikibase-entityid' then
result = mw.ustring.gsub(result, "(%d),(%d)", "%1%2")
local itemID = "Q" .. v.mainsnak.datavalue.value['numeric-id']
result = mw.ustring.gsub(result, "(%d)±.*", "%1")
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) .. "]]"
else
out[#out + 1] = linkName
end
else
out[#out + 1] = getValue(v.mainsnak)
end
end
end
end
return result
return table.concat(out, ", ")
else
else
return ""
return ""
Line 69: Line 88:
p.qualifier = function(frame)
p.qualifier = function(frame)
local entity, propertyID, propertyValue, qualifierID, claims
local entity, propertyID, propertyValue, qualifierID, claims
local firstArg = mw.text.trim(frame.args[1] or "")
local linked = false
local nextArg = mw.text.trim(frame.args[1] or "")
local nextIndex = 2
local nextIndex = 2
if firstArg:sub(1,1):upper() == "Q" then
if nextArg == "linked" then
entity = mw.wikibase.getEntity(firstArg)
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 "")
propertyID = mw.text.trim(frame.args[nextIndex] or "")
nextIndex = nextIndex + 1
nextIndex = nextIndex + 1
else
else
entity = mw.wikibase.getEntity()
entity = mw.wikibase.getEntity()
propertyID = firstArg
propertyID = nextArg
end
end
local nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextArg = mw.text.trim(frame.args[nextIndex] or "")
nextIndex = nextIndex + 1
nextIndex = nextIndex + 1
Line 102: Line 128:
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
out[#out + 1] = getValue(v2)
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) .. "]]"
else
out[#out + 1] = linkName
end
else
out[#out + 1] = getValue(v2)
end
end
end
end
end

Revision as of 06:42, 21 August 2016

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

local p = {}

local function formatAmount(amount)
	-- strip + signs from front
	amount = mw.ustring.gsub(amount, "\+(.+)", "%1")
	
	return amount
end

local function getRawValue(snak)
	if snak.snaktype ~= 'value' then return "" end
	
	if snak.datavalue.type == 'wikibase-entityid' then
		return "Q" .. snak.datavalue.value['numeric-id']
	elseif snak.datavalue.type == 'quantity' then
		return formatAmount(snak.datavalue.value['amount'])
	else
		return snak.datavalue.value
	end
end

local function getValue(snak)
	if snak.snaktype ~= 'value' then return "" end
	
	if snak.datavalue.type == 'wikibase-entityid' then
		return mw.wikibase.label("Q" .. snak.datavalue.value['numeric-id'])
	elseif snak.datavalue.type == 'quantity' then
		return formatAmount(snak.datavalue.value['amount'])
	else
		return snak.datavalue.value
	end
end

local function snakEqualsValue(snak, value)
	local snakValue = getRawValue(snak)
	
	if 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) .. "]]"
					else
						out[#out + 1] = linkName
					end
				else
					out[#out + 1] = getValue(v.mainsnak)
				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) .. "]]"
							else
								out[#out + 1] = linkName
							end
						else
							out[#out + 1] = getValue(v2)
						end
					end
				end
			end
		end
		return table.concat(out, ", ")
	else
		return ""
	end
end

p.label = function(frame)
	if frame.args[1] then
		return mw.wikibase.label(mw.text.trim(frame.args[1]))
	else
		return mw.wikibase.label()
	end
end

return p