Jump to content

Module:GetMembers: Difference between revisions

From Amateur Theatre Wiki
Test module
 
add limit
 
Line 18: Line 18:
         }
         }
         ORDER BY ?memberLabel
         ORDER BY ?memberLabel
        LIMIT 1
     ]], qid)
     ]], qid)



Latest revision as of 00:15, 23 January 2025

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

local p = {}

-- Function to query and return a table of members of a Dachverband
function p.getMembers(frame)
    -- Get the QID from the template arguments
    local qid = frame.args.qid or ""
    if qid == "" then
        return "Error: No QID provided."
    end

    -- Define the SPARQL query to retrieve the members
    local sparql = string.format([[
        SELECT ?member ?memberLabel ?headquarters ?foundingDate WHERE {
            ?member wdt:P463 wd:%s .  # Members of the Dachverband
            OPTIONAL { ?member wdt:P159 ?headquarters . }
            OPTIONAL { ?member wdt:P571 ?foundingDate . }
            SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
        }
        ORDER BY ?memberLabel
        LIMIT 1
    ]], qid)

    -- Run the SPARQL query
    local results = mw.ext.UnlinkedWikibase.query(sparql)

    -- Check if results are empty
    if not results or #results == 0 then
        return "No members found for the provided QID."
    end

    -- Build the HTML table
    local html = mw.html.create("table")
        :addClass("wikitable")
        :css("width", "100%")

    -- Add table headers
    html:tag("tr")
        :tag("th"):wikitext("Name"):done()
        :tag("th"):wikitext("Headquarters"):done()
        :tag("th"):wikitext("Founding Date"):done()
        :done()

    -- Populate the table with data
    for _, row in ipairs(results) do
        local name = row.memberLabel or "N/A"
        local headquarters = row.headquarters and row.headquarters.value or "N/A"
        local foundingDate = row.foundingDate and row.foundingDate.value or "N/A"

        html:tag("tr")
            :tag("td"):wikitext(name):done()
            :tag("td"):wikitext(headquarters):done()
            :tag("td"):wikitext(foundingDate):done()
            :done()
    end

    -- Return the HTML table as wikitext
    return tostring(html)
end

return p