Modul:Szerző

A Wikiforrásból
Ugrás a navigációhoz Ugrás a kereséshez

A modult a Modul:Szerző/doc lapon tudod dokumentálni

require('Modul:No globals')

local p = {}
local getArgs = require('Modul:Arguments').getArgs

local projects = {
	wp = { logo = 'Wikipedia-logo-v2.svg', text = "Szócikk a '''[[w:%s|Wikipédiában]]'''" },
	commons = { logo = 'Commons-logo.svg', text = "Médiaállományok a '''[[commons:%s|Wikimedia Commonsban]]'''" },
	wq = { logo = 'Wikiquote-logo.svg', text = "Idézetek a '''[[q:%s|Wikidézetben]]'''" },
	gutenberg = { logo = 'Gutenberg ico.png', text = "Művek a [//www.gutenberg.org/ebooks/author/%s Project Gutenbergben]" },
	dominiopu = { logo = 'Dpb.png', text = "Művek a [http://www.dominiopublico.gov.br/pesquisa/PesquisaObraForm.do?select_action=&co_autor=%s Domínio Públicón]" },
	domipubli = { logo = 'Dpe.png', text = "Művek a [http://www.dominiopublico.nom.es/autor.php?r=r&id=%s Dominio Públicón]" },
	cervantes = { logo = nil, text = "Művek a [http://www.cervantesvirtual.com/FichaAutor.html?Ref=%s Cervantes Virtualon]" },
	ebooksg = { logo = nil, text = "Művek az [http://www.ebooksgratuits.com/ebooks.php?id_categorie=1&id_genre=1&id_auteur=%s eBooks Gratuitson]" },
}

local function wikibase(id, overwrite, entityId, datatype)
	if (overwrite) then
		return overwrite ~= '-' and overwrite or nil
	end
	local entity = mw.wikibase.getEntity(entityId)
	if not entity then
		return nil
	end
	local statements = entity:getBestStatements( id:upper() )
	for _, v in pairs(statements) do
		local snak = v.mainsnak
		if snak.snaktype == 'value' then
			local datavalue = snak.datavalue
			if datavalue.type == (datatype or 'string') then
				return datavalue.value
			end
		end
	end
	return nil
end

local function sitelink(id, overwrite, entityId)
	if (overwrite) then
		return overwrite ~= '-' and overwrite or nil
	end
	return require('Modul:Wikibase').sitelink{id, entityId = entityId}
end

local function commonslink(overwrite, entityId)
	if overwrite then
		return overwrite ~= '-' and 'Category:' .. overwrite or nil
	end
	local sl = sitelink('commonswiki', entityId)
	if sl and string.find(sl, '^Category%:') then
		return sl
	end
	local P910 = wikibase('P910', nil, entityId, 'wikibase-entityid')
	if P910 then
		sl = sitelink('commonswiki', nil, P910.id)
		if sl and string.find(sl, '^Category%:') then
			return sl
		end
	end
	local P373 = wikibase('P373', nil, entityId)
	return P373 and 'Category:' .. P373
end

local function field(text, class, style, header)
	local row = mw.html.create('tr')
	local cell = mw.html.create( (header and 'th') or 'td' )
	if class then
		cell:addClass(class)
	end
	if style then
		cell:css(style)
	end
	cell:wikitext(text)
	row:node(cell)
	return row
end

local function image(filename, size, legend, entityId)
	if filename == '-' then
		return nil
	end
	if not filename then
		local item = mw.wikibase.getEntity(entityId)
		if not item then
			return nil
		end
		local image = item:getBestStatements('P18')[1]
		if image and image.mainsnak.snaktype == 'value' then
			filename = image.mainsnak.datavalue.value
			legend = nil
			if image.qualifiers and image.qualifiers.P2096 then
				for _, snak in ipairs(image.qualifiers.P2096) do
					if snak.snaktype == 'value' then
						if snak.datavalue.value.language == 'hu' then
							legend = snak.datavalue.value.text
						elseif snak.datavalue.value.language == 'en' and not legend then
							legend = snak.datavalue.value.text
						end
					end
				end
			end
		else
			return nil
		end
	end
	if not size then
		local title = mw.title.new('Media:' .. filename)
		if (
			title and title.file.width and title.file.width < 250
			and title.file.mimeType ~= 'image/svg' and title.file.mimeType ~= 'image/svg+xml'
		) then
			size = title.file.width
		else
			size = 250
		end
		size = tostring(size) .. 'px'
	end
	return field(string.format('[[Fájl:%s|bélyeg|%s%s]]', filename, size, (legend and ('|' .. legend) or '')), 'image')
end

local function seealsoLink(id, article)
	local project = projects[id]
	if not project then
		return nil
	end
	local logo, text = project.logo, project.text
	if logo then
		logo = '[[Fájl:' .. logo .. '|20px|alt=]] '
	else
		logo = ''
	end
	text = mw.ustring.format(text, article)
	return field(logo .. text)
end

function p.main(frame)
	local args = getArgs(frame)
	local title = args['nome'] or args['név'] or mw.title.getCurrentTitle().text
	local colorcss = { ['background-color'] = args['cor'] or args['szín'] or nil }
	local htmltable = mw.html.create('table')
	htmltable
		:addClass('infobox toccolours infobox-person')
		:css{ width = args['szélesség'] }
	htmltable:node( field(title, nil, colorcss, true) )
	htmltable:node( image( args['kép'], args['képméret'], args['képaláírás'], args.wikidata ) )
	local links = {
		wp = sitelink('huwiki', args['wikipédia'], args.wikidata),
		commons = commonslink(args['wikicommons'], args.wikidata),
		wq = sitelink('huwikiquote', args['wikidézet'], args.wikidata),
		gutenberg = wikibase( 'P1938', args['gutenberg'], args.wikidata ),
		dominiopu = args['DominioPu'],
		domipubli = args['DomuPubli'],
		cervantes = args['cervantes'],
		ebooksg = args['ebooksg']
	}
	local seealso = {}
	local is_seealso = false
	for n, v in pairs(links) do
		if v then
			local link = seealsoLink(n, v)
			table.insert( seealso, link )
			is_seealso = true
		end
	end
	if is_seealso then
		htmltable:node( field('Lásd még', nil, colorcss, true) )
		for _, v in pairs(seealso) do
			htmltable:node(v)
		end
	end
	return tostring(htmltable:done()) .. mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = 'Szerző/style.css' } )
end

return p