Fandom Developers Wiki
Documentation icon Module documentation

The documentation for this module is missing. Click here to create it.

-- <nowiki>
local p = {}

local getArgs = require("Module:Arguments").getArgs
local userError = require("Module:User error")
local i18n = require('Module:I18n').loadMessages('Enhancement_list')
local languages = require("Module:Languages")
local fallbacks = mw.loadData("Module:Fallbacklist")
local data = mw.loadData("Module:Infobox/data")
local fstr = mw.ustring.format
require("Module:No interwiki access")

function p.item(frame)
    local args = getArgs(frame)
    return p._item(args)
end

function p._item(args)
    for _, argName in ipairs{"name", "desc"} do
        if not args[argName] then
            return userError(
                "the <code>" .. argName .. "</code> parameter is required"
            )
        end
    end

    local links = {"[[" .. args.name .. "]]"}

    if args.langs then
        for lang in mw.text.gsplit(args.langs, "%s*,%s*") do
            table.insert(
                links,
                "([[" .. args.name .. "/" .. lang .. "|" .. lang .. "]])"
            )
        end
    end

    return ";" .. table.concat(links, " ") .. ":" .. args.desc
end

function p.list(frame)
    local args = getArgs(frame)
    return p._list(args)
end

-- test: =p._list{lang="JavaScript", type="management", scopes = "PS, P, S"}
function p._list(args)
    local lang = args.lang
    local typ = args.type
    local scopes = args.scopes or "PS"

    local personal = lang .. "/Personal use"
    local sitewide = lang .. "/Site-wide use"
    local subcat = data.categories[lang][typ]

    local currLang = mw.getCurrentFrame():callParserFunction('int:lang')
    local baseLang = mw.text.split(currLang, '-', true)[1]
    local useVariant = currLang ~= baseLang

    local output = {}

    for scope in mw.text.gsplit(scopes, "%s*,%s*") do
        local header = ""
        local filter = ""

        local notcategory = nil
        if scope == "PS" then
            header = i18n:msg('header-general')
            filter = "&" .. personal .. "&" .. sitewide
        elseif scope == "P" then
            header = i18n:msg('header-personal')
            filter = "&" .. personal
            notcategory = sitewide
        elseif scope == "S" then
            header = i18n:msg('header-sitewide')
            filter = "&" .. sitewide
            notcategory = personal
        end

        local dplArgs = {
            debug = "1",
            allowcachedresults = "1",
            category = subcat .. filter,
            notcategory = notcategory,
            include = 
                "{Infobox " .. lang .. "}:Description," ..
                "{Infobox " .. lang .. "/fng}:Description",
            resultsheader = "<h3>" .. header .. "</h3>",
            format = ",@@@@[[%TITLE%]],",
            includesubpages = "false",
            ordermethod = "title" -- make sure that non-variant language subpages are at the top
        } 
        local enResult = mw.getCurrentFrame():callParserFunction('#dpl:', dplArgs)

        local locRecords = {}
        dplArgs.includesubpages = "true"

        local function addLocRecords_direct(titlematch)
            -- localized result (direct infobox in source code)
            dplArgs.titlematch = titlematch
            dplArgs.include = 
                "{Infobox " .. lang .. "}:Description," ..
                "{Infobox " .. lang .. "/fng}:Description"
            dplArgs.format = ",@@@@[[%TITLE%]],"
            local locResult = mw.getCurrentFrame():callParserFunction('#dpl:', dplArgs)
            local locLines = mw.text.split(locResult, '@@@@', true)
            for i = 2, #locLines do
                local name, desc = mw.ustring.match(locLines[i], '^%[%[(.-)' .. '/' .. '.-%]%](.*)$')
                if locRecords[name] == nil and (name or "") ~= "" and (desc or "") ~= "" then
                    locRecords[name] = desc
                end
            end
        end

        local function addLocRecords_basepage(titlematch)
            -- localized result (BASEPAGENAME in source code)
            dplArgs.titlematch = titlematch
            dplArgs.include = "*"
            dplArgs.format = ",@@@@[[%TITLE%]]<nowiki>,</nowiki>,"
            
            local locResult = mw.getCurrentFrame():callParserFunction('#dpl:', dplArgs)
            local locLines = mw.text.split(locResult, '@@@@', true)
            for i = 2, #locLines do
                local name, desc = mw.ustring.match(locLines[i], '^%[%[(.-)' .. '/' .. '.-%]%](.*)$')
                if locRecords[name] == nil then
                    desc = mw.text.decode( desc, true )
                    for templateCall in mw.ustring.gmatch(desc, "%b{}") do
                        local m = mw.ustring.match(desc, "|%s*[Dd]esc%s*=%s*([^\n]+)\n[|}%s]")
                        if m == nil then
                            m = mw.ustring.match(desc, "|%s*[Dd]escription%s*=%s*([^\n]+)\n[|}%s]")
                        end
                        if m ~= nil then
                            locRecords[name] = m 
                        end
                    end
                end
            end
        end

        if currLang ~= "en" then
            -- Handle current language
            addLocRecords_direct(fstr("%%/%s", currLang))
            addLocRecords_basepage(fstr("%%/%s", currLang))
            -- Handle language fallback chain
            for i, v in ipairs(fallbacks[currLang] or {}) do
                addLocRecords_direct(fstr("%%/%s", v))
                addLocRecords_basepage(fstr("%%/%s", v))
            end
            -- Handle base language code
            addLocRecords_direct(fstr("%%/%s%%", baseLang))
            addLocRecords_basepage(fstr("%%/%s%%", baseLang))
        end

        -- English result
        local lines = mw.text.split(enResult, '@@@@', true)
        table.insert(output, lines[1])
        for i = 2, #lines do
            local name, desc = string.match(lines[i], '^%[%[(.-)%]%](.*)$')
            desc = locRecords[name] or desc -- localized desc or fall back
            if (name or "") ~= "" and (desc or "") ~= "" then
                local entry = p.item{
                    name = name,
                    langs = p.getSubpageLanguages(name),
                    desc = desc,
                }
                table.insert(output, entry)
            end
        end
        -- performance test
    end

    return mw.text.trim(table.concat(output, "\n"))
end

function p.dpl(frame)
    local args = getArgs(frame)
    return p.getSubpageLanguages(args[1])
end

function p.escape(str)
    return mw.ustring.gsub(str, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
end

function p.getSubpageLanguages(page)
    local oldLangs = languages.subpages(page)

    local langs = {}
    for i, lang in ipairs(oldLangs) do
        if #mw.language.fetchLanguageName(lang) > 0 then
            table.insert(langs, lang)
        end
    end

    if #langs == 0 then return nil end
    return table.concat(langs, ", ")
end

return p