Перайсьці да зьместу

Модуль:Вікізьвесткі/узнагароды

Зьвесткі зь Вікіпэдыі — вольнай энцыкляпэдыі

Дакумэнтацыю да гэтага модуля можна стварыць у Модуль:Вікізьвесткі/узнагароды/Дакумэнтацыя

local WDS = require( 'Модуль:Сьцьверджаньні Вікізьвестак' )
local moduleDate = require( 'Модуль:Вікізьвесткі/дата' )
local awardsOrder = mw.ext.data.get( "Wikidata/awards order.tab" )
local p = {}

local config = {
	--Схаваць узнагароду з ключом QID, калі ёсьць узнагарода з значэньнем QID
	absorption = {
		Q16675272 = 'Q654471',
		Q16481808 = 'Q8706404',
		Q1948730 = 'Q178473',
		Q1980962 = 'Q208167',
	}
}

--Атрымаць радок з датамі з табліцы кваліфікатараў
local function datesFromQualifier( context, options, qualifierId )
	local dates = {}
	local qualifiers = options.qualifiers[ qualifierId ]

	if qualifiers then
		for _, qualifier in pairs( qualifiers ) do
			if qualifier.datavalue then
				local dateValue = moduleDate.formatDate( context, options, qualifier.datavalue.value )
				if dateValue then
					table.insert( dates, dateValue )
				end
			end
		end
	end

	return table.concat( dates, ', ' )
end

--Функцыя ўласьцівасьці для [[d:Property:P166]]
function p.formatProperty( context, options )
	if ( not context ) then error( 'кантэкст не зададзены' ); end;
	if ( not options ) then error( 'парамэтры не зададзеныя' ); end;
	if ( not options.entity ) then error( 'адсутнічае options.entity' ); end;

	local claims;
	if options.property then -- TODO: Чаму тут можа ня быць property?
		claims = context.selectClaims( options, options.property );
	end
	if claims == nil then
		return '' --TODO памылка?
	end

	-- Абыход усіх заяваў сьцьверджаньня і з назапашваньнем аформленых
	-- пажаданых заяваў у табліцы.
	local formattedData = {}

	for i, claim in ipairs( claims ) do
		if ( claim.mainsnak and
			claim.mainsnak and
			claim.mainsnak.datavalue and
			claim.mainsnak.datavalue.type == 'wikibase-entityid'
		) then
			local valueId = claim.mainsnak.datavalue.value.id
			local formattedStatement = context.formatStatement( options, claim )
			-- тут можа вярнуцца альбо аформлены тэкст заявы, альбо радок памылкі, альбо nil
			if ( formattedStatement and formattedStatement ~= '' ) then
				formattedStatement = '<span class="wikidata-claim" data-wikidata-property-id="' .. string.upper( options.property ) .. '" data-wikidata-claim-id="' .. claim.id .. '">' .. formattedStatement .. '</span>'
				table.insert( formattedData, {
					id = valueId,
					html = formattedStatement,
				} )
			end
		end
	end

	-- Выдаленьне дубляў (мэдаль + званьне -> званьне)
	for i, awardData in ipairs( formattedData ) do
		local isAbsorptionFound = false
		if config.absorption[ awardData.id ] then
			local absorptionAwardId = config.absorption[ awardData.id ]
			for _, absorptionAwardData in ipairs( formattedData ) do
				if absorptionAwardData.id == absorptionAwardId then
					isAbsorptionFound = true
					break
				end
			end
		end
		if isAbsorptionFound then
			table.remove( formattedData, i )
		end
	end
	
	-- Сартаваньне мэдалёў паводле старшынства
	local orderedData = {}
	local lastValue;
	
	if ( type (awardsOrder) == 'table' ) then
		-- Калі не адсохлі структураваныя зьвесткі
		for i, awardFields in ipairs( awardsOrder.data ) do
			local awardOrder = awardFields[ 1 ]
			if awardOrder == '-' then
				-- разьдзяляльнік
				if lastValue ~= '-' then
					table.insert( orderedData, '<br>' )
					lastHeight = nil
				end
			else
				for k, awardData in ipairs( formattedData ) do
					if awardOrder == awardData.id and not awardData.used then
						table.insert( orderedData, awardData.html )
						formattedData[ k ].used = true
					end
				end
			end
		end
	end

	for i, awardData in ipairs( formattedData ) do
		if not awardData.used then
			table.insert( orderedData, awardData.html )
		end
	end

	local lastHeight
	for i, awardHtml in ipairs( orderedData ) do
		local height = mw.ustring.match( awardHtml, 'x%d+px' )
		if height and lastHeight and height ~= lastHeight then
			table.insert( orderedData, i, '<br>' )
		end
		lastHeight = height
	end

	-- стварэньне тэкставага радку з сьпісам аформленых заяваў з табліцы
	local out = mw.text.listToText( orderedData, options.separator, options.conjunction )
	if out ~= '' then
		if options.before then
			out = options.before .. out
		end
		if options.after then
			out = out .. options.after
		end
	end

	return out
end

local function getImageFromClaims( claims, property )
	local imageClaims = WDS.filter( claims, property )
	if imageClaims and #imageClaims then
		for i, claim in pairs( imageClaims ) do
			if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
				return claim.mainsnak.datavalue.value
			end
		end
	end
	return nil
