Módulo:Usuário

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

local p = {}
function p.link(frame)
		local usuario   = frame.args['usuário'] or ''
		local apelido   = frame.args['apelido'] or ''
		local separador = frame.args['separador'] or ' · '
		local prefixo   = frame.args['prefixo'] or '('
		local sufixo   = frame.args['sufixo'] or ')'
		
		if apelido == '' then apelido = usuario end
		
		-- ​ é o ZERO WIDTH SPACE, e é um workaround para um bug que
		-- eventualmente remove os espaços no começo e no final da string
		-- Sem ele o código produz algumas formatações erradas.  Não tirar.
		separador = '​' .. separador
		
		-- Quando esse script foi feito, mw.language não estava disponível no Scribunto
		---- local sexo = mw.language:gender(usuario, 'o', 'a', 'o(a)')
		-- utilizada a Magic Word {{GENDER|..}}
		local sexo = frame:callParserFunction( 'gender', usuario, 'o', 'a', 'o(a)' )
		
		local discussao                = frame.args['discussão'               ] or "discussão"
		local contribuicoes            = frame.args['contribuições'           ] or "contribuições"
		local contribuicoes_globais    = frame.args['contribuições globais'   ] or "contribuições globais"
		local contribuicoes_eliminadas = frame.args['contribuições eliminadas'] or "contribuições eliminadas"
		local numero_edicoes           = frame.args['número de edições'       ] or "número de edições"
		local registros                = frame.args['registros'               ] or "registros"
		local registros_usuario        = frame.args['registros do usuário'    ] or "registros d" .. sexo .. " usuári" .. sexo
		local registros_bloqueio       = frame.args['registros de bloqueio'   ] or "registros de bloqueio"
		local registros_movimento      = frame.args['registros de movimento'  ] or "registros de movimento"
		local registros_filtros        = frame.args['registros dos filtros'   ] or "registros dos filtros"
		local bloquear                 = frame.args['bloquear'                ] or "bloquear"
		
		local discussao_alt                = "Página de discussão d" .. sexo .. " usuári" .. sexo
		local contribuicoes_alt            = "Contribuições d" .. sexo .. " usuári" .. sexo
		local contribuicoes_globais_alt    = "Contribuições globais d" .. sexo .. " usuári" .. sexo
		local contribuicoes_eliminadas_alt = "Contribuições eliminadas"
		local numero_edicoes_alt           = "Relatório de edições d" .. sexo .. " usuári" .. sexo
		local registros_alt                = "Registros públicos d" .. sexo .. " usuári" .. sexo
		local registros_usuario_alt        = "Registros que afetam " .. sexo .. " usuári" .. sexo
		local registros_bloqueio_alt       = "Registros de bloqueio"
		local registros_movimento_alt      = "Registros de movimentação de páginas"
		local registros_filtros_alt        = "Registros dos filtros de edição"
		local bloquear_alt                 = "Bloquear usuári" .. sexo
		
		local usuarioURL = frame:callParserFunction('urlencode', usuario, 'QUERY')
		
		-- recupera link das estatísticas do usuário pela página [[Predefinição:EstatísticasUsuário]]
		local numero_edicoes_link = frame:expandTemplate{ title = 'EstatísticasUsuário', args = { usuario } }
		
		-- outros links que não podem ser informados através de Wikilinks
		local registros_usuario_link   = frame:callParserFunction('fullurl', 'Special:Log', 'page=User:' .. usuarioURL)
		local registros_bloqueio_link  = frame:callParserFunction('fullurl', 'Special:Log', 'type=block&page=User:' .. usuarioURL)
		local registros_movimento_link = frame:callParserFunction('fullurl', 'Special:Log', 'type=move&user=' .. usuarioURL)
		local registros_filtros_link   = frame:callParserFunction('fullurl', 'Special:AbuseLog', 'wpSearchUser=' .. usuarioURL)
		local txt = ''
		if discussao                ~= "" then
				if txt ~= '' then txt = txt .. separador end
				txt = txt .. '<span title="' .. discussao_alt .. '">[[User talk:' .. usuario .. '|' .. discussao .. ']]</span>'
		end
		if contribuicoes            ~= "" then
				if txt ~= '' then txt = txt .. separador end
				txt = txt .. '<span title="' .. contribuicoes_alt .. '">[[Special:Contributions/' .. usuario .. '|' .. contribuicoes .. ']]</span>'
		end
		if contribuicoes_globais    ~= "" then
				if txt ~= '' then txt = txt .. separador end
				txt = txt .. '<span title="' .. contribuicoes_globais_alt .. '">[[Special:CentralAuth/' .. usuario .. '|' .. contribuicoes_globais .. ']]</span>'
		end
		if contribuicoes_eliminadas ~= "" then
				txt = txt .. '<span class="sysop-show eliminator-show">' .. (txt ~= '' and separador or '')
				txt = txt .. '<span title="' .. contribuicoes_eliminadas_alt .. '">[[Special:DeletedContributions/' .. usuario .. '|' .. contribuicoes_eliminadas .. ']]</span>'
				txt = txt .. '</span>'
		end
		if numero_edicoes           ~= "" then
				if txt ~= '' then txt = txt .. separador end
				txt = txt .. '<span title="' .. numero_edicoes_alt .. '">[' .. numero_edicoes_link .. ' ' .. numero_edicoes .. ']</span>'
		end
		if registros                ~= "" then
				if txt ~= '' then txt = txt .. separador end
				txt = txt .. '<span title="' .. registros_alt .. '">[[Special:Log/' .. usuario .. '|' .. registros .. ']]</span>'
		end
		if registros_usuario       ~= "" then
				if txt ~= '' then txt = txt .. separador end
				txt = txt .. '<span title="' .. registros_usuario_alt .. '">[' .. registros_usuario_link .. ' ' .. registros_usuario .. ']</span>'
		end
		if registros_bloqueio       ~= "" then
				if txt ~= '' then txt = txt .. separador end
				txt = txt .. '<span title="' .. registros_bloqueio_alt .. '">[' .. registros_bloqueio_link .. ' ' .. registros_bloqueio .. ']</span>'
		end
		if registros_movimento      ~= "" then
				if txt ~= '' then txt = txt .. separador end
				txt = txt .. '<span title="' .. registros_movimento_alt .. '">[' .. registros_movimento_link .. ' ' .. registros_movimento .. ']</span>'
		end
		if registros_filtros      ~= "" then
				if txt ~= '' then txt = txt .. separador end
				txt = txt .. '<span title="' .. registros_filtros_alt .. '">[' .. registros_filtros_link .. ' ' .. registros_filtros .. ']</span>'
		end
		if bloquear                 ~= "" then
				if isIP(usuario) then
						txt = txt .. '<span class="sysop-show rollbacker-show">' .. (txt ~= '' and separador or '')
				else
						txt = txt .. '<span class="sysop-show">' .. (txt ~= '' and separador or '')
				end
				txt = txt .. '<span title="' .. bloquear_alt .. '">[[Special:Block/' .. usuario .. '|' .. bloquear .. ']]</span>'
				txt = txt .. '</span>'
		end
		
		if txt ~= '' then
				txt = ' ' .. prefixo .. txt .. sufixo
		end
		
		txt = '<span class="plainlinks">[[User:' .. usuario .. '|' .. apelido .. ']]' .. txt .. '</span>'
		return txt
end
function isIP(usuario)
		return GetIPType(usuario) ~= 0
end
-- code from http://stackoverflow.com/questions/10975935/lua-function-check-if-ipv4-or-ipv6-or-string
function GetIPType(ip)
		local R = {NOT_IP = 0, IPV4 = 1, IPV6 = 2}
		if type(ip) ~= "string" then return R.NOT_IP end
		-- check for format 1.11.111.111 for ipv4
		local chunks = {ip:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")}
		if #chunks == 4 then
				for _,v in pairs(chunks) do
						if tonumber(v) > 255 then return R.STRING end
				end
				return R.IPV4
		end
		-- check for ipv6 format, should be 8 'chunks' of numbers/letters
		-- without trailing chars
		local chunks = {ip:match(("([a-fA-F0-9]*):"):rep(8):gsub(":$","$"))}
		if #chunks == 8 then
				for _,v in pairs(chunks) do
						if #v > 0 and tonumber(v, 16) > 65535 then return R.STRING end
				end
				return R.IPV6
		end
		return R.NOT_IP
end
return p