Modulo:RandomCEO

Da WikiCeo.
Vai alla navigazione Vai alla ricerca

La documentazione per questo modulo può essere creata in Modulo:RandomCEO/man

local p = {}

function p.generateLinks(frame)
    local title = mw.title.getCurrentTitle()
    local category = 'CEO'
    local apiUrl = mw.site.getServer() .. mw.site.getPath() .. 'api.php'
    local parameters = {
        action = 'query',
        list = 'categorymembers',
        cmtitle = 'CEO:' .. category,
        cmnamespace = 0,
        cmlimit = 4,
        cmprop = 'title',
        format = 'json'
    }

    local success, result = pcall(mw.http.post, apiUrl, {
        body = mw.uri.encode(parameters),
        headers = { ['Content-Type'] = 'application/x-www-form-urlencoded' }
    })

    if not success or not result or result.statusCode ~= 200 then
        return ''
    end

    local data = mw.text.jsonDecode(result.body)
    if not data or not data.query or not data.query.categorymembers then
        return ''
    end

    local links = {}
    for _, member in ipairs(data.query.categorymembers) do
        local linkedTitle = mw.title.new(member.title)
        if linkedTitle and linkedTitle ~= title then
            local link = linkedTitle.fullUrl
            local text = linkedTitle.text
            table.insert(links, string.format('<li><a href="%s">%s</a></li>', link, text))
        end
    end

    if #links > 0 then
        return string.format('<ul>%s</ul>', table.concat(links))
    else
        return ''
    end
end

return p