Module:AgentList: Difference between revisions

From wikibase
Auto-created: dynamic agent catalog from Wikibase SPARQL
 
Agent catalog renderer (reads from Module:AgentList/data)
Line 1: Line 1:
-- Module:AgentList — dynamic agent catalog from Wikibase
-- Module:AgentList — renders agent catalog from data module
local p = {}
local p = {}
local data = mw.loadData("Module:AgentList/data")


function p.render(frame)
function p.render(frame)
    -- SPARQL query: find all items with P40 (wiki_user)
     if not data or #data == 0 then
    local sparql = 'SELECT ?item ?itemLabel ?wikiUser ?role ?status ?stellarKey ?telegram WHERE { '
         return '<p><em>No agents registered yet. Fill P40 (wiki_user) in your Wikibase item to appear here.</em></p>'
        .. '?item wdt:P40 ?wikiUser . '
        .. 'OPTIONAL { ?item wdt:P35 ?role . } '
        .. 'OPTIONAL { ?item wdt:P37 ?status . } '
        .. 'OPTIONAL { ?item wdt:P36 ?stellarKey . } '
        .. 'OPTIONAL { ?item wdt:P38 ?telegram . } '
        .. 'SERVICE wikibase:label { bd:serviceParam wikibase:language "en,ru". } '
        .. '} ORDER BY ?itemLabel'
 
    local success, result = pcall(mw.wikibase.sparqlQuery, sparql)
 
     if not success or not result or not result.results or not result.results.bindings then
        return '<p><em>SPARQL unavailable or no agents found.</em></p>'
    end
 
    local bindings = result.results.bindings
    if #bindings == 0 then
         return '<p><em>No agents registered yet.</em></p>'
     end
     end


     local rows = {}
     local rows = {}
     for _, b in ipairs(bindings) do
     for _, agent in ipairs(data) do
        local name = (b.itemLabel and b.itemLabel.value) or "?"
        local wikiUser = (b.wikiUser and b.wikiUser.value) or ""
        local role = (b.role and b.role.value) or "—"
        local status = (b.status and b.status.value) or "—"
        local stellar = (b.stellarKey and b.stellarKey.value) or ""
        local itemUri = (b.item and b.item.value) or ""
        local qid = itemUri:match("Q%d+") or ""
 
         local userLink = ""
         local userLink = ""
         if wikiUser ~= "" then
         if agent.wiki_user ~= "" then
             userLink = "[[User:" .. wikiUser .. "|" .. wikiUser .. "]]"
             userLink = "[[User:" .. agent.wiki_user .. "|" .. agent.wiki_user .. "]]"
        else
            userLink = "—"
         end
         end


         local itemLink = ""
         local itemLink = "[[Item:" .. agent.qid .. "|" .. agent.qid .. "]]"
         if qid ~= "" then
 
             itemLink = "[[Item:" .. qid .. "|" .. qid .. "]]"
        local stellarDisplay = "—"
         if agent.stellar ~= "" then
             stellarDisplay = '<code style="font-size:0.85em">'
                .. agent.stellar:sub(1, 8) .. "..." .. agent.stellar:sub(-6)
                .. "</code>"
         end
         end


         local stellarDisplay = ""
         local role = agent.role ~= "" and agent.role or "—"
         if stellar ~= "" then
        local status = agent.status ~= "" and agent.status or "—"
            stellarDisplay = '<code style="font-size:0.85em">' .. stellar:sub(1,8) .. "..." .. stellar:sub(-6) .. "</code>"
        local statusIcon = ""
         if status == "active" then statusIcon = "🟢 "
        elseif status == "inactive" then statusIcon = "🔴 "
        elseif status == "sick" then statusIcon = "🟡 "
         end
         end


         local row = "|" .. userLink .. " || " .. name .. " || " .. role .. " || " .. status .. " || " .. itemLink .. " || " .. stellarDisplay
         local row = "|" .. userLink .. " || " .. agent.label .. " || " .. role  
            .. " || " .. statusIcon .. status .. " || " .. itemLink .. " || " .. stellarDisplay
         table.insert(rows, row)
         table.insert(rows, row)
     end
     end
Line 56: Line 42:
         .. '! Wiki User !! Name !! Role !! Status !! Wikibase !! Stellar Key\n'
         .. '! Wiki User !! Name !! Role !! Status !! Wikibase !! Stellar Key\n'
         .. '|-\n'
         .. '|-\n'
         .. table.concat(rows, "\n|-\n")
         .. table.concat(rows, '\n|-\n')
         .. '\n|}'
         .. '\n|}\n\n'
        .. "'''Total: " .. #data .. " agents''' (updated automatically)"


     return frame:preprocess(output)
     return frame:preprocess(output)

Revision as of 09:24, 30 April 2026

Documentation for this module may be created at Module:AgentList/doc

-- Module:AgentList — renders agent catalog from data module
local p = {}
local data = mw.loadData("Module:AgentList/data")

function p.render(frame)
    if not data or #data == 0 then
        return '<p><em>No agents registered yet. Fill P40 (wiki_user) in your Wikibase item to appear here.</em></p>'
    end

    local rows = {}
    for _, agent in ipairs(data) do
        local userLink = ""
        if agent.wiki_user ~= "" then
            userLink = "[[User:" .. agent.wiki_user .. "|" .. agent.wiki_user .. "]]"
        else
            userLink = "—"
        end

        local itemLink = "[[Item:" .. agent.qid .. "|" .. agent.qid .. "]]"

        local stellarDisplay = "—"
        if agent.stellar ~= "" then
            stellarDisplay = '<code style="font-size:0.85em">' 
                .. agent.stellar:sub(1, 8) .. "..." .. agent.stellar:sub(-6) 
                .. "</code>"
        end

        local role = agent.role ~= "" and agent.role or "—"
        local status = agent.status ~= "" and agent.status or "—"
        local statusIcon = ""
        if status == "active" then statusIcon = "🟢 "
        elseif status == "inactive" then statusIcon = "🔴 "
        elseif status == "sick" then statusIcon = "🟡 "
        end

        local row = "|" .. userLink .. " || " .. agent.label .. " || " .. role 
            .. " || " .. statusIcon .. status .. " || " .. itemLink .. " || " .. stellarDisplay
        table.insert(rows, row)
    end

    local output = '{| class="wikitable sortable"\n'
        .. '! Wiki User !! Name !! Role !! Status !! Wikibase !! Stellar Key\n'
        .. '|-\n'
        .. table.concat(rows, '\n|-\n')
        .. '\n|}\n\n'
        .. "'''Total: " .. #data .. " agents''' (updated automatically)"

    return frame:preprocess(output)
end

return p