Модуль:Wikidata

Матеріал з Вікіджерел

Документацію для цього модуля можна створити у Модуль:Wikidata/документація

local getArguments = require('Модуль:Arguments').getArgs
local p = {}

-- private functions
local getArgs = function(input)
    if type(input) ~= 'table' then
        input = {input}
    end
    return getArguments(input)
end

local getClaims = function(propertyId)
    local entity = mw.wikibase.getEntityObject()
    if entity == nil then
        return nil
    end

    local claims = entity.claims
    if claims then
        return claims[propertyId]
    end

    return nil
end

local getDescription = function()
    local data = mw.wikibase.getEntityObject()
    if data == nil then
        return nil
    end

    data = data.descriptions
    if data == nil then
        return nil
    end

    data = data['uk']
    if data == nil then
        return nil
    end

    return data.value
end

local getSitelink = function(sitelinkId)
    local entity = mw.wikibase.getEntityObject()
    if entity == nil then
        return nil
    end

    local sitelink = entity:getSitelink(sitelinkId)
    return sitelink
end

local getLinkedItemLabel = function(propertyId, retrieveAll)
    local claims = getClaims(propertyId)
    if claims == nil then
        return nil
    end

    local out = {}
    for k, v in pairs(claims) do
        local entityName = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
        local sitelink = mw.wikibase.sitelink(entityName)
        if sitelink then
            out[#out + 1] = "[[" .. sitelink .. "]]"
        else
            out[#out + 1] = "[[:d:" .. entityName .. "|"..entityName .. "]]<small><abbr title='Сторінка недоступна на цьому проекті'>[?]</abbr></small>"
        end

        if not retrieveAll then
            break
        end
    end
    return table.concat(out, ", ")
end

local getItemLabel = function(propertyId, retrieveAll)
    local claims = getClaims(propertyId)
    if claims == nil then
        return nil
    end

    local out = {}
    local entityName, d_value
    for k, v in pairs(claims) do
    	d_value = v.mainsnak.datavalue
    	if d_value ~= nil then
    		entityName = "Q" .. d_value.value["numeric-id"]
    		out[#out + 1] = mw.wikibase.label(entityName)
    	end

        if not retrieveAll then
            break
        end
    end
    return table.concat(out, ", ")
end

local getRawValue = function(propertyId, retrieveAll)
    local claims = getClaims(propertyId)
    if claims == nil then
        return nil
    end

    local out = {}
    for k, v in pairs(claims) do
        local value = v.mainsnak.datavalue.value
        out[#out + 1] = value

        if not retrieveAll then
            break
        end
    end
    return table.concat(out, ", ")
end

local getDateValue = function(propertyId, dateFormat, retrieveAll)
    local claims = getClaims(propertyId)
    if claims == nil then
        return nil
    end

    local out = {}
    local dt = {}
    for k, v in pairs(claims) do
        if v.mainsnak.snaktype == 'somevalue' then
               out[#out + 1] = '?'
            else
                local value = v.mainsnak.datavalue.value
                local d = value.time
                d = d:gsub("%-00%-00T", "-01-01T")
                dt.sign = string.sub(d, 1, 1)
                dt.year = string.sub(d, 2, 5)
                dt.month = string.sub(d, 7, 8)
                dt.day = string.sub(d, 10, 11)
                if dateFormat == "y" then
                    local s = os.date("%Y", os.time(dt))
                    if value.precision == 8 then
                        s = s..'-ті'
                    end
                    out[#out + 1] = s
                elseif dateFormat == "ye" then
                    local s = os.date("%Y", os.time(dt))
                    if value.precision == 8 then
                        s = s..'-ті'
                    end
                    if dt.sign == "-" then
                    	s = s..' до н. е.'
                	end
                	out[#out + 1] = s
                elseif dateFormat == "dmy" then
                    out[#out + 1] = os.date("%e %B %Y", os.time(dt))
                elseif dateFormat == "dmye" then
                    local s = os.date("%e %B %Y", os.time(dt))
                    if dt.sign == "-" then
                    	s = s..' до н. е.'
                	end
                	out[#out + 1] = s
                elseif dateFormat == "e" then
                    if dt.sign == "-" then
                    	s = s..'до н. е.'
                	end
                	out[#out + 1] = s
                else
                    out[#out + 1] = os.date("%B %e, %Y", os.time(dt))
                end
        end

        if not retrieveAll then
            break
        end
    end
    return table.concat(out, ", ")
end

-- public functions
p.getLinkedItemLabel = function(frame)
    local args = getArgs(frame)

    local propertyId = args[1] or ""

    return getLinkedItemLabel(propertyId, false)
end

p.getLinkedItemLabels = function(frame)
    local args = getArgs(frame)

    local propertyId = args[1] or ""

    return getLinkedItemLabel(propertyId, true)
end

p.getItemLabel = function(frame)
    local args = getArgs(frame)

    local propertyId = args[1] or ""

    return getItemLabel(propertyId, false)
end

p.getItemLabels = function(frame)
    local args = getArgs(frame)

    local propertyId = args[1] or ""

    return getItemLabel(propertyId, true)
end

p.getRawValue = function(frame)
    local args = getArgs(frame)

    local propertyId = args[1] or ""

    return getRawValue(propertyId, false)
end

p.getRawValues = function(frame)
    local args = getArgs(frame)

    local propertyId = args[1] or ""

    return getRawValue(propertyId, true)
end

p.getDateValue = function(frame)
    local args = getArgs(frame)

    local propertyId = args[1] or ""
    local dateFormat = args[2] or ""

    return getDateValue(propertyId, dateFormat, false)
end

p.getDateValues = function(frame)
    local args = getArgs(frame)

    local propertyId = args[1] or ""
    local dateFormat = args[2] or ""

    return getDateValue(propertyId, dateFormat, true)
end

p.getSitelink = function(frame)
    local args = getArgs(frame)

    local sitelinkId = args[1] or ""

    local sitelink = getSitelink(sitelinkId)
    return sitelink
end

p.getId = function(frame)
    local entity = mw.wikibase.getEntityObject()
    if entity == nil then
        return nil
    end

    return entity.id
end

p.getDescription = function(frame)
    return getDescription()
end

return p