Jump to content

Module:WDfetch: Difference between revisions

From Amateur Theatre Wiki
test
Tag: Reverted
test
Tag: Reverted
Line 1: Line 1:
local p = {}
local p = {}


-- Function to fetch the value of a specified Wikidata property from an entity.
-- Function to fetch and display the value of a specified Wikidata property
-- from an entity, formatting the output similarly to the infobox display.
-- Requires both a property ID (e.g. P12081) and a Q‑ID (e.g. Q2934).
-- Requires both a property ID (e.g. P12081) and a Q‑ID (e.g. Q2934).
function p.getvalue(frame)
function p.getvalue(frame)
Line 16: Line 17:
     local propertyId = args[1] or args.pid
     local propertyId = args[1] or args.pid
     if not propertyId then
     if not propertyId then
        mw.log("p.getvalue: No property ID provided.")
         return "Error: No property ID provided."
         return "Error: No property ID provided."
     end
     end
Line 22: Line 24:
     local qid = args.qid
     local qid = args.qid
     if not qid then
     if not qid then
        mw.log("p.getvalue: No Qid provided.")
         return "Error: No Qid provided."
         return "Error: No Qid provided."
     end
     end


    mw.log("p.getvalue: Fetching entity for qid " .. qid)
     -- Retrieve the full entity object using the Lua interface.
     -- Retrieve the full entity object using the Lua interface.
     local entity = mw.ext.UnlinkedWikibase.getEntity(qid)
     local entity = mw.ext.UnlinkedWikibase.getEntity(qid)
     if not entity then
     if not entity then
        mw.log("p.getvalue: Entity not found for qid " .. qid)
         return "Error: Entity not found for Qid " .. qid
         return "Error: Entity not found for Qid " .. qid
     end
     end
Line 35: Line 40:
     local propertyClaims = claims[propertyId]
     local propertyClaims = claims[propertyId]
     if not propertyClaims or #propertyClaims == 0 then
     if not propertyClaims or #propertyClaims == 0 then
        mw.log("p.getvalue: No claims found for property " .. propertyId)
         return nil
         return nil
     end
     end
Line 41: Line 47:
     local claim = propertyClaims[1]
     local claim = propertyClaims[1]
     if not (claim and claim.mainsnak and claim.mainsnak.datavalue) then
     if not (claim and claim.mainsnak and claim.mainsnak.datavalue) then
        mw.log("p.getvalue: Claim structure invalid for property " .. propertyId)
         return nil
         return nil
     end
     end
    local datavalue = claim.mainsnak.datavalue
    local value = datavalue.value


     -- If the value is a table, handle common cases.
     local datatype = claim.mainsnak.datatype
     if type(value) == "table" then
     mw.log("p.getvalue: Processing claim for property " .. propertyId .. " with datatype " .. datatype)
         -- For linked entities, return the entity id.
    local value
         if value.id then
 
            value = value.id
    if datatype == "wikibase-item" then
         -- For monolingual text, return the text field.
         value = '{{wdl|' .. claim.mainsnak.datavalue.value.id .. '}}'
         elseif value.text then
    elseif datatype == "monolingualtext" then
            value = value.text
         value = claim.mainsnak.datavalue.value.text
         else
    elseif datatype == "commonsMedia" then
            value = tostring(value)
        value = '[[File:' .. claim.mainsnak.datavalue.value .. '|300px]]'
         end
    elseif datatype == "time" then
         -- Format the time value using the English date format.
         local timeValue = claim.mainsnak.datavalue.value.time
        local lang = mw.language.getContentLanguage()
         value = lang:formatDate('j F Y', timeValue)
    else
         value = '<small><em>Type ' .. datatype .. ' not handled by Module:Infobox yet.</em></small>'
     end
     end


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


    mw.log("p.getvalue: Returning value: " .. tostring(value))
     return value
     return value
end
end


return p
return p

Revision as of 00:50, 2 March 2025

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

local p = {}

-- Function to fetch and display the value of a specified Wikidata property
-- from an entity, formatting the output similarly to the infobox display.
-- Requires both a property ID (e.g. P12081) and a Q‑ID (e.g. Q2934).
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.
    local propertyId = args[1] or args.pid
    if not propertyId then
        mw.log("p.getvalue: No property ID provided.")
        return "Error: No property ID provided."
    end

    -- Get the Q-ID.
    local qid = args.qid
    if not qid then
        mw.log("p.getvalue: No Qid provided.")
        return "Error: No Qid provided."
    end

    mw.log("p.getvalue: Fetching entity for qid " .. qid)
    -- Retrieve the full entity object using the Lua interface.
    local entity = mw.ext.UnlinkedWikibase.getEntity(qid)
    if not entity then
        mw.log("p.getvalue: Entity not found for qid " .. qid)
        return "Error: Entity not found for Qid " .. qid
    end

    -- Extract claims from the entity.
    local claims = entity.claims or {}
    local propertyClaims = claims[propertyId]
    if not propertyClaims or #propertyClaims == 0 then
        mw.log("p.getvalue: No claims found for property " .. propertyId)
        return nil
    end

    -- Extract the first claim's value.
    local claim = propertyClaims[1]
    if not (claim and claim.mainsnak and claim.mainsnak.datavalue) then
        mw.log("p.getvalue: Claim structure invalid for property " .. propertyId)
        return nil
    end

    local datatype = claim.mainsnak.datatype
    mw.log("p.getvalue: Processing claim for property " .. propertyId .. " with datatype " .. datatype)
    local value

    if datatype == "wikibase-item" then
        value = '{{wdl|' .. claim.mainsnak.datavalue.value.id .. '}}'
    elseif datatype == "monolingualtext" then
        value = claim.mainsnak.datavalue.value.text
    elseif datatype == "commonsMedia" then
        value = '[[File:' .. claim.mainsnak.datavalue.value .. '|300px]]'
    elseif datatype == "time" then
        -- Format the time value using the English date format.
        local timeValue = claim.mainsnak.datavalue.value.time
        local lang = mw.language.getContentLanguage()
        value = lang:formatDate('j F Y', timeValue)
    else
        value = '<small><em>Type ' .. datatype .. ' not handled by Module:Infobox yet.</em></small>'
    end

    -- Capitalize the first letter if the result is a string.
    if type(value) == "string" then
        value = value:gsub("^%l", string.upper)
    end

    mw.log("p.getvalue: Returning value: " .. tostring(value))
    return value
end

return p