Jump to content

Module:Blue plaque

From Wikipedia, the free encyclopedia

local p = {}

local function firstValue(entity, property)
    if not entity or not entity.claims then
        return nil
    end

    local statements = entity.claims[property]
    if not statements then
        return nil
    end

    for _, statement in ipairs(statements) do
        if statement.mainsnak
            and statement.mainsnak.snaktype == 'value'
            and statement.mainsnak.datavalue then

            return statement.mainsnak.datavalue.value
        end
    end

    return nil
end

local function itemLabel(value)
    if not value or not value.id then
        return nil
    end
    return mw.wikibase.getLabel(value.id)
end

local function formatDate(value)
    if not value or not value.time then
        return nil
    end

    local year, month, day =
        value.time:match('^[+-](%d+)%-(%d%d)%-(%d%d)')

    if not year then
        return nil
    end

    year = tonumber(year)
    month = tonumber(month)
    day = tonumber(day)

    if value.precision and value.precision >= 11 then
        return string.format('%d %s %d',
            day,
            ({
                'January', 'February', 'March', 'April',
                'May', 'June', 'July', 'August',
                'September', 'October', 'November', 'December'
            })[month],
            year
        )
    elseif value.precision and value.precision >= 10 then
        local months = {
            'January', 'February', 'March', 'April',
            'May', 'June', 'July', 'August',
            'September', 'October', 'November', 'December'
        }
        return months[month] .. ' ' .. year
    end

    return tostring(year)
end

function p.main(frame)
    local args = frame:getParent().args
    local qid = args.qid
    local entity

    if qid and qid ~= '' then
        if mw.wikibase.isValidEntityId(qid) then
            entity = mw.wikibase.getEntity(qid)
        end
    else
        entity = mw.wikibase.getEntity()
    end

    local name = args.name
    local description = args.inscription
    local dates = args.date
    local location = args.location
    local image = args.image

    if entity then
        name = name or mw.wikibase.getLabel(entity.id)

 -- Prefer the actual inscription from Wikidata (P1684).
-- P1684 is monolingual text, stored as a table with a .text field.
local inscriptionValue = firstValue(entity, 'P1684')

if type(inscriptionValue) == 'table' then
    description = description or inscriptionValue.text
elseif type(inscriptionValue) == 'string' then
    description = description or inscriptionValue
end

-- Fall back to the Wikidata description if no inscription exists.
description = description
    or mw.wikibase.getDescription(entity.id)
        local birth = formatDate(firstValue(entity, 'P569'))
        local death = formatDate(firstValue(entity, 'P570'))

        if not dates and (birth or death) then
            dates = (birth or '?') .. ' – ' .. (death or '')
        end

        local imageValue = firstValue(entity, 'P18')
        if not image and type(imageValue) == 'string' then
            image = imageValue
        end

        local birthplace = itemLabel(firstValue(entity, 'P19'))
        location = location or birthplace
    end

    name = name or mw.title.getCurrentTitle().text
    description = description or 'Commemorative inscription'
    local authority = args.authority or 'BLUE PLAQUE'

    local root = mw.html.create('div')
        :addClass('blue-plaque')

    root:tag('div')
        :addClass('plaque-ornament')
        :wikitext('✦')
        :done()

    root:tag('div')
        :addClass('plaque-heading')
        :wikitext(authority)
        :done()

    root:tag('div')
        :addClass('plaque-rule')
        :done()

    if image and image ~= '' then
        root:tag('div')
            :addClass('plaque-image')
            :wikitext('[[File:' .. image .. '|frameless|100px]]')
            :done()
    end

    root:tag('div')
        :addClass('plaque-name')
        :wikitext(name)
        :done()

    root:tag('div')
        :addClass('plaque-inscription')
        :wikitext(description)
        :done()

    if dates and dates ~= '' then
        root:tag('div')
            :addClass('plaque-date')
            :wikitext(dates)
            :done()
    end

    if location and location ~= '' then
        root:tag('div')
            :addClass('plaque-location')
            :wikitext(location)
            :done()
    end

    root:tag('div')
        :addClass('plaque-ornament')
        :wikitext('✦')

    return tostring(root)
end

return p