Перейти до вмісту

Модуль:Author/пісочниця

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

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

local Error = require('Модуль:Error');
local Sister = require('Модуль:Plain sister');
local getArgs = require('Модуль:Arguments').getArgs
local Wikidata = require('Модуль:Wikidata')

local this = {};

local migrationNeeded = false;

local capitalize = function(str)
    if not str then
        return str
    end
    return mw.ustring.upper(mw.ustring.sub(str, 1, 1))..mw.ustring.sub(str, 2)
end

this.capitalize = capitalize

local getLocalValue = function(args, argNames)
    for key, argName in pairs(argNames) do
        local value = args[argName]
        if value then
            return value
        end
    end
    return nil
end

local getValue = function(args, localArgNames, wikidataValue)
    local localValue = getLocalValue(args, localArgNames)
    if localValue then
        migrationNeeded = true
    else
        localValue = wikidataValue
    end
    return localValue
end

local function parseDate(text) 
    local BC = mw.ustring.match(text, '^%s*%-') or mw.ustring.match(text, 'до н%.%s?е%.');
    local date = mw.ustring.match(text, '%d+');
    if not date then 
        return nil;
    end
    
    if BC then
        return date..' до н. е.'
    else
        return date;
    end;
end

function this.main(frame)
    -- parse arguments
    local args = getArgs(frame);
    
    local firstname = getLocalValue(args, {'ім\'я', 'firstname'}) or '';
    local lastname = getLocalValue(args, {'прізвище', 'lastname'}) or '';
    local fullname = firstname..' '..lastname;
    
    -- get the initial
    local initial = getLocalValue(args, {'ініціал', 'last_initial'});
    if not initial then 
        if mw.ustring.len(lastname)>0 then
            initial = mw.ustring.sub(lastname, 1, 1)
        elseif mw.ustring.len(firstname)>0 then
            initial = mw.ustring.sub(firstname, 1, 1)
        end
    end
    if initial then
        initial = capitalize(initial);
    end;

    -- get dates
    local rawBirth = getValue(args, {'рік_народження', 'birthyear'}, Wikidata.getDateValue({'P569', 'y'})) or '?';
    local rawDeath = getValue(args, {'рік_смерті', 'deathyear'}, Wikidata.getDateValue({'P570', 'y'})) or '';
    local birth = parseDate(rawBirth);
    local death = parseDate(rawDeath);
    
    -- other params
    local description = capitalize(getValue(args, {'опис', 'description'}, Wikidata.getDescription()) or '');
    local defaultsort = getLocalValue(args, {'defaultsort'});
    local image = getValue(args, {'зображення', 'image'}, Wikidata.getRawValue('P18'));
    local image_caption = getLocalValue(args, {'підпис_зображення', 'image_caption'}) or fullname;
    
    -- do the main job
    
    -- header
    local result = {'{| class="authortemplate"\n|-\n| class="gen_header_backlink" |\n| class="gen_header_title" |\'\'\''};
    result[#result + 1] = fullname;
    result[#result + 1] = '\'\'\'<br />';
    result[#result + 1] = rawBirth;
    result[#result + 1] = '—';
    result[#result + 1] = rawDeath;
    result[#result + 1] = '\n| class="gen_header_forelink" |\n|}\n'
    
    -- notes
    result[#result + 1] = '{| class="author_notes"\n|-\n|';
    result[#result + 1] = Sister.main(frame);
    result[#result + 1] = '\n'
    result[#result + 1] = description;
    result[#result + 1] = '\n|}'
    
    -- image
    if image then
        result[#result + 1] = '[[Файл:';
        result[#result + 1] = image;
        result[#result + 1] = '|thumb|';
        result[#result + 1] = frame:expandTemplate{ title = 'Center', args = {image_caption} };
        result[#result + 1] = ']]';
    end
    
    -- DEFAULTSORT and categories
    result[#result + 1] = '{{DEFAULTSORT:';
    if defaultsort then
        result[#result + 1] = defaultsort;
    else
        if lastname then
            result[#result + 1] = lastname;
            result[#result + 1] = ', ';
        end
        result[#result + 1] = firstname;
    end
    result[#result + 1] = '}}';
    result[#result + 1] = '[[Категорія:Автори';
    if initial then
        result[#result + 1] = '-';
        result[#result + 1] = initial;
    else
        result[#result + 1] = ' без ініціалів';
    end
    result[#result + 1] = ']]';
    if birth then
        result[#result + 1] = '[[Категорія:Народжені у ';
        result[#result + 1] = birth;
        result[#result + 1] = '-ому]]';
    end
    if death then
        result[#result + 1] = '[[Категорія:Померлі у ';
        result[#result + 1] = death;
        result[#result + 1] = '-ому]]';
    end
    
    if migrationNeeded then
        result[#result + 1] = '[[Категорія:Дані до перенесення до Вікіданих]]';
    end
    
    return frame:preprocess(table.concat(result));
end

return this;