Нет описания правки
импорт списка дубликатов из az:Modul:Wikidata/Medals
Строка 1: Строка 1:
local WDS = require( 'Module:WikidataSelectors' )
local WDS = require( 'Module:WikidataSelectors' )
local moduleDate = require( 'Module:Wikidata/date' )
local moduleDate = require( 'Module:Wikidata/date' )
local awardsOrder = '' -- mw.ext.data.get( "Wikidata/awards order.tab" )
local awardsOrder = mw.ext.data.get( "Wikidata/awards order.tab" )
local p = {}
local p = {}


Строка 11: Строка 11:
Q1948730 = 'Q178473',
Q1948730 = 'Q178473',
Q1980962 = 'Q208167',
Q1980962 = 'Q208167',
Q2032399 = 'Q2046996',
Q102183407 = 'Q103819965',
Q1262166 = 'Q80589',
Q749849 = 'Q1358055',
Q4287121 = 'Q4137462',
}
}
}
}
Строка 37: Строка 42:
if ( not context ) then error( 'context not specified' ); end;
if ( not context ) then error( 'context not specified' ); end;
if ( not options ) then error( 'options not specified' ); end;
if ( not options ) then error( 'options not specified' ); end;
-- if ( not options.entity ) then ''; end; -- error( 'options.entity missing' ); end;
if ( not options.entityId ) then error( 'options.entityId missing' ); end;


local claims;
local claims;
Строка 141: Строка 146:
end
end


local function getImageFromClaims( claims, property )
local function getImageFromProperty( entityId, propertyId )
local imageClaims = WDS.filter( claims, property )
local imageClaims = WDS.load( entityId, propertyId )
if imageClaims and #imageClaims then
if imageClaims and #imageClaims > 0 then
for i, claim in pairs( imageClaims ) do
for _, claim in ipairs( imageClaims ) do
if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
return claim.mainsnak.datavalue.value
return claim.mainsnak.datavalue.value
Строка 150: Строка 155:
end
end
end
end
return nil
return nil
end
end


-- Получение изображения (планки или иконки) и его размера
-- Получение изображения (планки или иконки) и его размера
function p.getImageFromEntity( entityOrId, actualDate )
function p.getImageFromEntity( entityId, actualDate )
local entity = entityOrId
if type( entityId ) ~= "string" then
if type( entityOrId ) == "string" then
entityId = entityId.id
entity = mw.wikibase.getEntity( entityOrId )
end
end


Строка 165: Строка 170:


-- получение изображения планки из элемента
-- получение изображения планки из элемента
image = getImageFromClaims( entity.claims, 'P2425' )
image = getImageFromProperty( entityId, 'P2425' )
if image then
if image then
border = true
border = true
Строка 172: Строка 177:
-- получение иконки из элемента
-- получение иконки из элемента
if not image then
if not image then
image = getImageFromClaims( entity.claims, 'P2910' )
image = getImageFromProperty( entityId, 'P2910' )
if image then
if image then
size = '40x40px'
size = '40x40px'
Строка 188: Строка 193:
end
end


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


local recipientCategory = ''
local recipientCategory = ''
if not options.nocat and options.nocat ~= '' then
if not options.nocat and options.nocat ~= '' then
recipientCategory = context.extractCategory( { category = 'P7084[P642:Q24571879]' }, entity )
recipientCategory = context.extractCategory( { category = 'P7084[P4224:Q24571879]' }, entityId )
if recipientCategory == '' then
if recipientCategory == '' then
recipientCategory = context.extractCategory( { category = 'P2517' }, entity )
recipientCategory = context.extractCategory( { category = 'P2517' }, entityId )
end
end
end
end
Строка 215: Строка 218:
if options.qualifiers.P27 then
if options.qualifiers.P27 then
mw.log('>>>>>>>>>>>>>')
for _, claim in ipairs( options.qualifiers.P27 ) do
for _, claim in ipairs( options.qualifiers.P27 ) do
if claim and claim.datavalue and claim.datavalue.value and claim.datavalue.value.id then
if claim and claim.datavalue and claim.datavalue.value and claim.datavalue.value.id then
recipientCategory = recipientCategory .. context.extractCategory(
local categoryOptions = { category = 'P7084[P27:' .. claim.datavalue.value.id .. ']' }
{ category = 'P7084[P642:' .. claim.datavalue.value.id .. ']' }, entity )
recipientCategory = recipientCategory .. context.extractCategory( categoryOptions, entityId )
end
end
end  
end  
Строка 236: Строка 240:
-- для степеней обычно только одна общая статья
-- для степеней обычно только одна общая статья
if not link then
if not link then
local partOfClaims = WDS.filter( entity.claims, 'P361' ) -- часть от
local partOfClaims = WDS.load( entityId, 'P361' ) -- часть от
if not partOfClaims or #partOfClaims == 0 then
if not partOfClaims or #partOfClaims == 0 then
partOfClaims = WDS.filter( entity.claims, 'P279' ) -- подкласс от
partOfClaims = WDS.load( entityId, 'P279' ) -- подкласс от
end
end
if partOfClaims and #partOfClaims then
if partOfClaims and #partOfClaims > 0 then
for i, claim in pairs( partOfClaims ) do
for _, claim in ipairs( partOfClaims ) do
if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
if claim.type == 'statement' and claim.mainsnak.snaktype == 'value' then
link = mw.wikibase.getSitelink( claim.mainsnak.datavalue.value.id )
link = mw.wikibase.getSitelink( claim.mainsnak.datavalue.value.id )
Строка 293: Строка 297:
table.insert( out, '|' .. i .. '|| colspan="3" | ----' )
table.insert( out, '|' .. i .. '|| colspan="3" | ----' )
else
else
local awardEntity = mw.wikibase.getEntity( awardId )
local image, size, border = p.getImageFromEntity( awardId )
 
