Módulo:User group rights

Da wiki Gamepedia Help Wiki
Ir para: navegação, pesquisa

A documentação para este módulo pode ser criada na página Módulo:User group rights/doc

local p = {}

local i18n = {
	user_rights = 'Privilégios',
	extension_ns = 'Extensão',
	non_default_ext = 'From non-default extensions'
}

function p.removeLinks(message)
	local cleanmsg = mw.text.trim(string.gsub(message,'%[%[[^%|%]]-%|([^%|%]]-)%]%]','%1'),'%p')
	return cleanmsg
end

function p.rightsSort(t1, t2) 
	local l1 = p.removeLinks(mw.message.new('right-' .. t1['right']):inLanguage(mw.getContentLanguage()):plain())
	local l2 = p.removeLinks(mw.message.new('right-' .. t2['right']):inLanguage(mw.getContentLanguage()):plain())
	if l1 < l2 then 
		return true
	end
	return false
end

function p.findIndex(et,ext)
	for i,v in ipairs(et) do
		if v.extName == ext then
			return i
		end
	end
	return -1
end

function p.extensionSort(t1,t2)
	if t1['extName'] < t2['extName'] then
		return true
	end
	return false
end

function p.main(frame)
	local rights = mw.loadData('Module:User_group_rights/data')
	local group = frame:getParent().args[1]
	local grouprights = {}
	local text = '<h2>' .. i18n['user_rights'] .. '</h2>\n'
	--get the rights in the selected group
	for i, right in ipairs(rights) do
		for i2, g in ipairs(right['groups']) do
			if g == group then
				table.insert(grouprights, right)
			end
		end
	end
	
	--now we sort the rights by the localized right description
	table.sort(grouprights,p.rightsSort)
	
	local extensionrights = {}
	--and now process default rights
	for i, right in ipairs(grouprights) do
		if right['extension'] == nil then
			text = text .. '* ' .. frame:preprocess(mw.message.new('right-' .. right['right']):inLanguage(mw.getContentLanguage()):plain()) .. ' <code>(' .. right['right'] .. ')</code>\n'
		else 
			local idx = p.findIndex(extensionrights, right['extension'])
			if idx == -1 then
				table.insert(extensionrights,{ extName = right['extension'], rights = {right} })
			else
				table.insert(extensionrights[idx]['rights'], right)
			end
		end
	end
	table.sort(extensionrights,p.extensionSort)
	local exttext = ''
	
	--and extension rights
	for i, extrights in ipairs(extensionrights) do
		local extension = extrights['extName']
		local rights = extrights['rights']
		exttext = exttext .. '\n<h4>[[' .. i18n['extension_ns'] .. ':'..extension..'|'..extension..']]</h4>\n'
		for i, right in ipairs(rights) do
			exttext = exttext .. '\n* ' .. frame:preprocess(mw.message.new('right-' .. right['right']):inLanguage(mw.getContentLanguage()):plain()) .. ' <code>(' .. right['right'] .. ')</code>'	
		end
	end
	
	if exttext > '' then
		text = text .. '<h3>' .. i18n['non_default_ext'] .. '</h3>\n'	.. exttext
	end
	return text
end

return p