پرش به محتوا

پودمان:پدیدآورنده

از سورسکال
نسخهٔ تاریخ ۴ نوامبر ۲۰۲۶، ساعت ۰۳:۵۶ توسط Shahroudi (بحث | مشارکت‌ها)
(تفاوت) → نسخهٔ قدیمی‌تر | نمایش نسخهٔ فعلی (تفاوت) | نسخهٔ جدیدتر ← (تفاوت)

توضیحات این پودمان می‌تواند در پودمان:پدیدآورنده/توضیحات قرار گیرد.

local p = {}
local cfg = require('Module:پدیدآورنده/تنظیمات')

local function filled(v)
    return type(v) == 'string'
        and mw.text.trim(v) ~= ''
end

local function safe(v)
    return mw.text.nowiki(tostring(v or ''))
end

local function statementValue(entity, property)
    if not filled(property) then
        return nil
    end

    local statements = entity:getBestStatements(property)

    for _, s in ipairs(statements or {}) do
        local snak = s.mainsnak

        if snak and snak.snaktype == 'value'
            and snak.datavalue then
            return snak.datavalue.value
        end
    end

    return nil
end

local function deathInfo(value, frame)
    if type(value) ~= 'table' or not value.time then
        return nil, nil
    end

    local y, m, d = value.time:match(
        '^[%+%-]?(%d+)%-(%d%d)%-(%d%d)'
    )

    y, m, d = tonumber(y), tonumber(m), tonumber(d)

    if not y or y < 1 or y > 9999 then
        return nil, nil
    end

    local precision = tonumber(value.precision) or 9
    if precision < 9 then
        return nil, nil
    end

    local today = frame:callParserFunction(
        '#time', 'Y-m-d'
    )

    local cy, cm, cd = today:match(
        '^(%d+)%-(%d+)%-(%d+)'
    )

    cy, cm, cd = tonumber(cy), tonumber(cm), tonumber(cd)

    if not cy then
        return tostring(y), nil
    end

    local years = cy - y

    if precision >= 11 and m and d
        and m >= 1 and m <= 12
        and d >= 1 and d <= 31 then

        if cm < m or (cm == m and cd < d) then
            years = years - 1
        end

    else
        -- محاسبه محافظه‌کارانه برای تاریخ ناقص
        years = years - 1
    end

    return tostring(y), math.max(0, years)
end