local image, size, border = p.getImageFromEntity( awardEntity )
if image then
if image then
image = '[[File:' .. image
image = '[[File:' .. image
Строка 306: Строка 308:
end
end


local label = awardEntity:getLabel() or ''
local label = mw.wikibase.getLabel( awardId ) or ''
local article = awardEntity:getSitelink()
local article = mw.wikibase.getSitelink( awardId )
if article then
if article then
if label == '' then
if label == '' then
Строка 315: Строка 317:
end
end


local country = awardEntity:formatPropertyValues( 'P17' )[ 'value' ]
local countryStatements = mw.wikibase.getBestStatements( awardId, 'P17' )
local countries = {}
if countryStatements then
for _, statement in ipairs( countryStatements ) do
if statement.mainsnak.datavalue and
statement.mainsnak.datavalue.type == 'wikibase-entityid'
then
local countryId = statement.mainsnak.datavalue.value.id
table.insert( countries, mw.wikibase.getLabel( countryId ) )
end
end
end


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

Версия от 20:24, 4 мая 2024

Для документации этого модуля может быть создана страница Модуль:Wikidata/Medals/doc

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

local config = {
	--Hide award with key QID if there is a reward with value QID
	absorption = {
		Q16675272 = 'Q654471',
		Q16481808 = 'Q8706404',
		Q1948730 = 'Q178473',
		Q1980962 = 'Q208167',
		Q2032399 = 'Q2046996',
		Q102183407 = 'Q103819965',
		Q1262166 = 'Q80589',
		Q749849 = 'Q1358055',
		Q4287121 = 'Q4137462',
	}
}

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

--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.entityId ) then error( 'options.entityId 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

	-- Удаление дублей (медаль + звание -> звание)
	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
		-- Если не отсохла stuctured data
		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
	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 getImageFromProperty( entityId, propertyId )
	local imageClaims = WDS.load( entityId, propertyId )
	if imageClaims and #imageClaims > 0 then
		for _, claim in ipairs( 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( entityId, actualDate )
	if type( entityId ) ~= "string" then
		entityId = entityId.id
	end

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

	-- получение изображения планки из элемента
	image = getImageFromProperty( entityId, 'P2425' )
	if image then
		border = true
	end

	-- получение иконки из элемента
	if not image then
		image = getImageFromProperty( entityId, 'P2910' )
		if image then
			size = '40x40px'
		end
	end
	
	return image, size, border
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 label = mw.wikibase.getLabel( entityId )
	local image, size, border = p.getImageFromEntity( entityId )

	local recipientCategory = ''
	if not options.nocat and options.nocat ~= '' then
		recipientCategory = context.extractCategory( { category = 'P7084[P4224:Q24571879]' }, entityId )
		if recipientCategory == '' then
			recipientCategory = context.extractCategory( { category = 'P2517' }, entityId )
		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
mw.log('>>>>>>>>>>>>>')
			for _, claim in ipairs( options.qualifiers.P27 ) do
				if claim and claim.datavalue and claim.datavalue.value and claim.datavalue.value.id then
					local categoryOptions = { category = 'P7084[P27:' .. claim.datavalue.value.id .. ']' }
					recipientCategory = recipientCategory .. context.extractCategory( categoryOptions, entityId )
				end
			end 
		end
	end

	-- получение ссылки по идентификатору и вывод планки
	if image then
		local link = mw.wikibase.getSitelink( entityId )
		local out = '[[File:' .. image
		if border == true then
			out = out .. '|border'
		end
		out = out .. '|' .. size .. '|link='
		
		-- получение ссылки из родительского элемента
		-- для степеней обычно только одна общая статья
		if not link then
			local partOfClaims = WDS.load( entityId, 'P361' ) -- часть от
			if not partOfClaims or #partOfClaims == 0 then
				partOfClaims = WDS.load( entityId, 'P279' ) -- подкласс от
			end
			if partOfClaims and #partOfClaims > 0 then
				for _, claim in ipairs( 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

--Table for documentation
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
			-- limits
			table.insert( out, '| … || … || … || … || …' )
		elseif i > 351 and i < #awardsOrder.data then
			-- do nothing
		elseif awardId == '-' then
			-- separator
			table.insert( out, '|' .. i .. '|| colspan="3" | ----' )
		else
			local image, size, border = p.getImageFromEntity( awardId )
			if image then
				image = '[[File:' .. image
				if border == true then
					image = image .. '|border'
				end
				image = image .. '|' .. size .. ']]'
			else
				image = ''
			end

			local label = mw.wikibase.getLabel( awardId ) or ''
			local article = mw.wikibase.getSitelink( awardId )
			if article then
				if label == '' then
					label = article
				end
				label = '[[' .. article .. '|' .. label .. ']]'
			end

			local countryStatements = mw.wikibase.getBestStatements( awardId, 'P17' )
			local countries = {}
			if countryStatements then
				for _, statement in ipairs( countryStatements ) do
					if statement.mainsnak.datavalue and
						statement.mainsnak.datavalue.type == 'wikibase-entityid'
					then
						local countryId = statement.mainsnak.datavalue.value.id
						table.insert( countries, mw.wikibase.getLabel( countryId ) )
					end
				end
			end

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

	return '{| class="wikitable"\n' ..
		'! # !! Элемент !! Планка !! Название !! Государство\n|-\n' ..
		table.concat( out, '\n|-\n' ) ..
		'\n|}'
end

return p