Модуль:Песочница/D.wine/airline

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация
local p = {}

local function isType(claims, type)
	return claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == type
end

local function getPages(entity, claims, propertyID, labelHook) 
	if labelHook == nil then
		labelHook = function (qnumber)
			return nil;
		end
	end

	if isType(claims, "wikibase-entityid") then
		local out = {}
		for _, claim in pairs(claims) do
			local qnumber = "Q" .. claim.mainsnak.datavalue.value["numeric-id"]
			local sitelink = mw.wikibase.sitelink(qnumber)
			local label = labelHook(qnumber) or mw.wikibase.label(qnumber) or qnumber
			if sitelink then
				out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
			else
				out[#out + 1] = "[[:d:" .. qnumber .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>"
			end
		end
		return out
	else
		return entity:formatPropertyValues(propertyID).value
	end
end

local function createTable(out, header)
	return "{| class=\"wikitable\"\n|+ '''"..header.."'''\n|-\n|" ..table.concat(out, "\n|-\n|")
end

local function getPropertyValue(itemID, propertyID)
	local entity = mw.wikibase.getEntity(itemID)
	local claims
	if entity and entity.claims then
		claims = entity.claims[propertyID]
	end
	if claims then
		return getPages(entity, claims, propertyID)
	end
	return nil
end

p.getPropertyValue = getPropertyValue

p.fleet = function(frame)
	local itemID = mw.text.trim(frame.args[1] or "")
	local values = getPropertyValue(itemID, 'P121')
	if values then
		return createTable(values, "Fleet")
	end
end

return p