Sari la conținut

Modul:NameAndImage

Permanently protected module
De la Wikipedia, enciclopedia liberă

local getArgs = require('Modul:Arguments').getArgs
local wikidata = require('Modul:Wikidata')
local StringUtils = require('Modul:StringUtils')
local DateUtils = require('Modul:DateUtils')
local GregorianDate = require('Modul:GregorianDate')
local join = require('Modul:Separated entries')._main
local libUtils = require('libraryUtil')
local p = {}

local maxSize = { ['P94'] = '23x23', ['P41'] = '23x23'}

p._imageFromOneOfProps = function(entityId, imagePropIds, timestamp)
	local imageName = nil
	local maxSizeOfImage = nil
	for _idx, eachPropId in pairs(imagePropIds) do
		local defaultImageName = wikidata.findOneValueNoRef(eachPropId, entityId)
		local specificImageName = nil
		local dataClaim = wikidata.findClaimForTimestamp(entityId, eachPropId, timestamp)
		if dataClaim then specificImageName = dataClaim.mainsnak.datavalue.value end

		if imageName == nil and (defaultImageName and mw.text.trim(defaultImageName) ~= '' or specificImageName and mw.text.trim(specificImageName) ~= '') then
			imageName = mw.text.trim(specificImageName or defaultImageName)
			maxSizeOfImage = StringUtils.appendIfMissing({maxSize[imagePropId] or '23x23', 'px'})
		end
	end
	
	local imageLink = ''
	if imageName and mw.text.trim(imageName) ~= '' then
		imageLink = '[[Fișier:' .. imageName .. '|' .. maxSizeOfImage .. ']]'
	end
	return imageLink
end

p._nameAndImageFromOneOfProps = function(entityId, imagePropIds, timestamp)
	libUtils.checkTypeForNamedArg('_nameAndImageFromOneOfProps', 'entityId', entityId, 'string', true)
	libUtils.checkTypeForNamedArg('_nameAndImageFromOneOfProps', 'imagePropIds', imagePropIds, 'table', false)
	libUtils.checkTypeForNamedArg('_nameAndImageFromOneOfProps', 'timestamp', timestamp, 'table', true)

	local articleLink = wikidata.findLinkToItem(entityId, true, false, true)

	local imageLink = p._imageFromOneOfProps(entityId, imagePropIds, timestamp)
	
	return mw.text.trim(join({imageLink, articleLink, separator = ' '}))
end

p._nameAndImage = function(entityId, imagePropId, timestamp)
	libUtils.checkTypeForNamedArg('_nameAndImage', 'imagePropId', imagePropId, 'string', false)
	libUtils.checkTypeForNamedArg('_nameAndImage', 'entityId', entityId, 'string', true)
	libUtils.checkTypeForNamedArg('_nameAndImage', 'timestamp', timestamp, 'table', true)

	return p._nameAndImageFromOneOfProps(entityId, {imagePropId}, timestamp)
end

p.nameAndImage = function(frame)
	local args = getArgs(frame)
	local entityId = args[1]
	local propId = args[2]
	local timestamp = args[2] or args['timestamp']
	if timestamp then timestamp = DateUtils.parseWikidataDate(timestamp) end
	return p._nameAndImage(entityId, propId, timestamp)
end

p.nameAndCoatOfArms = function(frame)
	local args = getArgs(frame)
	local entityId = args[1]
	local timestamp = args[2] or args['timestamp']
	if timestamp then timestamp = DateUtils.parseWikidataDate(timestamp) end
	return p._nameAndImage(entityId, 'P94', timestamp)
end

p.nameAndFlag = function(frame)
	local args = getArgs(frame)
	local entityId = args[1]
	local timestamp = args[2] or args['timestamp']
	if timestamp then timestamp = DateUtils.parseWikidataDate(timestamp) end
	return p._nameAndImage(entityId, 'P41', timestamp)
end

p.nameAndFlagFromPropOfEntity = function(frame)
	local args = getArgs(frame)
	local propId = args[1] or args['pid']
	local entityId = args[2] or args['qid']
	local timpestamp = args[3] or args['timestamp']
	
	if not propId then return '' end
	local entity = entityId
	if not entity then entity = mw.wikibase.getEntity() end
	if type(entityId) == 'string' then entity = mw.wikibase.getEntity(entityId) end
	if type(entityId) == 'number' then entity = mw.wikibase.getEntity('Q' .. tostring(entityId)) end
	
	local countryIdsList = {}
	if entity and entity.claims and entity.claims[propId] then
		for _,eachClaim in ipairs(entity.getBestStatements(propId)) do
			if eachClaim.mainsnak.snaktype == 'value' then
				table.insert(countryIdsList, eachClaim.mainsnak.datavalue.value.id)
			end
		end
	end

	local countriesWithFlags = {}
	for _,eachCountryId in ipairs(countryIdsList) do
		table.insert(countriesWithFlags, p._nameAndImage(eachCountryId, 'P41', timestamp))
	end
	return table.concat(countriesWithFlags, tostring(mw.html.create('br')))
end

p.flag = function(frame)
	local args = getArgs(frame)
	local entityId = args[1]
	local timestamp = args[2] or args['timestamp']
	if timestamp then timestamp = DateUtils.parseWikidataDate(timestamp) end
	return p._imageFromOneOfProps(entityId, {'P41'}, timestamp)
end

return p