Jump to content

Module:WDfetch: Difference between revisions

From Amateur Theatre Wiki
test
Tag: Reverted
Undo revision 1449 by PCAdmin (talk)
Tag: Undo
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- This Lua module is designed for use in MediaWiki templates,
-- particularly for MDwiki infoboxes, to retrieve and display clean values
-- from Wikidata via the UnlinkedWikibase extension.
--
-- The module fetches the value of a specified Wikidata property (P-ID)
-- using the {{#statements:}} parser function provided by the extension.
--
-- Example usage in a template:
-- {{#invoke:WDfetch|getvalue|pid=P12081}}
-- This retrieves and returns the value of the Wikidata property P12081
-- for the current page's associated Wikibase item.
local p = {}
local p = {}


-- Function to fetch the value of a specified Wikidata property from an entity.
-- Function to fetch the value of a specified Wikidata property.
-- Requires both a property ID (e.g. P12081) and a Q‑ID (e.g. Q2934).
function p.getvalue(frame)
function p.getvalue(frame)
     -- Collect arguments from the calling frame.
     -- Collect arguments from the calling frame
     local args = {}
     local args = {}
     for k, v in pairs(frame:getParent().args) do
     for k, v in pairs(frame:getParent().args) do
Line 13: Line 24:
     end
     end


     -- Get the property ID.
     -- Get the property ID from the arguments
     local propertyId = args[1] or args.pid
     local propertyId = args[1] or args.pid
     if not propertyId then
     if not propertyId then
Line 19: Line 30:
     end
     end


     -- Get the Q-ID.
     -- Use the #statements parser function to fetch the value
     local qid = args.qid
     local out = frame:callParserFunction("#statements", propertyId)
    if not qid then
        return "Error: No Qid provided."
    end


     -- Retrieve the full entity object using the Lua interface.
     -- If no result is found, return nil
     local entity = mw.ext.UnlinkedWikibase.getEntity(qid)
     if string.sub(out, 1, 4) ~= "<!--" then
    if not entity then
         -- Strip HTML markup: Remove <span> tags used by the extension
        return "Error: Entity not found for Qid " .. qid
        local p1 = string.find(out, ">", 1, true) or 0
    end
         local p2 = string.find(out, "<", -1, true) or #out + 1
 
         out = string.sub(out, p1 + 1, p2 - 1)
    -- Extract claims from the entity.
    local claims = entity.claims or {}
    local propertyClaims = claims[propertyId]
    if not propertyClaims or #propertyClaims == 0 then
         return nil
    end
 
    -- Extract the first claim's value.
    local claim = propertyClaims[1]
    if not (claim and claim.mainsnak and claim.mainsnak.datavalue) then
         return nil
    end
    local datavalue = claim.mainsnak.datavalue
    local value = datavalue.value
 
    -- If the value is a table, handle common cases.
    if type(value) == "table" then
        -- For linked entities, return the entity id.
        if value.id then
            value = value.id
        -- For monolingual text, return the text field.
         elseif value.text then
            value = value.text
        else
            value = tostring(value)
        end
    end


    -- Capitalize the first letter of the result if it's a string.
        -- Capitalize the first letter of the result
    if type(value) == "string" then
         out = out:gsub("^%l", string.upper)
         value = value:gsub("^%l", string.upper)
    else
        out = nil
     end
     end


     return value
     return out
end
end


return p
return p

Latest revision as of 12:31, 2 March 2025

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

-- This Lua module is designed for use in MediaWiki templates, 
-- particularly for MDwiki infoboxes, to retrieve and display clean values 
-- from Wikidata via the UnlinkedWikibase extension.
--
-- The module fetches the value of a specified Wikidata property (P-ID) 
-- using the {{#statements:}} parser function provided by the extension.
--
-- Example usage in a template:
-- {{#invoke:WDfetch|getvalue|pid=P12081}}
-- This retrieves and returns the value of the Wikidata property P12081 
-- for the current page's associated Wikibase item.

local p = {}

-- Function to fetch the value of a specified Wikidata property.
function p.getvalue(frame)
    -- Collect arguments from the calling frame
    local args = {}
    for k, v in pairs(frame:getParent().args) do
        if v ~= "" then args[k] = v end
    end
    for k, v in pairs(frame.args) do
        if v ~= "" then args[k] = v end
    end

    -- Get the property ID from the arguments
    local propertyId = args[1] or args.pid
    if not propertyId then
        return "Error: No property ID provided."
    end

    -- Use the #statements parser function to fetch the value
    local out = frame:callParserFunction("#statements", propertyId)

    -- If no result is found, return nil
    if string.sub(out, 1, 4) ~= "<!--" then
        -- Strip HTML markup: Remove <span> tags used by the extension
        local p1 = string.find(out, ">", 1, true) or 0
        local p2 = string.find(out, "<", -1, true) or #out + 1
        out = string.sub(out, p1 + 1, p2 - 1)

        -- Capitalize the first letter of the result
        out = out:gsub("^%l", string.upper)
    else
        out = nil
    end

    return out
end

return p