Kontent qismiga oʻtish

Modul:FIO

Vikipediya, erkin ensiklopediya

Bu modul uchun Modul:FIO/doc nomli hujjat sahifasini yaratishingiz mumkin

local get_args = require('Module:Arguments').getArgs
local mHatnote = require('Module:Hatnote')
local current_title_obj = mw.title.getCurrentTitle()

local p = {}
local hatnote_args = {
	hide_disambig = true,
	dot = true,
	empty_list_message = 'Birorta koʻp maʼnoli sahifa topilmadi',
	preview_error = true
}
local templates = {
	'[fF]amiliyadoshlar roʻyxati',
	'[aA]dashlar roʻyxati',
	'[tT]oʻliq adashlar roʻyxati',
	'[fF]amiliyadosh-adashlar roʻyxati',
	'[aA]dash-familiyadoshlar roʻyxati'
}
local disambig_box_names = {
	'[mM]aʼnolari',
	'[dD]isambig'
}
local disambig_box_params = {
	'[fF]amiliya',
	'[fF]amiliyadoshlar',
	'[aA]dashlar',
	'[fF]amiliyadosh-adashlar',
	'[tT]oʻliq adashlar'
}
local tracking_categories = {
	bad_link = 'Vikipediya:FIO andozasi bilan umumiy koʻp maʼnoli sahifalarga havola qilingan sahifalar',
	no_links = 'Vikipediya:FIO andozasi bilan havolasiz sahifalar',
	disambig_not_found = 'Vikipediya:FIO moduli bilan boshqa maqolaga havola qilingan sahifalar'
}

function is_surname_disambig(title)
	local title_obj = mw.title.new(title)
	
	if not title_obj.exists or mw.title.equals(title_obj, current_title_obj) then
		return false 
	end
	
	if title_obj.isRedirect then
		title_obj = title_obj.redirectTarget
		if not title_obj.exists or mw.title.equals(title_obj, current_title_obj) then
			return false
		end
	end

	local page_content = title_obj:getContent():gsub('<[^>]*>', '')
	for i, template in ipairs(templates) do
		if mw.ustring.match(page_content, '{{' .. template) then
			return true
		end
	end
	for i, name in ipairs(disambig_box_names) do
		for j, param in ipairs(disambig_box_params) do
			if mw.ustring.match(page_content, string.format('{{%s[^}]*%s', name, param)) then
				return true
			end
		end
	end
	return false
end
    
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

function find_disambig(title, categories, check_nil, nocat)
	local disambig_page
	local without = mHatnote.remove_precision{title}
	local with = without .. ' (maʼnolari)'
	
	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['FIO'] = 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 = 'Vikipediyada ushbu familiyaga ega boshqa kishilar haqida maqolalar mavjud, qarang:'
	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

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('Module:Boshqa maʼnolari/prefikslar')
		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['familiyadoshlar'] = template_by_type(frame, 'familiyadoshlar', 1)
p['adashlar-familiyadoshlar'] = template_by_type(frame, 'ism+familiya', 2)
p['toʻliq adashlar'] = template_by_type(frame, 'fio', 3)

return p