Ez a {{fej}}(?) sablon megvalósítása Luában.


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

local function getIdFromStatement( s )
	if s and s.mainsnak and s.mainsnak.snaktype == 'value' and s.mainsnak.datavalue.type == 'wikibase-entityid' then
		return s.mainsnak.datavalue.value.id
	else
		return nil
	end
end

local function getSourceWorkId( entity )
	local sources = entity:getBestStatements( 'P629' )
	if #sources == 1 then
		return getIdFromStatement( sources[1] )
	else
		return nil
	end
end

local function formatStatement( entity, prop )
	if entity then
		if #entity:getBestStatements( prop ) > 0 then
			local map = entity:formatStatements( prop )
			if map and map.value and map.value ~= '' then
				return map.value
			end
		end
		local source = getSourceWorkId( entity )
		if source then
			return formatStatement( mw.wikibase.getEntity( source ), prop )
		end
	end
	return nil
end

local function makeLink( str )
	if mw.title.new( str ) then -- valid title
		return string.format( '[[Szerző:%s|%s]]', str, str )
	else
		return str
	end
end

local function getAuthorCategories( entity, cats )
	local authors = entity:getBestStatements( 'P50' )
	if #authors > 0 then
		for _, v in ipairs( authors ) do
			local aut = getIdFromStatement( v )
			if aut then
				for _, w in ipairs( mw.wikibase.getBestStatements( aut, 'P910' ) ) do
					local cat = getIdFromStatement( w )
					if cat and mw.wikibase.getSitelink( cat ) then
						cat = mw.title.new( mw.wikibase.getSitelink( cat ) )
						if cat and cat.namespace == 14 then
							table.insert( cats, cat.text )
						end
					end
				end
			end
		end
	else
		local source = getSourceWorkId( entity )
		if source then
			getAuthorCategories( mw.wikibase.getEntity( source ), cats )
		end
	end
end

local function makeTable( args )
	local function getAuthors( parentNode )
		local ass = {}
		if args.aut_np then
			table.insert( ass, args.aut_np )
		elseif args.aut then
			table.insert( ass, string.format( 'szerző: %s', makeLink( args.aut ) ) )
		else
			local aut = formatStatement( args.entity, 'P50' )
			if aut then
				table.insert( ass, string.format( 'szerző: %s', aut ) )
			end
		end
		if args.trans then
			if args.trans == '?' or mw.ustring.lower( args.trans ) == 'ismeretlen' then
				table.insert( ass, 'ismeretlen fordító' )
			--[=[ ilyen még nincs
			elseif mw.ustring.lower( args.trans ) == 'wikiforrás' then
				table.insert( ass, 'fordította a [[Wikiforrás:Fordítások|Wikiforrás]]' )
			]=]
			else
				table.insert( ass, string.format( 'fordító: %s', makeLink( args.trans ) ) )
			end
		else
			local trans = formatStatement( args.entity, 'P655' )
			if trans then
				table.insert( ass, string.format( 'fordító: %s', trans ) )
			end
		end
		if #ass > 0 then
			parentNode:tag( 'br' )
			parentNode:tag( 'i' ):wikitext( table.concat( ass, ', ' ) )
		end
	end
	
	local header = mw.html.create( 'table' )
		:attr( 'id', 'fejsablon' )
		:addClass( 'fejsablon' )
		:tag( 'tr' )
	local prev = header:tag( 'td' ):addClass( 'fejsablon_elozo' )
	if args.prev then
		prev:tag( 'span' )
			:attr( 'id', 'fejsablonelozo' )
			:wikitext( '← ' .. args.prev )
	end
	local title = header:tag( 'td' ):addClass( 'fejsablon_cim' )
	title:tag( 'b' ):wikitext( args.tit or 'Címtelen' )
	getAuthors( title )
	if args.sect then
		title:tag( 'br' )
		title:wikitext( args.sect )
	end
	local next = header:tag( 'td' ):addClass( 'fejsablon_kovetkezo' )
	if args.next then
		next:tag( 'span' )
			:attr( 'id', 'fejsablonkovetkezo' )
			:wikitext( args.next .. ' →' )
	end
	return tostring( header:done() )
end

local function makeDiv( content )
	if content then
		content = mw.html.create( 'div' ):wikitext( content )
		return tostring( content:addClass( 'fejsablon_megjegyzes' ):tag( 'div' ):css( 'clear', 'both' ):allDone() )
	else
		return ''
	end
