モジュール:UserAN/sandbox

local modip = require('Module:IP/sandbox')
---Enclose a string by plainlinks
---@param str string
---@return string
local function plainlinks(str)
return  .. str .. 
end
--Alternative to ternary operator
local function ternary(cond, T, F)
if cond then return T else return F end
end
---OR evaluator
---@param comparator any
---@param ... unknown
---@return boolean
local function anyOf(comparator, ...)
for _, v in ipairs(arg) do
if comparator == v then return true end
end
return false
end
---Get links to show for a reportee in accordance with the UserAN type
---@param reportee string
---@param lcType string
---@param ipType "cidr"|"ip"|"none"
---@return string
local function getTypedLinks(reportee, lcType, ipType)
local usernolink = '利用者:' .. reportee
local userlink =  .. usernolink .. 
local reporteeUri = string.gsub(reportee, ' ', '_')
	local talk = '会話'
	local contribs = '投稿記録'
	local log = '//ja.wikipedia.org/w/index.php?title=特別:Log&page=User:' .. reporteeUri .. ' 記録'
local filterLog = '//ja.wikipedia.org/w/index.php?title=特別:AbuseLog&wpSearchUser=' .. reporteeUri .. ' フィルター記録'
	local ca = 'CA'
local guc = '//xtools.wmflabs.org/globalcontribs/ipr-' .. reporteeUri .. ' GUC'
local spur = '//spur.us/context/' .. reporteeUri .. ' SPUR'
	local block = 'ブロック'
local logid = 'Logid/' .. reporteeUri .. 
	local diff = '差分/' .. reporteeUri .. 'の投稿者'
