Модуль:PD-auto
Документацію для цього модуля можна створити у Модуль:PD-auto/документація
local Math = require('Модуль:Math');
local Error = require('Модуль:Error');
local Wikidata = require('Модуль:Wikidata');
local getArgs = require('Модуль:Arguments').getArgs
local getLocalValue = require('Модуль:Arguments').getLocalValue
local yesno = require('Module:Yesno')
local this = {};
local migrationNeeded = false
-- utility functions
local getValue = function(args, localArgNames, wikidataValue)
local localValue = getLocalValue(args, localArgNames, nil)
if localValue then
migrationNeeded = true
else
localValue = wikidataValue
end
return localValue
end
-- process empty arguments as not specified
local function filterEmpty(text, defaultValue)
if text and mw.ustring.len(text) > 0 then
return text;
end
return defaultValue;
end
-- show error message
local function formatError(frame, message)
return frame:preprocess('{{PD-Custom|текст='..Error.error{message}..'}}');
end
-- show proper message when there is no suitable license for US
local function noUS(frame, reason)
if reason then
if type(reason) == 'number' then
return frame:preprocess('{{US-none|year='..reason..'}}');
else
return frame:preprocess('{{US-none|reason='..reason..'}}');
end
end
return frame:preprocess('{{US-none}}');
end
-- show resulting message
local function formatResults(frame, results, authorMode)
local reasons = {};
local starter = #results > 1 and '\n* ' or ''
for i=1, #results do
if results[i].reason then
reasons[#reasons+1] = starter;
reasons[#reasons+1] = results[i].reason;
end
end;
reasons = table.concat(reasons);
if authorMode then
return frame:preprocess('{{PD-custom|текст='..reasons..'}}');
end
local pdCountries = {};
local countries = {};
for i=1, #results do
if results[i].error then
return noUS(frame, results[i].error);
end;
if results[i].year == 'PD' then
pdCountries[#pdCountries+1] = results[i].country;
end
end;
for i=1, #pdCountries do
if i > 1 then
if i == #pdCountries then
countries[#countries+1] = ' та '
else
countries[#countries+1] = ', '
end
end
countries[#countries+1] = pdCountries[i];
end
countries = table.concat(countries);
return frame:preprocess('{{PD-custom|текст=Ця робота перебуває в [[w:Суспільне надбання|суспільному надбанні]] в '..countries..'.|примітки='..reasons..'}}');
end
local function parseDate(text)
if not text then
return nil;
end
local BC = mw.ustring.match(text, '^%s*%-') or mw.ustring.match(text, 'до н%.%s?е%.') or string.match(text, 'BCE$');
local date = mw.ustring.match(text, '%d+');
if not date then
return nil;
end
if BC then
return -date;
else
return date;
end;
end
--main
function this.pd(frame)
local currentYear = os.date('*t').year;
local args = getArgs(frame);
-- from Wikidata
local birthYear = Math._cleanNumber(frame, parseDate(getValue(args, {'рік-народження', 'birth-year'}, Wikidata.getDateValue({'P569', 'ye'}))));
local deathYear = Math._cleanNumber(frame, parseDate(getValue(args, {'рік-смерті', 'death-year'}, Wikidata.getDateValue({'P570', 'ye'}))));
-- parse args
-- local country = mw.ustring.upper(filterEmpty(args['country'],'none'));
local country = mw.ustring.upper(filterEmpty(getLocalValue(args, {'країна', 'country'}, nil),'none'));
-- local deathYear = Math._cleanNumber(frame, args['death-year']);
-- local birthYear = Math._cleanNumber(frame, args['birth-year']);
-- local publicationYear = Math._cleanNumber(frame, args['publication-year']);
local publicationYear = Math._cleanNumber(frame, getLocalValue(args, {'рік-публікації', 'publication-year'}, nil));
-- local usPublicationYear = Math._cleanNumber(frame, args['US-publication-year']);
local usPublicationYear = Math._cleanNumber(frame, getLocalValue(args, {'рік-публікації-Штати', 'US-publication-year'}, nil));
-- local creationYear = Math._cleanNumber(frame, args['creation-year']);
local creationYear = Math._cleanNumber(frame, getLocalValue(args, {'рік-створення', 'creation-year'}, nil));
-- local rehabilitationYear = Math._cleanNumber(frame, args['rehabilitation-year']);
local rehabilitationYear = Math._cleanNumber(frame, getLocalValue(args, {'рік-реабілітації', 'rehabilitation-year'}, nil));
-- local renewed = toBoolean(args['renewed'], false);
local renewed = yesno(getLocalValue(args, {'поновлено', 'renewed'}, nil) or false, false);
-- local author = toBoolean(args['author-known'], true);
local author = yesno(getLocalValue(args, {'відомий-автор', 'author-known'}, nil) or true, true);
-- local notice = toBoolean(args['notice'], true);
local notice = yesno(getLocalValue(args, {'знак-копірайту', 'notice'}, nil) or true, true);
local authorMode = yesno(mw.title.getCurrentTitle():inNamespace(102), getLocalValue(args, {'режим-автора', 'author-mode'}, nil));
-- preprocess years
if not authorMode and publicationYear == nil and usPublicationYear == nil then
return formatError(frame, 'Наразі цей шаблон не підтримує неопублікованих творів.');
end
-- first date of publication
local firstPublished;
if publicationYear == nil then
firstPublished = usPublicationYear;
elseif usPublicationYear == nil then
firstPublished = publicationYear;
else
firstPublished = math.min(publicationYear, usPublicationYear);
end;
-- if the death year is not specified, but there is a known author, set some value for death year
if author and deathYear == nil then
-- якщо не вказано дати смерті, то в загальному випадку припускаємо, що автор ще живий. Але люди не живуть більше ста років після публікації зазвичай.
if firstPublished == nil then
deathYear = currentYear
else
deathYear = math.min(firstPublished + 100, currentYear);
end
end
-- create argument map for subroutines
local pdArgs = {authorMode = authorMode, death = deathYear, renewed = renewed, creation = creationYear, author = author, notice = notice,
rehabilitation = rehabilitationYear, };
local results = {};
local allPD = require('Модуль:PD-auto/all');
pdArgs.publication = firstPublished;
local allResult = allPD.pd(pdArgs);
-- public domain in U.S. before 1 january <last_pd_year>
local last_pd_year
local current_date = os.date("*t")
last_pd_year = current_date.year - 95
if authorMode then
if birthYear==nil or birthYear < last_pd_year then
results[#results+1] = {reason = 'Твори цього автора, опубліковані до 1 січня '.. last_pd_year ..' року, перебувають у суспільному надбанні в Сполучених Штатах;'
..' інші твори, що опубліковані пізніше, можуть мати різний ліцензійний статус.'};
end
-- only if it is not in public domain in general
elseif allResult.year ~= 'PD' then
-- no country specified
if country == 'NONE' then
return noUS(frame, 'Недостатньо інформації, щоб визначити ліцензійний статус у Сполучених Штатах');
-- United States
elseif country == 'US' then
local USPD = require('Модуль:PD-auto/US');
pdArgs.publication = usPublicationYear or publicationYear;
pdArgs.usPublication = pdArgs.publication;
local usResult = USPD.pd(pdArgs);
if usResult.year ~= 'PD' then
return noUS(frame, usResult.year);
end
results[#results+1] = usResult;
-- other countries
else
local success, countryPD = pcall(require, 'Модуль:PD-auto/'..country);
if not success then
return formatError(frame, 'Наразі цей шаблон не підтримує країну з кодом '..country);
end
local countryResult = countryPD.pd(pdArgs);
local usPublicationYear = Math._cleanNumber(frame, getLocalValue(args, {'рік-публікації-Штати', 'US-publication-year'}, nil));
local USPD = require('Модуль:PD-auto/US');
pdArgs.publication = publicationYear;
pdArgs.usPublication = usPublicationYear;
pdArgs.URAA = countryResult.URAA;
pdArgs.URAADate = countryResult.URAADate;
pdArgs.country = countryResult.country;
local usResult = USPD.pd(pdArgs);
if usResult.year ~= 'PD' then
return noUS(frame, usResult.year);
end
results[#results+1] = usResult;
results[#results+1] = countryResult;
end
end
results[#results+1] = allResult;
return formatResults(frame, results, authorMode);
end
return this;