end

local function getCategories( args )
	local cats = {}
	local lines = {}
	
	local function mainCats()
		if args.nocat then
			return
		end
		local title = mw.title.getCurrentTitle()
		if title.namespace ~= 0 then
			return
		end
		if title.isSubpage then
			local root = title.rootPageTitle
			if root.exists then
				table.insert( cats, root.text )
			end
			table.insert( lines, '__EXPECTED_UNCONNECTED_PAGE__' )
		else
			if args.aut and not args.aut_np and mw.title.new( args.aut ) then
				table.insert( cats, args.aut )
			elseif args.entity then
				getAuthorCategories( args.entity, cats )
			end
			if args.trans then
				--[=[ 
				if mw.ustring.lower( args.trans ) == 'wikiforrás'
					if not mw.title.getCurrentTitle().isSubpage then
						table.insert( cats, 'Wikiforrás-fordítások' )
					end
				else ]=]
				if mw.title.new( args.trans ) then
					table.insert( cats, args.trans .. ' fordításai' )
				end
			end
		end
	end

	local function trackingCats()
		if args.aut_np then
			table.insert( cats, 'A lapnélküli szerző paramétert használó lapok' )
		end
		if (args.aut and not mw.title.new( args.aut ) ) or (args.trans and not mw.title.new( args.trans ) ) then
			table.insert( cats, 'Linkfelismerést használó lapok' )
		end
	end

	if not args.defsort then
		local title = mw.title.getCurrentTitle()
		if title.namespace == 0 then
			args.defsort = p.getDefaultSort(title.text)
		end
	end
	if args.defsort and args.defsort ~= '-' then
		-- state stored, returns empty string; no need to add this to the table
		mw.getCurrentFrame():preprocess( string.format( '{{DEFAULTSORT:%s}}', args.defsort ) )
	end
	mainCats()
	trackingCats()
	local langLinks = require('Modul:Wikibase').wikisourceLangLinks{ entity = args.entity }
	if langLinks ~= '' then
		langLinks = '\n\n' .. langLinks
	end
	table.insert( lines, mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = 'Fej/style.css' } ) )
	for _, v in pairs( cats ) do
		table.insert( lines, string.format( '[[Kategória:%s]]', v ) )
	end
	return '\n' .. table.concat( lines, '\n' ) .. langLinks
end

function p.getDefaultSort(frame)
	local title
	if type(frame) == 'table' then
		title = frame.args[1]
	elseif frame then
		title = tostring(frame)
	else
		title = mw.title.getCurrentTitle().text
	end
	local replacements = {
		[''] = { ' ', '_', '"', '„', '”', '»', '«', "'", '’', '-', '–', '—', ':', '.', ',', ';', '!', '?', '…', '(', ')' },
		a = { 'Á', 'á' },
		e = { 'É', 'é' },
		i = { 'Í', 'í' },
		o = { 'Ó', 'ó' },
		['o~'] = { 'Ö', 'ö', 'Ő', 'ő' },
		u = { 'Ú', 'ú' },
		['u~'] = { 'Ü', 'ü', 'Ű', 'ű' }
	}
	for plain, accented in pairs(replacements) do
		if plain == '' then
			title = mw.ustring.gsub(title, '[%' .. table.concat(accented, '%') .. ']', '')
		else
			title = mw.ustring.gsub(title, '[' .. table.concat(accented) .. ']', plain)
		end
	end
	local lang = mw.language.getContentLanguage()
	return lang:ucfirst(lang:lc(title))
end

function p.main( frame )
	local args = getArgs( frame )
	local entity = mw.wikibase.getEntity( args.wikidata )
	local args2 = {
		aut		= args[ 'szerző' ],
		aut_np	= args[ 'lapnélküli_szerző' ] or args[ 'lap nélküli szerző' ],
		trans	= args[ 'fordító' ],
		tit		= args[ 'cím' ],
		sect	= args[ 'szakasz' ],
		prev	= args[ 'előző' ] or formatStatement( entity, 'P155' ),
		next	= args[ 'következő' ] or formatStatement( entity, 'P156' ),
		note	= args[ 'megjegyzés' ],
		defsort	= args[ 'kulcs' ],
		nocat	= not not (args[ 'nekategorizálj' ] or args[ 'minta' ]),
		entity  = entity
	}
	return makeTable( args2 ) .. makeDiv( args2.note ) .. getCategories( args2 )
end

return p