Jump to content

Module:Authors: Difference between revisions

From wikibase
Created page with "local p = {} function p.list(frame) local entity = mw.wikibase.getEntity() if not entity then return "No Wikibase entity found for this page." end local claims = entity.claims or {} local p2Claims = claims.P2 or {} local authors = {} for _, claim in ipairs(p2Claims) do local value = claim.mainsnak.datavalue if value and value.type == "wikibase-entityid" then local itemId = value.value.id..."
Tag: Recreated
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:


function p.list(frame)
function p.list(frame)
    -- Get the Wikibase entity for the current page
     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 claims for P2 ("has authors")
     local claims = entity.claims or {}
     local claims = entity.claims or {}
     local p2Claims = claims.P2 or {}
     local p2Claims = claims.P2 or {}
      
      
     local authors = {}
     local authors = {}
    -- Loop through each author claim
     for _, claim in ipairs(p2Claims) do
     for _, claim in ipairs(p2Claims) do
         local value = claim.mainsnak.datavalue
         local value = claim.mainsnak.datavalue
         if value and value.type == "wikibase-entityid" then
         if value and value.type == "wikibase-entityid" then
             local itemId = value.value.id
             local itemId = value.value.id
            -- Get label and sitelink for the author
             local label = mw.wikibase.getLabel(itemId) or itemId
             local label = mw.wikibase.getLabel(itemId) or itemId
             table.insert(authors, "[[" .. label .. "]]")
             local sitelink = mw.wikibase.getSitelink(itemId)
           
            -- Get the author's entity to fetch P5
            local authorEntity = mw.wikibase.getEntity(itemId)
            local p5Value = ""
            if authorEntity and authorEntity.claims and authorEntity.claims.P5 then
                -- Assume P5 is a string property; take the first value
                local p5Claim = authorEntity.claims.P5[1]
                if p5Claim and p5Claim.mainsnak.datavalue then
                    p5Value = p5Claim.mainsnak.datavalue.value or ""
                end
            end
           
            -- Format: link to author, with P5 in parentheses if available
            local display = label
            if p5Value ~= "" then
                display = display .. " (" .. p5Value .. ")"
            end
            if sitelink then
                table.insert(authors, "[[" .. sitelink .. "|" .. display .. "]]")
            else
                table.insert(authors, display)
            end
         end
         end
     end
     end
Line 24: Line 50:
     end
     end
      
      
    -- Return as comma-separated list
     return table.concat(authors, ", ")
     return table.concat(authors, ", ")
end
end


return p
return p

Latest revision as of 13:46, 17 April 2026

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

local p = {}

function p.list(frame)
    -- Get the Wikibase entity for the current page
    local entity = mw.wikibase.getEntity()
    if not entity then
        return "No Wikibase entity found for this page."
    end
    
    -- Access claims for P2 ("has authors")
    local claims = entity.claims or {}
    local p2Claims = claims.P2 or {}
    
    local authors = {}
    -- Loop through each author claim
    for _, claim in ipairs(p2Claims) do
        local value = claim.mainsnak.datavalue
        if value and value.type == "wikibase-entityid" then
            local itemId = value.value.id
            -- Get label and sitelink for the author
            local label = mw.wikibase.getLabel(itemId) or itemId
            local sitelink = mw.wikibase.getSitelink(itemId)
            
            -- Get the author's entity to fetch P5
            local authorEntity = mw.wikibase.getEntity(itemId)
            local p5Value = ""
            if authorEntity and authorEntity.claims and authorEntity.claims.P5 then
                -- Assume P5 is a string property; take the first value
                local p5Claim = authorEntity.claims.P5[1]
                if p5Claim and p5Claim.mainsnak.datavalue then
                    p5Value = p5Claim.mainsnak.datavalue.value or ""
                end
            end
            
            -- Format: link to author, with P5 in parentheses if available
            local display = label
            if p5Value ~= "" then
                display = display .. " (" .. p5Value .. ")"
            end
            if sitelink then
                table.insert(authors, "[[" .. sitelink .. "|" .. display .. "]]")
            else
                table.insert(authors, display)
            end
        end
    end
    
    if #authors == 0 then
        return "No authors found."
    end
    
    -- Return as comma-separated list
    return table.concat(authors, ", ")
end

return p