Jump to content

Module:Authors

From wikibase
Revision as of 11:47, 17 April 2026 by Design (talk | contribs)

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

local p = {}  -- Create a table to hold the module's functions

function p.list(frame)  -- Define the 'list' function that will be invoked from wiki pages
    -- Get the Wikibase entity for the current page (based on its sitelink)
    local entity = mw.wikibase.getEntity()
    if not entity then
        return "No Wikibase entity found for this page."
    end
    
    -- Access the claims (statements) on this entity
    local claims = entity.claims or {}
    -- Get all claims for property P2 ("has authors")
    local p2Claims = claims.P2 or {}
    
    -- Prepare a table to store the formatted author links
    local authors = {}
    -- Loop through each claim for P2
    for _, claim in ipairs(p2Claims) do
        -- Extract the main value from the claim
        local value = claim.mainsnak.datavalue
        -- Check if the value is a Wikibase item (entity ID)
        if value and value.type == "wikibase-entityid" then
            -- Get the item ID (e.g., "Q5")
            local itemId = value.value.id
            -- Get the label (display name) for the item
            local label = mw.wikibase.getLabel(itemId) or itemId
            -- Get the sitelink (the actual wiki page title linked to this item)
            local sitelink = mw.wikibase.getSitelink(itemId)
            -- If a sitelink exists, create a wiki link with label as display text
            if sitelink then
                table.insert(authors, "[[" .. sitelink .. "|" .. label .. "]]")
            else
                -- If no sitelink, just show the label without a link
                table.insert(authors, label)
            end
        end
    end
    
    -- If no authors were found, return a message
    if #authors == 0 then
        return "No authors found."
    end
    
    -- Join the authors with commas and return the result
    return table.concat(authors, ", ")
end

return p  -- Export the module table