{{i}} Документація модуля[перегляд] [редагувати] [історія] [очистити кеш]

Модуль містить функції форматувания для Властивості P166: отримані відзнаки і окремих значень нагород.

Функции модуля нельзя вызывать напрямую — они передаются в виде аргументов из соответствующих связанных шаблонов (и только из них, в другие шаблоны их также вставлять не нужно).

Обновление ред.

Порядок наград обновляется в полуавтоматическом режиме при помощи скрипта. Не обновляйте его вручную, вместо этого исправьте значения свойств в Викиданных, и через некоторое время он будет обновлён. Если список долго не обновляется, пишите участнику putnik.

Для того, чтобы награда попала в список, заполните в её элементе следующие свойства:

Порядок сортировки ред.

Див. commons:Data:Wikidata/awards order.tab — файл с порядком сортировки. Здійснено завелику кількість звертань до сутностей Вікідані.

local WDS = require( 'Module:WikidataSelectors' )
local moduleDate = require( 'Module:Wikidata/date' )
local awardsOrder = mw.ext.data.get( "Wikidata/awards order.tab" )
local p = {}

--Get string with dates from qualifiers table
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

--Property function for [[d:Property:P166]]
function p.formatProperty( context, options )
	if ( not context ) then error( 'context not specified' ); end;
	if ( not options ) then error( 'options not specified' ); end;
	if ( not options.entity ) then error( 'options.entity missing' ); end;

	local claims;
	if options.property then -- TODO: Почему тут может не быть property?
		claims = context.selectClaims( options, options.property );
	end
	if claims == nil then
		return '' --TODO error?
	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
	
	-- Сортировка медалей по старшинству
	local orderedData = {}
	local lastValue;

	for i, awardFields in ipairs( awardsOrder.data ) do
		local awardOrder = awardFields[ 1 ]
		if awardOrder == '-' then
			-- separator
			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

	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

-- Получение изображения (планки или иконки) и его размера
function getImageFromEntity( entity )
	local image = nil
	local size = 'x17px'
	local border = false

	-- получение изображения планки из элемента
	local ribbonImageClaims = WDS.filter( entity.claims, 'P2425' )
	if ribbonImageClaims and #ribbonImageClaims then
		for i, claim in pairs( ribbonImageClaims ) do
			if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
				image = claim.mainsnak.datavalue.value
				border = true
				break
			end
		end
	end

	-- получение иконки из элемента
	if not image then
		local imageClaims = WDS.filter( entity.claims, 'P2910' )
		if imageClaims and #imageClaims then
			for i, claim in pairs( imageClaims ) do
				if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
					image = claim.mainsnak.datavalue.value
					size = '40x40px'
					break
				end
			end
		end
	end
	
	return image, size, border
end

-- Получение категории лауреатов из элемента
function getRecipientCategoryFromEntity( entity )
	local category = ''
	local categoryClaims = WDS.filter( entity.claims, 'P2517' )

	if categoryClaims and #categoryClaims then
		for i, claim in pairs( categoryClaims ) do
			if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
				local categoryLink = mw.wikibase.getSitelink( claim.mainsnak.datavalue.value.id )
				if categoryLink then
					category = category .. '[[' .. categoryLink .. ']]'
				end
			end
		end
	end
	
	return category
end

--Value function for [[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 = getImageFromEntity( entity )

	local recipientCategory = ''
	if not options.nocat and options.nocat ~= '' then
		recipientCategory = getRecipientCategoryFromEntity( entity )
	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
	end

	-- получение ссылки по идентификатору и вывод планки
	if image then
		local link = entity:getSitelink()
		local out = '[[File:' .. 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 '<p style="text-align:left>' .. out .. recipientCategory .. '</p>'
	end
	
	return ''
end

--Table for documentation
function p.renderDoc()
	local out = {}
	for i, awardFields in ipairs( awardsOrder.data ) do
		local awardId = awardFields[ 1 ]
		if awardId == '-' then
			-- separator
			table.insert( out, '|' .. i .. '|| colspan="3" | ----' )
		else
			local awardEntity = mw.wikibase.getEntity( awardId )
			local link = '[[d:' .. awardId .. '|' .. awardId .. ']]'

			local image, size, border = getImageFromEntity( awardEntity )
			if image then
				image = '[[File:' .. 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="standard"\n' ..
		'! # !! Елемент !! Планка !! Назва !! Держава\n|-\n' ..
		table.concat( out, '\n|-\n' ) ..
		'\n|}'
end

-- LEGACY CODE, FUNCTIONS FOR [[Template:Wikidata/p166]] do not delete until new code wil be stable!!
--[[ 
  Функция для получения идентификатора сущностей 

  Принимает: объект таблицу сущности
  Возвращает: строковый индентификатор (типа P18, Q42)
]]
local function getEntityIdFromValue( value )
	local prefix = ''
	if value['entity-type'] == 'item' then
		prefix = 'Q'
	elseif value['entity-type'] == 'property' then
		prefix = 'P'
	else
		throwError( 'unknown-entity-type' )
	end
	return prefix .. value['numeric-id']
end

--Property:P166
function p.formatMedalValue( context, options, statement )
	local entityId = getEntityIdFromValue( statement )
	local label = mw.wikibase.label( entityId )

	-- вывод награды шаблоном, если это возможно
	if label then
		local title = mw.title.new( label, 'Template' )
		if title.exists then
			local template = options.frame:expandTemplate{ title = label, args = {} }
			if string.find( template, 'navbox', 0, true ) == nil then
				return template
			end
		end
	end

	local image = nil
	local entity = mw.wikibase.getEntity( entityId )

	-- получение изображения планки из элемента
	local ribbonImageClaims = WDS.filter( entity.claims, 'p2425' )
	if ribbonImageClaims and #ribbonImageClaims then
		for i, claim in pairs( ribbonImageClaims ) do
			if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
				image = claim.mainsnak.datavalue.value
				break
			end
		end
	end

	-- получение иконки из элемента
	if not image then
		local imageClaims = WDS.filter( entity.claims, 'p2910' )
		if imageClaims and #imageClaims then
			for i, claim in pairs( imageClaims ) do
				if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
					image = claim.mainsnak.datavalue.value
					break
				end
			end
		end
	end

	-- получение ссылки по идентификатору и вывод планки
	if image then
		local link = mw.wikibase.sitelink( entityId )
		local out = '[[File:' .. image .. '|border|40x40px|link='
		if link then
			out = out .. link
		else
			out = out .. 'd:' .. entityId
		end
		if label then
			out = out .. '|' .. label
		end
		out = out .. ']]'

		return out
	end

	local out = context.formatValueDefault( context, options, statement )
	if out and out ~= '' then
		return '<p style="text-align:left>' .. out .. '</p>'
	end
	
	return ''
end
-- END LEGACY CODE

return p