local function addRow(rows, label, value)
    if not filled(value) then
        return
    end

    rows[#rows + 1] =
        '<tr><th style="padding:9px;text-align:right;' ..
        'width:34%;color:#66776f;font-weight:normal;' ..
        'border-bottom:1px solid #edf0ee;">' ..
        safe(label) ..
        '</th><td style="padding:9px;' ..
        'border-bottom:1px solid #edf0ee;">' ..
        value .. '</td></tr>'
end

local function projectLink(entity, site, base, label)
    if not filled(site) or not filled(base) then
        return nil
    end

    local ok, title = pcall(function()
        return entity:getSitelink(site)
    end)

    if not ok or not filled(title) then
        return nil
    end

    local url = base .. mw.uri.encode(title, 'PATH')

    return '[' .. url .. ' ' .. label .. ']'
end

p['نمایش'] = function(frame)
    local args = frame:getParent()
        and frame:getParent().args or frame.args

    local title = mw.title.getCurrentTitle()

    local name = mw.text.trim(args['نام'] or '')

    if name == '' then
        name = title.text
    end

    local wb = mw.wikibase

    -- اگر صفحه به آیتم متصل نیست، هیچ داده‌ای نخوان.
    local id = wb and wb.getEntityIdForCurrentPage
        and wb.getEntityIdForCurrentPage()

    if not id then
        return '<div class="sourcical-author" ' ..
            'style="direction:rtl;float:left;clear:left;width:310px;max-width:100%;box-sizing:border-box;margin:0 16px 16px 0;' ..
            'padding:16px;border:1px solid #dce7e1;' ..
            'border-radius:12px;text-align:center;">' ..
            '<strong>' .. safe(name) .. '</strong></div>'
    end

    local ok, entity = pcall(function()
        return wb.getEntity(id)
    end)

    if not ok or not entity then
        return '<strong>' .. safe(name) .. '</strong>'
    end

    local description = entity.descriptions
        and entity.descriptions.fa
        and entity.descriptions.fa.value

    local birth = statementValue(
        entity, cfg.birthProperty
    )

    local death = statementValue(
        entity, cfg.deathProperty
    )

    local photo = statementValue(
        entity, cfg.imageProperty
    )

    local deathYear, years = deathInfo(death, frame)
    local birthYear = birth and birth.time
        and birth.time:match('^[%+%-]?(%d+)%-')

    local rows = {}

    if birthYear then
        addRow(rows, 'تولد', safe(birthYear) .. ' میلادی')
    end

    if deathYear then
        addRow(rows, 'درگذشت',
            safe(deathYear) .. ' میلادی')
    end

    local links = {}

    if filled(cfg.datacalURL) then
        links[#links + 1] =
            '[' .. cfg.datacalURL ..
            mw.uri.encode(id, 'PATH') ..
            ' دیتاکال]'
    end

    local projects = {
        {cfg.encyclopediaSite,
         cfg.encyclopediaURL, 'دانشنامه'},
        {cfg.commonsSite,
         cfg.commonsURL, 'انبار اسلامیکال'},
        {cfg.islamicoulSite,
         cfg.islamicoulURL, 'اسلامیکول'}
    }

    for _, item in ipairs(projects) do
        local link = projectLink(
            entity, item[1], item[2], item[3]
        )

        if link then
            links[#links + 1] = link
        end
    end

    local out = {}

    out[#out + 1] =
        '<div class="sourcical-author" ' ..
        'style="direction:rtl;float:left;clear:left;width:310px;max-width:100%;box-sizing:border-box;' ..
        'border:1px solid #dce7e1;border-radius:12px;' ..
        'overflow:hidden;background:#fff;' ..
        'margin:0 16px 16px 0;">'

    out[#out + 1] =
        '<div style="background:#f4f8f6;' ..
        'padding:20px;text-align:center;">'

    if type(photo) == 'string'
        and filled(photo) then
        out[#out + 1] =
            '[[پرونده:' .. photo ..
            '|140px|وسط|بدون‌قاب]]'
    end

    out[#out + 1] =
        '<div style="font-size:19px;' ..
        'font-weight:bold;color:#244b3d;">' ..
        safe(name) .. '</div>'

    if filled(description) then
        out[#out + 1] =
            '<div style="font-size:13px;' ..
            'color:#66776f;margin-top:8px;">' ..
            safe(description) .. '</div>'
    end

    out[#out + 1] = '</div>'

    if #rows > 0 then
        out[#out + 1] =
            '<table style="width:100%;border-collapse:' ..
            'collapse;font-size:13px;">' ..
            table.concat(rows) .. '</table>'
    end

    if #links > 0 then
        out[#out + 1] =
            '<div style="padding:12px;' ..
            'font-size:12px;line-height:2;' ..
            'text-align:center;">' ..
            table.concat(links, ' · ') ..
            '</div>'
    end

    out[#out + 1] = '</div>'

    -- وضعیت زمانی حق نشر
    local copyrightTemplate

    if years then
        if years >= 100 then
            copyrightTemplate = 'حق نشر/درگذشت ۱۰۰'
        elseif years >= 50 then
            copyrightTemplate = 'حق نشر/درگذشت ۵۰'
        elseif years >= 30 then
            copyrightTemplate = 'حق نشر/درگذشت ۳۰'
        else
            copyrightTemplate = 'حق نشر/درگذشت کمتر از ۳۰'
        end
    end

    if copyrightTemplate then
        out[#out + 1] = frame:expandTemplate({
            title = copyrightTemplate,
            args = {
                ['نام'] = name,
                ['سال'] = tostring(years)
            }
        })
    end

    return table.concat(out)
end

return p