Модуль:Wikidata2/media

Викикитап, ачык эчтәлекле китаплар җыентыгыннан

Для документации этого модуля может быть создана страница Модуль:Wikidata2/media/doc

local p = {}

function p.formatCommonsCategory( context, options, value )
    local link = 'commons:Category:' .. value
    local title = value .. ' Викиҗыентыкта'
    if ( options['text'] and options['text'] ~= '' ) then
    	title = options['text']
    end

	commons = '[[' .. link .. '|' .. title .. ']]'

	--Commons icon
    if ( not options['icon'] or options['icon'] ~= '-' ) then
    	local icon_size = '15px'
    	if ( options['icon_size'] and options['icon_size'] ~= '' ) then
    		icon_size = options['icon_size']
    	end
    	commons = '[[File:Commons-logo.svg|' .. icon_size .. '|link=' .. link .. ']] ' .. commons
    end

	--Text before and after link
    if ( options['text_before'] and options['text_before'] ~= '' ) then
    	if ( options['text_before'] ~= '-' ) then
	    	commons = options['text_before'] .. ' ' .. commons
	    end
    end

    if ( options['text_after'] and options['text_after'] ~= '' ) then
    	if ( options['text_after'] ~= '-' ) then
	    	commons =  commons .. ' ' .. options['text_after']
	    end
    end
	
    return commons
end

function p.formatImage( context, options, value )
	local image = '[[File:' .. value
	if options['border'] and options['border'] ~= '' then
    	image = image .. '|border'
    end
    if options['size'] and options['size'] ~= '' then
    	image = image .. '|' .. options['size']
    end
    if options['alt'] and options['alt'] ~= '' then
    	image = image .. '|' .. options['alt']
    end
	image = image .. ']]'
	
    return image
end

--[[ 
  Функция для оформления одного утверждения (statement) для изображения

  Принимает: объект-таблицу утверждение, таблицу параметров,
  объект-функцию оформления внутренних структур утверждения (snak) и
  объект-функцию оформления ссылки на источники (reference)
  Возвращает: строку оформленного текста с заявлением (claim)
]]
function p.formatImageClaim( context, options, statement )
	local snak = statement.mainsnak
	local hash = ''
	local mainSnakClass = ''
	if ( snak.hash ) then
		hash = ' data-wikidata-hash="' .. snak.hash .. '"'
	else
		mainSnakClass = ' wikidata-main-snak';
	end

	local before = '<span class="wikidata-snak ' .. mainSnakClass .. '"' .. hash .. '>'
	local after = '</span>'

    if snak.snaktype ~= 'value' then
    	return ''
    end

	--Image legend
	local legend = ''
    if ( statement.qualifiers and statement.qualifiers.P2096 ) then
		local langCode = mw.language.getContentLanguage():getCode()
		mw.logObject( statement.qualifiers.P2096 )
		mw.logObject( langCode )
    	for k, qualifier in pairs ( statement.qualifiers.P2096 ) do
    		if qualifier.snaktype == 'value'
    			and qualifier.datavalue.value.language == langCode
			then
				legend = '<br>' .. qualifier.datavalue.value.text
				break
			end
		end
	end

    return p.formatImage( context, options, snak.datavalue.value ) .. legend
end

return p