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

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация
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 = {}
		local qualifiers = {}
		for _, claim in pairs(claims) do
			if claim.qualifier then
				qualifiers[#qualifiers + 1] = claim.qualifier
			end
			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='local-article-not-found'>[*]</abbr>"
			end
		end
		return out, qualifiers
	else
		return entity:formatPropertyValues(propertyID).value
	end
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.line = function(frame)
	local itemID = mw.text.trim(frame.args.entityId or "")
	local lines = getPropertyValue(itemID, "P81")
	local values = getPropertyValue(itemID, "P197")
	return makeTable(lines[1], values[1], value[2])
end

p.getAdjacentStations = function(frame)
	local itemID = mw.text.trim(frame.args.entityId or "")
	local values = getPropertyValue(itemID, "P197")
	return mw.dumpObject(values)
end

p.previous = function(frame)
	local itemID = mw.text.trim(frame.args.entityId or "")
	local values = getPropertyValue(itemID, "P197")
	return values[1]
end

p.next = function(frame)
	local itemID = mw.text.trim(frame.args.entityId or "")
	local values = getPropertyValue(itemID, "P197")
	return values[2]
end

local getAdjacentStations = function(itemID)
	-- itemID = 'Q1097'
	if itemID then
		local res = mw.wikibase.getBestStatements(itemID, 'P197')
		if res and #res > 0 then
			return res
		end
	end
end

function makeTable(line, previous, next)
	return "{| class=\"toccolours noprint\" border=\"1\" cellpadding=\"4\" cellspacing=\"0\" style=\"border-collapse: collapse; margin:.5em auto;\n"..
	"|colspan=\"3\"|'''Остановочные пункты железной дороги '''\n"..
	"|- \"style=\"text-align: center\"\n"..
	"| style=\"font-size: 90%\" width=\"30%\" |Предыдущая остановка:<br>"..previous.."\n"..
	"| width=\"35%\"|'''"..line.."'''<br>\n".."| style=\"font-size: 90%\" width=\"30%\" |Следующая остановка:<br>"..next.."\n"..
	"|-\n"..
	"|}"
end

p.test = function()
	local res = ''
	local stations = getAdjacentStations('Q1097')
	 for k, claim in pairs(stations) do
	 	local stationID = claim.mainsnak.datavalue.value["id"]
	 	local label = mw.wikibase.getLabel(stationID)
	 	local sitelink = mw.wikibase.getSitelink(stationID)
	 	if sitelink then
			res = res .. "[[" .. sitelink .. "|" .. label .. "]]" .. '<br>'
		else
			res = res .. label .. '<br>'
	 	end
			
	end
	return res
end
return p