Модуль:Wikidata2/Medals

Wikipedia — ирекле энциклопедия проектыннан ([http://tt.wikipedia.org.ttcysuttlart1999.aylandirow.tmf.org.ru/wiki/Wikidata2/Medals latin yazuında])

Модульдә аерым буләк мәгънәләре (Калып:WD property) һәм үзлекне бизәү функцияләре тупланган.

Модульнең функцияләрен туры итеп чакырып булмый - алар туры килгән калыплардан (һәм шул калыплардан гына, башка калыпларга аларны кертергә кирәк түгел) аргументлар итеп тапшырыла.


local WDS = require( 'Module:Wikidata2Selectors' )
local moduleDate = require( 'Module:Wikidata2/date' )
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
			local dateValue = moduleDate.formatDate( context, options, qualifier.datavalue.value )
			if dateValue then
				table.insert( dates, dateValue )
			end
		end
	end

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

--Property:P166
function p.formatMedalValue( context, options, statement )
	local entityId = statement.id
	if not entityId then
		return statement
	end

	local label = mw.wikibase.label( entityId )
	local image = nil
	local entity = mw.wikibase.getEntity( entityId )
	local size = 'x17px'

	-- получение изображения планки из элемента
	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
					size = '40x40px'
					break
				end
			end
		end
	end

	-- получение категории
	local recipientCategoryClaims = WDS.filter( entity.claims, 'p2517' )
	local recipientCategory = ''
	if recipientCategoryClaims and #recipientCategoryClaims then
		for i, claim in pairs( recipientCategoryClaims ) do
			if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
				local categoryLink = mw.wikibase.sitelink( claim.mainsnak.datavalue.value.id )
				if categoryLink then
					recipientCategory = recipientCategory .. '[[' .. categoryLink .. ']]'
				end
			end
		end
	end

	local dates = ''
	if options.qualifiers then
		mw.logObject( options.qualifiers )
		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 = mw.wikibase.sitelink( entityId )
		local out = '[[File:' .. image .. '|border|' .. 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.sitelink( 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

return p;