local defaultLinks = {
user2 = userlink .. plainlinks('（' .. talk .. ' / ' .. contribs .. ' / ' .. log .. ' / ' .. filterLog .. ' / ' .. ca .. ' / ' .. block .. '）'),
unl = usernolink .. plainlinks('（' .. talk .. ' / ' .. contribs .. ' / ' .. log .. ' / ' .. filterLog .. ' / ' .. ca .. ' / ' .. block .. '）'),
ip2 = 'IP:' .. reportee .. plainlinks('（' .. talk .. ' / ' .. contribs .. ' / ' .. log .. ' / ' .. filterLog .. ' / ' .. guc .. ' / ' .. spur .. ' / ' .. block .. '）'),
ip2cidr = 'IP:' .. reportee .. plainlinks('（' .. talk .. ' / ' .. contribs .. ' / ' .. log .. ' / ' .. guc .. ' / ' .. spur .. ' / ' .. block .. '）'),
logid = logid,
diff = diff,
none = reportee
}
local links
if anyOf(lcType, 'usernolink', 'unl') then
links = defaultLinks.unl
elseif anyOf(lcType, 'ipuser2', 'ip2') then
links = ternary(ipType == 'cidr', defaultLinks.ip2cidr, defaultLinks.ip2)
elseif anyOf(lcType, 'log', 'logid') then
links = defaultLinks.logid
elseif anyOf(lcType, 'dif', 'diff') then
links = defaultLinks.diff
elseif lcType == 'none' then
links = defaultLinks.none
else
links = defaultLinks.user2
end
return links
end
local errCnt = 0
---Error handling function
---@param errorType "nousername"|"nonipexpected"|"ipexpected"|"invalidcidr"|"numberexpected"|"invalidtype"
---@param typeParam string
---@param invalidSubnet string?
---@return string
local function errHandler(errorType, typeParam, invalidSubnet)
errCnt = errCnt + 1
local err = {
nousername = '第一引数は必須です',
nonipexpected = 'type=' .. typeParam .. 'に対しIPアドレスが指定されています',
ipexpected = 'type=' .. typeParam .. 'には有効なIPアドレスを指定してください',
invalidcidr = ternary(invalidSubnet, invalidSubnet, ) .. 'は無効なIPサブネットです',
numberexpected = 'type=' .. typeParam .. 'には数字を指定してください',
invalidtype = 'type=' .. typeParam .. 'は存在しません'
}
local msg = ternary(errCnt == 1, 'UserANエラー: ', '; ') .. errerrorType
msg =  .. msg .. 
if errCnt == 1 and mw.getCurrentFrame():getParent():getTitle() == 'Template:UserAN' then -- Add category only when called from this template
msg = msg .. 
end
return msg
end
-- Icons to show leading the username
local defaultIcons = {
done = '済',
done2 = '済 済',
doing = '未了',
rejected = '却下 却下',
withdrawn = '取り下げ 取り下げ',
deferred = '見送り 見送り'
}
-- Main package function
local p = {}
function p.Main(frame)
local args = frame.args
local u = modip.Util.removeDirMarkers(args.username)
local t = modip.Util.removeDirMarkers(args.type)
t = ternary(t == , 'User2', t)
-- Evaluate the username parameter
if u ==  then
return errHandler('nousername', )
else
u = u:gsub('_', ' ')
end
-- Evaluate IP
local isIp = modip.Util.isIPAddress(u)
local isCidr, _, corrected = modip.Util.isIPAddress(u, true, true)
local isIPAddress = isIp or isCidr
u = ternary(isIPAddress, u:upper(), u) -- Capitalize u if it's an IP
if corrected ~= nil then corrected = corrected:upper() end -- Same as above
-- Evaluate the type parameter and its compatibility with the username parameter
local lcType = string.lower(t)
local function generateIpErr()
return errHandler('nonipexpected', t) .. ternary(corrected ~= nil, errHandler('invalidcidr', t, u), )
end
local errorMsg = 
if anyOf(lcType, 'usernolink', 'unl', 'user2', 'none') then
if isIPAddress then
errorMsg = generateIpErr()
lcType = 'ip2'
end
elseif anyOf(lcType, 'ipuser2', 'ip2') then
if not isIPAddress then
errorMsg = errHandler('ipexpected', t)
lcType = 'unl'
elseif corrected ~= nil then -- Cidr is provided but modified because it's invalid
errorMsg = errHandler('invalidcidr', t, u)
end
elseif anyOf(lcType, 'log', 'logid', 'dif', 'diff') then
if not u:find('^%d+$') then -- if not a number
errorMsg = errHandler('numberexpected', t)
lcType = 'unl'
if isIPAddress then
errorMsg = errorMsg .. generateIpErr()
lcType = 'ip2'
end
end
else
errorMsg = errHandler('invalidtype', t)
lcType = 'unl'
if isIPAddress then
errorMsg = errorMsg .. generateIpErr()
lcType = 'ip2'
end
end
errorMsg = ternary(errorMsg ~= ,  .. errorMsg .. , errorMsg)
-- Get links to show
local ipType = ternary(isCidr, 'cidr', ternary(isIp, 'ip', 'none'))
local links = getTypedLinks(ternary(corrected ~= nil, corrected, u), lcType, ipType)
-- Add icon
local icon
	local autostatus = args.autostatus -- 2=
local lcAutostatus = string.lower(autostatus)
	local manualstatus = args.manualstatus -- 状態=
if lcAutostatus ==  then
icon = ternary(manualstatus == , defaultIcons.doing, defaultIcons.done2)
elseif anyOf(lcAutostatus, 'done', '済', '済み') then
icon = defaultIcons.done
elseif anyOf(lcAutostatus, 'not done', '却下', '非対処') then
icon = defaultIcons.rejected
elseif lcAutostatus == '取り下げ' then
icon = defaultIcons.withdrawn
elseif lcAutostatus == '見送り' then
icon = defaultIcons.deferred
else
icon = defaultIcons.done .. ' ' .. autostatus .. 
end
-- Return the string to display
	return icon .. ' ' .. links .. errorMsg
end
return p