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 name utils

Permanently protected module
From Wikipedia, the free encyclopedia

-- Lua implementation of mediawiki/core/+/refs/heads/master/includes/User/UserNameUtils.php
-- with the default RIGOR_VALID option

local p = {}

local IPV4_ADDRESS = "%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?"
local lang = mw.getContentLanguage()

function p._getCanonical(name)
	if (name or '') == '' then
		return false
	end
	
	-- force usernames to capital
	name = lang:ucfirst(name)
	
	-- Reject names containing '#'; these will be cleaned up
    -- with title normalisation, but then it's too late to
    -- check elsewhere
	if mw.ustring.match(name, "#") then
		return false
	end

	-- Clean up name according to title rules
	local title = mw.title.new(name,2)

	-- Check for invalid titles
	if title == nil or title.namespace ~= 2 or title.isExternal then
		return false
	end

	name = title.text

	return p._isValid(name) and name or false
end

function p._isValid(name)
	if (name or '') == '' or
		require('Module:IPAddress')._isIpOrRange(name) ~= "" or
		mw.ustring.match(name, "^" .. IPV4_ADDRESS .. "$") or -- anyIPv4, even if invalid
		mw.ustring.match(name, "^" .. IPV4_ADDRESS .. "-" .. IPV4_ADDRESS .. "$") or -- isLikeIPv4DashRange
		mw.ustring.match(name, "/") or
		name ~= lang:ucfirst(name) then
		return false
	end

	-- Ensure that the name can't be misresolved as a different title,
    -- such as with extra namespace keys at the start.
	local title = mw.title.new(name)
	if not title or
		title.namespace ~= 0 or
		name ~= title.text then
		return false
	end

	return true	
end

function p.getCanonical(frame)
	local name = p._getCanonical(frame.args[1])
	return name and name or ""
end

function p.isValid(frame)
	return p._isValid(frame.args[1]) and "1" or ""
end

return p