Jump to content

Module:Authors: Difference between revisions

From wikibase
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {} -- Create a table to hold the module's functions


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


return p
return p -- Export the module table

Revision as of 11:47, 17 April 2026

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