end

-- Атрыманьне выявы (плянкі ці абразку) і ейнага памеру
function p.getImageFromEntity( entityOrId, actualDate )
	local entity = entityOrId
	if type( entityOrId ) == "string" then
		entity = mw.wikibase.getEntity( entityOrId )
	end

	local image = nil
	local size = 'x17px'
	local border = false

	-- атрыманьне выявы плянкі з элемэнту
	image = getImageFromClaims( entity.claims, 'P2425' )
	if image then
		border = true
	end

	-- атрыманьне абразку з элемэнту
	if not image then
		image = getImageFromClaims( entity.claims, 'P2910' )
		if image then
			size = '40x40px'
		end
	end
	
	return image, size, border
end

--Функцыя значэньня для [[d:Property:P166]]
function p.formatValue( context, options, statement )
	local entityId = statement.id
	if not entityId then
		return statement
	end

	local entity = mw.wikibase.getEntity( entityId )
	local label = entity:getLabel()
	
	local image, size, border = p.getImageFromEntity( entity )

	local recipientCategory = ''
	if not options.nocat and options.nocat ~= '' then
		recipientCategory = context.extractCategory( { category = 'P7084[P642:Q24571879]' }, entity )
		if recipientCategory == '' then
			recipientCategory = context.extractCategory( { category = 'P2517' }, entity )
		end
	end

	local dates = ''
	if options.qualifiers then
		local startDates = {}
		dates = datesFromQualifier( context, options, 'P580' )
		if dates ~= '' then
			local endDates = datesFromQualifier( context, options, 'P582' )
			if endDates and endDates ~= '' then
				dates = dates .. ' — ' .. endDates
			end
		else
			dates = datesFromQualifier( context, options, 'P585' )
		end
		
		if options.qualifiers.P27 then
			for _, claim in ipairs( options.qualifiers.P27 ) do
				if claim and claim.datavalue and claim.datavalue.value and claim.datavalue.value.id then
					recipientCategory = recipientCategory .. context.extractCategory( 
						{ category = 'P7084[P642:' .. claim.datavalue.value.id .. ']' }, entity )
				end
			end 
		end
	end

	-- атрыманьне спасылкі па ідэнтыфікатары і вывад плянкі
	if image then
		local link = mw.wikibase.getSitelink( entityId )
		local out = '[[Файл:' .. image
		if border == true then
			out = out .. '|border'
		end
		out = out .. '|' .. size .. '|link='

		-- атрыманьне спасылкі з бацькоўскага элемэнту
		-- для ступеняў звычайна толькі адзін агульны артыкул
		if not link then
			local partOfClaims = WDS.filter( entity.claims, 'P361' ) -- частка ад
			if not partOfClaims or #partOfClaims == 0 then
				partOfClaims = WDS.filter( entity.claims, 'P279' ) -- падкляс ад
			end
			if partOfClaims and #partOfClaims then
				for i, claim in pairs( partOfClaims ) do
					if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
						link = mw.wikibase.getSitelink( claim.mainsnak.datavalue.value.id )
						if link then
							break
						end
					end
				end
			end
		end

		if link then
			out = out .. link
		else
			out = out .. 'd:' .. entityId
		end
		if label then
			out = out .. '|' .. label
		end
		out = out .. ']]'
		out = out .. recipientCategory

		return out
	end

	local out = context.formatValueDefault( context, options, statement )
	if out and out ~= '' then
		if dates ~= '' then
			out = out .. ' (' .. dates .. ')'
		end
		return '<span style="display:inline-block; text-align:left>' .. out .. recipientCategory .. '</span>'
	end
	
	return ''
end

--Табліца для дакумэнтацыі
function p.renderDoc()
	local out = {}
	for i, awardFields in ipairs( awardsOrder.data ) do
		local awardId = awardFields[ 1 ]
		local link = '[[d:' .. awardId .. '|' .. awardId .. ']]'

		if i == 351 then
			-- граніцы
			table.insert( out, '| … || … || … || … || …' )
		elseif i > 351 and i < #awardsOrder.data then
			-- нічога не рабіць
		elseif awardId == '-' then
			-- разьдзяляльнік
			table.insert( out, '|' .. i .. '|| colspan="3" | ----' )
		else
			local awardEntity = mw.wikibase.getEntity( awardId )

			local image, size, border = p.getImageFromEntity( awardEntity )
			if image then
				image = '[[Файл:' .. image
				if border == true then
					image = image .. '|border'
				end
				image = image .. '|' .. size .. ']]'
			else
				image = ''
			end

			local label = awardEntity:getLabel() or ''
			local article = awardEntity:getSitelink()
			if article then
				if label == '' then
					label = article
				end
				label = '[[' .. article .. '|' .. label .. ']]'
			end

			local country = awardEntity:formatPropertyValues( 'P17' )[ 'value' ]

			table.insert( out, '|' .. i .. '||' .. link .. '||' .. image ..
				'||' .. label .. '||' .. country  )
		end
	end

	return '{| class="wikitable"\n' ..
		'! # !! Элемэнт !! Плянка !! Назва !! Краіна\n|-\n' ..
		table.concat( out, '\n|-\n' ) ..
		'\n|}'
end

return p
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy