Модуль:ФИО
Материал из вики «Вече»
Дополнительные действия
Для документации этого модуля может быть создана страница Модуль:ФИО/doc
local get_args = require('Модуль:Arguments').getArgs
local mHatnote = require('Модуль:Hatnote')
local current_title_obj = mw.title.getCurrentTitle()
local p = {}
local hatnote_args = {
hide_disambig = true,
dot = true,
empty_list_message = 'Не найдено ни одной страницы разрешения неоднозначности',
preview_error = true
}
local surname_categories = {
['Страницы значений:Однофамильцы'] = true,
['Страницы значений:Однофамильцы-тёзки'] = true,
['Страницы значений:Полные тёзки'] = true,
}
local tracking_categories = {
bad_link = 'Википедия:Страницы с шаблоном ФИО со ссылкой на общие неоднозначности',
no_links = 'Википедия:Страницы с шаблоном ФИО без ссылок',
disambig_not_found = 'Википедия:Страницы с модулем ФИО со ссылкой на другую статью',
}
local function is_surname_disambig(title)
local title_obj = mw.title.new(title)
if not title_obj or not title_obj.exists or title_obj.isRedirect or mw.title.equals(title_obj, current_title_obj) then
return false
end
for _, c in ipairs( title_obj.categories ) do
if surname_categories[ c ] == true then
return true
end
end
return false
end
local function get_items(_title)
local title = mHatnote.remove_precision{_title}
local surname = title:match('^[^,]+')
local surname_with_name = title:match('^[^,]+,%s[^%s]+')
local full_name = title:match('^[^,]+,%s[^%s]+%s[^%s]+')
return {surname, surname_with_name, full_name}
end
local function find_disambig(title, categories, check_nil, nocat)
local disambig_page
local without = mHatnote.remove_precision{title}
local with = without .. ' (значения)'
if is_surname_disambig(title) then
disambig_page = title
elseif without ~= title and is_surname_disambig(without) then
disambig_page = without
elseif with ~= title and is_surname_disambig(with) then
disambig_page = with
elseif current_title_obj.namespace ~= 0 then
disambig_page = without
elseif title ~= with and mHatnote.is_disambig{title} then
categories:add('bad_link', nocat)
disambig_page = title
elseif current_title_obj.text ~= with and mHatnote.is_disambig{with} then
categories:add('bad_link', nocat)
disambig_page = with
elseif without ~= title and mHatnote.is_disambig{without} then
categories:add('bad_link', nocat)
disambig_page = without
end
if not disambig_page and check_nil then
categories:add('disambig_not_found', nocat)
end
return disambig_page, without
end
p['ФИО'] = function (frame)
local args = get_args(frame)
local title = args.title or args[1] or current_title_obj.text
local categories = mHatnote.define_categories(tracking_categories)
local nocat = args.nocat
hatnote_args.prefix = 'В Википедии есть статьи о других людях с такой фамилией, см.'
hatnote_args.list_sep = '; '
hatnote_args.natural_join = false
hatnote_args.nocat = nocat
local lNum = 0
for i, v in ipairs(get_items(title)) do
local link, label = find_disambig(v, categories, false, nocat)
if link then
local t = mw.title.new(link)
if t.isRedirect then
link = t.redirectTarget.text
end
if link ~= title and link ~= current_title_obj.text then
local found = false
for ii, vv in ipairs(hatnote_args) do
if link == vv then
found=true
end
end
if not found then
lNum = lNum + 1
hatnote_args[lNum], hatnote_args['l' .. lNum] = link, label
end
end
end
end
return mHatnote.main(hatnote_args, tracking_categories) .. categories
end
local function template_by_type(frame, disambig_type, num)
return function (frame)
local args = get_args(frame)
local categories = mHatnote.define_categories(tracking_categories)
local nocat = args.nocat
local prefixes = mw.loadData('Модуль:Другие значения/префиксы')
local prefix = prefixes[disambig_type]
if type(prefix) == 'table' then
if args[2] then
hatnote_args.prefix = prefix.plural
else
hatnote_args.prefix = prefix.singular
end
else
hatnote_args.prefix = prefix
end
hatnote_args.nocat = nocat
if not args[1] then
local link = get_items(current_title_obj.text)[num]
local found_disambig, auto_label = find_disambig(link, categories, true, nocat)
hatnote_args[1] = found_disambig or link
hatnote_args['l1'] = args['l1'] or auto_label
else
local i = 1
while args[i] do
local link, label = mHatnote.parse_link{args[i]}
if link then
local found_disambig, auto_label = find_disambig(link, categories, true, nocat)
hatnote_args[i] = found_disambig or link
hatnote_args['l' .. i] = label or args['l' .. i] or auto_label
else
hatnote_args[i] = args[i]
end
i = i + 1
end
end
return mHatnote.main(hatnote_args, tracking_categories) .. categories
end
end
p['однофамильцы'] = template_by_type(frame, 'фамилия', 1)
p['однофамильцы-тёзки'] = template_by_type(frame, 'имя и фамилия', 2)
p['полные тёзки'] = template_by_type(frame, 'фио', 3)
return p