Модуль:Песочница/Volcanus/Test

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация
-- https://www.mediawiki.org/wiki/Wikibase/DataModel
-- https://www.wikidata.org/wiki/Wikidata:Glossary/ru
-- https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua/ru
-- https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual/ru

local p = {}

local function WikiDataValueToArray(strSrc)
	local result = {}
	for str in string.gmatch(strSrc, "[^,]+") do
		--str = str:match'^%s*(.*%S)' or '' -- trim
		--if str ~= 'значение неизвестно' and str ~= 'значение отсутствует' then
			table.insert(result, str)
		--end
	end
	return result
end

local function sortOneRank(medals)
	local result = {
		others = {}
	}
	
	for i, value in ipairs(medals) do
		table.insert(result.others, value)
	end
	
	return result
end

local function formatOneItems(medals, wd, frame)
	local result = ''
	
	for i, value in pairs(medals) do
		result = result .. '|' .. value .. '\n'
		--if math.fmod(i, 4) == 0 then
		--	result = result .. '|-\n'
		--end
	end
	
	return result .. '\n'
end

function p.display(frame)
	local result = {
		wd    = 0,
		items = {}
	}
	
	for key, value in pairs( frame.args ) do
		if string.sub(value, 0, 1) ~= '{' then
			table.insert(result.items, value)
		end
	end
	
	if next(result.items) == nil then -- если пусто, используем ВД
		result.wd = 1
		
		local entity = mw.wikibase.getEntity()
		local value = entity:formatPropertyValues('P2962').value

		table.insert(result.items, WikiDataValueToArray(value))
	end
	
	local result2 = {
		wd = result.wd,
		items = {}
	}
	
	-- Не сортировать, если источник ВП или пусто.
	if result.wd == 1 and next(result.items) ~= nil then
	    table.insert(result2.items, sortOneRank(result.items[1]))
	else
		result2.items[1] = result.items
	end
	
	local result3 = ''
	
	if next(result2.items) ~= nil then
		for i, value in ipairs(result2.items) do
			if result2.wd == 1 then
				result3 = result3 .. formatOneItems(value.others, result2.wd, frame)
			else
				result3 = result3 .. formatOneItems(value, result2.wd, frame)
			end
		end
	end
	return result3
end

return p