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 yesno = require('Modul:Yesno')
local p = {}

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

local function toWikidataTimestamp(timestamp)
	local ret = nil
	if timestamp then
		ret = timestamp
		if mw.ustring.gmatch(timestamp, '^%d+$') then
			return '+' .. timestamp .. '-00-00', 9
		end
		if mw.ustring.find(timestamp, '%d+') == 1 then
			ret = '+' .. timestamp
		end
	end
	return ret
end


p._imageFromOneOfProps = function(entityId, imagePropIds, timestamp, skiplink)
	local imageName = nil
	local maxSizeOfImage = nil
	for _idx, eachPropId in pairs(imagePropIds) do
		local defaultImageName = wikidata.findOneValueNoRef(eachPropId, entityId)
		local specificImageName = nil
		local dataClaim = nil
		if timestamp then
			dataClaim = wikidata.findClaimForTimestamp(entityId, eachPropId, timestamp)
		else
			local bestDataClaims = wikidata.findBestClaimsForProperty(entityId, eachPropId)
			if bestDataClaims and 0 < #bestDataClaims then dataClaim = bestDataClaims[1] end
		end
		if dataClaim and 'statement' == dataClaim.type and 'value' == dataClaim.mainsnak.snaktype 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
		if skiplink then imageLink = imageLink .. '|link=' end
		imageLink = imageLink .. '|border]]'
	end
	return imageLink
end

p._nameAndImageFromOneOfProps = function(entityId, imagePropIds, timestamp, skiplink)
	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, skiplink)
	
	return mw.text.trim(join({imageLink, articleLink, separator = ' '}))
end

p._nameAndImage = function(entityId, imagePropId, timestamp, skiplink)
	if not entityId or entityId == '' then return '' end
	
	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, skiplink)
end

p.nameAndImage = function(frame)
	local args = getArgs(frame)
	local entityId = args[1]
	local propId = args[2]
	local timestamp = args[3] or args['timestamp']
	local skiplink = yesno(args['nolink'], false)
	local wdTimestamp, wdTimestampPrecision = toWikidataTimestamp(timestamp)
	if timestamp then timestamp = DateUtils.parseWikidataDate(wdTimestamp, wdTimestampPrecision) end
	return p._nameAndImage(entityId, propId, timestamp, skiplink)
end

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

p.nameAndFlag = function(frame)
	local args = getArgs(frame)
	local entityId = args[1]
	local timestamp = args[2] or args['timestamp']
	local skiplink = yesno(args['nolink'], false)
	local wdTimestamp, wdTimestampPrecision = toWikidataTimestamp(timestamp)
	if timestamp then timestamp = DateUtils.parseDate(wdTimestamp, wdTimestampPrecision) end
	return p._nameAndImage(entityId, 'P41', timestamp, skiplink)
end

p._nameAndFlagFromPropOfEntity = function(propId, entityId, timestamp)
	if not propId then return '' end
	local entity = entityId
	local claims = nil
	if type(entity) == 'table' then
		claims = entity:getBestStatements(propId)
	else
		if not entity then entity = mw.wikibase.getEntityIdForCurrentPage() end
		if type(entity) == 'number' then entity = 'Q' .. tostring(entityId) end
		if not entity then return nil end
		claims = mw.wikibase.getBestStatements(entity, propId)
	end
	
	local countryIdsList = {}
	if claims and 0 < #claims then
		for _,eachClaim in ipairs(claims) 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.nameAndFlagFromPropOfEntity = function(frame)
	local args = getArgs(frame)
	local propId = args['pid'] or args[1]
	local entityId = args['qid'] or args[2]
	local timpestamp = args['timestamp'] or args[3]

	return p._nameAndFlagFromPropOfEntity(propId, entityId, timestamp)
end

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

return p