Jump to content

Module:Authors: Difference between revisions

From wikibase
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local p = {} -- Create a table to hold the module's functions
local p = {}


function p.list(frame) -- Define the 'list' function that will be invoked from wiki pages
function p.list(frame)
     -- Get the Wikibase entity for the current page (based on its sitelink)
     -- 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 8: Line 8:
     end
     end
      
      
     -- Access the claims (statements) on this entity
     -- Access claims for P2 ("has authors")
     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
     -- Loop through each author claim
     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
             -- Get label and sitelink for the author
             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
           
             -- 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
             if sitelink then
                 table.insert(authors, "[[" .. sitelink .. "|" .. label .. "]]")
                 table.insert(authors, "[[" .. sitelink .. "|" .. display .. "]]")
             else
             else
                -- If no sitelink, just show the label without a link
                 table.insert(authors, display)
                 table.insert(authors, label)
             end
             end
         end
         end
     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 as comma-separated list
     -- Instead of: return table.concat(authors, ", ")
     return table.concat(authors, ", ")
local result = ""
for _, author in ipairs(authors) do
    result = result .. "* " .. author .. "\n"
end
return result
end
end


return p -- Export the module table
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