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 = { }
maxSize['P94'] = '23x23'
maxSize['P41'] = '23x23'

p._nameAndImageFromOneOfProps = function(entityId, imagePropIds, timestamp)
	libUtils.checkTypeForNamedArg('_nameAndImage', 'imagePropIds', imagePropIds, 'table', false)
	libUtils.checkTypeForNamedArg('_nameAndImage', 'entityId', entityId, 'string', true)
	libUtils.checkTypeForNamedArg('_nameAndImage', 'timestamp', entityId, 'string', 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
		local allImageClaims = wikidata.findSortedClaimsForProperty(entityId, eachPropId)
		if timestamp then
			for _,eachImageClaim in ipairs(allImageClaims) do
				if eachImageClaim.mainsnak.snaktype == 'value' and eachImageClaim.qualifiers then
					local before, after
					if eachImageClaim.qualifiers['P580'] and eachImageClaim.qualifiers['P580'][1] and eachImageClaim.qualifiers['P580'][1].snaktype == 'value' then
						after = GregorianDate.convertToGregorianIfInInterval(DateUtils.extractDateFromWikidataSnak(eachImageClaim.qualifiers['P580'][1]))
					end
					if eachImageClaim.qualifiers['P582'] and eachImageClaim.qualifiers['P582'][1] and eachImageClaim.qualifiers['P582'][1].snaktype == 'value' then
						before = GregorianDate.convertToGregorianIfInInterval(DateUtils.extractDateFromWikidataSnak(eachImageClaim.qualifiers['P582'][1]))
					end
					if after == nil and before and DateUtils.compare(timestamp, before) < 0 then
						specificImageName = eachImageClaim.mainsnak.datavalue.value
						break
					elseif after and before and DateUtils.compare(timestamp, before) < 0 and DateUtils.compare(timestamp, after) > 0 then 
						specificImageName = eachImageClaim.mainsnak.datavalue.value
						break
					elseif after and before == nil and ts > after then
						specificImageName = eachImageClaim.mainsnak.datavalue.value
						break
					end
				end
			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', entityId, 'string', 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