コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

モジュール:UserAN/sandbox

モジュールの解説[表示] [編集] [履歴] [キャッシュを破棄]

Template:UserAN用のモジュール。

使い方

[編集]

書式

[編集]
{{#invoke:UserAN|Main}}
local modip = require('Module:IP/sandbox')

---Enclose a string by plainlinks
---@param str string
---@return string
local function plainlinks(str)
    return '<span class="plainlinks" style="font-size:smaller;">' .. str .. '</span>'
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 = '[[User talk:' .. reporteeUri .. '|会話]]'
	local contribs = '[[特別:投稿記録/' .. reporteeUri .. '|投稿記録]]'
	local log = '[//ja-two.iwiki.icu/w/index.php?title=特別:Log&page=User:' .. reporteeUri .. ' 記録]'
    local filterLog = '[//ja-two.iwiki.icu/w/index.php?title=特別:AbuseLog&wpSearchUser=' .. reporteeUri .. ' フィルター記録]'
	local ca = '[[特別:CentralAuth/' .. reporteeUri .. '|CA]]'
    local guc = '[//xtools.wmflabs.org/globalcontribs/ipr-' .. reporteeUri .. ' GUC]'
    local spur = '[//spur.us/context/' .. reporteeUri .. ' SPUR]'
	local block = '[[特別:block/' .. reporteeUri .. '|ブロック]]'
    local logid = '[[特別:転送/logid/' .. reporteeUri .. '|Logid/' .. reporteeUri .. ']]'
	local diff = '[[特別:差分/' .. reporteeUri .. '|差分/' .. 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, '[[Template:UserAN|UserAN]]エラー: ', '; ') .. err[errorType]
    msg = '<span style="color: red !important; font-weight: bold;">' .. msg .. '</span>'
    if errCnt == 1 and mw.getCurrentFrame():getParent():getTitle() == 'Template:UserAN' then -- Add category only when called from this template
        msg = msg .. '[[Category:テンプレート呼び出しエラーのあるページ/Template:UserAN]]'
    end
    return msg
end

-- Icons to show leading the username
local defaultIcons = {
    done = '[[File:Yes check.svg|20px|<span class="done">済</span>]]',
    done2 = '[[File:Yes check.svg|20px|<span class="done">済</span>]] <small><b>済</b></small>',
    doing = '[[File:Stock post message.svg|22px|<span class="doing">未了</span>]]',
    rejected = '[[File:X mark.svg|20px|<span class="done">却下</span>]] <small><b>却下</b></small>',
    withdrawn = '[[File:X mark.svg|20px|<span class="done">取り下げ</span>]] <small><b>取り下げ</b></small>',
    deferred = '[[File:X mark.svg|20px|<span class="done">見送り</span>]] <small><b>見送り</b></small>'
}

-- 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 ~= '', '<small>' .. errorMsg .. '</small>', 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 .. ' <small><b>' .. autostatus .. '</b></small>'
    end

    -- Return the string to display
	return icon .. ' ' .. links .. errorMsg

end

return p