Modul:Multiple images from Wikidata

Documentația acestui modul poate fi creată la Modul:Multiple images from Wikidata/doc

local multiImages = require('Modul:Multiple image').renderImages
local getArgs = require('Modul:Arguments').getArgs
local illWd = require('Modul:Ill-wd').fromArgs
local TableTools = require('Modul:TableTools')
local wikidata = require('Modul:Wikidata')
local join = require('Modul:Separated entries')._main
local StringUtils = require('Modul:StringUtils')

local capitalize = StringUtils._capitalize

local p = {}

local function extractOneClaim(q, propId)
	local claims = wikidata.findBestClaimsForProperty(q, propId)
	local array = {}
	if claims then
		for _,eachClaim in ipairs(claims) do
			if eachClaim.type == 'statement' and eachClaim.mainsnak.snaktype == 'value' then
				return eachClaim
			end
		end
	end
	return nil
end

function p.fromArgs(q, props, captionTemplates, styling)
	local images = {}
	local captions = {}
	
	for propIdx=1,#props do
		local eachProp = props[propIdx]
		local propClaim = extractOneClaim(q, eachProp)
		local imageClaim
		if propClaim then
			imageClaim = extractOneClaim(propClaim.mainsnak.datavalue.value.id, 'P18')
			if imageClaim then
				local imageName = imageClaim.mainsnak.datavalue.value
				
				local imageFile = mw.title.makeTitle('File', imageName)
				if imageFile.file and imageFile.file.exists then
					table.insert(images, {name = imageName, width = imageFile.file.width, height = imageFile.file.height})
					
					local crtCaptionTemplate = captionTemplates[propIdx]
					if not crtCaptionTemplate then crtCaptionTemplate = captionTemplates[1 + ((propIdx - 1) % #captionTemplates)] end
					local expandedCaptionTemplate = nil
					if crtCaptionTemplate then
						expandedCaptionTemplate = mw.ustring.gsub(crtCaptionTemplate, '__link__', illWd(propClaim.mainsnak.datavalue.value.id))
						table.insert(captions, expandedCaptionTemplate)
					end
				end
			end
		end
	end
	captions = TableTools.removeDuplicates(captions)
	captions.separator = '; '
	captions.conjunction = '; și '

	if 0 == #images then return '' end

	local multiImagesGeometryArgs = {}
	multiImagesGeometryArgs.dir = 'horizontal'
	multiImagesGeometryArgs.perrow = math.min(3, #images)
	multiImagesGeometryArgs.totalwidth = multiImagesGeometryArgs.perrow * 200
	multiImagesGeometryArgs.align = styling and styling.align or 'right'

	local multiImagesImgArgs = {}
	for eachImageIdx,eachImage in ipairs(images) do
		table.insert(multiImagesImgArgs,
			{
				image = eachImage.name,
				width = eachImage.width,
				height = eachImage.height
			})
	end

	local multiImageTextArgs = {
		footer = capitalize({join(captions)})
	}
	
	return multiImages(multiImagesImgArgs, multiImageTextArgs, multiImagesGeometryArgs)
end

function p.fromArray(array)
	if not array then return nil end
	local propIndices = TableTools.affixNums(array, 'prop')
	local props = {}
	
	for _,eachPropIdx in ipairs(propIndices) do
		table.insert(props, array['prop' .. tostring(eachPropIdx)])
	end
	
	local captionTemplates = {}
	local captionTemplatesIndices = TableTools.affixNums(array, 'caption_template')
	for _,eachCaptionTemplateIdx in ipairs(captionTemplatesIndices) do
		table.insert(captionTemplates, array['caption_template' .. tostring(eachCaptionTemplateIdx)])
	end
	
	local styling = {
		align = array['align'] or 'right'
	}
	
	return p.fromArgs(array.q, props, captionTemplates, styling)
end

function p.fromFrame(frame)
	return p.fromArray(getArgs(frame))
end

return p