Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:User type

From Wikipedia, the free encyclopedia

--- This module gets the user type for a given username.
--
-- @require Module:IPAddress
-- @release stable

local p = {}

p._ipMod = require("Module:IPAddress")

for k,v in pairs(p._ipMod) do
	p[k] = v
end

--- Detects whether the user is an account or not.
--
-- @param {string} s Username
-- @return The user type or an empty string if the user is an IP address.
function p._isUser(s)
	if p._isIpOrRange(s) ~= '' then return '' end
	local isTempUser = p._isTempUser(s)
	if isTempUser then return 'temp'
	else return 'perm' end
end

--- Detects whether the user is a temporary user or not.
--
-- @param {string} s Username
-- @return Whether the user is likely a permanent account.
function p._isTempUser(s)
	if p._isIpOrRange(s) ~= '' then return false end
	for k,v in ipairs( mw.text.split(s, '') ) do
		if ( k == 1 ) then
			if v ~= '~' then return false end
		elseif k == 6 or ( k > 6 and math.fmod( k, 6 ) == 0 ) then
			if v ~= '-' then return false end
		else
			if not v:match('%d') then return false end
		end
	end
	return true
end

--- Detects whether the user is a registered user or not.
--
-- @param {string} s Username
-- @return Whether the user is likely a permanent account.
function p._isPermUser(s)
	if p._isIpOrRange(s) ~= '' then return false end
	return not p._isTempUser(s)
end

--- Gets the user type.
--
-- @param {string} s Username
-- @return User type "ip", "range", "temp", or "perm".
function p._main(s)
	local isIpOrRange = p._isIpOrRange(s)
	local isUser = p._isUser(s)
	return isIpOrRange ~= '' and isIpOrRange or isUser
end

--- Adapted from [[Module:IPAddress]]
-- @see [[Module:IPAddress#L-54]]
local function input(args)
	-- Return input parameter after replacing any of following directional markers.
	-- LRM : LEFT-TO-RIGHT MARK (U+200E)         : hex e2 80 8e = 226 128 142
	-- LRE : LEFT-TO-RIGHT EMBEDDING (U+202A)    : hex e2 80 aa = 226 128 170
	-- PDF : POP DIRECTIONAL FORMATTING (U+202C) : hex e2 80 ac = 226 128 172
	-- This is required for MediaWiki:Blockedtext message.
	return (args[1] or ''):gsub('\226\128[\142\170\172]', ' ')  -- replace LRM, LRE, PDF with space delimiter
end

--- Entrypoint for templates to detect whether a user is a temporary account.
--
-- @param {Frame} frame Calling frame
-- @param {table} frame.args Calling arguments
-- @param {string} frame.args[1] String to test
-- @return Whether the user is likely a temporary account.
function p.isTempUser(frame) return p._isTempUser(input(frame)) and "1" or "0" end

--- Entrypoint for templates to detect whether a user is a registered account.
--
-- @param {Frame} frame Calling frame
-- @param {table} frame.args Calling arguments
-- @param {string} frame.args[1] String to test
-- @return Whether the user is likely a registered account.
function p.isPermUser(frame) return p._isPermUser(input(frame)) and "1" or "0" end

--- Entrypoint for templates to detect the type of user account.
--
-- @param {Frame} frame Calling frame
-- @param {table} frame.args Calling arguments
-- @param {string} frame.args[1] String to test
-- @return Whether the user is likely an account.
function p.isUser(frame) return p._isUser(input(frame)) or "" end

--- Entrypoint for {{tl|user type}}.
--
-- @param {Frame} frame Calling frame
-- @param {table} frame.args Calling arguments
-- @param {string} frame.args[1] String to test
-- @return Whether the user is likely an account.
function p.main(frame)
	local args = require("Module:Arguments").getArgs(frame)
	local sanitizeInput = input(args)
	if sanitizeInput == '' then return args['#default'] or '' end
	local result = p._main(sanitizeInput)
	if args[result] ~= nil then
		return args[result]
	elseif args['#default'] then
		return args['#default']
	else
		return result
	end
end

return p