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._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 imageName = nil
	local maxSizeOfImage = nil
	for _idx, eachPropId in pairs(imagePropIds) do
		local defaultImageName = wikidata.findOneValueNoRef(eachPropId, entityId)
		local specificImageName = nil
		if timestamp then
			local dataClaim = wikidata.findClaimForTimestamp(entityId, eachPropId, timestamp)
			if dataClaim then specificImageName = dataClaim.mainsnak.datavalue.value end
		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 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

return p