跳转到内容

模組:KillRSP

维基百科,自由的百科全书
local p = {}

function p.getKillData(frame)
    -- 这里的 key 是模块名,value 的第一项是类型,第二项是来源标识 (zh/en等)
    local sources = {
        ['Module:RSP/Data'] = { 'data', 'zh' },
        ['Module:RSP/Dataen'] = { 'data', 'en' },
        ['Module:RSP/Blacklist'] = { 'blacklist_mod', 'zh' },
        ['Module:RSP/Primary'] = { 'primary_mod', 'zh' }
    }

    local tag_map = {
        ["b"] = "blacklist",
        ["列入黑名單"] = "blacklist",
        ["d"] = "unreliable",
        ["gu"] = "unreliable",
        ["gunrel"] = "unreliable",
        ["通常不可靠"] = "unreliable",
        ["應停用"] = "unreliable"
    }

    local res = {
        blacklist = { absolute = {}, suspected = {} },
        unreliable = { absolute = {}, suspected = {} },
        primary = { absolute = {}, suspected = {} }
    }

    -- 记录已处理的条目,防止跨模块重复,同时作为去重表
    local seen_dom = { blacklist = {}, unreliable = {}, primary = {} }
    local seen_name = { blacklist = {}, unreliable = {}, primary = {} }

    for module_name, config in pairs(sources) do
        local mod_type = config[1]
        local lang_tag = config[2]
        
        -- 使用 require 获取数据,loadData 对大量数据的 RSP 模块更友好
        local success, data = pcall(mw.loadData, module_name)
        if success and type(data) == 'table' then
            for _, entry in pairs(data) do
                local target_tag = nil

                -- 判定逻辑:优先看模块定义的强制类型,再看 entry.statuses 映射
                if mod_type == 'primary_mod' then
                    target_tag = 'primary'
                elseif mod_type == 'blacklist_mod' then
                    target_tag = 'blacklist'
                elseif type(entry) == 'table' and entry.statuses then
                    target_tag = tag_map[entry.statuses]
                end

                if target_tag and res[target_tag] then
                    local domains = {}
                    if type(entry) == 'table' then
                        if type(entry.domains) == 'table' then
                            domains = entry.domains
                        elseif type(entry[1]) == 'string' and mod_type == 'primary_mod' then
                            -- 兼容旧版或特殊格式的 Primary 模块
                            domains = entry
                        end
                    end

                    -- 处理域名 (absolute)
                    for _, d in ipairs(domains) do
                        if type(d) == 'string' and d ~= "" and not seen_dom[target_tag][d] then
                            -- 注入对象,包含域名和来源标识
                            table.insert(res[target_tag].absolute, {
                                d = d,
                                src = lang_tag
                            })
                            seen_dom[target_tag][d] = true
                        end
                    end

                    -- 处理名称 (suspected)
                    local name = type(entry) == 'table' and entry.name or nil
                    if name and name ~= "" and not seen_name[target_tag][name] then
                        table.insert(res[target_tag].suspected, {
                            d = name,
                            src = lang_tag
                        })
                        seen_name[target_tag][name] = true
                    end
                end
            end
        end
    end

    return mw.text.jsonEncode(res)
end

return p