Модуль:Поиск персоны

Материал из Кадровый состав НКВД 1935-1939
Перейти к навигации Перейти к поиску

Для документации этого модуля может быть создана страница Модуль:Поиск персоны/doc

local pp = {}
local global = mw.ext.luaglobal
local tools = require( 'Module:Tools' )
global.strict( true )

function pp.makePattern( ourName )
	local fio = mw.text.split( ourName, '[%. ]+' )
	if #fio == 1 then
		fio[1] = fio[1] .. ', %'
	else
		if mw.ustring.sub( fio[1], -1, -1) ~= ',' then
			fio[1] = fio[1] .. ','
		end
		if mw.ustring.len( fio[2] ) == 1 then
			fio[2] = fio[2] .. '%'
		end
		if fio[3] and mw.ustring.len( fio[3] ) == 1 then
			fio[3] = fio[3] .. '%'
		end
	end

	if fio[5] then
		return '<p class=error>Невозможно разобрать. Это точно ФИО?</p>'
	end
	return table.concat( fio, ' ' )
end

function pp.makeP( unknow )
	local fio = mw.text.trim( unknow ) .. ' '
	fio = mw.ustring.gsub( fio, '([%.,])([^ ])', '%1 %2')
	fio = mw.ustring.gsub( fio, '  +', ' ')
	fio = mw.text.split( fio, ' ', true )
	if string.sub( fio[1], -1, -1 ) == ',' then
		fio[1] = string.sub( fio[1], 1, -2 )
	end
	if #fio > 4 or ( fio[4] or '' ) ~= '' then
		return nil
	end
	fio[#fio] = nil
	if #fio > 1 and string.sub( fio[2], -1, -1 ) == '.' then
		fio[2] = string.sub( fio[2], 1, -2 ) .. '%'
	end
	if #fio > 2 and string.sub( fio[3], -1, -1 ) == '.' then
		fio[3] = string.sub( fio[3], 1, -2 ) .. '%'
	end
	if #fio == 1 then
		fio = fio[1] .. ','
	elseif #fio == 2 then
		fio = fio[1] .. ', ' .. fio[2]
	else
		fio = fio[1] .. ', ' .. fio[2] .. ' ' .. fio[3]
	end
	if string.sub( fio, -1, -1 ) ~= '%' then
		fio = fio .. '%'
	end
	
	return fio
end


pp[''] = function ( frame )
	local ourName = mw.title.getCurrentTitle()
	if ourName.text == ourName.subpageText then
		ourName = ourName.text
	else
		return 'Это подстраница с дополнительными материалами (' .. ourName.baseText .. ')'
	end

	local fio = pp.makeP( ourName )
	if not fio then
		return  '<p class=error>Невозможно разобрать. Это точно ФИО?</p>'
	end
	
	local query = tools.cq( 'names', 'name,disambig,_pageName', {
		where = 'name LIKE "' .. fio .. '"',
		limit = '999',
	} )
	
		
	local out = {}
	if #query == 0 then
		return 'В базе не найдено людей с похожим именем (' .. fio .. ')'
	end
	for  _, pers in ipairs( query ) do
		local fio
		if pers.name == pers._pageName then
			fio = '[[' .. pers._pageName .. ']]'
		elseif pers.name .. ' ' .. pers.disambig == pers._pageName then
			fio = '[[' .. pers._pageName .. '|' .. pers.name .. ' <span class="nkvd-idx">' .. pers.disambig .. '</span>]]'
		elseif pers.disambig == '' then
			fio = pers.name .. ' (<i>он же</i> [[' .. pers._pageName .. ']])'
		else
			fio = pers.name .. ' <span class="nkvd-idx">' .. pers.disambig .. '</span> (<i>он же</i> [[' .. pers._pageName .. ']])'
		end
		table.insert( out, fio )
	end
	return 'Возможно, вас интересует одна из следующих персоналий: ' -- .. '(' .. fio .. '/' .. #query .. ') '
		.. table.concat( out, ' &bull; ' )

end
return pp