Module:Sandbox/Ahecht/getCanonicalUsername
Appearance
< Module:Sandbox | Ahecht
-- 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)
return p._getCanonical(frame.args[1]) and frame.args[1] or ""
end
function p.isValid(frame)
return p._isValid(frame.args[1]) and frame.args[1] or ""
end
return p