Sari la conținut

Modul:Multiple images from Wikidata: Diferență între versiuni

De la Wikipedia, enciclopedia liberă
Conținut șters Conținut adăugat
Fără descriere a modificării
Fără descriere a modificării
Linia 5: Linia 5:
local wikidata = require('Modul:Wikidata')
local wikidata = require('Modul:Wikidata')
local join = require('Modul:Separated entries')._main
local join = require('Modul:Separated entries')._main
local StringUtils = require('Modul:StringUtils')

local capitalize = StringUtils._capitalize


local p = {}
local p = {}
Linia 23: Linia 26:
function p.fromArgs(q, props, captionTemplates)
function p.fromArgs(q, props, captionTemplates)
local images = {}
local images = {}
local captions = { separator = '; ', conjunction = '; și '}
local captions = {}
for propIdx=1,#props do
for propIdx=1,#props do
local eachProp = props[propIdx]
local eachProp = props[propIdx]
Linia 49: Linia 52:
end
end
captions = TableTools.removeDuplicates(captions)
captions = TableTools.removeDuplicates(captions)
captions.separator = '; '
captions.conjunction = '; și '



if 0 == #images then return '' end
if 0 == #images then return '' end
Linia 55: Linia 61:
local multiImagesGeometryArgs = {}
local multiImagesGeometryArgs = {}
multiImagesGeometryArgs.dir = tostring(3 < #images and 'vertical' or 'horizontal')
multiImagesGeometryArgs.dir = tostring(3 < #images and 'vertical' or 'horizontal')
multiImagesGeometryArgs.totalwidth = math.min(3, #images) * 200
local multiImagesImgArgs = {}
local multiImagesImgArgs = {}
Linia 67: Linia 74:


local multiImageTextArgs = {
local multiImageTextArgs = {
footer = join(captions)
footer = capitalize({join(captions)})
}
}
Linia 85: Linia 92:
local captionTemplatesIndices = TableTools.affixNums(array, 'caption_template')
local captionTemplatesIndices = TableTools.affixNums(array, 'caption_template')
for _,eachCaptionTemplateIdx in ipairs(captionTemplatesIndices) do
for _,eachCaptionTemplateIdx in ipairs(captionTemplatesIndices) do
table.insert(captionTemplates, array[eachCaptionTemplateIdx])
table.insert(captionTemplates, array['caption_template' .. tostring(eachCaptionTemplateIdx)])
end
end

Versiunea de la 2 octombrie 2017 13:16

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)
	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 = tostring(3 < #images and 'vertical' or 'horizontal')
	multiImagesGeometryArgs.totalwidth = math.min(3, #images) * 200
	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
	
	return p.fromArgs(array.q, props, captionTemplates)
end

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

return p